From cd52bbb02b633893f7f77758df9078809bc0c2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 8 Jun 2024 20:17:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E7=B1=BB=E5=9E=8B=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filter.go | 66 ++++++++++++++++++++++--------------------------------- go.mod | 4 ++-- go.sum | 4 ++++ 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/filter.go b/filter.go index a3a4900..dcd4a81 100644 --- a/filter.go +++ b/filter.go @@ -10,6 +10,7 @@ package filter import ( "encoding/json" "fmt" + "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/wrapper" "strings" @@ -184,48 +185,33 @@ func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValu strVal := wrapper.String(sourceValueStr) switch dataType { - case wrapper.DataTypeInt8: - return strVal.ToInt8().Value, strVal.ToInt8().Err - case wrapper.DataTypeInt16: - return strVal.ToInt16().Value, strVal.ToInt16().Err - case wrapper.DataTypeInt32: - return strVal.ToInt32().Value, strVal.ToInt32().Err - case wrapper.DataTypeInt64: - return strVal.ToInt64().Value, strVal.ToInt64().Err - case wrapper.DataTypeInt: - return strVal.ToInt().Value, strVal.ToInt().Err - case wrapper.DataTypeUint8: - return strVal.ToUint8().Value, strVal.ToUint8().Err - case wrapper.DataTypeUint16: - return strVal.ToUint16().Value, strVal.ToUint16().Err - case wrapper.DataTypeUint32: - return strVal.ToUint32().Value, strVal.ToUint32().Err - case wrapper.DataTypeUint64: - return strVal.ToUint64().Value, strVal.ToUint64().Err - case wrapper.DataTypeUint: - return strVal.ToUint().Value, strVal.ToUint().Err - case wrapper.DataTypeBool: - return strVal.ToBool().Value, strVal.ToBool().Err - case wrapper.DataTypeFloat32: - return strVal.ToFloat64().Value, strVal.ToFloat64().Err - case wrapper.DataTypeFloat64: - fallthrough - case wrapper.DataTypeFloat: - fallthrough - case wrapper.DataTypeDouble: - return strVal.ToFloat64().Value, strVal.ToFloat64().Err - case wrapper.DataTypeNumber: - return strVal.ToNumber().Value, strVal.ToNumber().Err - case wrapper.DataTypeString: - return sourceValueStr, nil - case wrapper.DataTypeAny: - return sourceValue.Value(), nil - case wrapper.DataTypeAnySlice: + case consts.DataTypeInt: + intVal := strVal.ToInt() + return intVal.Value, intVal.Err + case consts.DataTypeUint: + uintVal := strVal.ToUint64() + return uintVal.Value, uintVal.Err + case consts.DataTypeBool: + boolVal := strVal.ToBool() + return boolVal.Value, boolVal.Err + case consts.DataTypeFloat: + floatVal := strVal.ToFloat64() + return floatVal.Value, floatVal.Err + case consts.DataTypeString: + return strVal.Value(), nil + case consts.DataTypeAny: + if sourceValue.Exists() { + return sourceValue.Value(), nil + } + return defaultValue, nil + case consts.DataTypeSliceAny: // 任意类型的list - return strVal.ToAnySlice().Value, strVal.ToAnySlice().Err - case wrapper.DataTypeObject: + sliceVal := strVal.ToAnySlice() + return sliceVal.Value, sliceVal.Err + case consts.DataTypeMapStrAny: // object - return strVal.ToObject().Value, strVal.ToObject().Err + objectVal := strVal.ToObject() + return objectVal.Value, objectVal.Err default: return nil, errors.New(dataType + " is not support!") } diff --git a/go.mod b/go.mod index 7bd6691..f8b15a5 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,11 @@ go 1.21 toolchain go1.21.5 require ( + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608115700-4311f44fabf6 git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 - git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240520124754-3faebb9145e3 + git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240608120635-588df729e0d3 github.com/pkg/errors v0.9.1 github.com/smartystreets/goconvey v1.8.1 github.com/tidwall/gjson v1.17.1 @@ -17,7 +18,6 @@ require ( ) require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608104035-feddc6a70f9d // indirect git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect github.com/BurntSushi/toml v1.4.0 // indirect diff --git a/go.sum b/go.sum index 9dc63b6..a5c472a 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240517060653-203cc568fbec h1:ENemx9 git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240517060653-203cc568fbec/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608104035-feddc6a70f9d h1:qKou2RVGqRd7ojAHq2H8xTxfwayjYvfn35Rk+Fz0+zU= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608104035-feddc6a70f9d/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608115700-4311f44fabf6 h1:3xXT341KFAN8AzliAUJtOkyTugNpHpVXY4wDXwE0xmw= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608115700-4311f44fabf6/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-20230307094841-e437ba87af10 h1:+Lg4vXFEiWVKjhUJdXuoP0AgjGT49oqJ3301STnZErk= @@ -38,6 +40,8 @@ git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240320040704-e125c7e75dfb h1:B9KrV git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240320040704-e125c7e75dfb/go.mod h1:W2Dk+WLkUL6MzVS4XRKz9qZ7jfw25tqoXm04eDzGIKw= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240520124754-3faebb9145e3 h1:fDDWQV3Xu/ntjrtKjwK30URPHr4kJJpqZocw52x+DKw= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240520124754-3faebb9145e3/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240608120635-588df729e0d3 h1:IDzOawDhqBWHACsPKWVv/sALUj09NxrjfB2whWaovvY= +git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240608120635-588df729e0d3/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=