v0.0.381
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m28s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m28s
This commit is contained in:
parent
890e16241d
commit
7e16e799e4
@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.380"
|
||||
const GoextVersion = "0.0.381"
|
||||
|
||||
const GoextVersionTimestamp = "2024-01-22T12:33:41+0100"
|
||||
const GoextVersionTimestamp = "2024-01-23T17:51:52+0100"
|
||||
|
72
reflectext/convertToMap.go
Normal file
72
reflectext/convertToMap.go
Normal file
@ -0,0 +1,72 @@
|
||||
package reflectext
|
||||
|
||||
import "reflect"
|
||||
|
||||
func ConvertStructToMap(v any) any {
|
||||
return reflectToMap(reflect.ValueOf(v))
|
||||
}
|
||||
|
||||
func reflectToMap(fv reflect.Value) any {
|
||||
|
||||
if fv.Kind() == reflect.Ptr {
|
||||
|
||||
if fv.IsNil() {
|
||||
return nil
|
||||
} else {
|
||||
return reflectToMap(fv.Elem())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if fv.Kind() == reflect.Func {
|
||||
|
||||
// skip
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
if fv.Kind() == reflect.Array {
|
||||
|
||||
arrlen := fv.Len()
|
||||
arr := make([]any, arrlen)
|
||||
for i := 0; i < arrlen; i++ {
|
||||
arr[i] = reflectToMap(fv.Index(i))
|
||||
}
|
||||
return arr
|
||||
|
||||
}
|
||||
|
||||
if fv.Kind() == reflect.Slice {
|
||||
|
||||
arrlen := fv.Len()
|
||||
arr := make([]any, arrlen)
|
||||
for i := 0; i < arrlen; i++ {
|
||||
arr[i] = reflectToMap(fv.Index(i))
|
||||
}
|
||||
return arr
|
||||
|
||||
}
|
||||
|
||||
if fv.Kind() == reflect.Chan {
|
||||
|
||||
// skip
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
if fv.Kind() == reflect.Struct {
|
||||
|
||||
res := make(map[string]any)
|
||||
|
||||
for i := 0; i < fv.NumField(); i++ {
|
||||
if fv.Type().Field(i).IsExported() {
|
||||
res[fv.Type().Field(i).Name] = reflectToMap(fv.Field(i))
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
|
||||
}
|
||||
|
||||
return fv.Interface()
|
||||
}
|
Loading…
Reference in New Issue
Block a user