From 8cca556d6f79893d7e7de1d70569b68e912fe3ea 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, 30 Nov 2024 21:53:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- error.go | 14 ++++++++++++++ filter.go | 19 ++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 error.go diff --git a/error.go b/error.go new file mode 100644 index 0000000..6896b95 --- /dev/null +++ b/error.go @@ -0,0 +1,14 @@ +// Package filter ... +// +// Description : filter ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-11-30 19:14 +package filter + +import "errors" + +var ( + ErrDataIsNotObject = errors.New("data is not an object") +) diff --git a/filter.go b/filter.go index f8c92f1..3e59717 100644 --- a/filter.go +++ b/filter.go @@ -242,6 +242,7 @@ func (f *filter) getValue(dataType consts.DataType, sourceValue gjson.Result, de return strVal.Value(), nil case consts.DataTypeAny: if sourceValue.Exists() { + // TODO : 可能存在精度丢失 return sourceValue.Value(), nil } return defaultValue, nil @@ -251,7 +252,7 @@ func (f *filter) getValue(dataType consts.DataType, sourceValue gjson.Result, de return sliceVal.Value, sliceVal.Err case consts.DataTypeSliceInt, consts.DataTypeSliceIntWithChar: // 任意类型的list - if strings.HasPrefix(strVal.Value(), "[") && strings.HasPrefix(strVal.Value(), "]") { + if strings.HasPrefix(strVal.Value(), "[") && strings.HasSuffix(strVal.Value(), "]") { // 序列化之后的数组 sliceVal := strVal.ToInt64Slice() return sliceVal.Value, sliceVal.Err @@ -310,54 +311,54 @@ func (f *filter) getValue(dataType consts.DataType, sourceValue gjson.Result, de return res, err case consts.DataTypeMapStrInt: if !sourceValue.IsObject() { - return nil, errors.New("data type is not object") + return nil, ErrDataIsNotObject } 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") + return nil, ErrDataIsNotObject } 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") + return nil, ErrDataIsNotObject } 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") + return nil, ErrDataIsNotObject } var res map[string]bool err := strVal.ToStruct(&res) return res, err case consts.DataTypeMapStrAny: if !sourceValue.IsObject() { - return nil, errors.New("data type is not object") + return nil, ErrDataIsNotObject } 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") + return nil, ErrDataIsNotObject } 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") + return nil, ErrDataIsNotObject } var res map[string][]any err := strVal.ToStruct(&res) return res, err default: - return nil, errors.New(dataType.String() + " is not support!") + return nil, errors.New("`" + dataType.String() + "` is not support!") } }