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:
commit
66093c4776
@ -38,15 +38,22 @@ func ToStruct(m any, v any) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func camelCaseMap(m map[string]any) map[string]any {
|
func CamelCase(v any) any {
|
||||||
nm := map[string]any{}
|
switch vv := v.(type) {
|
||||||
for k, v := range m {
|
case map[string]any:
|
||||||
if vm, ok := v.(map[string]any); ok {
|
n := map[string]any{}
|
||||||
v = camelCaseMap(vm)
|
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) {
|
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 {
|
if err := ms.Decode(v); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
m, ok := CamelCase(m).(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
panic("not map")
|
||||||
|
}
|
||||||
|
|
||||||
return camelCaseMap(m), nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user