Skip to content

Commit 9bb874c

Browse files
committed
cue: Expr handles empty lists to or/and builtins
Reflect semantics of builtins, which translate empty lists to an error or top, respectively. Fixes #1234 Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Change-Id: I9cb3fd242f9ccfa86213fee198125617c1d68dcf Signed-off-by: Marcel van Lohuizen <mpvl@golang.org> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/527312 Unity-Result: CUEcueckoo <cueckoo@cuelang.org> TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Reviewed-by: Paul Jolly <paul@myitcv.io>
1 parent 72936e4 commit 9bb874c

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

cue/types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,20 @@ func (v Value) Expr() (Op, []Value) {
23702370
if fn.Name == "and" {
23712371
op = AndOp
23722372
}
2373+
2374+
if len(a) == 0 {
2375+
// Mimic semantics of builtin.
2376+
switch op {
2377+
case AndOp:
2378+
a = append(a, remakeValue(v, env, &adt.Top{}))
2379+
case OrOp:
2380+
a = append(a, remakeValue(v, env, &adt.Bottom{
2381+
Code: adt.IncompleteError,
2382+
Err: errors.Newf(x.Src.Fun.Pos(), "empty list in call to or"),
2383+
}))
2384+
}
2385+
op = NoOp
2386+
}
23732387
break
23742388
}
23752389
a = append(a, remakeValue(v, env, x.Fun))

cue/types_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,13 +3351,20 @@ func TestExpr(t *testing.T) {
33513351
want: `&({c:a} {b:a})`,
33523352
}, {
33533353
input: `v: [...number] | *[1, 2, 3]`,
3354-
want: `([...number]|*[1,2,3])`,
3354+
// Filter defaults that are subsumed by another value.
3355+
want: `[...number]`,
33553356
}, {
33563357
input: `v: or([1, 2, 3])`,
33573358
want: `|(1 2 3)`,
3359+
}, {
3360+
input: `v: or([])`,
3361+
want: `_|_(empty list in call to or)`,
33583362
}, {
33593363
input: `v: and([1, 2, 3])`,
33603364
want: `&(1 2 3)`,
3365+
}, {
3366+
input: `v: and([])`,
3367+
want: `_`,
33613368
}}
33623369
for _, tc := range testCases {
33633370
t.Run(tc.input, func(t *testing.T) {
@@ -3373,7 +3380,7 @@ func TestExpr(t *testing.T) {
33733380
func exprStr(v Value) string {
33743381
op, operands := v.Expr()
33753382
if op == NoOp {
3376-
return compactRawStr(v)
3383+
return compactRawStr(operands[0])
33773384
}
33783385
s := op.String()
33793386
s += "("

encoding/openapi/openapi_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,36 @@ func walk(om *openapi.OrderedMap) {
217217
}
218218
}
219219

220+
// TODO: move OpenAPI testing to txtar and allow errors.
221+
func TestIssue1234(t *testing.T) {
222+
var r cue.Runtime
223+
inst, err := r.Compile("test", `
224+
#Test: or([])
225+
226+
`)
227+
if err != nil {
228+
t.Fatal(err)
229+
}
230+
231+
_, err = openapi.Gen(inst, &openapi.Config{})
232+
if err == nil {
233+
t.Fatal("expected error")
234+
}
235+
}
236+
220237
// This is for debugging purposes. Do not remove.
221238
func TestX(t *testing.T) {
222239
t.Skip()
223240

224241
var r cue.Runtime
225242
inst, err := r.Compile("test", `
226-
AnyField: "any value"
227243
`)
228244
if err != nil {
229245
t.Fatal(err)
230246
}
231247

232248
b, err := openapi.Gen(inst, &openapi.Config{
233-
ExpandReferences: true,
249+
// ExpandReferences: true,
234250
})
235251
if err != nil {
236252
t.Fatal(errors.Details(err, nil))

0 commit comments

Comments
 (0)