fix nesting of time fields
This commit is contained in:
parent
d2e1722aca
commit
80b007702e
10
structs.go
10
structs.go
@ -2,6 +2,7 @@
|
|||||||
package structs
|
package structs
|
||||||
|
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
import "time"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultTagName is the default tag name for struct fields which provides
|
// 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
|
// look out for embedded structs, and convert them to a
|
||||||
// map[string]interface{} too
|
// map[string]interface{} too
|
||||||
n := New(val.Interface())
|
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
|
// look out for embedded structs, and convert them to a
|
||||||
// []interface{} to be added to the final values slice
|
// []interface{} to be added to the final values slice
|
||||||
for _, embeddedVal := range Values(val.Interface()) {
|
for _, embeddedVal := range Values(val.Interface()) {
|
||||||
@ -447,3 +448,8 @@ func IsStruct(s interface{}) bool {
|
|||||||
func Name(s interface{}) string {
|
func Name(s interface{}) string {
|
||||||
return New(s).Name()
|
return New(s).Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isTime(v interface{}) bool {
|
||||||
|
_, ok := v.(time.Time)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|||||||
@ -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) {
|
func TestStruct(t *testing.T) {
|
||||||
var T = struct{}{}
|
var T = struct{}{}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user