fix nesting of time fields

This commit is contained in:
sergeyt 2015-11-23 01:41:40 +06:00
parent d2e1722aca
commit 80b007702e
2 changed files with 22 additions and 2 deletions

View File

@ -2,6 +2,7 @@
package structs
import "reflect"
import "time"
var (
// DefaultTagName is the default tag name for struct fields which provides
@ -89,7 +90,7 @@ func (s *Struct) Map() map[string]interface{} {
}
}
if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
if IsStruct(val.Interface()) && !isTime(val.Interface()) && !tagOpts.Has("omitnested") {
// look out for embedded structs, and convert them to a
// map[string]interface{} too
n := New(val.Interface())
@ -148,7 +149,7 @@ func (s *Struct) Values() []interface{} {
}
}
if IsStruct(val.Interface()) && !tagOpts.Has("omitnested") {
if IsStruct(val.Interface()) && !isTime(val.Interface()) && !tagOpts.Has("omitnested") {
// look out for embedded structs, and convert them to a
// []interface{} to be added to the final values slice
for _, embeddedVal := range Values(val.Interface()) {
@ -447,3 +448,8 @@ func IsStruct(s interface{}) bool {
func Name(s interface{}) string {
return New(s).Name()
}
func isTime(v interface{}) bool {
_, ok := v.(time.Time)
return ok
}

View File

@ -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{}{}