add gutil.SliceToMapWithColumnAsKey

This commit is contained in:
qinyuguang 2021-06-09 18:49:49 +08:00
parent fe7209e76d
commit f2bc29e5c1
2 changed files with 46 additions and 0 deletions

View File

@ -65,3 +65,29 @@ func SliceToMap(slice interface{}) map[string]interface{} {
}
return nil
}
// SliceToMapWithColumnAsKey converts slice type variable `slice` to `map[interface{}]interface{}`
// The value of specified column use as the key for returned map.
// Eg:
// SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K1") => {"v1": {"K1": "v1", "K2": 1}, "v2": {"K1": "v2", "K2": 2}}
// SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K2") => {1: {"K1": "v1", "K2": 1}, 2: {"K1": "v2", "K2": 2}}
func SliceToMapWithColumnAsKey(slice interface{}, key interface{}) map[interface{}]interface{} {
var (
reflectValue = reflect.ValueOf(slice)
reflectKind = reflectValue.Kind()
)
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
data := make(map[interface{}]interface{})
switch reflectKind {
case reflect.Slice, reflect.Array:
for i := 0; i < reflectValue.Len(); i++ {
if k, ok := ItemValue(reflectValue.Index(i), key); ok {
data[k] = reflectValue.Index(i).Interface()
}
}
}
return data
}

View File

@ -35,3 +35,23 @@ func Test_SliceToMap(t *testing.T) {
t.Assert(m, nil)
})
}
func Test_SliceToMapWithColumnAsKey(t *testing.T) {
m1 := g.Map{"K1": "v1", "K2": 1}
m2 := g.Map{"K1": "v2", "K2": 2}
s := g.Slice{m1, m2}
gtest.C(t, func(t *gtest.T) {
m := gutil.SliceToMapWithColumnAsKey(s, "K1")
t.Assert(m, g.MapAnyAny{
"v1": m1,
"v2": m2,
})
})
gtest.C(t, func(t *gtest.T) {
m := gutil.SliceToMapWithColumnAsKey(s, "K2")
t.Assert(m, g.MapAnyAny{
1: m1,
2: m2,
})
})
}