save code
This commit is contained in:
parent
db3fe19e6a
commit
b281eca01e
@ -78,7 +78,7 @@ func (df *DataFilter) Filter() (string, error) {
|
||||
// 数据源路径不识数组, 多个key写入到同一个map key, 并且map key 不是以[]结尾, 自动格式化
|
||||
// 目标位置, 是一个数组
|
||||
if df.pathIsArrayValue(item.MapKey) {
|
||||
realMapKey := strings.ReplaceAll(item.MapKey, ".[]", "")
|
||||
realMapKey := strings.Trim(item.MapKey, ".[]")
|
||||
if exist := jsonObject.Exists(realMapKey); !exist {
|
||||
if _, err = jsonObject.ArrayP(realMapKey); nil != err {
|
||||
return "", err
|
||||
@ -92,6 +92,12 @@ func (df *DataFilter) Filter() (string, error) {
|
||||
if dataType != obg2slice[realMapKey] {
|
||||
return "", errors.New(realMapKey + " 预期写入的字段数据类型不一致")
|
||||
}
|
||||
sourcePathArr := strings.Split(item.SourceKey, ".[].")
|
||||
mapPathArr := strings.Split(realMapKey, ".[].")
|
||||
|
||||
result := gabs.New()
|
||||
_, _ = result.ArrayP(mapPathArr[0])
|
||||
df.SetArrayData("{\""+sourcePathArr[0]+"\":"+gjson.Get(df.source, sourcePathArr[0]).String()+"}", result, sourcePathArr, mapPathArr)
|
||||
if err = jsonObject.ArrayAppend(valueResult.Value(), realMapKey); nil != err {
|
||||
return "", err
|
||||
}
|
||||
@ -249,3 +255,40 @@ func (df *DataFilter) getArrayData(source string, pathArr []string) []gjson.Resu
|
||||
}
|
||||
return resultList
|
||||
}
|
||||
|
||||
// SetArrayData 设置数组数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:05 下午 2022/2/2
|
||||
func (df *DataFilter) SetArrayData(sourceData string, jsonObject *gabs.Container, sourcePathArr []string, mapPathArr []string) *gabs.Container {
|
||||
jsonObject = gabs.New()
|
||||
|
||||
for idx, sourcePath := range sourcePathArr {
|
||||
if idx < len(sourcePathArr)-1 {
|
||||
if !jsonObject.Exists(sourcePath) {
|
||||
_, _ = jsonObject.ArrayP(sourcePath)
|
||||
}
|
||||
}
|
||||
instance, _ := gabs.ParseJSON([]byte(sourceData))
|
||||
if !instance.Exists() {
|
||||
fmt.Println(sourcePathArr[len(sourcePathArr)-1] + " 不存在")
|
||||
} else {
|
||||
dataList, _ := instance.Children()
|
||||
for _, item := range dataList {
|
||||
cItem := gabs.New()
|
||||
cItem.SetP(gjson.Get(item.String(), sourcePath).String(), mapPathArr[idx])
|
||||
jsonObject.ArrayAppendP(cItem.Data(), mapPathArr[idx])
|
||||
}
|
||||
//jsonObject.ArrayAppend(jsonObject.Data())
|
||||
// fmt.Println("数据 : ", jsonObject.String())
|
||||
// jsonObject.ArrayAppendP(result.Data(), mapPathArr[idx])
|
||||
|
||||
}
|
||||
|
||||
df.SetArrayData(gjson.Get(sourceData, sourcePathArr[idx]).String(), jsonObject, sourcePathArr[idx+1:], mapPathArr[idx+1:])
|
||||
// jsonObject.ArrayAppendP(v.Data(), mapPathArr[idx])
|
||||
}
|
||||
fmt.Println("最终 : ", jsonObject.String())
|
||||
return jsonObject
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ func TestDataFilterDiffArr(t *testing.T) {
|
||||
"table": []map[string]interface{}{
|
||||
{"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}}}},
|
||||
{"user_list": []interface{}{map[string]interface{}{"name": "andy", "age": 28, "number": 2}}},
|
||||
},
|
||||
}
|
||||
rule := []*FilterDataRule{
|
||||
@ -272,5 +272,6 @@ func TestDataFilterDiffArr(t *testing.T) {
|
||||
byteData, _ := json.Marshal(source)
|
||||
filter := NewDataFilter(string(byteData), rule)
|
||||
filter.UserItemToSlice()
|
||||
fmt.Println(filter.Filter())
|
||||
filter.Filter()
|
||||
//fmt.Println()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user