save code

This commit is contained in:
白茶清欢 2022-01-26 18:36:05 +08:00
parent 3047c60428
commit 632ef3ed7f
2 changed files with 13 additions and 9 deletions

View File

@ -8,6 +8,7 @@
package json_tool
import (
"encoding/json"
"fmt"
"reflect"
"strings"
@ -218,18 +219,21 @@ func (df *DataFilter) dealDiffArr(diffArr []string) {
// 没处理过, 开始处理
jsonResultList := df.getArrayData(df.source, diffArr)
fmt.Println(jsonResultList)
if len(jsonResultList) == 0 {
return
}
newDataList := gabs.New()
newPath := util.GenRandomString("", 8)
var result map[string]interface{}
_ = json.Unmarshal([]byte(df.source), &result)
JSONObject, _ := gabs.Consume(result)
_, _ = JSONObject.ArrayP(newPath)
for _, item := range jsonResultList {
newDataList.ArrayAppend(item.String())
if err := JSONObject.ArrayAppendP(item.Value(), newPath); nil != err {
fmt.Println(err.Error())
}
}
JSONObject, _ := gabs.Consume(df.source)
_, _ = JSONObject.SetP(newDataList.Data(), newPath)
fmt.Println(123, JSONObject.String())
df.source = JSONObject.String()
}
// getArrayData 获取数据

View File

@ -259,9 +259,9 @@ func TestDataFilterDiffArr(t *testing.T) {
"slice_data": []int{1, 2, 3},
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4},
"table": []map[string]interface{}{
{"user_list": map[string]interface{}{"name": "alex", "age": 18, "number": 1}},
{"user_list": map[string]interface{}{"name": "bob", "age": 28, "number": 2}},
{"user_list": map[string]interface{}{"name": "andy", "age": 28, "number": 2, "list": []int{1, 2, 3}}},
{"user_list": []interface{}{map[string]interface{}{"name": "alex", "age": 18, "number": 1}}},
{"user_list": []interface{}{map[string]interface{}{"name": "bob", "age": 28, "number": 2}}},
{"user_list": []interface{}{map[string]interface{}{"name": "andy", "age": 28, "number": 2, "list": []int{1, 2, 3}}}},
},
}
rule := []*FilterDataRule{