updated the fix to avoid performance impact (PR #40)

This commit is contained in:
sergeyt 2015-11-24 20:12:59 +06:00
parent 80b007702e
commit 77ad876ced

View File

@ -2,7 +2,6 @@
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
@ -90,12 +89,17 @@ func (s *Struct) Map() map[string]interface{} {
} }
} }
if IsStruct(val.Interface()) && !isTime(val.Interface()) && !tagOpts.Has("omitnested") { if IsStruct(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())
n.TagName = s.TagName n.TagName = s.TagName
finalVal = n.Map() m := n.Map()
if len(m) == 0 {
finalVal = val.Interface()
} else {
finalVal = m
}
} else { } else {
finalVal = val.Interface() finalVal = val.Interface()
} }
@ -149,7 +153,7 @@ func (s *Struct) Values() []interface{} {
} }
} }
if IsStruct(val.Interface()) && !isTime(val.Interface()) && !tagOpts.Has("omitnested") { if IsStruct(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()) {
@ -448,8 +452,3 @@ 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
}