- add util func RemoveGeneralMapKeys
This commit is contained in:
@@ -201,3 +201,14 @@ func TrimBlanChar(str string) string {
|
|||||||
return strValue == " " || strValue == "\t" || strValue == "\n" || strValue == "\r"
|
return strValue == " " || strValue == "\t" || strValue == "\n" || strValue == "\r"
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveGeneralMapKeys(obj map[string]interface{}, keys ...string) map[string]interface{} {
|
||||||
|
if obj != nil {
|
||||||
|
for _, key := range keys {
|
||||||
|
if _, ok := obj[key]; ok {
|
||||||
|
delete(obj, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|||||||
@@ -102,3 +102,26 @@ func TestFilterMb4(t *testing.T) {
|
|||||||
t.Fatal("FilterMb4 can not filter mb4 characters")
|
t.Fatal("FilterMb4 can not filter mb4 characters")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoveGeneralMapKeys(t *testing.T) {
|
||||||
|
obj1 := map[string]interface{}{
|
||||||
|
"key1": 1,
|
||||||
|
"key2": "2",
|
||||||
|
"key3": 3,
|
||||||
|
"key4": nil,
|
||||||
|
}
|
||||||
|
obj11 := RemoveGeneralMapKeys(obj1, "key2", "key3", "key4")
|
||||||
|
if obj11["key1"] != 1 {
|
||||||
|
t.Fatal("RemoveGeneralMapKeys doesn't keep key correctly")
|
||||||
|
}
|
||||||
|
if _, ok := obj11["key2"]; ok {
|
||||||
|
t.Fatal("RemoveGeneralMapKeys doesn't remove key correctly")
|
||||||
|
}
|
||||||
|
if _, ok := obj11["key4"]; ok {
|
||||||
|
t.Fatal("RemoveGeneralMapKeys doesn't remove key correctly")
|
||||||
|
}
|
||||||
|
t.Log(obj11)
|
||||||
|
if RemoveGeneralMapKeys(nil, "key1") != nil {
|
||||||
|
t.Fatal("RemoveGeneralMapKeys handle nil wrong")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user