save code

This commit is contained in:
白茶清欢 2023-08-31 21:06:35 +08:00
parent 1cdf5449fd
commit 435b339604

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中