rano/util/structs/structs.go
Ankit Patial 26a00c9f7c working on auth.
mailer, basic setup with html template and a dev treansport
2024-11-15 21:42:15 +05:30

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
}