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

added map support for over writable configs #6116

Open
wants to merge 5 commits into
base: rc/v1.7.next1
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions common/reflectcommon/structFieldsUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func trySetTheNewValue(value *reflect.Value, newValue interface{}) error {
structVal := reflect.ValueOf(newValue)

return trySetStructValue(value, structVal)
case reflect.Map:
mapValue := reflect.ValueOf(newValue)

return tryUpdateMapValue(value, mapValue)
default:
return fmt.Errorf("unsupported type <%s> when trying to set the value '%v' of type <%s>", valueKind, newValue, reflect.TypeOf(newValue))
}
Expand Down Expand Up @@ -163,6 +167,31 @@ func trySetStructValue(value *reflect.Value, newValue reflect.Value) error {
}
}

func tryUpdateMapValue(value *reflect.Value, newValue reflect.Value) error {
if value.IsNil() {
value.Set(reflect.MakeMap(value.Type()))
}

switch newValue.Kind() {
case reflect.Map:
for _, key := range newValue.MapKeys() {
item := newValue.MapIndex(key)
newItem := reflect.New(value.Type().Elem()).Elem()

err := trySetTheNewValue(&newItem, item.Interface())
if err != nil {
return err
}

value.SetMapIndex(key, newItem)
}
default:
return fmt.Errorf("unsupported type <%s> when trying to add value in type <%s>", newValue.Kind(), value.Kind())
}

return nil
}

func updateStructFromMap(value *reflect.Value, newValue reflect.Value) error {
for _, key := range newValue.MapKeys() {
fieldName := key.String()
Expand Down