Decoder: show struct field in type mismatch errors (#684)

The goal is to provide some context as to why the type were mismatched. This
change only works for that case, on structs. This is the same a encoding/json. A
more general solution would be great, but this would require a broader change in
the decoder, which I don't think is necessary at the moment.

Fixes #628
This commit is contained in:
Thomas Pelletier
2021-11-24 20:43:56 -05:00
committed by GitHub
parent d8997efb5a
commit b226db6a29
2 changed files with 57 additions and 11 deletions
+14
View File
@@ -1790,6 +1790,20 @@ func TestUnmarshalOverflows(t *testing.T) {
}
}
func TestUnmarshalErrors(t *testing.T) {
type mystruct struct {
Bar string
}
data := `bar = 42`
s := mystruct{}
err := toml.Unmarshal([]byte(data), &s)
require.Error(t, err)
require.Equal(t, "toml: cannot decode TOML integer into struct field toml_test.mystruct.Bar of type string", err.Error())
}
func TestUnmarshalInvalidTarget(t *testing.T) {
x := "foo"
err := toml.Unmarshal([]byte{}, x)