From d5db65128251fbae78c52b47dd0ef9fd3f17b077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 25 Sep 2024 17:26:49 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 2 +- filter.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/define.go b/define.go index a80d083..e121323 100644 --- a/define.go +++ b/define.go @@ -14,7 +14,7 @@ package filter // Date : 12:21 2022/7/4 type MapRule struct { SourcePath string `json:"source_path"` // 原路径 - MapPath string `json:"map_path"` // 映射路径 + TargetPath string `json:"target_path"` // 目标路径路径 Required bool `json:"required"` // 必须存在 DataType string `json:"data_type"` // 数据类型 DefaultValue string `json:"default_value"` // 默认值, 以字符串传入, 会转换成 DataType diff --git a/filter.go b/filter.go index dcd4a81..e9055c2 100644 --- a/filter.go +++ b/filter.go @@ -67,7 +67,7 @@ func (f *filter) Deal() error { if formatVal, err = f.getValue(rule.DataType, sourceResult, rule.DefaultValue); nil != err { return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error()) } - if f.formatResult, err = sjson.Set(f.formatResult, rule.MapPath, formatVal); nil != err { + if f.formatResult, err = sjson.Set(f.formatResult, rule.TargetPath, formatVal); nil != err { return err } } @@ -97,7 +97,7 @@ func (f *filter) handleArray(rule MapRule) error { for idx, item := range sourcePathArray { sourcePathArray[idx] = strings.Trim(item, ".") } - mapPathArray := strings.Split(strings.TrimRight(rule.MapPath, ".[]"), "[]") + mapPathArray := strings.Split(strings.TrimRight(rule.TargetPath, ".[]"), "[]") for idx, item := range mapPathArray { mapPathArray[idx] = strings.Trim(item, ".") } -- 2.36.6 From 0e4fabcaee442aa36fe5fedbf459ff226fce2332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 25 Sep 2024 18:03:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E4=BC=98=E5=8C=96=20getV?= =?UTF-8?q?alue=20=E6=95=B0=E6=8D=AE=E5=80=BC=E8=8E=B7=E5=8F=96=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.go | 110 +++++++++++++++++++++++++++++++++++++++++++++++-- filter_test.go | 25 +++++++---- 2 files changed, 125 insertions(+), 10 deletions(-) diff --git a/filter.go b/filter.go index e9055c2..bd2adc7 100644 --- a/filter.go +++ b/filter.go @@ -208,10 +208,114 @@ func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValu // 任意类型的list sliceVal := strVal.ToAnySlice() return sliceVal.Value, sliceVal.Err + case consts.DataTypeSliceInt, consts.DataTypeSliceIntWithChar: + // 任意类型的list + if strings.HasPrefix(strVal.Value(), "[") && strings.HasPrefix(strVal.Value(), "]") { + // 序列化之后的数组 + sliceVal := strVal.ToInt64Slice() + return sliceVal.Value, sliceVal.Err + } + // 分隔的数组 + sliceVal := strVal.ToInt64Slice(",") + return sliceVal.Value, sliceVal.Err + case consts.DataTypeSliceUint, consts.DataTypeSliceUintWithChar: + // 任意类型的list + if strings.HasPrefix(strVal.Value(), "[") && strings.HasPrefix(strVal.Value(), "]") { + // 序列化之后的数组 + sliceVal := strVal.ToUint64Slice() + return sliceVal.Value, sliceVal.Err + } + // 分隔的数组 + sliceVal := strVal.ToUint64Slice(",") + return sliceVal.Value, sliceVal.Err + case consts.DataTypeSliceFloat, consts.DataTypeSliceFloatWithChar: + // 任意类型的list + if strings.HasPrefix(strVal.Value(), "[") && strings.HasPrefix(strVal.Value(), "]") { + // 序列化之后的数组 + sliceVal := strVal.ToFloat64Slice() + return sliceVal.Value, sliceVal.Err + } + // 分隔的数组 + sliceVal := strVal.ToFloat64Slice(",") + return sliceVal.Value, sliceVal.Err + case consts.DataTypeSliceBool, consts.DataTypeSliceBoolWithChar: + // 任意类型的list + if strings.HasPrefix(strVal.Value(), "[") && strings.HasPrefix(strVal.Value(), "]") { + // 序列化之后的数组 + sliceVal := strVal.ToBoolSlice() + return sliceVal.Value, sliceVal.Err + } + // 分隔的数组 + sliceVal := strVal.ToBoolSlice(",") + return sliceVal.Value, sliceVal.Err + case consts.DataTypeSliceString, consts.DataTypeSliceStringWithChar: + // 任意类型的list + if strings.HasPrefix(strVal.Value(), "[") && strings.HasPrefix(strVal.Value(), "]") { + // 序列化之后的数组 + sliceVal := strVal.ToStringSlice() + return sliceVal.Value, sliceVal.Err + } + // 分隔的数组 + sliceVal := strVal.ToStringSlice(",") + return sliceVal.Value, sliceVal.Err + case consts.DataTypeSliceSlice, consts.DataTypeMapAnyAny: + return nil, errors.New(consts.DataTypeSliceSlice + " : data type is not support") + case consts.DataTypeSliceMapStringAny: + if !sourceValue.IsArray() { + return nil, errors.New("data type is not array") + } + var res []map[string]any + err := strVal.ToStruct(&res) + return res, err + case consts.DataTypeMapStrInt: + if !sourceValue.IsObject() { + return nil, errors.New("data type is not object") + } + var res map[string]int64 + err := strVal.ToStruct(&res) + return res, err + case consts.DataTypeMapStrUint: + if !sourceValue.IsObject() { + return nil, errors.New("data type is not object") + } + var res map[string]uint64 + err := strVal.ToStruct(&res) + return res, err + case consts.DataTypeMapStrFloat: + if !sourceValue.IsObject() { + return nil, errors.New("data type is not object") + } + var res map[string]float64 + err := strVal.ToStruct(&res) + return res, err + case consts.DataTypeMapStrBool: + if !sourceValue.IsObject() { + return nil, errors.New("data type is not object") + } + var res map[string]bool + err := strVal.ToStruct(&res) + return res, err case consts.DataTypeMapStrAny: - // object - objectVal := strVal.ToObject() - return objectVal.Value, objectVal.Err + if !sourceValue.IsObject() { + return nil, errors.New("data type is not object") + } + var res map[string]any + err := strVal.ToStruct(&res) + return res, err + case consts.DataTypeMapStrStr: + if !sourceValue.IsObject() { + return nil, errors.New("data type is not object") + } + var res map[string]string + err := strVal.ToStruct(&res) + return res, err + case consts.DataTypeMapStrSlice: + if !sourceValue.IsObject() { + return nil, errors.New("data type is not object") + } + var res map[string][]any + err := strVal.ToStruct(&res) + return res, err default: return nil, errors.New(dataType + " is not support!") } diff --git a/filter_test.go b/filter_test.go index ae15bf8..7c8f263 100644 --- a/filter_test.go +++ b/filter_test.go @@ -8,6 +8,7 @@ package filter import ( + "encoding/json" "fmt" "testing" @@ -37,35 +38,35 @@ func TestNewFilter(t *testing.T) { filterRuleList := []MapRule{ { SourcePath: "base.name", - MapPath: "user_name", + TargetPath: "user_name", Required: true, DataType: "string", DefaultValue: "", }, { SourcePath: "base.age", - MapPath: "user_age", + TargetPath: "user_age", Required: true, DataType: "int", DefaultValue: "", }, { SourcePath: "base.height", - MapPath: "user_height", + TargetPath: "user_height", Required: true, DataType: "string", DefaultValue: "", }, { SourcePath: "company.name", - MapPath: "company_name", + TargetPath: "company_name", Required: true, DataType: "string", DefaultValue: "", }, { SourcePath: "company.start", - MapPath: "company_start", + TargetPath: "company_start", Required: true, DataType: "string", DefaultValue: "", @@ -105,7 +106,7 @@ func TestNewFilterForArrayOne(t *testing.T) { filterRuleList := []MapRule{ { SourcePath: "[].name", - MapPath: "user_name.[]", + TargetPath: "user_name.[]", Required: true, DataType: "string", DefaultValue: "", @@ -145,7 +146,7 @@ func TestNewFilterForArrayTwo(t *testing.T) { filterRuleList := []MapRule{ { SourcePath: "user_list.[].name", - MapPath: "user.name_list.[]", + TargetPath: "user.name_list.[]", Required: true, DataType: "string", DefaultValue: "", @@ -158,3 +159,13 @@ func TestNewFilterForArrayTwo(t *testing.T) { fmt.Println(f.String()) }) } + +func TestAntMap(t *testing.T) { + testMap := map[any]any{ + "name": "zhangde", + 1: 1, + 1.234: 2.345, + } + byteData, err := json.Marshal(testMap) + fmt.Println(string(byteData), err) +} -- 2.36.6 From 3107ca239f7ef54e919e16dac9309c4dea4711f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 25 Sep 2024 18:35:40 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0map=E5=86=B2=E7=AA=81?= =?UTF-8?q?=E6=80=A7=E9=85=8D=E7=BD=AE=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/filter.go b/filter.go index bd2adc7..78dd7ff 100644 --- a/filter.go +++ b/filter.go @@ -11,7 +11,9 @@ import ( "encoding/json" "fmt" "git.zhangdeman.cn/zhangdeman/consts" + "git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/wrapper" + "reflect" "strings" "github.com/tidwall/gjson" @@ -52,7 +54,7 @@ type filter struct { func (f *filter) Deal() error { var ( err error - formatVal interface{} + formatVal any ) for _, rule := range f.filterRuleList { @@ -67,6 +69,27 @@ func (f *filter) Deal() error { if formatVal, err = f.getValue(rule.DataType, sourceResult, rule.DefaultValue); nil != err { return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error()) } + if reflect.TypeOf(formatVal).Kind() == reflect.Map { + // 获取的数据是map类型, 处理数据覆盖 + // eg : 配置如下两个规则 process.id(string) 、process(map[string]any) + // 若输入数据的process.id为int类型, 则格式化后的process.id必为 string, 应为 process.id 规则的控制更精细 + gjsonVal := gjson.Get(f.formatResult, rule.TargetPath) + if gjsonVal.Exists() && gjsonVal.IsObject() { + var ( + existRes = map[string]any{} + formatRes = map[string]any{} + ) + // 已存在, 且是对象 + _ = serialize.JSON.UnmarshalWithNumber([]byte(gjsonVal.String()), &existRes) + if err = serialize.JSON.Transition(formatVal, &formatRes); nil != err { + return errors.New("conflict data path config deal fail : " + err.Error()) + } + for k, v := range existRes { + formatRes[k] = v + } + formatVal = formatRes // 重新赋值 formatVal + } + } if f.formatResult, err = sjson.Set(f.formatResult, rule.TargetPath, formatVal); nil != err { return err } @@ -173,7 +196,7 @@ func (f *filter) Parse(receiver interface{}) error { // Author : go_developer@163.com<白茶清欢> // // Date : 12:25 2022/7/4 -func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValue string) (interface{}, error) { +func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValue string) (any, error) { sourceValueStr := defaultValue if sourceValue.Exists() { str := sourceValue.String() -- 2.36.6