Skip to content

Commit b49cbbd

Browse files
committed
cue: add NewList
Fixes #343: Fill already supports filling Values. There is a NewValue planned as well, which adds full struct creation support, but with this CL all requirements for this issue are satisfied. Change-Id: Ia421ec0a9f16b3f73d4fce0a63dd4be8cec03181 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9502 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 6d67b48 commit b49cbbd

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

cue/context.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,26 @@ func (c *Context) EncodeType(x interface{}, option ...EncodeOption) Value {
249249
return c.make(n)
250250
}
251251

252+
// NewList creates a Value that is a list of the given values.
253+
//
254+
// All Values must be created by c.
255+
func (c *Context) NewList(v ...Value) Value {
256+
a := make([]adt.Value, len(v))
257+
for i, x := range v {
258+
if x.idx != (*runtime.Runtime)(c) {
259+
panic("values must be from same Context")
260+
}
261+
a[i] = x.v
262+
}
263+
return c.make(c.ctx().NewList(a...))
264+
}
265+
252266
// TODO:
253267

254268
// func (c *Context) NewExpr(op Op, v ...Value) Value {
255269
// return Value{}
256270
// }
257271

258-
// func (c *Context) NewList(v ...Value) Value {
259-
// return Value{}
260-
// }
261-
262272
// func (c *Context) NewValue(v ...ValueElem) Value {
263273
// return Value{}
264274
// }

cue/context_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2021 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cue_test
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
21+
"cuelang.org/go/cue"
22+
"cuelang.org/go/cue/cuecontext"
23+
)
24+
25+
func TestNewList(t *testing.T) {
26+
ctx := cuecontext.New()
27+
28+
intList := ctx.CompileString("[...int]")
29+
30+
l123 := ctx.NewList(
31+
ctx.Encode(1),
32+
ctx.Encode(2),
33+
ctx.Encode(3),
34+
)
35+
36+
testCases := []struct {
37+
desc string
38+
v cue.Value
39+
out string
40+
}{{
41+
v: ctx.NewList(),
42+
out: `[]`,
43+
}, {
44+
v: l123,
45+
out: `[1, 2, 3]`,
46+
}, {
47+
v: l123.Unify(intList),
48+
out: `[1, 2, 3]`,
49+
}, {
50+
v: l123.Unify(intList).Unify(l123),
51+
out: `[1, 2, 3]`,
52+
}, {
53+
v: intList.Unify(ctx.NewList(ctx.Encode("string"))),
54+
out: `_|_ // 0: conflicting values "string" and int (mismatched types string and int)`,
55+
}, {
56+
v: ctx.NewList().Unify(l123),
57+
out: `_|_ // incompatible list lengths (0 and 3)`,
58+
}, {
59+
v: ctx.NewList(
60+
intList,
61+
intList,
62+
).Unify(ctx.NewList(
63+
ctx.NewList(
64+
ctx.Encode(1),
65+
ctx.Encode(3),
66+
),
67+
ctx.NewList(
68+
ctx.Encode(5),
69+
ctx.Encode(7),
70+
),
71+
)),
72+
out: `[[1, 3], [5, 7]]`,
73+
}}
74+
for _, tc := range testCases {
75+
t.Run(tc.desc, func(t *testing.T) {
76+
got := fmt.Sprint(tc.v)
77+
if got != tc.out {
78+
t.Errorf(" got: %v\nwant: %v", got, tc.out)
79+
}
80+
})
81+
}
82+
}

internal/core/adt/eval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,7 @@ outer:
20742074

20752075
// Terminate early n case of runaway comprehension.
20762076
if !isOpen && int(index) > max {
2077-
n.invalidListLength(max, int(index), maxNode, l.list)
2077+
n.invalidListLength(max, len(l.list.Elems), maxNode, l.list)
20782078
continue outer
20792079
}
20802080
}

0 commit comments

Comments
 (0)