Skip to content

Commit 58e292a

Browse files
author
Dean Karn
authored
Merge pull request #15 from htmd/master
get correct field name in struct tag
2 parents 5b5b101 + 730ac5a commit 58e292a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"reflect"
55
"sync"
66
"sync/atomic"
7+
"strings"
78
)
89

910
type cachedField struct {
@@ -76,6 +77,10 @@ func (s *structCacheMap) parseStruct(mode Mode, current reflect.Value, key refle
7677
continue
7778
}
7879

80+
if commaIndex := strings.Index(name, ","); commaIndex != -1 {
81+
name = name[:commaIndex]
82+
}
83+
7984
if mode == ModeExplicit && len(name) == 0 {
8085
continue
8186
}

decoder_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,3 +1451,25 @@ func TestDecoderExplicit(t *testing.T) {
14511451
Equal(t, test.Name, "Joeybloggs")
14521452
Equal(t, test.Age, 0)
14531453
}
1454+
1455+
func TestDecoderStructWithJSONTag(t *testing.T) {
1456+
type Test struct {
1457+
Name string `json:"name,omitempty"`
1458+
Age int `json:",omitempty"`
1459+
}
1460+
1461+
values := map[string][]string{
1462+
"name": {"Joeybloggs"},
1463+
"Age": {"3"},
1464+
}
1465+
1466+
var test Test
1467+
1468+
d := NewDecoder()
1469+
d.SetTagName("json")
1470+
1471+
err := d.Decode(&test, values)
1472+
Equal(t, err, nil)
1473+
Equal(t, test.Name, "Joeybloggs")
1474+
Equal(t, test.Age, int(3))
1475+
}

0 commit comments

Comments
 (0)