From fcfa9f7043a82bf22670547e44d6fe006534a5f5 Mon Sep 17 00:00:00 2001 From: Dimitri Tcaciuc Date: Tue, 11 Jan 2022 21:40:49 -0800 Subject: [PATCH] Decode S=[] into a non-nil []interface{}. This restores v0.3 behaviour and also makes it consistent with how the same decodes into something like struct { S []string }. Closes #338 --- decode_test.go | 11 +++++++++++ parse.go | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/decode_test.go b/decode_test.go index c89b572c..f826ecfb 100644 --- a/decode_test.go +++ b/decode_test.go @@ -618,6 +618,17 @@ func TestDecodeSlices(t *testing.T) { } } +func TestDecodeInterfaceSlice(t *testing.T) { + var v map[string]interface{} + if _, err := Decode(`S = []`, &v); err != nil { + t.Errorf(err.Error()) + } + // S = [] should decode into a non-nil []interface{} (see issue #338). + if reflect.ValueOf(v["S"]).IsNil() { + t.Errorf("S is nil") + } +} + func TestDecodePrimitive(t *testing.T) { type S struct { P Primitive diff --git a/parse.go b/parse.go index 618cbe4d..05535a8e 100644 --- a/parse.go +++ b/parse.go @@ -350,6 +350,10 @@ func (p *parser) valueArray(it item) (interface{}, tomlType) { array []interface{} types []tomlType ) + // Initialize to a non-nil empty slice. This makes it + // consistent with how S = [] decodes into a non-nil slice + // inside something like struct { S []string }. + array = []interface{}{} for it = p.next(); it.typ != itemArrayEnd; it = p.next() { if it.typ == itemCommentStart { p.expect(itemText)