v0.0.549
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m4s

This commit is contained in:
Mike Schwörer 2024-12-10 13:24:06 +01:00
parent 62f2ce9268
commit a8c6e39ac5
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 19 additions and 3 deletions

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.548" const GoextVersion = "0.0.549"
const GoextVersionTimestamp = "2024-12-09T17:39:35+0100" const GoextVersionTimestamp = "2024-12-10T13:24:06+0100"

View File

@ -4,11 +4,13 @@ import (
"encoding/json" "encoding/json"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"reflect" "reflect"
"strings"
) )
type ConvertStructToMapOpt struct { type ConvertStructToMapOpt struct {
KeepJsonMarshalTypes bool KeepJsonMarshalTypes bool
MaxDepth *int MaxDepth *int
UseTagsAsKeys *string
} }
func ConvertStructToMap(v any, opts ...ConvertStructToMapOpt) map[string]any { func ConvertStructToMap(v any, opts ...ConvertStructToMapOpt) map[string]any {
@ -90,7 +92,21 @@ func reflectToMap(fv reflect.Value, depth int, opt ConvertStructToMapOpt) any {
for i := 0; i < fv.NumField(); i++ { for i := 0; i < fv.NumField(); i++ {
if fv.Type().Field(i).IsExported() { if fv.Type().Field(i).IsExported() {
res[fv.Type().Field(i).Name] = reflectToMap(fv.Field(i), depth+1, opt)
k := fv.Type().Field(i).Name
if opt.UseTagsAsKeys != nil {
if tagval, ok := fv.Type().Field(i).Tag.Lookup(*opt.UseTagsAsKeys); ok {
if strings.Contains(tagval, ",") {
k = strings.TrimSpace(strings.Split(tagval, ",")[0])
} else {
k = strings.TrimSpace(tagval)
}
} else {
continue
}
}
res[k] = reflectToMap(fv.Field(i), depth+1, opt)
} }
} }