Make PatchSliceOfMaps case insensitive

This fixes some case-sensitivity issues with using camel case in configuration files.
pull/7191/head
Matt Keeler 2020-01-31 09:56:02 -05:00
parent 1b74a68780
commit 3a46e1d15f
No known key found for this signature in database
GPG Key ID: 04DBAE1857E0081B
2 changed files with 17 additions and 5 deletions

View File

@ -2,14 +2,25 @@ package lib
import ( import (
"fmt" "fmt"
"strings"
) )
func PatchSliceOfMaps(m map[string]interface{}, skip []string, skipTree []string) map[string]interface{} { func PatchSliceOfMaps(m map[string]interface{}, skip []string, skipTree []string) map[string]interface{} {
return patchValue("", m, skip, skipTree).(map[string]interface{}) lowerSkip := make([]string, len(skip))
lowerSkipTree := make([]string, len(skipTree))
for i, val := range skip {
lowerSkip[i] = strings.ToLower(val)
}
for i, val := range skipTree {
lowerSkipTree[i] = strings.ToLower(val)
}
return patchValue("", m, lowerSkip, lowerSkipTree).(map[string]interface{})
} }
func patchValue(name string, v interface{}, skip []string, skipTree []string) interface{} { func patchValue(name string, v interface{}, skip []string, skipTree []string) interface{} {
// fmt.Printf("%q: %T\n", name, v)
switch x := v.(type) { switch x := v.(type) {
case map[string]interface{}: case map[string]interface{}:
if len(x) == 0 { if len(x) == 0 {
@ -70,8 +81,9 @@ func patchValue(name string, v interface{}, skip []string, skipTree []string) in
} }
func strSliceContains(s string, v []string) bool { func strSliceContains(s string, v []string) bool {
lower := strings.ToLower(s)
for _, vv := range v { for _, vv := range v {
if s == vv { if lower == vv {
return true return true
} }
} }

View File

@ -40,7 +40,7 @@ func TestPatchSliceOfMaps(t *testing.T) {
}, },
{ {
in: `{ in: `{
"services": [ "Services": [
{ {
"checks": [ "checks": [
{ {
@ -53,7 +53,7 @@ func TestPatchSliceOfMaps(t *testing.T) {
] ]
}`, }`,
out: `{ out: `{
"services": [ "Services": [
{ {
"checks": [ "checks": [
{ {