Skip to content

Commit

Permalink
cue: allow Value to be set at any path in Fill
Browse files Browse the repository at this point in the history
Change-Id: I9f1df1c4bfde299ebb2914b573d7c4989fc64fe7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6460
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jun 25, 2020
1 parent ab43a15 commit 097cf16
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
6 changes: 6 additions & 0 deletions cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ func convertRec(ctx *context, src source, nilIsTop bool, x interface{}) evaluate
}
return &nullLit{src.base()}

case Value:
if ctx.index != v.ctx().index {
panic("value of type Value is not created with same Runtime as Instance")
}
return v.eval(ctx)

case *ast.File:
x := newVisitorCtx(ctx, nil, nil, nil, false)
return ctx.manifest(x.walk(v))
Expand Down
10 changes: 1 addition & 9 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,7 @@ func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
for i := len(path) - 1; i >= 0; i-- {
x = map[string]interface{}{path[i]: x}
}
var value evaluated
if v, ok := x.(Value); ok {
if inst.index != v.ctx().index {
panic("value of type Value is not created with same Runtime as Instance")
}
value = v.eval(ctx)
} else {
value = convert(ctx, root, true, x)
}
value := convert(ctx, root, true, x)
eval := binOp(ctx, baseValue{}, opUnify, root, value)
// TODO: validate recursively?
err := inst.Err
Expand Down
10 changes: 1 addition & 9 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,15 +1596,7 @@ func (v Value) Fill(x interface{}, path ...string) Value {
for i := len(path) - 1; i >= 0; i-- {
x = map[string]interface{}{path[i]: x}
}
var value evaluated
if v, ok := x.(Value); ok {
if ctx.index != v.ctx().index {
panic("value of type Value is not created with same Runtime as Instance")
}
value = v.eval(ctx)
} else {
value = convert(ctx, root, true, x)
}
value := convert(ctx, root, true, x)
a := v.path.arc
a.v = mkBin(ctx, v.Pos(), opUnify, root, value)
a.cache = a.v.evalPartial(ctx)
Expand Down
28 changes: 28 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,34 @@ func TestFill(t *testing.T) {
}
}

func TestFill2(t *testing.T) {
r := &Runtime{}

root, err := r.Compile("test", `
#Provider: {
ID: string
notConcrete: bool
}
`)

if err != nil {
t.Fatal(err)
}

spec := root.LookupDef("#Provider")
providerInstance := spec.Fill("12345", "ID")
root, err = root.Fill(providerInstance, "providers", "myprovider")
if err != nil {
t.Fatal(err)
}

got := fmt.Sprint(root.Value())

if got != `{#Provider: C{ID: string, notConcrete: bool}, providers: {myprovider: C{ID: (string & "12345"), notConcrete: bool}}}` {
t.Error(got)
}
}

func TestValue_LookupDef(t *testing.T) {
r := &Runtime{}

Expand Down

0 comments on commit 097cf16

Please sign in to comment.