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

Commit

Permalink
cue: fix marshaling bug
Browse files Browse the repository at this point in the history
Closes #326

Change-Id: I9568c60e953f645af5080446f354217a8dd3f234
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5420
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 1, 2020
1 parent 56509a5 commit 72dba3d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/issue315.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cmp stderr expect-stderr

-- expect-stderr --
incomplete value X.y in interpolation:
incomplete value 'X.y' in interpolation:
./file.cue:16:3
./file.cue:3:5
-- file.cue --
Expand Down
2 changes: 1 addition & 1 deletion cue/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (x *interpolation) evalPartial(ctx *context) (result evaluated) {
}
if incomplete != nil {
return ctx.mkErr(incomplete, codeIncomplete,
"incomplete value %s in interpolation", ctx.str(incomplete))
"incomplete value '%s' in interpolation", ctx.str(incomplete))
}
return &stringLit{x.baseValue, buf.String(), nil}
}
Expand Down
13 changes: 12 additions & 1 deletion cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,18 @@ func (v Value) marshalJSON() (b []byte, err error) {
i := Iterator{ctx: ctx, val: v, iter: l, len: len(l.elem.arcs)}
return marshalList(&i)
case structKind:
obj, _ := v.structValData(ctx)
obj, err := v.structValData(ctx)
st := obj.obj
if len(st.comprehensions) > 0 {
// This should always evaluate to incomplete. However, fall back
// to a bad error message, rather than crashing, in case it doesn't.
err, _ := st.comprehensions[0].comp.evalPartial(ctx).(*bottom)
return nil, toMarshalErr(v, err)
}

if err != nil {
return nil, toMarshalErr(v, err)
}
return obj.marshalJSON()
case bottomKind:
return nil, toMarshalErr(v, x.(*bottom))
Expand Down
30 changes: 30 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,36 @@ func TestMarshalJSON(t *testing.T) {
foo: Task & {"op": "pull"}
`,
json: `{"foo":{"op":"pull","tag":"latest","tagInString":"latestdd"}}`,
}, {
// Issue #326
value: `x: "\(string)": "v"`,
err: `x: incomplete value 'string' in interpolation`,
}, {
// Issue #326
value: `x: "\(bool)": "v"`,
err: `x: expression in interpolation must evaluate to a number kind or string (found bool)`,
}, {
// Issue #326
value: `
x: {
for k, v in y {
"\(k)": v
}
}
y: {}
`,
json: `{"x":{},"y":{}}`,
}, {
// Issue #326
value: `
x: {
for k, v in y {
"\(k)": v
}
}
y: _
`,
err: `x: incomplete feed source`,
}}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d/%v", i, tc.value), func(t *testing.T) {
Expand Down

0 comments on commit 72dba3d

Please sign in to comment.