增加obj2slice,以及数据类型检测
This commit is contained in:
@ -8,8 +8,11 @@
|
||||
package json_tool
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"github.com/Jeffail/gabs"
|
||||
@ -59,11 +62,35 @@ func (df *DataFilter) Filter() (string, error) {
|
||||
jsonObject *gabs.Container
|
||||
err error
|
||||
)
|
||||
// 记录 obj => slice 的数据类型
|
||||
obg2slice := make(map[string]string)
|
||||
// 创建数据的根结点
|
||||
jsonObject = gabs.New()
|
||||
for _, item := range df.filterRule {
|
||||
if strings.Contains(item.SourceKey, "[]") {
|
||||
// 数组, 特殊处理
|
||||
// 1. 判断数据源数组深度与目标数组深度是否一致
|
||||
continue
|
||||
}
|
||||
// 目标位置, 是一个数组
|
||||
if strings.HasSuffix(item.MapKey, "[]") {
|
||||
realMapKey := strings.ReplaceAll(item.MapKey, ".[]", "")
|
||||
if exist := jsonObject.Exists(realMapKey); !exist {
|
||||
if _, err = jsonObject.ArrayP(realMapKey); nil != err {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
valueResult := gjson.Get(df.source, item.SourceKey)
|
||||
dataType := df.getValueType(valueResult)
|
||||
if _, exist := obg2slice[realMapKey]; !exist {
|
||||
obg2slice[realMapKey] = dataType
|
||||
}
|
||||
if dataType != obg2slice[realMapKey] {
|
||||
return "", errors.New(realMapKey + " 预期写入的字段数据类型不一致")
|
||||
}
|
||||
if err = jsonObject.ArrayAppend(valueResult.Value(), realMapKey); nil != err {
|
||||
return "", err
|
||||
}
|
||||
continue
|
||||
}
|
||||
sourceSearchResult := gjson.Get(df.source, item.SourceKey)
|
||||
@ -81,3 +108,19 @@ func (df *DataFilter) Filter() (string, error) {
|
||||
}
|
||||
return jsonObject.String(), nil
|
||||
}
|
||||
|
||||
// getValueType 获取数据类型
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2022/1/23 12:45 AM
|
||||
func (df *DataFilter) getValueType(valueResult gjson.Result) string {
|
||||
dataType := reflect.TypeOf(valueResult.Value()).String()
|
||||
if strings.Contains(dataType, "int") {
|
||||
return "int64"
|
||||
}
|
||||
if strings.Contains(dataType, "float") {
|
||||
return "float64"
|
||||
}
|
||||
return dataType
|
||||
}
|
||||
|
Reference in New Issue
Block a user