Skip to content

Commit

Permalink
Fix: open list lit for fill value (#87)
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <[email protected]>

Signed-off-by: FogDong <[email protected]>
  • Loading branch information
FogDong committed Jan 3, 2023
1 parent 6b7697b commit dd7a3c0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/cue/model/sets/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func strategyUnify(base cue.Value, patch cue.Value, params *UnifyParams, patchOp
} else if params.PatchStrategy == StrategyJSONPatch {
return jsonPatch(base, patch.LookupPath(cue.ParsePath("operations")))
}
openBase, err := openListLit(base)
openBase, err := OpenListLit(base)
if err != nil {
return cue.Value{}, errors.Wrapf(err, "failed to open list it for merge")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cue/model/sets/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,9 @@ func OpenBaiscLit(val cue.Value) (*ast.File, error) {
return f, err
}

// OpenListLit make that the listLit can be modified.
// nolint:staticcheck
func openListLit(val cue.Value) (*ast.File, error) {
func OpenListLit(val cue.Value) (*ast.File, error) {
f, err := ToFile(val.Syntax(cue.Docs(true), cue.ResolveReferences(true)))
if err != nil {
return nil, err
Expand Down
7 changes: 6 additions & 1 deletion pkg/cue/model/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ func (val *Value) FillRaw(x string, paths ...string) error {

// FillValueByScript unify the value x at the given script path.
func (val *Value) FillValueByScript(x *Value, path string) error {
newV := val.v.FillPath(FieldPath(path), x.v)
f, err := sets.OpenListLit(val.v)
if err != nil {
return err
}
v := val.r.BuildFile(f)
newV := v.FillPath(FieldPath(path), x.v)
if err := newV.Err(); err != nil {
return err
}
Expand Down
28 changes: 14 additions & 14 deletions pkg/cue/model/value/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,13 @@ func TestFillByScript(t *testing.T) {
v string
expected string
}{
{
name: "insert array",
raw: `a: ["hello"]`,
path: "a[1]",
v: `"world"`,
expected: `a: ["hello", "world", ...]
`},
{
name: "insert array",
raw: `a: b: [{x: 100},...]`,
Expand Down Expand Up @@ -970,7 +977,7 @@ a: b: c: [{x: 100}, {x: 101}, {x: 102}]`,
x: 101
}, {
x: 102
}]
}, ...]
}
}
`,
Expand All @@ -986,9 +993,9 @@ a: b: c: [{x: 100}, {x: 101}, {x: 102}]`,
y: [{
name: "key"
value: "foo"
}]
}, ...]
}
}]
}, ...]
}
`,
},
Expand All @@ -1002,9 +1009,9 @@ a: b: c: [{x: 100}, {x: 101}, {x: 102}]`,
x: {
y: [{
name: "key"
}]
}, ...]
}
}]
}, ...]
c: {
x: "foo"
}
Expand All @@ -1021,9 +1028,9 @@ a: b: c: [{x: 100}, {x: 101}, {x: 102}]`,
x: {
y: [{
name: "key"
}]
}, ...]
}
}]
}, ...]
c: {
x: "foo"
}
Expand Down Expand Up @@ -1073,13 +1080,6 @@ a: b: c: [{x: 100}, {x: 101}, {x: 102}]`,
v: `"foo"`,
err: "a.b.0.x.y.0.name: conflicting values \"foo\" and \"key\"",
},
{
name: "filled value with incompatible list lengths",
raw: `a: b: [{x: y:[{name: "key"}]}]`,
path: "a.b[3].x.y[0].value",
v: `"foo"`,
err: "a.b: incompatible list lengths (1 and 5)",
},
}

for _, errCase := range errCases {
Expand Down

0 comments on commit dd7a3c0

Please sign in to comment.