34 lines
576 B
Go
34 lines
576 B
Go
package structs
|
|
|
|
import (
|
|
"reflect"
|
|
)
|
|
|
|
func Map(obj interface{}) map[string]interface{} {
|
|
result := make(map[string]interface{})
|
|
|
|
val := reflect.ValueOf(obj)
|
|
|
|
if val.Kind() == reflect.Ptr {
|
|
val = val.Elem()
|
|
}
|
|
|
|
typ := val.Type()
|
|
|
|
for i := 0; i < val.NumField(); i++ {
|
|
fieldName := typ.Field(i).Name
|
|
fieldValueKind := val.Field(i).Kind()
|
|
var fieldValue interface{}
|
|
|
|
if fieldValueKind == reflect.Struct {
|
|
fieldValue = Map(val.Field(i).Interface())
|
|
} else {
|
|
fieldValue = val.Field(i).Interface()
|
|
}
|
|
|
|
result[fieldName] = fieldValue
|
|
}
|
|
|
|
return result
|
|
}
|