Skip to content

Commit 097cf16

Browse files
committed
cue: allow Value to be set at any path in Fill
Change-Id: I9f1df1c4bfde299ebb2914b573d7c4989fc64fe7 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6460 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent ab43a15 commit 097cf16

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

cue/go.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ func convertRec(ctx *context, src source, nilIsTop bool, x interface{}) evaluate
214214
}
215215
return &nullLit{src.base()}
216216

217+
case Value:
218+
if ctx.index != v.ctx().index {
219+
panic("value of type Value is not created with same Runtime as Instance")
220+
}
221+
return v.eval(ctx)
222+
217223
case *ast.File:
218224
x := newVisitorCtx(ctx, nil, nil, nil, false)
219225
return ctx.manifest(x.walk(v))

cue/instance.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,7 @@ func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
334334
for i := len(path) - 1; i >= 0; i-- {
335335
x = map[string]interface{}{path[i]: x}
336336
}
337-
var value evaluated
338-
if v, ok := x.(Value); ok {
339-
if inst.index != v.ctx().index {
340-
panic("value of type Value is not created with same Runtime as Instance")
341-
}
342-
value = v.eval(ctx)
343-
} else {
344-
value = convert(ctx, root, true, x)
345-
}
337+
value := convert(ctx, root, true, x)
346338
eval := binOp(ctx, baseValue{}, opUnify, root, value)
347339
// TODO: validate recursively?
348340
err := inst.Err

cue/types.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,15 +1596,7 @@ func (v Value) Fill(x interface{}, path ...string) Value {
15961596
for i := len(path) - 1; i >= 0; i-- {
15971597
x = map[string]interface{}{path[i]: x}
15981598
}
1599-
var value evaluated
1600-
if v, ok := x.(Value); ok {
1601-
if ctx.index != v.ctx().index {
1602-
panic("value of type Value is not created with same Runtime as Instance")
1603-
}
1604-
value = v.eval(ctx)
1605-
} else {
1606-
value = convert(ctx, root, true, x)
1607-
}
1599+
value := convert(ctx, root, true, x)
16081600
a := v.path.arc
16091601
a.v = mkBin(ctx, v.Pos(), opUnify, root, value)
16101602
a.cache = a.v.evalPartial(ctx)

cue/types_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,34 @@ func TestFill(t *testing.T) {
842842
}
843843
}
844844

845+
func TestFill2(t *testing.T) {
846+
r := &Runtime{}
847+
848+
root, err := r.Compile("test", `
849+
#Provider: {
850+
ID: string
851+
notConcrete: bool
852+
}
853+
`)
854+
855+
if err != nil {
856+
t.Fatal(err)
857+
}
858+
859+
spec := root.LookupDef("#Provider")
860+
providerInstance := spec.Fill("12345", "ID")
861+
root, err = root.Fill(providerInstance, "providers", "myprovider")
862+
if err != nil {
863+
t.Fatal(err)
864+
}
865+
866+
got := fmt.Sprint(root.Value())
867+
868+
if got != `{#Provider: C{ID: string, notConcrete: bool}, providers: {myprovider: C{ID: (string & "12345"), notConcrete: bool}}}` {
869+
t.Error(got)
870+
}
871+
}
872+
845873
func TestValue_LookupDef(t *testing.T) {
846874
r := &Runtime{}
847875

0 commit comments

Comments
 (0)