类型使用枚举值适配

This commit is contained in:
白茶清欢 2024-11-25 14:29:19 +08:00
parent 31af48e926
commit 44de8814d8
4 changed files with 17 additions and 10 deletions

View File

@ -7,15 +7,17 @@
// Date : 2022-07-04 18:02
package filter
import "git.zhangdeman.cn/zhangdeman/consts/enums"
// MapRule 映射规则
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:21 2022/7/4
type MapRule struct {
SourcePath string `json:"source_path"` // 原路径
TargetPath string `json:"target_path"` // 目标路径路径
Required bool `json:"required"` // 必须存在
DataType string `json:"data_type"` // 数据类型
DefaultValue string `json:"default_value"` // 默认值, 以字符串传入, 会转换成 DataType
SourcePath string `json:"source_path"` // 原路径
TargetPath string `json:"target_path"` // 目标路径路径
Required bool `json:"required"` // 必须存在
DataType enums.DataType `json:"data_type"` // 数据类型
DefaultValue string `json:"default_value"` // 默认值, 以字符串传入, 会转换成 DataType
}

View File

@ -11,6 +11,7 @@ import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/consts/enums"
"git.zhangdeman.cn/zhangdeman/serialize"
"git.zhangdeman.cn/zhangdeman/wrapper"
"reflect"
@ -196,7 +197,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) (any, error) {
func (f *filter) getValue(dataType enums.DataType, sourceValue gjson.Result, defaultValue string) (any, error) {
sourceValueStr := defaultValue
if sourceValue.Exists() {
str := sourceValue.String()
@ -282,7 +283,7 @@ func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValu
sliceVal := strVal.ToStringSlice(",")
return sliceVal.Value, sliceVal.Err
case consts.DataTypeSliceSlice, consts.DataTypeMapAnyAny:
return nil, errors.New(consts.DataTypeSliceSlice + " : data type is not support")
return nil, errors.New(consts.DataTypeSliceSlice.String() + " : data type is not support")
case consts.DataTypeSliceMapStringAny:
if !sourceValue.IsArray() {
return nil, errors.New("data type is not array")
@ -340,6 +341,6 @@ func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValu
err := strVal.ToStruct(&res)
return res, err
default:
return nil, errors.New(dataType + " is not support!")
return nil, errors.New(dataType.String() + " is not support!")
}
}

4
go.mod
View File

@ -5,11 +5,11 @@ go 1.21
toolchain go1.21.5
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241106102517-46cd353dd617
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0
github.com/pkg/errors v0.9.1
github.com/smartystreets/goconvey v1.8.1
github.com/tidwall/gjson v1.18.0

4
go.sum
View File

@ -2,6 +2,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608115700-4311f44fabf6 h1:3xXT34
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608115700-4311f44fabf6/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 h1:BiAlBJ+DuRs/xD7nDQD2JT8Oc+V+0Uwt36qZwdXGvzI=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI=
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
@ -22,6 +24,8 @@ git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240608120635-588df729e0d3 h1:IDzOa
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240608120635-588df729e0d3/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241106102517-46cd353dd617 h1:4rwv7bYKA4+IQgQiT8yNeM92t+fGd2tR5zD2ixpP6i8=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241106102517-46cd353dd617/go.mod h1:uEuMBRzTlUJ9k3H+PymZWPn1b48U9lJPQ+ZBWmP+O/c=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0 h1:aGQADmQKTF7c8+s3acnn569sTJwjlLhhhZfQ6f4nAH0=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0/go.mod h1:lvVOIrYDvCQHUzBdaOwKSrxT9ubcXQJBsafWMWu+I14=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=