mirror of
https://github.com/wonli/aqi.git
synced 2024-06-28 09:57:05 +08:00
31 lines
468 B
Go
31 lines
468 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
)
|
||
|
|
||
|
func StructToMap(input any) map[string]any {
|
||
|
out := make(map[string]any)
|
||
|
v := reflect.ValueOf(input)
|
||
|
if v.Kind() == reflect.Ptr {
|
||
|
v = v.Elem()
|
||
|
}
|
||
|
|
||
|
// we only accept structs
|
||
|
if v.Kind() != reflect.Struct {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
typ := v.Type()
|
||
|
for i := 0; i < v.NumField(); i++ {
|
||
|
fi := typ.Field(i)
|
||
|
// skip unexported fields
|
||
|
if fi.PkgPath != "" {
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
out[fi.Name] = v.Field(i).Interface()
|
||
|
}
|
||
|
return out
|
||
|
}
|