Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal: replace internal.CoreValue with more type-safe variant
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mpvl committed Apr 13, 2021
1 parent 80a0a6e commit 22abdad
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 58 deletions.
5 changes: 2 additions & 3 deletions cmd/cue/cmd/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
itask "cuelang.org/go/internal/task"
"cuelang.org/go/internal/value"
_ "cuelang.org/go/pkg/tool/cli" // Register tasks
_ "cuelang.org/go/pkg/tool/exec"
_ "cuelang.org/go/pkg/tool/file"
Expand Down Expand Up @@ -76,8 +76,7 @@ func addCustom(c *Command, parent *cobra.Command, typ, name string, tools *cue.I
// TODO: remove this block to allow commands to be defined in any file.
outer:
for _, v := range []cue.Value{tools.Lookup(typ), o} {
_, vv := internal.CoreValue(v)
w := vv.(*adt.Vertex)
_, w := value.ToInternal(v)
for _, c := range w.Conjuncts {
src := c.Source()
if src == nil {
Expand Down
7 changes: 0 additions & 7 deletions cue/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ func init() {
}
return &Runtime{idx: newIndex()}
}

internal.CoreValue = func(value interface{}) (runtime, vertex interface{}) {
if v, ok := value.(Value); ok && v.v != nil {
return v.idx, v.v
}
return nil, nil
}
}

func dummyLoad(token.Pos, string) *build.Instance { return nil }
Expand Down
9 changes: 9 additions & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/core/subsume"
"cuelang.org/go/internal/core/validate"
"cuelang.org/go/internal/types"
)

// Kind determines the underlying type of a Value.
Expand Down Expand Up @@ -564,6 +565,14 @@ type Value struct {
v *adt.Vertex
}

type hiddenValue = Value

// Core is for internal use only.
func (v hiddenValue) Core(x *types.Value) {
x.V = v.v
x.R = v.idx
}

func newErrValue(v Value, b *adt.Bottom) Value {
node := &adt.Vertex{BaseValue: b}
if v.v != nil {
Expand Down
14 changes: 4 additions & 10 deletions internal/core/convert/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ import (
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/compile"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/types"
)

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

func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
if internal.CoreValue != nil {
if ii, iv := internal.CoreValue(x); ii != nil {
i := ii.(*runtime.Runtime)
v := iv.(*adt.Vertex)
// TODO: panic if nto the same runtime.
_ = i
return v
}
if t := (&types.Value{}); types.CastValue(t, x) {
// TODO: panic if nto the same runtime.
return t.V
}
src := ctx.Source()
switch v := x.(type) {
Expand Down
1 change: 0 additions & 1 deletion internal/core/convert/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/cockroachdb/apd/v2"
"github.com/google/go-cmp/cmp"

_ "cuelang.org/go/cue" // set internal.CoreValue
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/convert"
Expand Down
13 changes: 4 additions & 9 deletions internal/core/dep/dep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import (
"testing"

"cuelang.org/go/cue"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/debug"
"cuelang.org/go/internal/core/dep"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/cuetest"
"cuelang.org/go/internal/cuetxtar"
"cuelang.org/go/internal/value"
)

func TestVisit(t *testing.T) {
Expand All @@ -45,8 +44,7 @@ func TestVisit(t *testing.T) {
t.Fatal(inst.Err())
}

rx, nx := internal.CoreValue(inst)
ctxt := eval.NewContext(rx.(*runtime.Runtime), nx.(*adt.Vertex))
ctxt := eval.NewContext(value.ToInternal(inst))

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

_, nx = internal.CoreValue(v)
n := nx.(*adt.Vertex)
_, n := value.ToInternal(v)
w := t.Writer(tc.name)

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

v := inst.Lookup("a")

rx, nx := internal.CoreValue(v)
r := rx.(*runtime.Runtime)
n := nx.(*adt.Vertex)
r, n := value.ToInternal(v)

ctxt := eval.NewContext(r, n)

Expand Down
4 changes: 0 additions & 4 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ var UnifyBuiltin func(v interface{}, kind string) interface{}
// GetRuntime reports the runtime for an Instance or Value.
var GetRuntime func(instance interface{}) interface{}

// CoreValue returns an *runtime.Index and *adt.Vertex for a cue.Value.
// It returns nil if value is not a cue.Value.
var CoreValue func(value interface{}) (runtime, vertex interface{})

// MakeInstance makes a new instance from a value.
var MakeInstance func(value interface{}) (instance interface{})

Expand Down
7 changes: 3 additions & 4 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/value"
)

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

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

for _, x := range nx.(*adt.Vertex).Conjuncts {
for _, x := range nx.Conjuncts {
if src := x.Source(); src != nil {
a = append(a, src.Pos())
}
Expand Down
37 changes: 37 additions & 0 deletions internal/types/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2021 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

import (
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/runtime"
)

type Value struct {
R *runtime.Runtime
V *adt.Vertex
}

type Interface interface {
Core(v *Value)
}

func CastValue(t *Value, x interface{}) bool {
c, ok := x.(Interface)
if ok {
c.Core(t)
}
return ok
}
40 changes: 40 additions & 0 deletions internal/value/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package value contains functions for converting values to internal types
// and various other Value-related utilities.
package value

import (
"cuelang.org/go/cue"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/types"
)

func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) {
var t types.Value
v.Core(&t)
return t.R, t.V
}

// TODO:
//
// func Make(r *runtime.Runtime, v *adt.Vertex) cue.Value {
// return cue.Value{}
// }

// func MakeError(r *runtime.Runtime, err error) cue.Value {
// return cue.Value{}
// }
10 changes: 4 additions & 6 deletions tools/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/convert"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/value"
)

var (
Expand Down Expand Up @@ -197,8 +196,7 @@ func (c *Controller) addErr(err error, msg string) {
// New creates a Controller for a given Instance and TaskFunc.
func New(cfg *Config, inst *cue.Instance, f TaskFunc) *Controller {
v := inst.Value()
rx, nx := internal.CoreValue(v)
ctx := eval.NewContext(rx.(*runtime.Runtime), nx.(*adt.Vertex))
ctx := eval.NewContext(value.ToInternal(v))

c := &Controller{
isTask: f,
Expand Down Expand Up @@ -335,8 +333,8 @@ func (t *Task) isReady() bool {
}

func (t *Task) vertex() *adt.Vertex {
_, x := internal.CoreValue(t.v)
return x.(*adt.Vertex)
_, x := value.ToInternal(t.v)
return x
}

func (t *Task) addDep(path string, dep *Task) {
Expand Down
9 changes: 3 additions & 6 deletions tools/flow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ package flow
import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/value"
)

func (c *Controller) runLoop() {
_, x := internal.CoreValue(c.inst)
root := x.(*adt.Vertex)
_, root := value.ToInternal(c.inst)

// Copy the initial conjuncts.
n := len(root.Conjuncts)
Expand Down Expand Up @@ -64,8 +62,7 @@ func (c *Controller) runLoop() {
t.state = Running
c.updateTaskValue(t)

rx, nx := internal.CoreValue(t.v)
t.ctxt = eval.NewContext(rx.(*runtime.Runtime), nx.(*adt.Vertex))
t.ctxt = eval.NewContext(value.ToInternal(t.v))

go func(t *Task) {
if err := t.r.Run(t, nil); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions tools/flow/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ package flow
import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/dep"
"cuelang.org/go/internal/value"
)

// initTasks takes the current configuration and adds tasks to the list of
Expand Down Expand Up @@ -82,8 +82,7 @@ func (c *Controller) findRootTasks(v cue.Value) {
// getTask finds and marks tasks that are descendents of v.
func (c *Controller) getTask(scope *Task, v cue.Value) *Task {
// Look up cached node.
_, n := internal.CoreValue(v)
w := n.(*adt.Vertex)
_, w := value.ToInternal(v)
if t, ok := c.nodes[w]; ok {
return t
}
Expand Down
7 changes: 2 additions & 5 deletions tools/trim/trim.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/ast/astutil"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/debug"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/core/subsume"
"cuelang.org/go/internal/value"
)

// Config configures trim options.
Expand All @@ -73,9 +72,7 @@ type Config struct {
// Trimming is done on a best-effort basis and only when the removed field
// is clearly implied by another field, rather than equal sibling fields.
func Files(files []*ast.File, inst *cue.Instance, cfg *Config) error {
rx, vx := internal.CoreValue(inst.Value())
r := rx.(*runtime.Runtime)
v := vx.(*adt.Vertex)
r, v := value.ToInternal(inst.Value())

t := &trimmer{
Config: *cfg,
Expand Down

0 comments on commit 22abdad

Please sign in to comment.