1
1
mirror of https://github.com/wader/fq.git synced 2024-12-25 14:23:18 +03:00

Merge pull request #313 from wader/mapstruct-camelcase-nested

mapstruct: Handle nested values when converting to camel case
This commit is contained in:
Mattias Wadman 2022-07-10 10:57:17 +02:00 committed by GitHub
commit 66093c4776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,15 +38,22 @@ func ToStruct(m any, v any) error {
return nil
}
func camelCaseMap(m map[string]any) map[string]any {
nm := map[string]any{}
for k, v := range m {
if vm, ok := v.(map[string]any); ok {
v = camelCaseMap(vm)
func CamelCase(v any) any {
switch vv := v.(type) {
case map[string]any:
n := map[string]any{}
for k, v := range vv {
n[CamelToSnake(k)] = CamelCase(v)
}
nm[CamelToSnake(k)] = v
return n
case []any:
n := make([]any, len(vv))
for i, v := range vv {
n[i] = CamelCase(v)
}
return n
}
return nm
return v
}
func ToMap(v any) (map[string]any, error) {
@ -60,6 +67,10 @@ func ToMap(v any) (map[string]any, error) {
if err := ms.Decode(v); err != nil {
return nil, err
}
m, ok := CamelCase(m).(map[string]any)
if !ok {
panic("not map")
}
return camelCaseMap(m), nil
return m, nil
}