升级优化 getValue 数据值获取逻辑
This commit is contained in:
		
							
								
								
									
										110
									
								
								filter.go
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								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!")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user