From 60fe4eb1100e8270f476892dafa493358989a354 Mon Sep 17 00:00:00 2001 From: Albert Le Batteux Date: Thu, 11 Jan 2018 13:12:08 +0100 Subject: [PATCH] Add ToStringMapStruct() --- cast.go | 6 ++++++ cast_test.go | 32 ++++++++++++++++++++++++++++++++ caste.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/cast.go b/cast.go index 8b8c208..4af2eb8 100644 --- a/cast.go +++ b/cast.go @@ -128,6 +128,12 @@ func ToStringMap(i interface{}) map[string]interface{} { return v } +// ToStringMapStruct casts an interface to a map[string]struct{} type. +func ToStringMapStruct(i interface{}) map[string]struct{} { + v, _ := ToStringMapStructE(i) + return v +} + // ToSlice casts an interface to a []interface{} type. func ToSlice(i interface{}) []interface{} { v, _ := ToSliceE(i) diff --git a/cast_test.go b/cast_test.go index 404fe76..af52600 100644 --- a/cast_test.go +++ b/cast_test.go @@ -774,6 +774,38 @@ func TestToStringMapE(t *testing.T) { } } +func TestToStringMapStructE(t *testing.T) { + tests := []struct { + input interface{} + expect map[string]struct{} + iserr bool + }{ + {map[string]interface{}{"v1": true, "v2": false}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false}, + {[]string{"v1", "v2"}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false}, + {[]interface{}{"v1", "v2"}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false}, + {map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, map[string]struct{}{"v1": struct{}{}, "v2": struct{}{}}, false}, + // errors + {nil, nil, true}, + {testing.T{}, nil, true}, + } + for i, test := range tests { + errmsg := fmt.Sprintf("i = %d", i) // assert helper message + + v, err := ToStringMapStructE(test.input) + if test.iserr { + assert.Error(t, err, errmsg) + continue + } + + assert.NoError(t, err, errmsg) + assert.Equal(t, test.expect, v, errmsg) + + // Non-E test + v = ToStringMapStruct(test.input) + assert.Equal(t, test.expect, v, errmsg) + } +} + func TestToStringMapBoolE(t *testing.T) { tests := []struct { input interface{} diff --git a/caste.go b/caste.go index 81511fe..b45deaf 100644 --- a/caste.go +++ b/caste.go @@ -977,6 +977,36 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) { } } +// ToStringMapStructE casts an interface to a map[string]struct{} type. +func ToStringMapStructE(i interface{}) (map[string]struct{}, error) { + var m = map[string]struct{}{} + + fmt.Println("type: ", reflect.TypeOf(i)) + switch v := i.(type) { + case map[string]interface{}: + for k := range v { + m[k] = struct{}{} + } + return m, nil + case []string: + fmt.Println("[]string") + for _, s := range v { + m[s] = struct{}{} + } + return m, nil + case []interface{}: + for _, k := range v { + m[ToString(k)] = struct{}{} + } + return m, nil + case map[string]struct{}: + return v, nil + default: + fmt.Println("cast err") + return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i) + } +} + // ToSliceE casts an interface to a []interface{} type. func ToSliceE(i interface{}) ([]interface{}, error) { var s []interface{}