diff --git a/structs.go b/structs.go index a0b77e6..f3dcf9a 100644 --- a/structs.go +++ b/structs.go @@ -94,7 +94,12 @@ func (s *Struct) Map() map[string]interface{} { // map[string]interface{} too n := New(val.Interface()) n.TagName = s.TagName - finalVal = n.Map() + m := n.Map() + if len(m) == 0 { + finalVal = val.Interface() + } else { + finalVal = m + } } else { finalVal = val.Interface() } diff --git a/structs_test.go b/structs_test.go index 14e3de7..43af9c8 100644 --- a/structs_test.go +++ b/structs_test.go @@ -296,6 +296,20 @@ func TestMap_Anonymous(t *testing.T) { } } +func TestMap_TimeField(t *testing.T) { + type A struct { + CreatedAt time.Time + } + + a := &A{CreatedAt: time.Now().UTC()} + m := Map(a) + + _, ok := m["CreatedAt"].(time.Time) + if !ok { + t.Error("Time field must be final") + } +} + func TestStruct(t *testing.T) { var T = struct{}{}