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

Commit

Permalink
cue: Value.Expr: process or and and builtins semantically.
Browse files Browse the repository at this point in the history
Fixes #928

Change-Id: I9db16a30931ab025272029e747ad26daefba526b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9523
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 28, 2021
1 parent 1ad66ab commit f60c88a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,21 @@ func (v Value) Expr() (Op, []Value) {
a = append(a, remakeValue(v, env, x.Hi))
op = SliceOp
case *adt.CallExpr:
// Interpret "and" and "or" builtin semantically.
if fn, ok := x.Fun.(*adt.Builtin); ok && len(x.Args) == 1 &&
(fn.Name == "or" || fn.Name == "and") {

iter, _ := remakeValue(v, env, x.Args[0]).List()
for iter.Next() {
a = append(a, iter.Value())
}

op = OrOp
if fn.Name == "and" {
op = AndOp
}
break
}
a = append(a, remakeValue(v, env, x.Fun))
for _, arg := range x.Args {
a = append(a, remakeValue(v, env, arg))
Expand Down
6 changes: 6 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3326,6 +3326,12 @@ func TestExpr(t *testing.T) {
}, {
input: `v: [...number] | *[1, 2, 3]`,
want: `([...number]|*[1,2,3])`,
}, {
input: `v: or([1, 2, 3])`,
want: `|(1 2 3)`,
}, {
input: `v: and([1, 2, 3])`,
want: `&(1 2 3)`,
}}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
Expand Down

0 comments on commit f60c88a

Please sign in to comment.