Skip to content

Commit 66efc67

Browse files
committed
cue: allow setting list elements in FillPath
This is a quick-n-dirty implementation, but it works for now. Fixes #923 Change-Id: Idbc6dac2d0403db86a7853d2100e0a85acc08812 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9526 Reviewed-by: Paul Jolly <[email protected]> Reviewed-by: CUE cueckoo <[email protected]>
1 parent 819cf95 commit 66efc67

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

cue/types.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,12 +1615,26 @@ func (v Value) FillPath(p Path, x interface{}) Value {
16151615
expr = convert.GoValueToValue(ctx, x, true)
16161616
}
16171617
for i := len(p.path) - 1; i >= 0; i-- {
1618-
expr = &adt.StructLit{Decls: []adt.Decl{
1619-
&adt.Field{
1620-
Label: p.path[i].sel.feature(v.idx),
1621-
Value: expr,
1622-
},
1623-
}}
1618+
switch sel := p.path[i]; {
1619+
case sel.sel.kind() == adt.IntLabel:
1620+
i := sel.sel.feature(ctx.Runtime).Index()
1621+
list := &adt.ListLit{}
1622+
any := &adt.Top{}
1623+
// TODO(perf): make this a constant thing. This will be possible with the query extension.
1624+
for k := 0; k < i; k++ {
1625+
list.Elems = append(list.Elems, any)
1626+
}
1627+
list.Elems = append(list.Elems, expr, &adt.Ellipsis{})
1628+
expr = list
1629+
1630+
default:
1631+
expr = &adt.StructLit{Decls: []adt.Decl{
1632+
&adt.Field{
1633+
Label: p.path[i].sel.feature(v.idx),
1634+
Value: expr,
1635+
},
1636+
}}
1637+
}
16241638
}
16251639
n := &adt.Vertex{}
16261640
n.AddConjunct(adt.MakeRootConjunct(nil, expr))

cue/types_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,11 @@ func TestFillPath(t *testing.T) {
11341134
`,
11351135
x: ast.NewIdent("#foo"),
11361136
out: `{1, #foo: 1}`,
1137+
}, {
1138+
in: `[...int]`,
1139+
x: 1,
1140+
path: ParsePath("0"),
1141+
out: `[1]`,
11371142
}}
11381143

11391144
for _, tc := range testCases {

0 commit comments

Comments
 (0)