2023-06-01 18:31:21 +08:00
|
|
|
// Package wrapper ...
|
|
|
|
//
|
|
|
|
// Description : wrapper ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-06-01 18:18
|
|
|
|
package wrapper
|
|
|
|
|
2023-10-11 15:16:00 +08:00
|
|
|
import (
|
2023-12-06 21:32:22 +08:00
|
|
|
"fmt"
|
2024-06-12 16:38:51 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
2023-10-11 15:16:00 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
|
|
"reflect"
|
|
|
|
)
|
2023-06-01 18:31:21 +08:00
|
|
|
|
|
|
|
// AnyDataType ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:19 2023/6/1
|
2024-06-12 16:38:51 +08:00
|
|
|
func AnyDataType(data any) *AnyType {
|
2023-10-11 15:05:01 +08:00
|
|
|
at := &AnyType{
|
2023-06-01 18:31:21 +08:00
|
|
|
data: data,
|
|
|
|
}
|
2023-10-11 15:16:00 +08:00
|
|
|
at.dataType = at.Type()
|
2023-10-11 15:05:01 +08:00
|
|
|
return at
|
2023-06-01 18:31:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnyType ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:19 2023/6/1
|
|
|
|
type AnyType struct {
|
2024-06-12 16:38:51 +08:00
|
|
|
data any
|
2023-10-11 15:05:01 +08:00
|
|
|
dataType string
|
2023-06-01 18:31:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil 是否为 nil
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:21 2023/6/1
|
|
|
|
func (at *AnyType) IsNil() bool {
|
|
|
|
return at.data == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type 获取类型
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:22 2023/6/1
|
|
|
|
func (at *AnyType) Type() string {
|
2023-10-11 15:05:01 +08:00
|
|
|
if len(at.dataType) > 0 {
|
|
|
|
// 已经处理过的,无需在处理
|
|
|
|
return at.dataType
|
|
|
|
}
|
2023-06-01 18:31:21 +08:00
|
|
|
if at.IsNil() {
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeNil
|
2023-06-01 18:31:21 +08:00
|
|
|
}
|
|
|
|
reflectType := reflect.TypeOf(at.data)
|
|
|
|
switch reflectType.Kind() {
|
2023-10-11 15:16:00 +08:00
|
|
|
case reflect.String:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeString
|
2023-10-11 15:16:00 +08:00
|
|
|
case reflect.Slice, reflect.Array:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeSliceAny
|
2023-10-11 15:16:00 +08:00
|
|
|
case reflect.Map, reflect.Struct:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeMapAnyAny
|
2023-06-01 18:31:21 +08:00
|
|
|
case reflect.Pointer:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypePtr
|
2023-10-11 15:16:00 +08:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeInt
|
2023-10-11 15:16:00 +08:00
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeUint
|
2023-10-11 15:16:00 +08:00
|
|
|
case reflect.Bool:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeBool
|
2023-10-11 15:16:00 +08:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeFloat
|
2023-06-01 18:31:21 +08:00
|
|
|
default:
|
2024-06-12 16:38:51 +08:00
|
|
|
return consts.DataTypeUnknown
|
2023-06-01 18:31:21 +08:00
|
|
|
}
|
|
|
|
}
|
2023-06-01 18:32:45 +08:00
|
|
|
|
|
|
|
// ToString 转为字符串
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:32 2023/6/1
|
2023-10-11 15:16:00 +08:00
|
|
|
func (at *AnyType) ToString() String {
|
2024-06-12 16:38:51 +08:00
|
|
|
dataType := at.Type()
|
|
|
|
switch dataType {
|
|
|
|
case consts.DataTypeUnknown, consts.DataTypeNil:
|
2023-10-11 15:16:00 +08:00
|
|
|
return String("")
|
2024-06-12 16:38:51 +08:00
|
|
|
case consts.DataTypeString:
|
|
|
|
return String(fmt.Sprintf("%v", at.data))
|
|
|
|
case consts.DataTypeSliceAny:
|
|
|
|
var val []any
|
|
|
|
_ = serialize.JSON.Transition(at.data, &val)
|
|
|
|
return String(ArrayType[any](val).ToString().Value)
|
|
|
|
case consts.DataTypeMapAnyAny:
|
|
|
|
return String(EasyMap(at.data).ToString())
|
|
|
|
case consts.DataTypeInt:
|
|
|
|
return String(Int(at.data.(int64)).ToString().Value)
|
|
|
|
case consts.DataTypeUint:
|
|
|
|
return String(Int(at.data.(uint)).ToString().Value)
|
|
|
|
case consts.DataTypeFloat:
|
|
|
|
return String(Float(at.data.(float64)).ToString().Value)
|
|
|
|
case consts.DataTypeBool:
|
|
|
|
return String(fmt.Sprintf("%v", at.data))
|
|
|
|
default:
|
2023-10-11 15:16:00 +08:00
|
|
|
return String(serialize.JSON.MarshalForString(at.data))
|
|
|
|
}
|
2023-10-11 17:22:29 +08:00
|
|
|
}
|