rano/util/structs/structs.go
2024-11-18 20:53:13 +05:30

38 lines
733 B
Go

// Copyright 2024 Patial Tech. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
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
}