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

Commit

Permalink
cue: allow setting list elements in FillPath
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mpvl committed Apr 29, 2021
1 parent 819cf95 commit 66efc67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 20 additions & 6 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,12 +1615,26 @@ func (v Value) FillPath(p Path, x interface{}) Value {
expr = convert.GoValueToValue(ctx, x, true)
}
for i := len(p.path) - 1; i >= 0; i-- {
expr = &adt.StructLit{Decls: []adt.Decl{
&adt.Field{
Label: p.path[i].sel.feature(v.idx),
Value: expr,
},
}}
switch sel := p.path[i]; {
case sel.sel.kind() == adt.IntLabel:
i := sel.sel.feature(ctx.Runtime).Index()
list := &adt.ListLit{}
any := &adt.Top{}
// TODO(perf): make this a constant thing. This will be possible with the query extension.
for k := 0; k < i; k++ {
list.Elems = append(list.Elems, any)
}
list.Elems = append(list.Elems, expr, &adt.Ellipsis{})
expr = list

default:
expr = &adt.StructLit{Decls: []adt.Decl{
&adt.Field{
Label: p.path[i].sel.feature(v.idx),
Value: expr,
},
}}
}
}
n := &adt.Vertex{}
n.AddConjunct(adt.MakeRootConjunct(nil, expr))
Expand Down
5 changes: 5 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,11 @@ func TestFillPath(t *testing.T) {
`,
x: ast.NewIdent("#foo"),
out: `{1, #foo: 1}`,
}, {
in: `[...int]`,
x: 1,
path: ParsePath("0"),
out: `[1]`,
}}

for _, tc := range testCases {
Expand Down

0 comments on commit 66efc67

Please sign in to comment.