Skip to content

Commit 22abdad

Browse files
committed
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 <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 80a0a6e commit 22abdad

File tree

14 files changed

+110
-58
lines changed

14 files changed

+110
-58
lines changed

cmd/cue/cmd/custom.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import (
3030
"cuelang.org/go/cue"
3131
"cuelang.org/go/cue/errors"
3232
"cuelang.org/go/internal"
33-
"cuelang.org/go/internal/core/adt"
3433
itask "cuelang.org/go/internal/task"
34+
"cuelang.org/go/internal/value"
3535
_ "cuelang.org/go/pkg/tool/cli" // Register tasks
3636
_ "cuelang.org/go/pkg/tool/exec"
3737
_ "cuelang.org/go/pkg/tool/file"
@@ -76,8 +76,7 @@ func addCustom(c *Command, parent *cobra.Command, typ, name string, tools *cue.I
7676
// TODO: remove this block to allow commands to be defined in any file.
7777
outer:
7878
for _, v := range []cue.Value{tools.Lookup(typ), o} {
79-
_, vv := internal.CoreValue(v)
80-
w := vv.(*adt.Vertex)
79+
_, w := value.ToInternal(v)
8180
for _, c := range w.Conjuncts {
8281
src := c.Source()
8382
if src == nil {

cue/build.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ func init() {
5656
}
5757
return &Runtime{idx: newIndex()}
5858
}
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-
}
6659
}
6760

6861
func dummyLoad(token.Pos, string) *build.Instance { return nil }

cue/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"cuelang.org/go/internal/core/runtime"
4141
"cuelang.org/go/internal/core/subsume"
4242
"cuelang.org/go/internal/core/validate"
43+
"cuelang.org/go/internal/types"
4344
)
4445

4546
// Kind determines the underlying type of a Value.
@@ -564,6 +565,14 @@ type Value struct {
564565
v *adt.Vertex
565566
}
566567

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
574+
}
575+
567576
func newErrValue(v Value, b *adt.Bottom) Value {
568577
node := &adt.Vertex{BaseValue: b}
569578
if v.v != nil {

internal/core/convert/go.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ import (
3333
"cuelang.org/go/cue/errors"
3434
"cuelang.org/go/cue/parser"
3535
"cuelang.org/go/cue/token"
36-
"cuelang.org/go/internal"
3736
"cuelang.org/go/internal/core/adt"
3837
"cuelang.org/go/internal/core/compile"
39-
"cuelang.org/go/internal/core/runtime"
38+
"cuelang.org/go/internal/types"
4039
)
4140

4241
// This file contains functionality for converting Go to CUE.
@@ -225,14 +224,9 @@ func isNil(x reflect.Value) bool {
225224
}
226225

227226
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
236230
}
237231
src := ctx.Source()
238232
switch v := x.(type) {

internal/core/convert/go_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/cockroachdb/apd/v2"
2424
"github.com/google/go-cmp/cmp"
2525

26-
_ "cuelang.org/go/cue" // set internal.CoreValue
2726
"cuelang.org/go/cue/errors"
2827
"cuelang.org/go/internal/core/adt"
2928
"cuelang.org/go/internal/core/convert"

internal/core/dep/dep_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ import (
2020
"testing"
2121

2222
"cuelang.org/go/cue"
23-
"cuelang.org/go/internal"
2423
"cuelang.org/go/internal/core/adt"
2524
"cuelang.org/go/internal/core/debug"
2625
"cuelang.org/go/internal/core/dep"
2726
"cuelang.org/go/internal/core/eval"
28-
"cuelang.org/go/internal/core/runtime"
2927
"cuelang.org/go/internal/cuetest"
3028
"cuelang.org/go/internal/cuetxtar"
29+
"cuelang.org/go/internal/value"
3130
)
3231

3332
func TestVisit(t *testing.T) {
@@ -45,8 +44,7 @@ func TestVisit(t *testing.T) {
4544
t.Fatal(inst.Err())
4645
}
4746

48-
rx, nx := internal.CoreValue(inst)
49-
ctxt := eval.NewContext(rx.(*runtime.Runtime), nx.(*adt.Vertex))
47+
ctxt := eval.NewContext(value.ToInternal(inst))
5048

5149
testCases := []struct {
5250
name string
@@ -69,8 +67,7 @@ func TestVisit(t *testing.T) {
6967
for _, tc := range testCases {
7068
v := inst.LookupPath(cue.ParsePath(tc.root))
7169

72-
_, nx = internal.CoreValue(v)
73-
n := nx.(*adt.Vertex)
70+
_, n := value.ToInternal(v)
7471
w := t.Writer(tc.name)
7572

7673
t.Run(tc.name, func(sub *testing.T) {
@@ -105,9 +102,7 @@ func TestX(t *testing.T) {
105102

106103
v := inst.Lookup("a")
107104

108-
rx, nx := internal.CoreValue(v)
109-
r := rx.(*runtime.Runtime)
110-
n := nx.(*adt.Vertex)
105+
r, n := value.ToInternal(v)
111106

112107
ctxt := eval.NewContext(r, n)
113108

internal/internal.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ var UnifyBuiltin func(v interface{}, kind string) interface{}
7070
// GetRuntime reports the runtime for an Instance or Value.
7171
var GetRuntime func(instance interface{}) interface{}
7272

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-
7773
// MakeInstance makes a new instance from a value.
7874
var MakeInstance func(value interface{}) (instance interface{})
7975

internal/task/task.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import (
2323
"cuelang.org/go/cue"
2424
"cuelang.org/go/cue/errors"
2525
"cuelang.org/go/cue/token"
26-
"cuelang.org/go/internal"
27-
"cuelang.org/go/internal/core/adt"
26+
"cuelang.org/go/internal/value"
2827
)
2928

3029
// A Context provides context for running a task.
@@ -117,9 +116,9 @@ func (t *taskError) Position() token.Pos {
117116
}
118117

119118
func (t *taskError) InputPositions() (a []token.Pos) {
120-
_, nx := internal.CoreValue(t.v)
119+
_, nx := value.ToInternal(t.v)
121120

122-
for _, x := range nx.(*adt.Vertex).Conjuncts {
121+
for _, x := range nx.Conjuncts {
123122
if src := x.Source(); src != nil {
124123
a = append(a, src.Pos())
125124
}

internal/types/value.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

internal/value/value.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// }

0 commit comments

Comments
 (0)