- first edition.
This commit is contained in:
78
utils/utils.go
Normal file
78
utils/utils.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) interface{} {
|
||||
if data == nil || (keysToKeep == nil && keysToRemove == nil) {
|
||||
return data
|
||||
}
|
||||
|
||||
var keysToRemoveMap map[string]int
|
||||
var keysToKeepMap map[string]int
|
||||
if keysToKeep != nil {
|
||||
keysToKeepMap = make(map[string]int)
|
||||
for _, v := range keysToKeep {
|
||||
keysToKeepMap[v] = 1
|
||||
}
|
||||
} else if keysToRemove != nil {
|
||||
keysToRemoveMap = make(map[string]int)
|
||||
for _, v := range keysToRemove {
|
||||
keysToRemoveMap[v] = 1
|
||||
}
|
||||
}
|
||||
|
||||
dataIsSlice := true
|
||||
valueOfData := reflect.ValueOf(data)
|
||||
if valueOfData.Kind() != reflect.Slice {
|
||||
dataIsSlice = false
|
||||
valueOfData = reflect.ValueOf([]interface{}{data})
|
||||
} else if valueOfData.Len() == 0 {
|
||||
return data
|
||||
}
|
||||
|
||||
retVal := make([]interface{}, valueOfData.Len())
|
||||
|
||||
for index := 0; index < valueOfData.Len(); index++ {
|
||||
realV := valueOfData.Index(index)
|
||||
if realV.Kind() == reflect.Interface || realV.Kind() == reflect.Ptr {
|
||||
realV = realV.Elem()
|
||||
}
|
||||
if keysToRemoveMap != nil || keysToKeepMap != nil {
|
||||
mapV := make(map[string]interface{})
|
||||
for _, key := range realV.MapKeys() {
|
||||
fieldName := key.String()
|
||||
wantThisField := true
|
||||
if keysToKeepMap != nil {
|
||||
if _, ok := keysToKeepMap[fieldName]; !ok {
|
||||
wantThisField = false
|
||||
}
|
||||
} else if keysToRemoveMap != nil {
|
||||
if _, ok := keysToRemoveMap[fieldName]; ok {
|
||||
wantThisField = false
|
||||
}
|
||||
}
|
||||
if wantThisField {
|
||||
mapV[fieldName] = realV.MapIndex(key)
|
||||
}
|
||||
}
|
||||
retVal[index] = mapV
|
||||
} else {
|
||||
retVal[index] = realV.Interface()
|
||||
}
|
||||
}
|
||||
|
||||
if !dataIsSlice {
|
||||
return retVal[0]
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
|
||||
func UnmarshalUseNumber(data []byte, result interface{}) error {
|
||||
d := json.NewDecoder(bytes.NewReader(data))
|
||||
d.UseNumber()
|
||||
return d.Decode(result)
|
||||
}
|
||||
35
utils/utils_test.go
Normal file
35
utils/utils_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type II interface {
|
||||
foo()
|
||||
}
|
||||
|
||||
type TT struct {
|
||||
}
|
||||
|
||||
func (t TT) foo() {
|
||||
|
||||
}
|
||||
func TestDictKeysMan(t *testing.T) {
|
||||
testData := map[string]string{"k1": "v1", "k2": "v2"}
|
||||
result1 := DictKeysMan(testData, []string{"k1"}, nil).(map[string]interface{})
|
||||
if _, ok := result1["k1"]; ok {
|
||||
t.Error("Params keysToRemove can not remove key!")
|
||||
}
|
||||
if _, ok := result1["k2"]; !ok {
|
||||
t.Error("Params keysToRemove removed wrong key!")
|
||||
}
|
||||
|
||||
result2 := DictKeysMan([]interface{}{testData}, nil, []string{"k1"}).([]interface{})
|
||||
result20 := result2[0].(map[string]interface{})
|
||||
if _, ok := result20["k1"]; !ok {
|
||||
t.Error("Params keysToKeep can not keep key!")
|
||||
}
|
||||
if _, ok := result20["k2"]; ok {
|
||||
t.Error("Params keysToKeep keep wrong key!")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user