From 9aebba15663827118d49b25824945d710f3a1e41 Mon Sep 17 00:00:00 2001 From: MarkJoyMa <64180138+MarkJoyMa@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:46:32 +0800 Subject: [PATCH] fix/conf_multi_layer_map (#4407) Co-authored-by: aiden.ma --- core/conf/config.go | 4 +++- core/conf/config_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/core/conf/config.go b/core/conf/config.go index d3795c0a..309ea4e9 100644 --- a/core/conf/config.go +++ b/core/conf/config.go @@ -189,7 +189,7 @@ func buildFieldsInfo(tp reflect.Type, fullName string) (*fieldInfo, error) { switch tp.Kind() { case reflect.Struct: return buildStructFieldsInfo(tp, fullName) - case reflect.Array, reflect.Slice: + case reflect.Array, reflect.Slice, reflect.Map: return buildFieldsInfo(mapping.Deref(tp.Elem()), fullName) case reflect.Chan, reflect.Func: return nil, fmt.Errorf("unsupported type: %s", tp.Kind()) @@ -332,6 +332,8 @@ func toLowerCaseKeyMap(m map[string]any, info *fieldInfo) map[string]any { res[lk] = toLowerCaseInterface(v, ti) } else if info.mapField != nil { res[k] = toLowerCaseInterface(v, info.mapField) + } else if vv, ok := v.(map[string]any); ok { + res[k] = toLowerCaseKeyMap(vv, info) } else { res[k] = v } diff --git a/core/conf/config_test.go b/core/conf/config_test.go index 7735e619..e3025fba 100644 --- a/core/conf/config_test.go +++ b/core/conf/config_test.go @@ -1192,6 +1192,29 @@ Email = "bar"`) assert.Len(t, c.Value, 2) } }) + + t.Run("multi layer map", func(t *testing.T) { + type Value struct { + User struct { + Name string + } + } + + type Config struct { + Value map[string]map[string]Value + } + + var input = []byte(` +[Value.first.User1.User] +Name = "foo" +[Value.second.User2.User] +Name = "bar" +`) + var c Config + if assert.NoError(t, LoadFromTomlBytes(input, &c)) { + assert.Len(t, c.Value, 2) + } + }) } func Test_getFullName(t *testing.T) {