Skip to content

Commit 66ebe0f

Browse files
bozaromvdan
authored andcommitted
cue: decode empty list as empty slice instead of slice-typed nil
These three cases weren't consistent, since using cue.Value.Decode on an empty interface type as a destination would result in nil: { value: `[]`, dst: new(interface{}), want: ([]interface{})(nil), }, { value: `[]`, dst: new([]interface{}), want: []interface{}{}, }, { value: `[]`, dst: new([]int), want: []int{}, } Now all result in an empty slice, which is consistent and intuitive. Closes #2471 as merged as of commit f96a18c. Signed-off-by: Artem V. Navrotskiy <[email protected]> Change-Id: I040c09343df12a153e6b2ee645ec9fd118b524ee Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167647 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 3286ead commit 66ebe0f

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

cue/decode.go

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ func (d *decoder) interfaceValue(v Value) (x interface{}) {
280280
for list.Next() {
281281
a = append(a, d.interfaceValue(list.Value()))
282282
}
283+
if a == nil {
284+
a = []interface{}{}
285+
}
283286
x = a
284287

285288
case StructKind:

cue/decode_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ func TestDecode(t *testing.T) {
231231
`,
232232
dst: &S{},
233233
err: "Decode: x: cannot use value 1 (type int) as (string|bytes)",
234+
}, {
235+
value: `[]`,
236+
dst: new(interface{}),
237+
want: []interface{}{},
234238
}}
235239
for _, tc := range testCases {
236240
t.Run(tc.value, func(t *testing.T) {

0 commit comments

Comments
 (0)