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: error on array table mismatched type #804

Merged
merged 2 commits into from Aug 15, 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
4 changes: 2 additions & 2 deletions unmarshaler.go
Expand Up @@ -344,9 +344,9 @@ func (d *decoder) handleArrayTableCollectionLast(key ast.Iterator, v reflect.Val
elem := v.Index(idx)
_, err := d.handleArrayTable(key, elem)
return v, err
default:
return reflect.Value{}, fmt.Errorf("toml: cannot decode array table into a %s", v.Type())
}

return d.handleArrayTable(key, v)
}

// When parsing an array table expression, each part of the key needs to be
Expand Down
38 changes: 38 additions & 0 deletions unmarshaler_test.go
Expand Up @@ -1735,6 +1735,28 @@ B = "data"`,
}
},
},
{
desc: "kv that points to a slice",
input: "a.b.c = 'foo'",
gen: func() test {
doc := map[string][]string{}
return test{
target: &doc,
err: true,
}
},
},
{
desc: "kv that points to a pointer to a slice",
input: "a.b.c = 'foo'",
gen: func() test {
doc := map[string]*[]string{}
return test{
target: &doc,
err: true,
}
},
},
}

for _, e := range examples {
Expand Down Expand Up @@ -2413,6 +2435,22 @@ Host = 'main.domain.com'
require.Equal(t, expected, string(b))
}

func TestIssue799(t *testing.T) {
const testTOML = `
# notice the double brackets
[[test]]
answer = 42
`

var s struct {
// should be []map[string]int
Test map[string]int `toml:"test"`
}

err := toml.Unmarshal([]byte(testTOML), &s)
require.Error(t, err)
}

func TestUnmarshalDecodeErrors(t *testing.T) {
examples := []struct {
desc string
Expand Down