basic common app packages
This commit is contained in:
34
structs/structs.go
Normal file
34
structs/structs.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// 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
|
||||
}
|
Reference in New Issue
Block a user