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

Fix UI config conversion yet again #4128

Merged
Merged
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
69 changes: 37 additions & 32 deletions internal/manager/config/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// HACK: viper changes map keys to case insensitive values, so the workaround is to
// convert the map to use snake-case keys

// toSnakeCase converts a string to snake_case
// toSnakeCase converts a string from camelCase to snake_case
// NOTE: a double capital will be converted in a way that will yield a different result
// when converted back to camel case.
// For example: someIDs => some_ids => someIds
Expand All @@ -30,6 +30,7 @@ func toSnakeCase(v string) string {
return buf.String()
}

// fromSnakeCase converts a string from snake_case to camelCase
func fromSnakeCase(v string) string {
var buf bytes.Buffer
leadingUnderscore := true
Expand All @@ -51,62 +52,66 @@ func fromSnakeCase(v string) string {
return buf.String()
}

// copyAndInsensitiviseMap behaves like insensitiviseMap, but creates a copy of
// any map it makes case insensitive.
func toSnakeCaseMap(m map[string]interface{}) map[string]interface{} {
nm := make(map[string]interface{})

for key, val := range m {
adjKey := toSnakeCase(key)

switch v := val.(type) {
case map[string]interface{}:
nm[adjKey] = toSnakeCaseMap(v)
default:
nm[adjKey] = val
}
}

return nm
// fromSnakeCaseMap recursively converts a map using snake_case keys to camelCase keys
func fromSnakeCaseMap(m map[string]interface{}) map[string]interface{} {
return fromSnakeCaseValue(m).(map[string]interface{})
}

// convertMapValue converts values into something that can be marshalled in JSON
// This means converting map[interface{}]interface{} to map[string]interface{} where ever
// encountered.
func convertMapValue(val interface{}) interface{} {
func fromSnakeCaseValue(val interface{}) interface{} {
switch v := val.(type) {
case map[interface{}]interface{}:
ret := cast.ToStringMap(v)
for k, vv := range ret {
adjKey := fromSnakeCase(k)
ret[adjKey] = convertMapValue(vv)
ret[adjKey] = fromSnakeCaseValue(vv)
}
return ret
case map[string]interface{}:
ret := make(map[string]interface{})
for k, vv := range v {
adjKey := fromSnakeCase(k)
ret[adjKey] = convertMapValue(vv)
ret[adjKey] = fromSnakeCaseValue(vv)
}
return ret
case []interface{}:
ret := make([]interface{}, len(v))
for i, vv := range v {
ret[i] = convertMapValue(vv)
ret[i] = fromSnakeCaseValue(vv)
}
return ret
default:
return v
}
}

func fromSnakeCaseMap(m map[string]interface{}) map[string]interface{} {
nm := make(map[string]interface{})
// toSnakeCaseMap recursively converts a map using camelCase keys to snake_case keys
func toSnakeCaseMap(m map[string]interface{}) map[string]interface{} {
return toSnakeCaseValue(m).(map[string]interface{})
}

for key, val := range m {
adjKey := fromSnakeCase(key)
nm[adjKey] = convertMapValue(val)
func toSnakeCaseValue(val interface{}) interface{} {
switch v := val.(type) {
case map[interface{}]interface{}:
ret := cast.ToStringMap(v)
for k, vv := range ret {
adjKey := toSnakeCase(k)
ret[adjKey] = toSnakeCaseValue(vv)
}
return ret
case map[string]interface{}:
ret := make(map[string]interface{})
for k, vv := range v {
adjKey := toSnakeCase(k)
ret[adjKey] = toSnakeCaseValue(vv)
}
return ret
case []interface{}:
ret := make([]interface{}, len(v))
for i, vv := range v {
ret[i] = toSnakeCaseValue(vv)
}
return ret
default:
return v
}

return nm
}