Browse Source

Merge ed2d46d821 into 1ffadf5510

pull/58/merge
Albert Le Batteux GitHub 4 years ago
parent
commit
92e28cfa29
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions
  1. +6
    -0
      cast.go
  2. +32
    -0
      cast_test.go
  3. +30
    -0
      caste.go

+ 6
- 0
cast.go View File

@@ -140,6 +140,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)


+ 32
- 0
cast_test.go View File

@@ -786,6 +786,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{}


+ 30
- 0
caste.go View File

@@ -990,6 +990,35 @@ 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)
}

// ToStringMapIntE casts an interface to a map[string]int{} type.
func ToStringMapIntE(i interface{}) (map[string]int, error) {
var m = map[string]int{}
@@ -1069,6 +1098,7 @@ func ToStringMapInt64E(i interface{}) (map[string]int64, error) {
mVal.SetMapIndex(keyVal, reflect.ValueOf(val))
}
return m, nil

}

// ToSliceE casts an interface to a []interface{} type.


Loading…
Cancel
Save