|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
@ -173,7 +179,7 @@ func (df *DataFilter) formatRule() {
|
|
|
|
|
}
|
|
|
|
|
// 数组层级深度不同,重新对对齐数据
|
|
|
|
|
diffArr := sourcePathArr[0 : len(sourcePathArr)-len(mapPathArr)+1]
|
|
|
|
|
if newPath := df.dealDiffArr(diffArr, sourcePathArr[len(sourcePathArr)-len(mapPathArr)-1:], mapPathArr); len(newPath) > 0 {
|
|
|
|
|
if newPath := df.dealDiffArr(diffArr); len(newPath) > 0 {
|
|
|
|
|
sourcePathArr[len(sourcePathArr)-len(mapPathArr)] = newPath
|
|
|
|
|
item.SourceKey = strings.Join(sourcePathArr[len(sourcePathArr)-len(mapPathArr):], ".[].")
|
|
|
|
|
}
|
|
|
|
@ -202,7 +208,7 @@ func (df *DataFilter) formatRule() {
|
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
|
//
|
|
|
|
|
// Date : 5:04 下午 2022/1/25
|
|
|
|
|
func (df *DataFilter) dealDiffArr(diffArr []string, sourceAfterArr []string, mapArr []string) string {
|
|
|
|
|
func (df *DataFilter) dealDiffArr(diffArr []string) string {
|
|
|
|
|
if len(diffArr) == 0 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|