Skip to content

Commit

Permalink
cue: decode empty list as empty slice instead of slice-typed nil
Browse files Browse the repository at this point in the history
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 <bozaro@yandex.ru>
Change-Id: I040c09343df12a153e6b2ee645ec9fd118b524ee
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167647
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
  • Loading branch information
bozaro authored and mvdan committed Aug 18, 2023
1 parent 3286ead commit 66ebe0f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cue/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ func (d *decoder) interfaceValue(v Value) (x interface{}) {
for list.Next() {
a = append(a, d.interfaceValue(list.Value()))
}
if a == nil {
a = []interface{}{}
}
x = a

case StructKind:
Expand Down
4 changes: 4 additions & 0 deletions cue/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func TestDecode(t *testing.T) {
`,
dst: &S{},
err: "Decode: x: cannot use value 1 (type int) as (string|bytes)",
}, {
value: `[]`,
dst: new(interface{}),
want: []interface{}{},
}}
for _, tc := range testCases {
t.Run(tc.value, func(t *testing.T) {
Expand Down

0 comments on commit 66ebe0f

Please sign in to comment.