Skip to content

Commit

Permalink
Decode S=[] into a non-nil []interface{}. (#339)
Browse files Browse the repository at this point in the history
This restores v0.3 behaviour and also makes it
consistent with how the same decodes into something
like struct { S []string }.

Closes #338
  • Loading branch information
dtcaciuc committed Jan 12, 2022
1 parent 7d0236f commit 9515b92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions decode_test.go
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions parse.go
Expand Up @@ -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)
Expand Down

0 comments on commit 9515b92

Please sign in to comment.