完成数据过主干逻辑,细节健壮性有待继续完善 #4

Merged
zhangdeman merged 13 commits from feature/data_filter into master 2023-09-02 22:17:14 +08:00
Showing only changes of commit 435b339604 - Show all commits

View File

@ -21,6 +21,10 @@ import (
"github.com/Jeffail/gabs"
)
const (
virtualRoot = "__virtual__root"
)
// FilterDataRule 参数过滤规则
//
// Author : go_developer@163.com<白茶清欢>
@ -39,6 +43,19 @@ type FilterDataRule struct {
//
// Date : 2022/1/22 9:50 PM
func NewDataFilter(source string, filterRule []*FilterDataRule) *DataFilter {
jsonTree := gjson.Parse(source)
isVirtual := false
if jsonTree.IsArray() {
source = fmt.Sprintf(`{"%v":%v}`, virtualRoot, source)
isVirtual = true
}
if isVirtual {
for _, item := range filterRule {
item.MapKey = virtualRoot + "." + item.MapKey
item.SourceKey = virtualRoot + "." + item.SourceKey
}
}
return &DataFilter{
source: source,
filterRule: filterRule,
@ -56,6 +73,7 @@ type DataFilter struct {
filterRule []*FilterDataRule
itemKeyToSlice bool
hasDealDiffPath map[string]string
isVirtualRoot bool
}
// Filter 数据过滤
@ -116,7 +134,11 @@ func (df *DataFilter) Filter() (string, error) {
return "", err
}
}
return jsonObject.String(), nil
final := jsonObject.String()
if df.isVirtualRoot {
return gjson.Get(final, virtualRoot).String(), nil
}
return final, nil
}
// UserItemToSlice 支持多个独立的字段合并到slice中