Skip to content

Commit

Permalink
fix/conf_multi_layer_map
Browse files Browse the repository at this point in the history
  • Loading branch information
aiden.ma authored and aiden.ma committed Oct 4, 2024
1 parent afcbca8 commit fa9cecd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions core/conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fa9cecd

Please sign in to comment.