feature/upgrade_filter #7
18
filter.go
18
filter.go
@ -11,6 +11,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"reflect"
|
||||
@ -58,8 +59,20 @@ func (f *filter) Deal() error {
|
||||
)
|
||||
|
||||
for _, rule := range f.filterRuleList {
|
||||
if len(rule.TargetPath) == 0 {
|
||||
// 未配置目标路径则, 目标路径和源路径保持一致
|
||||
rule.TargetPath = rule.SourcePath
|
||||
}
|
||||
if strings.Contains(rule.SourcePath, gjson_hack.ArrayIdxTpl) {
|
||||
// 数组,验证数组层级是否一致
|
||||
sourceArr := strings.Split(rule.SourcePath, gjson_hack.ArrayIdxTpl)
|
||||
TargetArr := strings.Split(rule.TargetPath, gjson_hack.ArrayIdxTpl)
|
||||
if len(sourceArr) != len(TargetArr) {
|
||||
return errors.New(rule.SourcePath + " and " + rule.TargetPath + " array deep not match")
|
||||
}
|
||||
}
|
||||
if f.IsArray(rule) {
|
||||
// 对于list的处理
|
||||
// 对于list的处理, 展开层级, 并自动追加到f.filterRuleList 后面
|
||||
if err = f.handleArray(rule); nil != err {
|
||||
return err
|
||||
}
|
||||
@ -103,7 +116,7 @@ func (f *filter) Deal() error {
|
||||
//
|
||||
// Date : 17:48 2023/1/1
|
||||
func (f *filter) IsArray(rule MapRule) bool {
|
||||
return strings.Contains(rule.SourcePath, "[]")
|
||||
return strings.Contains(rule.SourcePath, gjson_hack.ArrayIdxTpl)
|
||||
}
|
||||
|
||||
// handleArray 处理数组(最复杂的场景)
|
||||
@ -112,6 +125,7 @@ func (f *filter) IsArray(rule MapRule) bool {
|
||||
//
|
||||
// Date : 17:41 2023/1/1
|
||||
func (f *filter) handleArray(rule MapRule) error {
|
||||
// TODO : 对于list的处理, 展开层级, 并自动追加到f.filterRuleList 后面
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user