Compare commits
8 Commits
e228983e73
...
feature/ge
Author | SHA1 | Date | |
---|---|---|---|
50aa5ae7f8 | |||
3533617196 | |||
9ff1c213bb | |||
8d056baada | |||
ed0c57913a | |||
588df729e0 | |||
3faebb9145 | |||
5163fd6f49 |
89
any.go
89
any.go
@ -9,6 +9,7 @@ package wrapper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"reflect"
|
||||
)
|
||||
@ -18,7 +19,7 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:19 2023/6/1
|
||||
func AnyDataType(data interface{}) *AnyType {
|
||||
func AnyDataType(data any) *AnyType {
|
||||
at := &AnyType{
|
||||
data: data,
|
||||
}
|
||||
@ -32,7 +33,7 @@ func AnyDataType(data interface{}) *AnyType {
|
||||
//
|
||||
// Date : 18:19 2023/6/1
|
||||
type AnyType struct {
|
||||
data interface{}
|
||||
data any
|
||||
dataType string
|
||||
}
|
||||
|
||||
@ -56,28 +57,28 @@ func (at *AnyType) Type() string {
|
||||
return at.dataType
|
||||
}
|
||||
if at.IsNil() {
|
||||
return DataTypeNil
|
||||
return consts.DataTypeNil
|
||||
}
|
||||
reflectType := reflect.TypeOf(at.data)
|
||||
switch reflectType.Kind() {
|
||||
case reflect.String:
|
||||
return DataTypeString
|
||||
return consts.DataTypeString
|
||||
case reflect.Slice, reflect.Array:
|
||||
return DataTypeSlice
|
||||
return consts.DataTypeSliceAny
|
||||
case reflect.Map, reflect.Struct:
|
||||
return DataTypeObject
|
||||
return consts.DataTypeMapAnyAny
|
||||
case reflect.Pointer:
|
||||
return DataTypePtr
|
||||
return consts.DataTypePtr
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return DataTypeInt
|
||||
return consts.DataTypeInt
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return DataTypeUint
|
||||
return consts.DataTypeUint
|
||||
case reflect.Bool:
|
||||
return DataTypeBool
|
||||
return consts.DataTypeBool
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return DataTypeFloat
|
||||
return consts.DataTypeFloat
|
||||
default:
|
||||
return DataTypeUnknown
|
||||
return consts.DataTypeUnknown
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,51 +88,27 @@ func (at *AnyType) Type() string {
|
||||
//
|
||||
// Date : 18:32 2023/6/1
|
||||
func (at *AnyType) ToString() String {
|
||||
switch at.dataType {
|
||||
case DataTypeNil:
|
||||
dataType := at.Type()
|
||||
switch dataType {
|
||||
case consts.DataTypeUnknown, consts.DataTypeNil:
|
||||
return String("")
|
||||
case DataTypeObject:
|
||||
fallthrough
|
||||
case DataTypeSlice:
|
||||
fallthrough
|
||||
case DataTypePtr:
|
||||
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, 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:
|
||||
return String(serialize.JSON.MarshalForString(at.data))
|
||||
}
|
||||
return String(fmt.Sprintf("%v", at.data))
|
||||
}
|
||||
|
||||
// ToObject 任意类型转为对象
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:19 2023/10/11
|
||||
func (at *AnyType) ToObject() MapResult {
|
||||
return MapResult{
|
||||
Value: EasyMapFromString(at.ToString().Value()),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (at *AnyType) ToIntSlice() {
|
||||
|
||||
}
|
||||
|
||||
func (at *AnyType) ToUintSlice() {
|
||||
|
||||
}
|
||||
|
||||
func (at *AnyType) ToStringSlice() {
|
||||
|
||||
}
|
||||
|
||||
func (at *AnyType) ToFloatSlice() {
|
||||
|
||||
}
|
||||
|
||||
func (at *AnyType) ToBoolSlice() {
|
||||
|
||||
}
|
||||
|
||||
func (at *AnyType) ToAnySlice() {
|
||||
|
||||
}
|
||||
|
64
array.go
64
array.go
@ -9,7 +9,9 @@ package wrapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/op_type"
|
||||
"github.com/tidwall/gjson"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
@ -19,8 +21,8 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:03 2023/6/11
|
||||
func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
|
||||
at := &Array[Bt]{
|
||||
func ArrayType[Bt op_type.BaseType, ExtractDataType op_type.BaseType](value []Bt) *Array[Bt, ExtractDataType] {
|
||||
at := &Array[Bt, ExtractDataType]{
|
||||
value: value,
|
||||
}
|
||||
return at
|
||||
@ -31,7 +33,7 @@ func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:05 2023/6/11
|
||||
type Array[Bt op_type.BaseType] struct {
|
||||
type Array[Bt op_type.BaseType, ExtractDataType op_type.BaseType] struct {
|
||||
value []Bt
|
||||
convertErr error
|
||||
itemType reflect.Kind // 简单list场景下, 每一项的数据类型
|
||||
@ -42,7 +44,7 @@ type Array[Bt op_type.BaseType] struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:11 2023/6/11
|
||||
func (at *Array[Bt]) IsNil() bool {
|
||||
func (at *Array[Bt, ExtractDataType]) IsNil() bool {
|
||||
return at.value == nil
|
||||
}
|
||||
|
||||
@ -51,7 +53,7 @@ func (at *Array[Bt]) IsNil() bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:42 2024/4/22
|
||||
func (at *Array[Bt]) ToStringSlice() []string {
|
||||
func (at *Array[Bt, ExtractDataType]) ToStringSlice() []string {
|
||||
list := make([]string, 0)
|
||||
for _, item := range at.value {
|
||||
byteData, _ := json.Marshal(item)
|
||||
@ -65,7 +67,7 @@ func (at *Array[Bt]) ToStringSlice() []string {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:43 2023/6/12
|
||||
func (at *Array[Bt]) Unique() []Bt {
|
||||
func (at *Array[Bt, ExtractDataType]) Unique() []Bt {
|
||||
result := make([]Bt, 0)
|
||||
dataTable := make(map[string]bool)
|
||||
|
||||
@ -90,7 +92,7 @@ func (at *Array[Bt]) Unique() []Bt {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:28 2023/7/31
|
||||
func (at *Array[Bt]) Has(input Bt) int {
|
||||
func (at *Array[Bt, ExtractDataType]) Has(input Bt) int {
|
||||
for idx := 0; idx < len(at.value); idx++ {
|
||||
if reflect.TypeOf(at.value[idx]).String() != reflect.TypeOf(input).String() {
|
||||
// 类型不同
|
||||
@ -111,7 +113,7 @@ func (at *Array[Bt]) Has(input Bt) int {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:57 2023/9/28
|
||||
func (at *Array[Bt]) ToString() StringResult {
|
||||
func (at *Array[Bt, ExtractDataType]) ToString() StringResult {
|
||||
if at.IsNil() {
|
||||
return StringResult{
|
||||
Value: "",
|
||||
@ -130,7 +132,7 @@ func (at *Array[Bt]) ToString() StringResult {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:42 2023/10/25
|
||||
func (at *Array[Bt]) ToStringWithSplit(split string) StringResult {
|
||||
func (at *Array[Bt, ExtractDataType]) ToStringWithSplit(split string) StringResult {
|
||||
if at.IsNil() {
|
||||
return StringResult{
|
||||
Value: "",
|
||||
@ -142,3 +144,47 @@ func (at *Array[Bt]) ToStringWithSplit(split string) StringResult {
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// ExtractField 提取指定字段, 转换成数组
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:24 2024/8/6
|
||||
func (at *Array[Bt, ExtractDataType]) ExtractField(fieldPath string) ([]ExtractDataType, error) {
|
||||
strValResult := at.ToString()
|
||||
if nil != strValResult.Err {
|
||||
return make([]ExtractDataType, 0), nil
|
||||
}
|
||||
gjsonResult := gjson.Parse(strValResult.Value)
|
||||
if !gjsonResult.IsArray() {
|
||||
return make([]ExtractDataType, 0), errors.New("input value is not slice")
|
||||
}
|
||||
arrList := gjsonResult.Array()
|
||||
if len(arrList) == 0 {
|
||||
return make([]ExtractDataType, 0), nil
|
||||
}
|
||||
res := make([]ExtractDataType, 0)
|
||||
for _, item := range arrList {
|
||||
valueResult := item.Get(fieldPath)
|
||||
if !valueResult.Exists() {
|
||||
// 不存在
|
||||
continue
|
||||
}
|
||||
var val ExtractDataType
|
||||
if err := ConvertAssign(&val, valueResult.String()); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
res = append(res, val)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ExtractFieldIgnoreError 提取指定字段并忽略异常
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:28 2024/8/6
|
||||
func (at *Array[Bt, ExtractDataType]) ExtractFieldIgnoreError(field string) []ExtractDataType {
|
||||
res, _ := at.ExtractField(field)
|
||||
return res
|
||||
}
|
||||
|
@ -13,6 +13,24 @@ import (
|
||||
)
|
||||
|
||||
func TestArray_Unique(t *testing.T) {
|
||||
fmt.Println(ArrayType([]any{"1", 1, 1, "1", 2, 3}).Unique())
|
||||
fmt.Println(ArrayType([]int{1, 1, 2, 3}).Unique())
|
||||
fmt.Println(ArrayType[any, any]([]any{"1", 1, 1, "1", 2, 3}).Unique())
|
||||
fmt.Println(ArrayType[int, any]([]int{1, 1, 2, 3}).Unique())
|
||||
}
|
||||
|
||||
func TestArray_ExtractField(t *testing.T) {
|
||||
testMap := []any{
|
||||
map[string]any{
|
||||
"age": 18,
|
||||
"name": "baicha",
|
||||
},
|
||||
map[string]any{
|
||||
"age": 20,
|
||||
"name": "qinghuan",
|
||||
},
|
||||
map[string]any{
|
||||
"foo": "bar",
|
||||
},
|
||||
}
|
||||
fmt.Println(ArrayType[any, int](testMap).ExtractField("age"))
|
||||
fmt.Println(ArrayType[any, string](testMap).ExtractField("name"))
|
||||
}
|
||||
|
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
|
||||
}
|
||||
|
19
define.go
19
define.go
@ -9,19 +9,6 @@ package wrapper
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
DataTypeUnknown = "unknown"
|
||||
DataTypeNil = "nil"
|
||||
DataTypePtr = "ptr"
|
||||
DataTypeString = "string"
|
||||
DataTypeInt = "int"
|
||||
DataTypeUint = "uint"
|
||||
DataTypeBool = "bool"
|
||||
DataTypeFloat = "float"
|
||||
DataTypeSlice = "slice"
|
||||
DataTypeObject = "object"
|
||||
)
|
||||
|
||||
// Int8Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -268,7 +255,7 @@ type Float64PtrResult struct {
|
||||
//
|
||||
// Date : 16:40 2023/5/8
|
||||
type Any struct {
|
||||
Value interface{}
|
||||
Value any
|
||||
Err error
|
||||
}
|
||||
|
||||
@ -298,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
|
||||
}
|
||||
|
||||
@ -488,6 +475,6 @@ type MapResult struct {
|
||||
//
|
||||
// Date : 18:28 2023/5/8
|
||||
type AnySliceResult struct {
|
||||
Value []interface{}
|
||||
Value []any
|
||||
Err error
|
||||
}
|
||||
|
15
go.mod
15
go.mod
@ -5,22 +5,25 @@ go 1.21
|
||||
toolchain go1.21.4
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
|
||||
github.com/spaolacci/murmur3 v1.1.0
|
||||
github.com/stretchr/testify v1.8.4
|
||||
github.com/tidwall/gjson v1.17.3
|
||||
)
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc // indirect
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect
|
||||
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mozillazg/go-pinyin v0.20.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
19
go.sum
19
go.sum
@ -4,6 +4,12 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240419080457-9d9562469008 h1:6z99+X
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240419080457-9d9562469008/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc h1:kPz9xiUVruM8kwbUUVpxyCTX8pGgyKt60K5zX77oyC4=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608124542-4d97bd80dc68 h1:AaWKU0bKHnNot24OMhaOCBKtpfhz4o05DKHrRFgYd8M=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608124542-4d97bd80dc68/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMXOTy0NFKdodsGKA4gTNHJMfob3yRFYMXrZj7ek=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4 h1:mibnyzYbZullK0aTHVASHl3UeoVr8IgytQZsuyv+yEM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4 h1:93JYY8JLbFcrlq37q/uKyxs2r2e3modsjvfSbnZQ/UI=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
|
||||
@ -14,10 +20,16 @@ git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240110090803-399e964daa0c h1:k7V
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240110090803-399e964daa0c/go.mod h1:w7kG4zyTJ1uPFaTWhze+OQuaUBINT2XnDxpyiM6ctc0=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||
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=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
@ -34,6 +46,13 @@ github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0b
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
|
||||
github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
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
|
||||
}
|
||||
|
21
string.go
21
string.go
@ -13,6 +13,7 @@ import (
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/axgle/mahonia"
|
||||
"github.com/spaolacci/murmur3"
|
||||
"io"
|
||||
"math/rand"
|
||||
"strings"
|
||||
@ -521,7 +522,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 +535,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 +993,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 +1003,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 {
|
||||
@ -1175,3 +1176,15 @@ func (str String) HasSubStr(subStrList []string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HashNumber ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:05 2024/6/27
|
||||
func (str String) HashNumber() Uint64Result {
|
||||
return Uint64Result{
|
||||
Value: murmur3.Sum64([]byte(str.Value())),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
@ -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{},
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user