21
0
Fork 0

v0.0.381
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m28s Details

This commit is contained in:
Mike Schwörer 2024-01-23 17:51:52 +01:00
parent 890e16241d
commit 7e16e799e4
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 74 additions and 2 deletions

View File

@ -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"

View 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()
}