2019-04-30 22:19:19 +00:00
|
|
|
package lib
|
2017-10-10 23:40:59 +00:00
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
2017-10-10 23:40:59 +00:00
|
|
|
|
|
|
|
// TranslateKeys recursively translates all keys from m in-place to their
|
|
|
|
// canonical form as defined in dict which maps an alias name to the canonical
|
|
|
|
// name. If m already has a value for the canonical name then that one is used
|
|
|
|
// and the value for the alias name is discarded. Alias names are matched
|
|
|
|
// case-insensitive.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2022-10-21 19:58:06 +00:00
|
|
|
// m = TranslateKeys(m, map[string]string{"snake_case": "CamelCase"})
|
2017-10-10 23:40:59 +00:00
|
|
|
//
|
2018-09-27 13:33:12 +00:00
|
|
|
// If the canonical string provided is the empty string, the effect is to stop
|
|
|
|
// recursing into any key matching the left hand side. In this case the left
|
|
|
|
// hand side must use periods to specify a full path e.g.
|
|
|
|
// `connect.proxy.config`. The path must be the canonical key names (i.e.
|
2019-08-09 19:19:30 +00:00
|
|
|
// CamelCase) AFTER translation so NodeName not node_name. These are still match
|
2018-09-27 13:33:12 +00:00
|
|
|
// in a case-insensitive way.
|
|
|
|
//
|
|
|
|
// This is needed for example because parts of the Service Definition are
|
|
|
|
// "opaque" maps of metadata or config passed to another process or component.
|
|
|
|
// If we allow translation to recurse we might mangle the "opaque" keys given
|
2019-08-09 19:19:30 +00:00
|
|
|
// where the clash with key names in other parts of the definition :sob:
|
2018-09-27 13:33:12 +00:00
|
|
|
//
|
|
|
|
// Example:
|
2022-10-21 19:58:06 +00:00
|
|
|
//
|
|
|
|
// m - TranslateKeys(m, map[string]string{
|
|
|
|
// "foo_bar": "FooBar",
|
|
|
|
// "widget.config": "",
|
|
|
|
// // Assume widgets is an array, this will prevent recursing into any
|
|
|
|
// // item's config field
|
|
|
|
// "widgets.config": "",
|
|
|
|
// })
|
2020-05-27 18:42:01 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use lib/decode.HookTranslateKeys instead.
|
2017-10-10 23:40:59 +00:00
|
|
|
func TranslateKeys(v map[string]interface{}, dict map[string]string) {
|
2018-09-27 13:33:12 +00:00
|
|
|
// Convert all dict keys for exclusions to lower. so we can match against them
|
|
|
|
// unambiguously with a single lookup.
|
|
|
|
for k, v := range dict {
|
|
|
|
if v == "" {
|
|
|
|
dict[strings.ToLower(k)] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ck(v, dict, "")
|
2017-10-10 23:40:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 13:33:12 +00:00
|
|
|
func ck(v interface{}, dict map[string]string, pathPfx string) interface{} {
|
|
|
|
// In array case we don't add a path segment for the item as they are all
|
|
|
|
// assumed to be same which is why we check the prefix doesn't already end in
|
|
|
|
// a .
|
|
|
|
if pathPfx != "" && !strings.HasSuffix(pathPfx, ".") {
|
|
|
|
pathPfx += "."
|
|
|
|
}
|
2017-10-10 23:40:59 +00:00
|
|
|
switch x := v.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
for k, v := range x {
|
2018-09-27 13:33:12 +00:00
|
|
|
lowerK := strings.ToLower(k)
|
|
|
|
|
|
|
|
// Check if this path has been excluded
|
|
|
|
val, ok := dict[pathPfx+lowerK]
|
|
|
|
if ok && val == "" {
|
|
|
|
// Don't recurse into this key
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
canonKey, ok := dict[lowerK]
|
2017-10-10 23:40:59 +00:00
|
|
|
|
|
|
|
// no canonical key? -> use this key
|
2018-09-27 13:33:12 +00:00
|
|
|
if !ok {
|
|
|
|
x[k] = ck(v, dict, pathPfx+lowerK)
|
2017-10-10 23:40:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the alias
|
|
|
|
delete(x, k)
|
|
|
|
|
|
|
|
// if there is a value for the canonical key then keep it
|
|
|
|
if _, ok := x[canonKey]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise translate to the canonical key
|
2018-09-27 13:33:12 +00:00
|
|
|
x[canonKey] = ck(v, dict, pathPfx+strings.ToLower(canonKey))
|
2017-10-10 23:40:59 +00:00
|
|
|
}
|
|
|
|
return x
|
|
|
|
|
|
|
|
case []interface{}:
|
|
|
|
var a []interface{}
|
|
|
|
for _, xv := range x {
|
2018-09-27 13:33:12 +00:00
|
|
|
a = append(a, ck(xv, dict, pathPfx))
|
2017-10-10 23:40:59 +00:00
|
|
|
}
|
|
|
|
return a
|
|
|
|
|
|
|
|
default:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|