structure: check for pointer structs

This commit is contained in:
Fatih Arslan 2014-07-30 21:17:46 +03:00
parent 778d3311fd
commit 4875a9916b
2 changed files with 7 additions and 6 deletions

View File

@ -29,7 +29,8 @@ func Map(s interface{}) map[string]interface{} {
val := v.Field(i) val := v.Field(i)
var finalVal interface{} var finalVal interface{}
if val.Kind() == reflect.Struct {
if IsStruct(val.Interface()) {
// 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
finalVal = Map(val.Interface()) finalVal = Map(val.Interface())
@ -64,7 +65,7 @@ func Values(s interface{}) []interface{} {
t := make([]interface{}, 0) t := make([]interface{}, 0)
for i := range fields { for i := range fields {
val := v.Field(i) val := v.Field(i)
if val.Kind() == reflect.Struct { if IsStruct(val.Interface()) {
// 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()) {
@ -93,7 +94,7 @@ func IsZero(s interface{}) bool {
for i := range fields { for i := range fields {
val := v.Field(i) val := v.Field(i)
if val.Kind() == reflect.Struct { if IsStruct(val.Interface()) {
ok := IsZero(val.Interface()) ok := IsZero(val.Interface())
if !ok { if !ok {
return false return false
@ -130,7 +131,7 @@ func Fields(s interface{}) []string {
keys := make([]string, 0) keys := make([]string, 0)
for i, field := range fields { for i, field := range fields {
val := v.Field(i) val := v.Field(i)
if val.Kind() == reflect.Struct { if IsStruct(val.Interface()) {
// look out for embedded structs, and convert them to a // look out for embedded structs, and convert them to a
// []string to be added to the final values slice // []string to be added to the final values slice
for _, embeddedVal := range Fields(val.Interface()) { for _, embeddedVal := range Fields(val.Interface()) {

View File

@ -93,10 +93,10 @@ func TestMap_Nested(t *testing.T) {
type A struct { type A struct {
Name string Name string
} }
a := A{Name: "example"} a := &A{Name: "example"}
type B struct { type B struct {
A A A *A
} }
b := &B{A: a} b := &B{A: a}