Merge branch 'master' of git.zhangdeman.cn:zhangdeman/wrapper
This commit is contained in:
commit
8d056baada
18
convert.go
18
convert.go
@ -38,7 +38,7 @@ var errNilPtr = errors.New("destination pointer is nil") // embedded in descript
|
||||
// convertAssign copies to dest the value in src, converting it if possible.
|
||||
// An error is returned if the copy would result in loss of information.
|
||||
// dest should be a pointer type.
|
||||
func ConvertAssign(dest, src interface{}) error {
|
||||
func ConvertAssign(dest, src any) error {
|
||||
// Common cases, without reflect.
|
||||
switch s := src.(type) {
|
||||
case string:
|
||||
@ -70,7 +70,7 @@ func ConvertAssign(dest, src interface{}) error {
|
||||
}
|
||||
*d = string(s)
|
||||
return nil
|
||||
case *interface{}:
|
||||
case *any:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
@ -112,7 +112,7 @@ func ConvertAssign(dest, src interface{}) error {
|
||||
}
|
||||
case nil:
|
||||
switch d := dest.(type) {
|
||||
case *interface{}:
|
||||
case *any:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
@ -164,7 +164,7 @@ func ConvertAssign(dest, src interface{}) error {
|
||||
*d = bv.(bool)
|
||||
}
|
||||
return err
|
||||
case *interface{}:
|
||||
case *any:
|
||||
*d = src
|
||||
return nil
|
||||
}
|
||||
@ -271,11 +271,11 @@ func cloneBytes(b []byte) []byte {
|
||||
return c
|
||||
}
|
||||
|
||||
func ToString(src interface{}) string {
|
||||
func ToString(src any) string {
|
||||
return asString(src)
|
||||
}
|
||||
|
||||
func asString(src interface{}) string {
|
||||
func asString(src any) string {
|
||||
switch v := src.(type) {
|
||||
case string:
|
||||
return v
|
||||
@ -327,14 +327,14 @@ func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
|
||||
// []byte
|
||||
// string
|
||||
// time.Time
|
||||
type Value interface{}
|
||||
type Value any
|
||||
|
||||
type boolType struct{}
|
||||
|
||||
var Bool boolType
|
||||
|
||||
func (boolType) String() string { return "Bool" }
|
||||
func (boolType) ConvertValue(src interface{}) (Value, error) {
|
||||
func (boolType) ConvertValue(src any) (Value, error) {
|
||||
switch s := src.(type) {
|
||||
case bool:
|
||||
return s, nil
|
||||
@ -390,5 +390,5 @@ type Scanner interface {
|
||||
// Reference types such as []byte are only valid until the next call to Scan
|
||||
// and should not be retained. Their underlying memory is owned by the driver.
|
||||
// If retention is necessary, copy their values before the next call to Scan.
|
||||
Scan(src interface{}) error
|
||||
Scan(src any) error
|
||||
}
|
||||
|
@ -255,7 +255,7 @@ type Float64PtrResult struct {
|
||||
//
|
||||
// Date : 16:40 2023/5/8
|
||||
type Any struct {
|
||||
Value interface{}
|
||||
Value any
|
||||
Err error
|
||||
}
|
||||
|
||||
@ -285,7 +285,7 @@ type BoolPtrResult struct {
|
||||
//
|
||||
// Date : 16:38 2023/5/8
|
||||
type ObjectResult struct {
|
||||
Value map[string]interface{}
|
||||
Value map[string]any
|
||||
Err error
|
||||
}
|
||||
|
||||
@ -475,6 +475,6 @@ type MapResult struct {
|
||||
//
|
||||
// Date : 18:28 2023/5/8
|
||||
type AnySliceResult struct {
|
||||
Value []interface{}
|
||||
Value []any
|
||||
Err error
|
||||
}
|
||||
|
22
map.go
22
map.go
@ -8,11 +8,11 @@
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/easymap"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
@ -21,7 +21,7 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:02 2023/8/10
|
||||
func EasyMap(mapData interface{}) Map {
|
||||
func EasyMap(mapData any) Map {
|
||||
m, _ := EasyMapWithError(mapData)
|
||||
return m
|
||||
}
|
||||
@ -31,14 +31,14 @@ func EasyMap(mapData interface{}) Map {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:06 2023/8/10
|
||||
func EasyMapWithError(mapData interface{}) (Map, error) {
|
||||
func EasyMapWithError(mapData any) (Map, error) {
|
||||
if nil == mapData {
|
||||
return easymap.NewNormal(), nil
|
||||
}
|
||||
m := easymap.NewNormal()
|
||||
reflectType := reflect.TypeOf(mapData)
|
||||
if reflectType.Kind() != reflect.Map {
|
||||
mapFormatData := make(map[string]interface{})
|
||||
mapFormatData := make(map[string]any)
|
||||
if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByte(mapData), &mapFormatData); nil != err {
|
||||
return m, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
|
||||
}
|
||||
@ -58,7 +58,7 @@ func EasyMapWithError(mapData interface{}) (Map, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:11 2023/8/10
|
||||
func EasyMapFromStruct(data interface{}) Map {
|
||||
func EasyMapFromStruct(data any) Map {
|
||||
byteData, _ := json.Marshal(data)
|
||||
return EasyMapFromByte(byteData)
|
||||
}
|
||||
@ -78,11 +78,13 @@ func EasyMapFromString(data string) Map {
|
||||
//
|
||||
// Date : 16:12 2023/8/10
|
||||
func EasyMapFromByte(data []byte) Map {
|
||||
var tmpMap map[interface{}]interface{}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
_ = decoder.Decode(&tmpMap)
|
||||
return EasyMap(tmpMap)
|
||||
res := easymap.NewNormal()
|
||||
jsonRes := gjson.Parse(string(data))
|
||||
jsonRes.ForEach(func(key, value gjson.Result) bool {
|
||||
res.Set(key.Value(), value.Value())
|
||||
return true
|
||||
})
|
||||
return res
|
||||
}
|
||||
|
||||
// Map ...
|
||||
|
14
object.go
14
object.go
@ -19,10 +19,10 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:36 2023/6/1
|
||||
func ObjectData(data interface{}) *ObjectType {
|
||||
func ObjectData(data any) *ObjectType {
|
||||
ot := &ObjectType{
|
||||
source: data,
|
||||
data: map[interface{}]interface{}{},
|
||||
data: map[any]any{},
|
||||
byteData: []byte{},
|
||||
isValid: true,
|
||||
invalidErr: errors.New("data is invalid"),
|
||||
@ -49,8 +49,8 @@ func ObjectData(data interface{}) *ObjectType {
|
||||
//
|
||||
// Date : 18:38 2023/6/1
|
||||
type ObjectType struct {
|
||||
source interface{}
|
||||
data map[interface{}]interface{}
|
||||
source any
|
||||
data map[any]any
|
||||
byteData []byte
|
||||
isValid bool
|
||||
invalidErr error
|
||||
@ -106,7 +106,7 @@ func (ot *ObjectType) ToString() StringResult {
|
||||
// Date : 16:17 2023/6/2
|
||||
func (ot *ObjectType) ToMapStringAny() ObjectResult {
|
||||
res := ObjectResult{
|
||||
Value: map[string]interface{}{},
|
||||
Value: map[string]any{},
|
||||
Err: nil,
|
||||
}
|
||||
if ot.IsNil() {
|
||||
@ -121,7 +121,7 @@ func (ot *ObjectType) ToMapStringAny() ObjectResult {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:41 2023/6/2
|
||||
func (ot *ObjectType) ToStruct(receiver interface{}) error {
|
||||
func (ot *ObjectType) ToStruct(receiver any) error {
|
||||
if nil == receiver {
|
||||
return errors.New("receiver is nil")
|
||||
}
|
||||
@ -136,7 +136,7 @@ func (ot *ObjectType) ToStruct(receiver interface{}) error {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:42 2023/6/2
|
||||
func (ot *ObjectType) ToStructIgnoreErr(receiver interface{}) {
|
||||
func (ot *ObjectType) ToStructIgnoreErr(receiver any) {
|
||||
if nil == receiver {
|
||||
return
|
||||
}
|
||||
|
@ -521,7 +521,7 @@ func (str String) ToStringPtr() StringPtrResult {
|
||||
func (str String) ToObject() ObjectResult {
|
||||
var (
|
||||
res = ObjectResult{
|
||||
Value: map[string]interface{}{},
|
||||
Value: map[string]any{},
|
||||
Err: nil,
|
||||
}
|
||||
)
|
||||
@ -534,7 +534,7 @@ func (str String) ToObject() ObjectResult {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:38 2023/5/4
|
||||
func (str String) ToStruct(receiver interface{}) error {
|
||||
func (str String) ToStruct(receiver any) error {
|
||||
if nil == receiver {
|
||||
return errors.New("receiver is nil")
|
||||
}
|
||||
@ -992,7 +992,7 @@ func (str String) ToStringSlice(splitChar ...string) StringSliceResult {
|
||||
// Date : 15:01 2023/5/5
|
||||
func (str String) ToAnySlice(splitCharList ...string) AnySliceResult {
|
||||
result := AnySliceResult{
|
||||
Value: []interface{}{},
|
||||
Value: []any{},
|
||||
Err: nil,
|
||||
}
|
||||
|
||||
@ -1002,7 +1002,7 @@ func (str String) ToAnySlice(splitCharList ...string) AnySliceResult {
|
||||
}
|
||||
|
||||
valArr := strings.Split(str.Value(), splitCharList[0])
|
||||
valList := make([]interface{}, 0)
|
||||
valList := make([]any, 0)
|
||||
for _, item := range valArr {
|
||||
v := String(item)
|
||||
if res := v.ToInt64(); nil == res.Err {
|
||||
|
@ -17,7 +17,7 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:07 2023/8/10
|
||||
func NewStruct(data interface{}) *Struct {
|
||||
func NewStruct(data any) *Struct {
|
||||
s, _ := NewStructWithError(data)
|
||||
return s
|
||||
}
|
||||
@ -27,7 +27,7 @@ func NewStruct(data interface{}) *Struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:17 2023/8/10
|
||||
func NewStructWithError(data interface{}) (*Struct, error) {
|
||||
func NewStructWithError(data any) (*Struct, error) {
|
||||
if data == nil {
|
||||
return nil, errors.New("input data is nil")
|
||||
}
|
||||
@ -47,7 +47,7 @@ func NewStructWithError(data interface{}) (*Struct, error) {
|
||||
//
|
||||
// Date : 16:05 2023/8/10
|
||||
type Struct struct {
|
||||
data interface{}
|
||||
data any
|
||||
}
|
||||
|
||||
// ToMap 转为Map
|
||||
@ -58,7 +58,7 @@ type Struct struct {
|
||||
func (s *Struct) ToMap() MapResult {
|
||||
if nil == s.data {
|
||||
return MapResult{
|
||||
Value: EasyMap(map[interface{}]interface{}{}),
|
||||
Value: EasyMap(map[any]any{}),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
@ -69,12 +69,12 @@ type CustomDiffFunc func(field string, inputVal wrapper.Map, storageVal wrapper.
|
||||
//
|
||||
// Date : 11:10 2024/3/8
|
||||
type DiffResult struct {
|
||||
Field string `json:"field"` // 字段名
|
||||
OldVal interface{} `json:"old_val"` // 当前field在storageVal中的值
|
||||
NewVal interface{} `json:"new_val"` // 当前field在inputVal中的值
|
||||
IsSame bool `json:"is_same"` // 两个值是否相同
|
||||
DiffReason string `json:"diff_reason"` // 两个值不同的原因
|
||||
Err error `json:"err"` // 对比过程中是否出现异常
|
||||
Field string `json:"field"` // 字段名
|
||||
OldVal any `json:"old_val"` // 当前field在storageVal中的值
|
||||
NewVal any `json:"new_val"` // 当前field在inputVal中的值
|
||||
IsSame bool `json:"is_same"` // 两个值是否相同
|
||||
DiffReason string `json:"diff_reason"` // 两个值不同的原因
|
||||
Err error `json:"err"` // 对比过程中是否出现异常
|
||||
}
|
||||
|
||||
const (
|
||||
@ -86,7 +86,7 @@ const (
|
||||
|
||||
var (
|
||||
// 当前仅支持基础类型的比较,不支持slice/map/struct等复杂类型的比较
|
||||
supportValueTypeTable = map[reflect.Kind]interface{}{
|
||||
supportValueTypeTable = map[reflect.Kind]any{
|
||||
reflect.Bool: true,
|
||||
reflect.Int: true,
|
||||
reflect.Int8: true,
|
||||
@ -135,9 +135,9 @@ func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map,
|
||||
Err: nil,
|
||||
}
|
||||
var (
|
||||
inputFieldVal interface{}
|
||||
inputFieldVal any
|
||||
inputFieldValExist bool
|
||||
storageFieldVal interface{}
|
||||
storageFieldVal any
|
||||
storageFieldValExist bool
|
||||
)
|
||||
|
||||
|
@ -20,13 +20,13 @@ func TestDefaultDiffFunc(t *testing.T) {
|
||||
num3 string = "1"
|
||||
num4 string = "1.00"
|
||||
)
|
||||
input := wrapper.EasyMap(map[string]interface{}{
|
||||
input := wrapper.EasyMap(map[string]any{
|
||||
"num": num1,
|
||||
"num3": num3,
|
||||
"num4": num4,
|
||||
"num5": num1,
|
||||
})
|
||||
storage := wrapper.EasyMap(map[string]interface{}{
|
||||
storage := wrapper.EasyMap(map[string]any{
|
||||
"num": num2,
|
||||
"num3": num2,
|
||||
"num4": num2,
|
||||
|
@ -13,8 +13,8 @@ package try
|
||||
//
|
||||
// Date : 11:26 2023/7/20
|
||||
type ICatchHandler interface {
|
||||
Catch(errCode string, handler func(errCode string, data map[string]interface{})) ICatchHandler
|
||||
CatchAll(handler func(errCode string, data map[string]interface{})) IFinalHandler
|
||||
Catch(errCode string, handler func(errCode string, data map[string]any)) ICatchHandler
|
||||
CatchAll(handler func(errCode string, data map[string]any)) IFinalHandler
|
||||
IFinalHandler
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ type ICatchHandler interface {
|
||||
//
|
||||
// Date : 11:27 2023/7/20
|
||||
type IFinalHandler interface {
|
||||
Finally(data map[string]interface{}, handlers ...func(data map[string]interface{}))
|
||||
Finally(data map[string]any, handlers ...func(data map[string]any))
|
||||
}
|
||||
|
||||
// ILogicFunction 逻辑函数约束
|
||||
|
@ -15,7 +15,7 @@ package try
|
||||
type DefaultCatchHandler struct {
|
||||
hasDeal bool // 异常是否已被处理
|
||||
errCode string
|
||||
data map[string]interface{}
|
||||
data map[string]any
|
||||
}
|
||||
|
||||
// hasDealError 判断异常是否已经被处理
|
||||
@ -38,7 +38,7 @@ func (d *DefaultCatchHandler) hasDealError() bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:19 2023/7/20
|
||||
func (d *DefaultCatchHandler) Catch(errCode string, handler func(errCode string, data map[string]interface{})) ICatchHandler {
|
||||
func (d *DefaultCatchHandler) Catch(errCode string, handler func(errCode string, data map[string]any)) ICatchHandler {
|
||||
if d.hasDealError() {
|
||||
return d
|
||||
}
|
||||
@ -61,7 +61,7 @@ func (d *DefaultCatchHandler) Catch(errCode string, handler func(errCode string,
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:47 2023/7/20
|
||||
func (d *DefaultCatchHandler) CatchAll(handler func(errCode string, data map[string]interface{})) IFinalHandler {
|
||||
func (d *DefaultCatchHandler) CatchAll(handler func(errCode string, data map[string]any)) IFinalHandler {
|
||||
if d.hasDealError() {
|
||||
return d
|
||||
}
|
||||
@ -80,9 +80,9 @@ func (d *DefaultCatchHandler) CatchAll(handler func(errCode string, data map[str
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:48 2023/7/20
|
||||
func (d *DefaultCatchHandler) Finally(data map[string]interface{}, handlers ...func(data map[string]interface{})) {
|
||||
func (d *DefaultCatchHandler) Finally(data map[string]any, handlers ...func(data map[string]any)) {
|
||||
if data == nil {
|
||||
data = map[string]interface{}{}
|
||||
data = map[string]any{}
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); nil != r {
|
||||
|
@ -13,7 +13,7 @@ package try
|
||||
//
|
||||
// Date : 11:30 2023/7/20
|
||||
type LogicFuncInput struct {
|
||||
Parameter map[string]interface{}
|
||||
Parameter map[string]any
|
||||
}
|
||||
|
||||
// LogicFuncOutput ...
|
||||
@ -22,8 +22,8 @@ type LogicFuncInput struct {
|
||||
//
|
||||
// Date : 11:30 2023/7/20
|
||||
type LogicFuncOutput struct {
|
||||
ErrCode string // 错误标识码
|
||||
Data map[string]interface{} // 错误时返回的数据
|
||||
ErrCode string // 错误标识码
|
||||
Data map[string]any // 错误时返回的数据
|
||||
}
|
||||
|
||||
// NewLogicFuncOutput 获取逻辑函数输出数据
|
||||
@ -31,9 +31,9 @@ type LogicFuncOutput struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:33 2023/7/20
|
||||
func NewLogicFuncOutput(code string, data map[string]interface{}) LogicFuncOutput {
|
||||
func NewLogicFuncOutput(code string, data map[string]any) LogicFuncOutput {
|
||||
if data == nil {
|
||||
data = map[string]interface{}{}
|
||||
data = map[string]any{}
|
||||
}
|
||||
r := LogicFuncOutput{
|
||||
ErrCode: code,
|
||||
|
@ -22,14 +22,14 @@ func Try(fn ILogicFunction, input *LogicFuncInput) ICatchHandler {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
catchHandler.errCode = LogicFuncPanic
|
||||
catchHandler.data = map[string]interface{}{
|
||||
catchHandler.data = map[string]any{
|
||||
"message": r.(error).Error(),
|
||||
}
|
||||
}
|
||||
}()
|
||||
if nil == input {
|
||||
input = &LogicFuncInput{
|
||||
Parameter: map[string]interface{}{},
|
||||
Parameter: map[string]any{},
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user