From 66ebe0ff3031096da45b79a2d18c5c66af22b16e Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Mon, 3 Jul 2023 23:40:48 +0300 Subject: [PATCH] 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 f96a18c9. Signed-off-by: Artem V. Navrotskiy Change-Id: I040c09343df12a153e6b2ee645ec9fd118b524ee Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167647 Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo Reviewed-by: Paul Jolly --- cue/decode.go | 3 +++ cue/decode_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/cue/decode.go b/cue/decode.go index ba50dab1a42..19f0251b205 100644 --- a/cue/decode.go +++ b/cue/decode.go @@ -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: diff --git a/cue/decode_test.go b/cue/decode_test.go index a3b60ca50f7..acb435fd0a6 100644 --- a/cue/decode_test.go +++ b/cue/decode_test.go @@ -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) {