Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/conf_recursion #4406

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 48 additions & 22 deletions core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func LoadConfig(file string, v any, opts ...Option) error {

// LoadFromJsonBytes loads config into v from content json bytes.
func LoadFromJsonBytes(content []byte, v any) error {
info, err := buildFieldsInfo(reflect.TypeOf(v), "")
info, err := buildFieldsInfo(reflect.TypeOf(v), "", make(fieldCache))
if err != nil {
return err
}
Expand Down Expand Up @@ -143,10 +143,11 @@ func addOrMergeFields(info *fieldInfo, key string, child *fieldInfo, fullName st
return nil
}

func buildAnonymousFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.Type, fullName string) error {
func buildAnonymousFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.Type, fullName string,
cache fieldCache) error {
switch ft.Kind() {
case reflect.Struct:
fields, err := buildFieldsInfo(ft, fullName)
fields, err := buildFieldsInfo(ft, fullName, cache)
if err != nil {
return err
}
Expand All @@ -157,7 +158,7 @@ func buildAnonymousFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.T
}
}
case reflect.Map:
elemField, err := buildFieldsInfo(mapping.Deref(ft.Elem()), fullName)
elemField, err := buildFieldsInfo(mapping.Deref(ft.Elem()), fullName, cache)
if err != nil {
return err
}
Expand All @@ -183,14 +184,17 @@ func buildAnonymousFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.T
return nil
}

func buildFieldsInfo(tp reflect.Type, fullName string) (*fieldInfo, error) {
func buildFieldsInfo(tp reflect.Type, fullName string, cache fieldCache) (*fieldInfo, error) {
tp = mapping.Deref(tp)
if info, ok := cache[tp]; ok {
return info, nil
}

switch tp.Kind() {
case reflect.Struct:
return buildStructFieldsInfo(tp, fullName)
return buildStructFieldsInfo(tp, fullName, cache)
case reflect.Array, reflect.Slice, reflect.Map:
return buildFieldsInfo(mapping.Deref(tp.Elem()), fullName)
return buildFieldsInfo(mapping.Deref(tp.Elem()), fullName, cache)
case reflect.Chan, reflect.Func:
return nil, fmt.Errorf("unsupported type: %s", tp.Kind())
default:
Expand All @@ -200,33 +204,32 @@ func buildFieldsInfo(tp reflect.Type, fullName string) (*fieldInfo, error) {
}
}

func buildNamedFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.Type, fullName string) error {
func buildNamedFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.Type, fullName string,
cache fieldCache) error {
var finfo *fieldInfo
var err error

switch ft.Kind() {
case reflect.Struct:
finfo, err = buildFieldsInfo(ft, fullName)
finfo, err = buildFieldsInfo(ft, fullName, cache)
if err != nil {
return err
}
case reflect.Array, reflect.Slice:
finfo, err = buildFieldsInfo(ft.Elem(), fullName)
finfo, err = buildFieldsInfo(ft.Elem(), fullName, cache)
if err != nil {
return err
}
case reflect.Map:
elemInfo, err := buildFieldsInfo(mapping.Deref(ft.Elem()), fullName)
elemInfo, err := buildFieldsInfo(mapping.Deref(ft.Elem()), fullName, cache)
if err != nil {
return err
}

finfo = &fieldInfo{
children: make(map[string]*fieldInfo),
mapField: elemInfo,
}
finfo = cache.get(mapping.Deref(ft.Elem()))
finfo.mapField = elemInfo
default:
finfo, err = buildFieldsInfo(ft, fullName)
finfo, err = buildFieldsInfo(ft, fullName, cache)
if err != nil {
return err
}
Expand All @@ -235,10 +238,8 @@ func buildNamedFieldInfo(info *fieldInfo, lowerCaseName string, ft reflect.Type,
return addOrMergeFields(info, lowerCaseName, finfo, fullName)
}

func buildStructFieldsInfo(tp reflect.Type, fullName string) (*fieldInfo, error) {
info := &fieldInfo{
children: make(map[string]*fieldInfo),
}
func buildStructFieldsInfo(tp reflect.Type, fullName string, cache fieldCache) (*fieldInfo, error) {
info := cache.get(tp)

for i := 0; i < tp.NumField(); i++ {
field := tp.Field(i)
Expand All @@ -252,11 +253,11 @@ func buildStructFieldsInfo(tp reflect.Type, fullName string) (*fieldInfo, error)
// flatten anonymous fields
if field.Anonymous {
if err := buildAnonymousFieldInfo(info, lowerCaseName, ft,
getFullName(fullName, lowerCaseName)); err != nil {
getFullName(fullName, lowerCaseName), cache); err != nil {
return nil, err
}
} else if err := buildNamedFieldInfo(info, lowerCaseName, ft,
getFullName(fullName, lowerCaseName)); err != nil {
getFullName(fullName, lowerCaseName), cache); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -361,3 +362,28 @@ func getFullName(parent, child string) string {

return strings.Join([]string{parent, child}, ".")
}

type fieldCache map[reflect.Type]*fieldInfo

func (c fieldCache) get(tp reflect.Type) *fieldInfo {
tp = mapping.Deref(tp)
switch tp.Kind() {
case reflect.Struct:
case reflect.Array, reflect.Slice:
tp = mapping.Deref(tp.Elem())
default:
return &fieldInfo{
children: make(map[string]*fieldInfo),
}
}

info, ok := c[tp]
if !ok {
info = &fieldInfo{
children: make(map[string]*fieldInfo),
}
c[tp] = info
}

return info
}
49 changes: 48 additions & 1 deletion core/conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,53 @@ Name = "bar"
})
}

func Test_FieldRecursion(t *testing.T) {
t.Run("map recursion", func(t *testing.T) {
type Inner struct {
Name string
Children map[string]Inner
}

type Config struct {
Inner Inner
}

input := []byte(`{"Inner": {"Name": "foo"}}`)
var c Config
assert.NoError(t, LoadFromJsonBytes(input, &c))
})

t.Run("array recursion", func(t *testing.T) {
type Inner struct {
Name string
Children map[string]Inner
}

type Config struct {
Inners []Inner
}

input := []byte(`{"Inners": [{"Name": "foo"}]}`)
var c Config
assert.NoError(t, LoadFromJsonBytes(input, &c))
})

t.Run("map anonymous recursion", func(t *testing.T) {
type Inner struct {
Name string
Children map[string]Inner
}

type Config struct {
Inner
}

input := []byte(`{"Name": "foo"}`)
var c Config
assert.NoError(t, LoadFromJsonBytes(input, &c))
})
}

func Test_getFullName(t *testing.T) {
assert.Equal(t, "a.b", getFullName("a", "b"))
assert.Equal(t, "a", getFullName("", "a"))
Expand Down Expand Up @@ -1300,7 +1347,7 @@ func Test_buildFieldsInfo(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := buildFieldsInfo(tt.t, "")
_, err := buildFieldsInfo(tt.t, "", make(fieldCache))
if tt.ok {
assert.NoError(t, err)
} else {
Expand Down