Merge pull request #29 from fatih/fix-tag-names
structs: fix custom tag names for nested structs
This commit is contained in:
commit
b4641b79cf
@ -92,7 +92,9 @@ func (s *Struct) Map() map[string]interface{} {
|
||||
if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
|
||||
// look out for embedded structs, and convert them to a
|
||||
// map[string]interface{} too
|
||||
finalVal = Map(val.Interface())
|
||||
n := New(val.Interface())
|
||||
n.TagName = s.TagName
|
||||
finalVal = n.Map()
|
||||
} else {
|
||||
finalVal = val.Interface()
|
||||
}
|
||||
|
||||
@ -115,17 +115,21 @@ func TestMap_Tag(t *testing.T) {
|
||||
|
||||
func TestMap_CustomTag(t *testing.T) {
|
||||
var T = struct {
|
||||
A string `dd:"x"`
|
||||
B int `dd:"y"`
|
||||
C bool `dd:"z"`
|
||||
A string `json:"x"`
|
||||
B int `json:"y"`
|
||||
C bool `json:"z"`
|
||||
D struct {
|
||||
E string `json:"jkl"`
|
||||
} `json:"nested"`
|
||||
}{
|
||||
A: "a-value",
|
||||
B: 2,
|
||||
C: true,
|
||||
}
|
||||
T.D.E = "e-value"
|
||||
|
||||
s := New(T)
|
||||
s.TagName = "dd"
|
||||
s.TagName = "json"
|
||||
|
||||
a := s.Map()
|
||||
|
||||
@ -144,6 +148,20 @@ func TestMap_CustomTag(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
nested, ok := a["nested"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("Map should contain the D field that is tagged as 'nested'")
|
||||
}
|
||||
|
||||
e, ok := nested["jkl"].(string)
|
||||
if !ok {
|
||||
t.Fatalf("Map should contain the D.E field that is tagged as 'jkl'")
|
||||
}
|
||||
|
||||
if e != "e-value" {
|
||||
t.Errorf("D.E field should be equal to 'e-value', got: '%v'", e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMap_MultipleCustomTag(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user