diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/maps/maps.go | 9 | ||||
-rw-r--r-- | common/maps/maps_test.go | 14 | ||||
-rw-r--r-- | common/maps/params.go | 12 |
3 files changed, 29 insertions, 6 deletions
diff --git a/common/maps/maps.go b/common/maps/maps.go index 5fb079009..79fcc23d0 100644 --- a/common/maps/maps.go +++ b/common/maps/maps.go @@ -49,6 +49,15 @@ func ToParamsAndPrepare(in interface{}) (Params, bool) { return m, true } +// MustToParamsAndPrepare calls ToParamsAndPrepare and panics if it fails. +func MustToParamsAndPrepare(in interface{}) Params { + if p, ok := ToParamsAndPrepare(in); ok { + return p + } else { + panic(fmt.Sprintf("cannot convert %T to maps.Params", in)) + } +} + // ToStringMap converts in to map[string]interface{}. func ToStringMap(in interface{}) map[string]interface{} { m, _ := ToStringMapE(in) diff --git a/common/maps/maps_test.go b/common/maps/maps_test.go index dbe97a15a..ba3c25087 100644 --- a/common/maps/maps_test.go +++ b/common/maps/maps_test.go @@ -21,10 +21,10 @@ import ( qt "github.com/frankban/quicktest" ) -func TestToLower(t *testing.T) { +func TestPrepareParams(t *testing.T) { tests := []struct { - input map[string]interface{} - expected map[string]interface{} + input Params + expected Params }{ { map[string]interface{}{ @@ -47,6 +47,9 @@ func TestToLower(t *testing.T) { "gHi": map[string]interface{}{ "J": 25, }, + "jKl": map[string]string{ + "M": "26", + }, }, Params{ "abc": 32, @@ -60,13 +63,16 @@ func TestToLower(t *testing.T) { "ghi": Params{ "j": 25, }, + "jkl": Params{ + "m": "26", + }, }, }, } for i, test := range tests { t.Run(fmt.Sprint(i), func(t *testing.T) { - // ToLower modifies input. + // PrepareParams modifies input. PrepareParams(test.input) if !reflect.DeepEqual(test.expected, test.input) { t.Errorf("[%d] Expected\n%#v, got\n%#v\n", i, test.expected, test.input) diff --git a/common/maps/params.go b/common/maps/params.go index 7e94d593b..e5a1bd07d 100644 --- a/common/maps/params.go +++ b/common/maps/params.go @@ -226,7 +226,7 @@ func toMergeStrategy(v interface{}) ParamsMergeStrategy { // PrepareParams // * makes all the keys in the given map lower cased and will do so // * This will modify the map given. -// * Any nested map[interface{}]interface{} will be converted to Params. +// * Any nested map[interface{}]interface{}, map[string]interface{},map[string]string will be converted to Params. // * Any _merge value will be converted to proper type and value. func PrepareParams(m Params) { for k, v := range m { @@ -236,7 +236,7 @@ func PrepareParams(m Params) { v = toMergeStrategy(v) retyped = true } else { - switch v.(type) { + switch vv := v.(type) { case map[interface{}]interface{}: var p Params = cast.ToStringMap(v) v = p @@ -247,6 +247,14 @@ func PrepareParams(m Params) { v = p PrepareParams(p) retyped = true + case map[string]string: + p := make(Params) + for k, v := range vv { + p[k] = v + } + v = p + PrepareParams(p) + retyped = true } } |