From c862c344b302d865a2db7db2dadf09b7bfed6c52 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Tue, 30 Nov 2021 21:59:22 -0500 Subject: [PATCH] Decoder: allow commas in tags (#693) --- unmarshaler.go | 11 +++++++++-- unmarshaler_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/unmarshaler.go b/unmarshaler.go index a0237bc..28dce4e 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -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 } diff --git a/unmarshaler_test.go b/unmarshaler_test.go index e771dec..c29f9ac 100644 --- a/unmarshaler_test.go +++ b/unmarshaler_test.go @@ -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++ {