this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal: replace internal.CoreValue with more type-safe variant

CoreValue is now typed throught the internal/{value|types} packages.

Change-Id: I4b2df582a21fff2aaa83df61735141de11b6acbf
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9366
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>

+110 -58
+2 -3
cmd/cue/cmd/custom.go
··· 30 30 "cuelang.org/go/cue" 31 31 "cuelang.org/go/cue/errors" 32 32 "cuelang.org/go/internal" 33 - "cuelang.org/go/internal/core/adt" 34 33 itask "cuelang.org/go/internal/task" 34 + "cuelang.org/go/internal/value" 35 35 _ "cuelang.org/go/pkg/tool/cli" // Register tasks 36 36 _ "cuelang.org/go/pkg/tool/exec" 37 37 _ "cuelang.org/go/pkg/tool/file" ··· 76 76 // TODO: remove this block to allow commands to be defined in any file. 77 77 outer: 78 78 for _, v := range []cue.Value{tools.Lookup(typ), o} { 79 - _, vv := internal.CoreValue(v) 80 - w := vv.(*adt.Vertex) 79 + _, w := value.ToInternal(v) 81 80 for _, c := range w.Conjuncts { 82 81 src := c.Source() 83 82 if src == nil {
-7
cue/build.go
··· 56 56 } 57 57 return &Runtime{idx: newIndex()} 58 58 } 59 - 60 - internal.CoreValue = func(value interface{}) (runtime, vertex interface{}) { 61 - if v, ok := value.(Value); ok && v.v != nil { 62 - return v.idx, v.v 63 - } 64 - return nil, nil 65 - } 66 59 } 67 60 68 61 func dummyLoad(token.Pos, string) *build.Instance { return nil }
+9
cue/types.go
··· 40 40 "cuelang.org/go/internal/core/runtime" 41 41 "cuelang.org/go/internal/core/subsume" 42 42 "cuelang.org/go/internal/core/validate" 43 + "cuelang.org/go/internal/types" 43 44 ) 44 45 45 46 // Kind determines the underlying type of a Value. ··· 562 563 type Value struct { 563 564 idx *runtime.Runtime 564 565 v *adt.Vertex 566 + } 567 + 568 + type hiddenValue = Value 569 + 570 + // Core is for internal use only. 571 + func (v hiddenValue) Core(x *types.Value) { 572 + x.V = v.v 573 + x.R = v.idx 565 574 } 566 575 567 576 func newErrValue(v Value, b *adt.Bottom) Value {
+4 -10
internal/core/convert/go.go
··· 33 33 "cuelang.org/go/cue/errors" 34 34 "cuelang.org/go/cue/parser" 35 35 "cuelang.org/go/cue/token" 36 - "cuelang.org/go/internal" 37 36 "cuelang.org/go/internal/core/adt" 38 37 "cuelang.org/go/internal/core/compile" 39 - "cuelang.org/go/internal/core/runtime" 38 + "cuelang.org/go/internal/types" 40 39 ) 41 40 42 41 // This file contains functionality for converting Go to CUE. ··· 225 224 } 226 225 227 226 func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value { 228 - if internal.CoreValue != nil { 229 - if ii, iv := internal.CoreValue(x); ii != nil { 230 - i := ii.(*runtime.Runtime) 231 - v := iv.(*adt.Vertex) 232 - // TODO: panic if nto the same runtime. 233 - _ = i 234 - return v 235 - } 227 + if t := (&types.Value{}); types.CastValue(t, x) { 228 + // TODO: panic if nto the same runtime. 229 + return t.V 236 230 } 237 231 src := ctx.Source() 238 232 switch v := x.(type) {
-1
internal/core/convert/go_test.go
··· 23 23 "github.com/cockroachdb/apd/v2" 24 24 "github.com/google/go-cmp/cmp" 25 25 26 - _ "cuelang.org/go/cue" // set internal.CoreValue 27 26 "cuelang.org/go/cue/errors" 28 27 "cuelang.org/go/internal/core/adt" 29 28 "cuelang.org/go/internal/core/convert"
+4 -9
internal/core/dep/dep_test.go
··· 20 20 "testing" 21 21 22 22 "cuelang.org/go/cue" 23 - "cuelang.org/go/internal" 24 23 "cuelang.org/go/internal/core/adt" 25 24 "cuelang.org/go/internal/core/debug" 26 25 "cuelang.org/go/internal/core/dep" 27 26 "cuelang.org/go/internal/core/eval" 28 - "cuelang.org/go/internal/core/runtime" 29 27 "cuelang.org/go/internal/cuetest" 30 28 "cuelang.org/go/internal/cuetxtar" 29 + "cuelang.org/go/internal/value" 31 30 ) 32 31 33 32 func TestVisit(t *testing.T) { ··· 45 44 t.Fatal(inst.Err()) 46 45 } 47 46 48 - rx, nx := internal.CoreValue(inst) 49 - ctxt := eval.NewContext(rx.(*runtime.Runtime), nx.(*adt.Vertex)) 47 + ctxt := eval.NewContext(value.ToInternal(inst)) 50 48 51 49 testCases := []struct { 52 50 name string ··· 69 67 for _, tc := range testCases { 70 68 v := inst.LookupPath(cue.ParsePath(tc.root)) 71 69 72 - _, nx = internal.CoreValue(v) 73 - n := nx.(*adt.Vertex) 70 + _, n := value.ToInternal(v) 74 71 w := t.Writer(tc.name) 75 72 76 73 t.Run(tc.name, func(sub *testing.T) { ··· 105 102 106 103 v := inst.Lookup("a") 107 104 108 - rx, nx := internal.CoreValue(v) 109 - r := rx.(*runtime.Runtime) 110 - n := nx.(*adt.Vertex) 105 + r, n := value.ToInternal(v) 111 106 112 107 ctxt := eval.NewContext(r, n) 113 108
-4
internal/internal.go
··· 70 70 // GetRuntime reports the runtime for an Instance or Value. 71 71 var GetRuntime func(instance interface{}) interface{} 72 72 73 - // CoreValue returns an *runtime.Index and *adt.Vertex for a cue.Value. 74 - // It returns nil if value is not a cue.Value. 75 - var CoreValue func(value interface{}) (runtime, vertex interface{}) 76 - 77 73 // MakeInstance makes a new instance from a value. 78 74 var MakeInstance func(value interface{}) (instance interface{}) 79 75
+3 -4
internal/task/task.go
··· 23 23 "cuelang.org/go/cue" 24 24 "cuelang.org/go/cue/errors" 25 25 "cuelang.org/go/cue/token" 26 - "cuelang.org/go/internal" 27 - "cuelang.org/go/internal/core/adt" 26 + "cuelang.org/go/internal/value" 28 27 ) 29 28 30 29 // A Context provides context for running a task. ··· 117 116 } 118 117 119 118 func (t *taskError) InputPositions() (a []token.Pos) { 120 - _, nx := internal.CoreValue(t.v) 119 + _, nx := value.ToInternal(t.v) 121 120 122 - for _, x := range nx.(*adt.Vertex).Conjuncts { 121 + for _, x := range nx.Conjuncts { 123 122 if src := x.Source(); src != nil { 124 123 a = append(a, src.Pos()) 125 124 }
+37
internal/types/value.go
··· 1 + // Copyright 2021 CUE Authors 2 + // 3 + // Licensed under the Apache License, Version 2.0 (the "License"); 4 + // you may not use this file except in compliance with the License. 5 + // You may obtain a copy of the License at 6 + // 7 + // http://www.apache.org/licenses/LICENSE-2.0 8 + // 9 + // Unless required by applicable law or agreed to in writing, software 10 + // distributed under the License is distributed on an "AS IS" BASIS, 11 + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 + // See the License for the specific language governing permissions and 13 + // limitations under the License. 14 + 15 + package types 16 + 17 + import ( 18 + "cuelang.org/go/internal/core/adt" 19 + "cuelang.org/go/internal/core/runtime" 20 + ) 21 + 22 + type Value struct { 23 + R *runtime.Runtime 24 + V *adt.Vertex 25 + } 26 + 27 + type Interface interface { 28 + Core(v *Value) 29 + } 30 + 31 + func CastValue(t *Value, x interface{}) bool { 32 + c, ok := x.(Interface) 33 + if ok { 34 + c.Core(t) 35 + } 36 + return ok 37 + }
+40
internal/value/value.go
··· 1 + // Copyright 2021 CUE Authors 2 + // 3 + // Licensed under the Apache License, Version 2.0 (the "License"); 4 + // you may not use this file except in compliance with the License. 5 + // You may obtain a copy of the License at 6 + // 7 + // http://www.apache.org/licenses/LICENSE-2.0 8 + // 9 + // Unless required by applicable law or agreed to in writing, software 10 + // distributed under the License is distributed on an "AS IS" BASIS, 11 + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 + // See the License for the specific language governing permissions and 13 + // limitations under the License. 14 + 15 + // Package value contains functions for converting values to internal types 16 + // and various other Value-related utilities. 17 + package value 18 + 19 + import ( 20 + "cuelang.org/go/cue" 21 + "cuelang.org/go/internal/core/adt" 22 + "cuelang.org/go/internal/core/runtime" 23 + "cuelang.org/go/internal/types" 24 + ) 25 + 26 + func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) { 27 + var t types.Value 28 + v.Core(&t) 29 + return t.R, t.V 30 + } 31 + 32 + // TODO: 33 + // 34 + // func Make(r *runtime.Runtime, v *adt.Vertex) cue.Value { 35 + // return cue.Value{} 36 + // } 37 + 38 + // func MakeError(r *runtime.Runtime, err error) cue.Value { 39 + // return cue.Value{} 40 + // }
+4 -6
tools/flow/flow.go
··· 71 71 72 72 "cuelang.org/go/cue" 73 73 "cuelang.org/go/cue/errors" 74 - "cuelang.org/go/internal" 75 74 "cuelang.org/go/internal/core/adt" 76 75 "cuelang.org/go/internal/core/convert" 77 76 "cuelang.org/go/internal/core/eval" 78 - "cuelang.org/go/internal/core/runtime" 77 + "cuelang.org/go/internal/value" 79 78 ) 80 79 81 80 var ( ··· 197 196 // New creates a Controller for a given Instance and TaskFunc. 198 197 func New(cfg *Config, inst *cue.Instance, f TaskFunc) *Controller { 199 198 v := inst.Value() 200 - rx, nx := internal.CoreValue(v) 201 - ctx := eval.NewContext(rx.(*runtime.Runtime), nx.(*adt.Vertex)) 199 + ctx := eval.NewContext(value.ToInternal(v)) 202 200 203 201 c := &Controller{ 204 202 isTask: f, ··· 335 333 } 336 334 337 335 func (t *Task) vertex() *adt.Vertex { 338 - _, x := internal.CoreValue(t.v) 339 - return x.(*adt.Vertex) 336 + _, x := value.ToInternal(t.v) 337 + return x 340 338 } 341 339 342 340 func (t *Task) addDep(path string, dep *Task) {
+3 -6
tools/flow/run.go
··· 28 28 import ( 29 29 "cuelang.org/go/cue" 30 30 "cuelang.org/go/cue/errors" 31 - "cuelang.org/go/internal" 32 31 "cuelang.org/go/internal/core/adt" 33 32 "cuelang.org/go/internal/core/eval" 34 - "cuelang.org/go/internal/core/runtime" 33 + "cuelang.org/go/internal/value" 35 34 ) 36 35 37 36 func (c *Controller) runLoop() { 38 - _, x := internal.CoreValue(c.inst) 39 - root := x.(*adt.Vertex) 37 + _, root := value.ToInternal(c.inst) 40 38 41 39 // Copy the initial conjuncts. 42 40 n := len(root.Conjuncts) ··· 64 62 t.state = Running 65 63 c.updateTaskValue(t) 66 64 67 - rx, nx := internal.CoreValue(t.v) 68 - t.ctxt = eval.NewContext(rx.(*runtime.Runtime), nx.(*adt.Vertex)) 65 + t.ctxt = eval.NewContext(value.ToInternal(t.v)) 69 66 70 67 go func(t *Task) { 71 68 if err := t.r.Run(t, nil); err != nil {
+2 -3
tools/flow/tasks.go
··· 20 20 import ( 21 21 "cuelang.org/go/cue" 22 22 "cuelang.org/go/cue/errors" 23 - "cuelang.org/go/internal" 24 23 "cuelang.org/go/internal/core/adt" 25 24 "cuelang.org/go/internal/core/dep" 25 + "cuelang.org/go/internal/value" 26 26 ) 27 27 28 28 // initTasks takes the current configuration and adds tasks to the list of ··· 82 82 // getTask finds and marks tasks that are descendents of v. 83 83 func (c *Controller) getTask(scope *Task, v cue.Value) *Task { 84 84 // Look up cached node. 85 - _, n := internal.CoreValue(v) 86 - w := n.(*adt.Vertex) 85 + _, w := value.ToInternal(v) 87 86 if t, ok := c.nodes[w]; ok { 88 87 return t 89 88 }
+2 -5
tools/trim/trim.go
··· 56 56 "cuelang.org/go/cue" 57 57 "cuelang.org/go/cue/ast" 58 58 "cuelang.org/go/cue/ast/astutil" 59 - "cuelang.org/go/internal" 60 59 "cuelang.org/go/internal/core/adt" 61 60 "cuelang.org/go/internal/core/debug" 62 - "cuelang.org/go/internal/core/runtime" 63 61 "cuelang.org/go/internal/core/subsume" 62 + "cuelang.org/go/internal/value" 64 63 ) 65 64 66 65 // Config configures trim options. ··· 73 72 // Trimming is done on a best-effort basis and only when the removed field 74 73 // is clearly implied by another field, rather than equal sibling fields. 75 74 func Files(files []*ast.File, inst *cue.Instance, cfg *Config) error { 76 - rx, vx := internal.CoreValue(inst.Value()) 77 - r := rx.(*runtime.Runtime) 78 - v := vx.(*adt.Vertex) 75 + r, v := value.ToInternal(inst.Value()) 79 76 80 77 t := &trimmer{ 81 78 Config: *cfg,