From cb23772664e631556b254fce7e0a77f7efff9e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 29 Nov 2024 17:55:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20todo=20=E5=BE=85=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=BA=8B=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/filter.go b/filter.go index 4a71285..d389fd0 100644 --- a/filter.go +++ b/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 )