Skip to content

Commit 9e7d4d6

Browse files
committed
internal/core/export: treat empty data vertices as structs
The current algorithm treated them as `_` Fixes #1131 Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Change-Id: Ib8834b7574619c48b1b649551a91d55d2f0b8885 Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/528072 Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
1 parent 4d505e0 commit 9e7d4d6

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

cue/cuecontext/cuecontext_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ func TestAPI(t *testing.T) {
3535
return ctx.BuildExpr(expr)
3636
},
3737
want: `close({})`,
38+
}, {
39+
name: "issue1131",
40+
fun: func() cue.Value {
41+
m := make(map[string]interface{})
42+
ctx := New()
43+
cv := ctx.Encode(m)
44+
return cv
45+
},
46+
want: "", // empty file.
3847
}}
3948
for _, tc := range testCases {
4049
t.Run(tc.name, func(t *testing.T) {

internal/core/export/export_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ func TestGenerated(t *testing.T) {
203203
},
204204
out: `"foo", #enum: 2`,
205205
p: export.All,
206+
}, {
207+
// Issue #1131
208+
in: func(r *adt.OpContext) (adt.Expr, error) {
209+
m := make(map[string]interface{})
210+
v := ctx.Encode(m)
211+
_, x := value.ToInternal(v)
212+
return x, nil
213+
},
214+
out: ``, // empty file
215+
}, {
216+
in: func(r *adt.OpContext) (adt.Expr, error) {
217+
v := &adt.Vertex{}
218+
v.SetValue(r, adt.Finalized, &adt.StructMarker{})
219+
return v, nil
220+
},
221+
out: ``, // empty file
206222
}}
207223
for _, tc := range testCases {
208224
t.Run("", func(t *testing.T) {

internal/core/export/expr.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (x *exporter) mergeValues(label adt.Feature, src *adt.Vertex, a []conjunct,
173173
if len(e.attrs) > 0 {
174174
break
175175
}
176-
if len(e.structs) > 0 {
176+
if len(e.structs) > 0 || e.isData {
177177
return e.wrapCloseIfNecessary(s, src)
178178
}
179179
return ast.NewIdent("_")
@@ -278,6 +278,10 @@ type conjuncts struct {
278278
fields map[adt.Feature]field
279279
attrs []*ast.Attribute
280280
hasEllipsis bool
281+
282+
// A value is a struct if it has a non-zero structs slice or if isData is
283+
// set to true. Data vertices may not have conjuncts associated with them.
284+
isData bool
281285
}
282286

283287
func (c *conjuncts) addValueConjunct(src *adt.Vertex, env *adt.Environment, x adt.Expr) {
@@ -376,6 +380,7 @@ func (e *conjuncts) addExpr(env *adt.Environment, src *adt.Vertex, x adt.Expr, i
376380

377381
case v.IsData():
378382
e.structs = append(e.structs, v.Structs...)
383+
e.isData = true
379384

380385
if y, ok := v.BaseValue.(adt.Value); ok {
381386
e.addValueConjunct(src, env, y)

0 commit comments

Comments
 (0)