- 将FlatMap,Struct2FlatMap从jxutils移到utils中
+ 添加MarshalJSONIgnoreValues, MustMarshalJSONIgnoreValues
This commit is contained in:
@@ -425,8 +425,66 @@ func Map2KeySlice(flagMap map[string]int) (keyList []string) {
|
||||
return keyList
|
||||
}
|
||||
|
||||
// 合并map,相同字段以前面的优先,后来相同的字段忽略
|
||||
func MergeMaps(firstMap map[string]interface{}, otherMaps ...map[string]interface{}) (retVal map[string]interface{}) {
|
||||
retVal = make(map[string]interface{})
|
||||
allMaps := append([]map[string]interface{}{firstMap}, otherMaps...)
|
||||
mapCount := len(allMaps)
|
||||
for index := range allMaps {
|
||||
oneMap := allMaps[mapCount-index-1]
|
||||
for k, v := range oneMap {
|
||||
retVal[k] = v
|
||||
}
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
|
||||
func Struct2MapByJson(obj interface{}) (mapData map[string]interface{}) {
|
||||
structsObj := structs.New(obj)
|
||||
structsObj.TagName = "json"
|
||||
return structsObj.Map()
|
||||
}
|
||||
|
||||
// 此函数将MAP中所有的子MAP中的数据提升到最上层,相同字段会覆盖父MAP的
|
||||
func FlatMap(in map[string]interface{}) map[string]interface{} {
|
||||
keys := []string{}
|
||||
maps := []map[string]interface{}{}
|
||||
for k, v := range in {
|
||||
if vMap, ok := v.(map[string]interface{}); ok {
|
||||
vMap = FlatMap(vMap)
|
||||
maps = append(maps, vMap)
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
if len(maps) > 0 {
|
||||
retVal := MergeMaps(in, maps...)
|
||||
for _, v := range keys {
|
||||
delete(retVal, v)
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
return in
|
||||
}
|
||||
|
||||
func Struct2FlatMap(obj interface{}) map[string]interface{} {
|
||||
m := Struct2MapByJson(obj)
|
||||
return FlatMap(m)
|
||||
}
|
||||
|
||||
func Struct2MapWithIgnore(obj interface{}, ignoreValues map[string]interface{}) map[string]interface{} {
|
||||
mapData := Struct2FlatMap(obj)
|
||||
for k, v := range mapData {
|
||||
if ignoreValues[k] == v {
|
||||
delete(mapData, k)
|
||||
}
|
||||
}
|
||||
return mapData
|
||||
}
|
||||
|
||||
func MarshalJSONIgnoreValues(obj interface{}, ignoreValues map[string]interface{}) ([]byte, error) {
|
||||
return json.Marshal(Struct2MapWithIgnore(obj, ignoreValues))
|
||||
}
|
||||
|
||||
func MustMarshalJSONIgnoreValues(obj interface{}, ignoreValues map[string]interface{}) []byte {
|
||||
return MustMarshal(Struct2MapWithIgnore(obj, ignoreValues))
|
||||
}
|
||||
|
||||
@@ -108,18 +108,6 @@ func GetAPIOperator(userName string) string {
|
||||
return retVal
|
||||
}
|
||||
|
||||
func MergeMaps(firstMap map[string]interface{}, otherMaps ...map[string]interface{}) (retVal map[string]interface{}) {
|
||||
retVal = make(map[string]interface{})
|
||||
allMaps := append(otherMaps, firstMap)
|
||||
for _, oneMap := range allMaps {
|
||||
for k, v := range oneMap {
|
||||
retVal[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return retVal
|
||||
}
|
||||
|
||||
func CallFuncLogError(funcToCall func() error, msg string, params ...interface{}) error {
|
||||
err := funcToCall()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user