Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode S=[] into a non-nil []interface{}. #339

Merged
merged 1 commit into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also test nested arrays and inline tables?

arr = []
nested = [[]]
tbl = {arr = []}

Just to make sure this keeps working in all future updates!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at this a bit closer the tests should be integrated with TestDecodeSlices, I'll just merge this as-is and fix it up; thanks!

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{}{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you mention your issue in this comment? Just for future reference.

Also, might as well move this up with the declaration: array = []interface{}{} rather than defining it and then assigning a value.

for it = p.next(); it.typ != itemArrayEnd; it = p.next() {
if it.typ == itemCommentStart {
p.expect(itemText)
Expand Down