appcore/structs/structs.go

35 lines
607 B
Go
Raw Normal View History

2025-06-16 22:19:00 +05:30
// Copyright 2024 Patial Tech (Ankit Patial).
// All rights reserved.
package structs
import (
"reflect"
)
func Map(obj any) map[string]any {
result := make(map[string]any)
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
typ := val.Type()
for i := range val.NumField() {
fieldName := typ.Field(i).Name
fieldValueKind := val.Field(i).Kind()
var fieldValue any
if fieldValueKind == reflect.Struct {
fieldValue = Map(val.Field(i).Interface())
} else {
fieldValue = val.Field(i).Interface()
}
result[fieldName] = fieldValue
}
return result
}