Decoder: allow commas in tags (#693)

This commit is contained in:
Thomas Pelletier
2021-11-30 21:59:22 -05:00
committed by GitHub
parent 0d20a84523
commit c862c344b3
2 changed files with 37 additions and 2 deletions
+9 -2
View File
@@ -1163,8 +1163,15 @@ func forEachField(t reflect.Type, path []int, do func(name string, path []int))
continue
}
name, ok := f.Tag.Lookup("toml")
if !ok {
name := f.Tag.Get("toml")
if name == "-" {
continue
}
if i := strings.IndexByte(name, ','); i >= 0 {
name = name[:i]
}
if name == "" {
name = f.Name
}
+28
View File
@@ -2698,6 +2698,34 @@ world'`,
}
}
func TestUnmarshalTags(t *testing.T) {
type doc struct {
Dash string `toml:"-,"`
Ignore string `toml:"-"`
A string `toml:"hello"`
B string `toml:"comma,omitempty"`
}
data := `
'-' = "dash"
Ignore = 'me'
hello = 'content'
comma = 'ok'
`
d := doc{}
expected := doc{
Dash: "dash",
Ignore: "",
A: "content",
B: "ok",
}
err := toml.Unmarshal([]byte(data), &d)
require.NoError(t, err)
require.Equal(t, expected, d)
}
func TestASCIIControlCharacters(t *testing.T) {
invalidCharacters := []byte{0x7F}
for c := byte(0x0); c <= 0x08; c++ {