Skip to content

Commit dfeb3df

Browse files
committed
更新一些Map
MapKeyFilter MapKeyFilterToArray MapValueFilter MapValueFilterToArray MapKeys MapValues AssetMapSet
1 parent 787ecae commit dfeb3df

File tree

2 files changed

+137
-5
lines changed

2 files changed

+137
-5
lines changed

example/example.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,40 @@ func main() {
8181
}
8282

8383
fmt.Println(lib.AssetsReturn(false, "1", nil))
84+
85+
testMap := map[string]int{
86+
"a": 1, "b": 2, "c": 3,
87+
}
88+
89+
fmt.Println(lib.MapKeyFilter(&testMap, func(d interface{}) bool {
90+
return lib.InArray([]string{"a", "b"}, d.(string))
91+
}))
92+
93+
fmt.Println(lib.MapKeyFilterToArray(&testMap, func(d interface{}) bool {
94+
return lib.InArray([]string{"a", "b"}, d.(string))
95+
}))
96+
97+
fmt.Println(lib.MapValueFilter(&testMap, func(d interface{}) bool {
98+
return d.(int) >= 2
99+
}))
100+
101+
fmt.Println(lib.MapValueFilterToArray(&testMap, func(d interface{}) bool {
102+
return d.(int) >= 2
103+
}))
104+
105+
fmt.Println(lib.MapKeys(&testMap, nil))
106+
107+
fmt.Println(lib.MapKeys(&testMap, func(d interface{}) interface{} {
108+
return lib.StringJoin(d.(string), "-haha")
109+
}))
110+
111+
fmt.Println(lib.MapValues(&testMap, func(d interface{}) interface{} {
112+
return d.(int) + 1
113+
}))
114+
115+
testMapSet := map[string]string{}
116+
117+
lib.AssetMapSet(true, &testMapSet, "a", "哈")
118+
lib.AssetMapSet(false, &testMapSet, "b", "嘻")
119+
fmt.Println(testMapSet)
84120
}

tool.go

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func MapToInterface(data interface{}) map[interface{}]interface{} {
7070
v := TryInterfacePtr(data)
7171

7272
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
73-
7473
keys := v.MapKeys()
7574
dst := make(map[interface{}]interface{}, len(keys))
7675

@@ -82,14 +81,111 @@ func MapToInterface(data interface{}) map[interface{}]interface{} {
8281
}
8382

8483
func MapMap(data interface{}, cb func(interface{}) interface{}) interface{} {
85-
dataTransform := MapToInterface(data)
86-
dst := make(map[interface{}]interface{}, len(dataTransform))
87-
for index, d := range dataTransform {
88-
dst[index] = cb(d)
84+
v := TryInterfacePtr(data)
85+
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
86+
keys := v.MapKeys()
87+
dst := make(map[interface{}]interface{}, v.Len())
88+
for _, key := range keys {
89+
dst[key.Type().Name()] = cb(v.MapIndex(key).Interface())
90+
}
91+
return dst
92+
}
93+
94+
func MapKeyFilter(data interface{}, cb func(interface{}) bool) interface{} {
95+
v := TryInterfacePtr(data)
96+
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
97+
keys := v.MapKeys()
98+
dst := make(map[interface{}]interface{}, v.Len())
99+
for _, key := range keys {
100+
k := key.Interface()
101+
if cb(k) {
102+
dst[k] = v.MapIndex(key).Interface()
103+
}
104+
}
105+
return dst
106+
}
107+
108+
func MapKeyFilterToArray(data interface{}, cb func(interface{}) bool) interface{} {
109+
v := TryInterfacePtr(data)
110+
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
111+
keys := v.MapKeys()
112+
dst := make([]interface{}, 0, v.Len())
113+
for _, key := range keys {
114+
k := key.Interface()
115+
if cb(k) {
116+
dst = append(dst, v.MapIndex(key).Interface())
117+
}
118+
}
119+
return dst
120+
}
121+
122+
func MapValueFilter(data interface{}, cb func(interface{}) bool) interface{} {
123+
v := TryInterfacePtr(data)
124+
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
125+
keys := v.MapKeys()
126+
dst := make(map[interface{}]interface{}, v.Len())
127+
for _, key := range keys {
128+
value := v.MapIndex(key).Interface()
129+
if cb(value) {
130+
dst[key.Interface()] = value
131+
}
132+
}
133+
return dst
134+
}
135+
136+
func MapValueFilterToArray(data interface{}, cb func(interface{}) bool) interface{} {
137+
v := TryInterfacePtr(data)
138+
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
139+
keys := v.MapKeys()
140+
dst := make([]interface{}, 0, v.Len())
141+
for _, key := range keys {
142+
value := v.MapIndex(key).Interface()
143+
if cb(value) {
144+
dst = append(dst, value)
145+
}
89146
}
90147
return dst
91148
}
92149

150+
func MapKeys(data interface{}, cb func(interface{}) interface{}) interface{} {
151+
v := TryInterfacePtr(data)
152+
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
153+
keys := v.MapKeys()
154+
return ArrayMap(&keys, func(d interface{}) interface{} {
155+
k := d.(reflect.Value).Interface()
156+
if cb != nil {
157+
return cb(k)
158+
}
159+
return d.(reflect.Value).Interface()
160+
})
161+
}
162+
163+
func MapValues(data interface{}, cb func(interface{}) interface{}) interface{} {
164+
v := TryInterfacePtr(data)
165+
AssetsMap(v.Kind(), "map转换接口错误, 非map接口")
166+
keys := v.MapKeys()
167+
return ArrayMap(&keys, func(d interface{}) interface{} {
168+
value := v.MapIndex(d.(reflect.Value)).Interface()
169+
if cb != nil {
170+
return cb(value)
171+
}
172+
return value
173+
})
174+
}
175+
176+
func AssetMapSet(do bool, m interface{}, key interface{}, value interface{}) {
177+
if do {
178+
v := reflect.ValueOf(m)
179+
180+
if v.Kind() != reflect.Ptr {
181+
AssetsSlice(v.Kind(), "非指针, 无法set map")
182+
} else {
183+
obj := v.Elem()
184+
obj.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
185+
}
186+
}
187+
}
188+
93189
func ArrayMap(data interface{}, cb func(interface{}) interface{}) interface{} {
94190
v := TryInterfacePtr(data)
95191
AssetsSlice(v.Kind(), "数组转换接口错误, 非数组接口")

0 commit comments

Comments
 (0)