优化error
This commit is contained in:
14
error.go
Normal file
14
error.go
Normal file
@ -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")
|
||||||
|
)
|
||||||
19
filter.go
19
filter.go
@ -242,6 +242,7 @@ func (f *filter) getValue(dataType consts.DataType, sourceValue gjson.Result, de
|
|||||||
return strVal.Value(), nil
|
return strVal.Value(), nil
|
||||||
case consts.DataTypeAny:
|
case consts.DataTypeAny:
|
||||||
if sourceValue.Exists() {
|
if sourceValue.Exists() {
|
||||||
|
// TODO : 可能存在精度丢失
|
||||||
return sourceValue.Value(), nil
|
return sourceValue.Value(), nil
|
||||||
}
|
}
|
||||||
return defaultValue, nil
|
return defaultValue, nil
|
||||||
@ -251,7 +252,7 @@ func (f *filter) getValue(dataType consts.DataType, sourceValue gjson.Result, de
|
|||||||
return sliceVal.Value, sliceVal.Err
|
return sliceVal.Value, sliceVal.Err
|
||||||
case consts.DataTypeSliceInt, consts.DataTypeSliceIntWithChar:
|
case consts.DataTypeSliceInt, consts.DataTypeSliceIntWithChar:
|
||||||
// 任意类型的list
|
// 任意类型的list
|
||||||
if strings.HasPrefix(strVal.Value(), "[") && strings.HasPrefix(strVal.Value(), "]") {
|
if strings.HasPrefix(strVal.Value(), "[") && strings.HasSuffix(strVal.Value(), "]") {
|
||||||
// 序列化之后的数组
|
// 序列化之后的数组
|
||||||
sliceVal := strVal.ToInt64Slice()
|
sliceVal := strVal.ToInt64Slice()
|
||||||
return sliceVal.Value, sliceVal.Err
|
return sliceVal.Value, sliceVal.Err
|
||||||
@ -310,54 +311,54 @@ func (f *filter) getValue(dataType consts.DataType, sourceValue gjson.Result, de
|
|||||||
return res, err
|
return res, err
|
||||||
case consts.DataTypeMapStrInt:
|
case consts.DataTypeMapStrInt:
|
||||||
if !sourceValue.IsObject() {
|
if !sourceValue.IsObject() {
|
||||||
return nil, errors.New("data type is not object")
|
return nil, ErrDataIsNotObject
|
||||||
}
|
}
|
||||||
var res map[string]int64
|
var res map[string]int64
|
||||||
err := strVal.ToStruct(&res)
|
err := strVal.ToStruct(&res)
|
||||||
return res, err
|
return res, err
|
||||||
case consts.DataTypeMapStrUint:
|
case consts.DataTypeMapStrUint:
|
||||||
if !sourceValue.IsObject() {
|
if !sourceValue.IsObject() {
|
||||||
return nil, errors.New("data type is not object")
|
return nil, ErrDataIsNotObject
|
||||||
}
|
}
|
||||||
var res map[string]uint64
|
var res map[string]uint64
|
||||||
err := strVal.ToStruct(&res)
|
err := strVal.ToStruct(&res)
|
||||||
return res, err
|
return res, err
|
||||||
case consts.DataTypeMapStrFloat:
|
case consts.DataTypeMapStrFloat:
|
||||||
if !sourceValue.IsObject() {
|
if !sourceValue.IsObject() {
|
||||||
return nil, errors.New("data type is not object")
|
return nil, ErrDataIsNotObject
|
||||||
}
|
}
|
||||||
var res map[string]float64
|
var res map[string]float64
|
||||||
err := strVal.ToStruct(&res)
|
err := strVal.ToStruct(&res)
|
||||||
return res, err
|
return res, err
|
||||||
case consts.DataTypeMapStrBool:
|
case consts.DataTypeMapStrBool:
|
||||||
if !sourceValue.IsObject() {
|
if !sourceValue.IsObject() {
|
||||||
return nil, errors.New("data type is not object")
|
return nil, ErrDataIsNotObject
|
||||||
}
|
}
|
||||||
var res map[string]bool
|
var res map[string]bool
|
||||||
err := strVal.ToStruct(&res)
|
err := strVal.ToStruct(&res)
|
||||||
return res, err
|
return res, err
|
||||||
case consts.DataTypeMapStrAny:
|
case consts.DataTypeMapStrAny:
|
||||||
if !sourceValue.IsObject() {
|
if !sourceValue.IsObject() {
|
||||||
return nil, errors.New("data type is not object")
|
return nil, ErrDataIsNotObject
|
||||||
}
|
}
|
||||||
var res map[string]any
|
var res map[string]any
|
||||||
err := strVal.ToStruct(&res)
|
err := strVal.ToStruct(&res)
|
||||||
return res, err
|
return res, err
|
||||||
case consts.DataTypeMapStrStr:
|
case consts.DataTypeMapStrStr:
|
||||||
if !sourceValue.IsObject() {
|
if !sourceValue.IsObject() {
|
||||||
return nil, errors.New("data type is not object")
|
return nil, ErrDataIsNotObject
|
||||||
}
|
}
|
||||||
var res map[string]string
|
var res map[string]string
|
||||||
err := strVal.ToStruct(&res)
|
err := strVal.ToStruct(&res)
|
||||||
return res, err
|
return res, err
|
||||||
case consts.DataTypeMapStrSlice:
|
case consts.DataTypeMapStrSlice:
|
||||||
if !sourceValue.IsObject() {
|
if !sourceValue.IsObject() {
|
||||||
return nil, errors.New("data type is not object")
|
return nil, ErrDataIsNotObject
|
||||||
}
|
}
|
||||||
var res map[string][]any
|
var res map[string][]any
|
||||||
err := strVal.ToStruct(&res)
|
err := strVal.ToStruct(&res)
|
||||||
return res, err
|
return res, err
|
||||||
default:
|
default:
|
||||||
return nil, errors.New(dataType.String() + " is not support!")
|
return nil, errors.New("`" + dataType.String() + "` is not support!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user