From 460672bcd02eaae3dfab8d91071bc70e2abb843a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 11:17:53 +0800 Subject: [PATCH 1/8] feat: remove doc && update json tag --- define.go | 384 +++++++++++++++--------------------------------------- string.go | 255 +----------------------------------- 2 files changed, 108 insertions(+), 531 deletions(-) diff --git a/define.go b/define.go index c7fb631..74d2660 100644 --- a/define.go +++ b/define.go @@ -9,474 +9,294 @@ package wrapper import ( "time" + + "git.zhangdeman.cn/zhangdeman/op_type" ) +// BaseTypeValueResult 基础类型结果 +type BaseTypeValueResult[BaseType op_type.BaseType] struct { + Value BaseType `json:"value"` + Err error `json:"err"` +} + // Int8Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:25 2023/5/8 type Int8Result struct { - Value int8 - Err error + Value int8 `json:"value"` + Err error `json:"err"` } // Int8PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:05 2023/5/15 type Int8PtrResult struct { - Value *int8 - Err error + Value *int8 `json:"value"` + Err error `json:"err"` } // Int16Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:26 2023/5/8 type Int16Result struct { - Value int16 - Err error + Value int16 `json:"value"` + Err error `json:"err"` } // Int16PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:05 2023/5/15 type Int16PtrResult struct { - Value *int16 - Err error + Value *int16 `json:"value"` + Err error `json:"err"` } // Int32Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:26 2023/5/8 type Int32Result struct { - Value int32 - Err error + Value int32 `json:"value"` + Err error `json:"err"` } // Int32PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:04 2023/5/15 type Int32PtrResult struct { - Value *int32 - Err error + Value *int32 `json:"value"` + Err error `json:"err"` } // Int64Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:26 2023/5/8 type Int64Result struct { - Value int64 - Err error + Value int64 `json:"value"` + Err error `json:"err"` } // Int64PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:04 2023/5/15 type Int64PtrResult struct { - Value *int64 - Err error + Value *int64 `json:"value"` + Err error `json:"err"` } // IntResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:26 2023/5/8 type IntResult struct { - Value int - Err error + Value int `json:"value"` + Err error `json:"err"` } // IntPtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:51 2023/5/15 type IntPtrResult struct { - Value *int - Err error + Value *int `json:"value"` + Err error `json:"err"` } // Uint8Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:28 2023/5/8 type Uint8Result struct { - Value uint8 - Err error + Value uint8 `json:"value"` + Err error `json:"err"` } // Uint8PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:49 2023/5/16 type Uint8PtrResult struct { - Value *uint8 - Err error + Value *uint8 `json:"value"` + Err error `json:"err"` } // Uint16Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:28 2023/5/8 type Uint16Result struct { - Value uint16 - Err error + Value uint16 `json:"value"` + Err error `json:"err"` } // Uint16PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:49 2023/5/16 type Uint16PtrResult struct { - Value *uint16 - Err error + Value *uint16 `json:"value"` + Err error `json:"err"` } // Uint32Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:28 2023/5/8 type Uint32Result struct { - Value uint32 - Err error + Value uint32 `json:"value"` + Err error `json:"err"` } // Uint32PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:49 2023/5/16 type Uint32PtrResult struct { - Value *uint32 - Err error + Value *uint32 `json:"value"` + Err error `json:"err"` } // Uint64Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:28 2023/5/8 type Uint64Result struct { - Value uint64 - Err error + Value uint64 `json:"value"` + Err error `json:"err"` } // Uint64PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:50 2023/5/16 type Uint64PtrResult struct { - Value *uint64 - Err error + Value *uint64 `json:"value"` + Err error `json:"err"` } // UintResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:29 2023/5/8 type UintResult struct { - Value uint - Err error + Value uint `json:"value"` + Err error `json:"err"` } // UintPtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:51 2023/5/16 type UintPtrResult struct { - Value *uint - Err error + Value *uint `json:"value"` + Err error `json:"err"` } // Float32Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:31 2023/5/8 type Float32Result struct { - Value float32 - Err error + Value float32 `json:"value"` + Err error `json:"err"` } // Float32PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:10 2023/5/16 type Float32PtrResult struct { - Value *float32 - Err error + Value *float32 `json:"value"` + Err error `json:"err"` } // Float64Result ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:32 2023/5/8 type Float64Result struct { - Value float64 - Err error + Value float64 `json:"value"` + Err error `json:"err"` } // Float64PtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:52 2023/5/16 type Float64PtrResult struct { - Value *float64 - Err error + Value *float64 `json:"value"` + Err error `json:"err"` } // Any ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:40 2023/5/8 type Any struct { - Value any - Err error + Value any `json:"value"` + Err error `json:"err"` } // BoolResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:36 2023/5/8 type BoolResult struct { - Value bool - Err error + Value bool `json:"value"` + Err error `json:"err"` } // BoolPtrResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:53 2023/5/16 type BoolPtrResult struct { - Value *bool - Err error + Value *bool `json:"value"` + Err error `json:"err"` } // ObjectResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:38 2023/5/8 type ObjectResult struct { - Value map[string]any - Err error + Value map[string]any `json:"value"` + Err error `json:"err"` } // StringResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:57 2023/5/8 type StringResult struct { - Value string - Err error + Value string `json:"value"` + Err error `json:"err"` } // StringPtrResult 字符串指针 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:02 2023/5/15 type StringPtrResult struct { - Value *string - Err error + Value *string `json:"value"` + Err error `json:"err"` } // Int8SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:49 2023/5/8 type Int8SliceResult struct { - Value []int8 - Err error + Value []int8 `json:"value"` + Err error `json:"err"` } // Int16SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:49 2023/5/8 type Int16SliceResult struct { - Value []int16 - Err error + Value []int16 `json:"value"` + Err error `json:"err"` } // Int32SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:50 2023/5/8 type Int32SliceResult struct { - Value []int32 - Err error + Value []int32 `json:"value"` + Err error `json:"err"` } // Int64SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:50 2023/5/8 type Int64SliceResult struct { - Value []int64 - Err error + Value []int64 `json:"value"` + Err error `json:"err"` } // IntSliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:50 2023/5/8 type IntSliceResult struct { - Value []int - Err error + Value []int `json:"value"` + Err error `json:"err"` } // Uint8SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:55 2023/5/8 type Uint8SliceResult struct { - Value []uint8 - Err error + Value []uint8 `json:"value"` + Err error `json:"err"` } // Uint16SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:55 2023/5/8 type Uint16SliceResult struct { - Value []uint16 - Err error + Value []uint16 `json:"value"` + Err error `json:"err"` } // Uint32SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:55 2023/5/8 type Uint32SliceResult struct { - Value []uint32 - Err error + Value []uint32 `json:"value"` + Err error `json:"err"` } // Uint64SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:55 2023/5/8 type Uint64SliceResult struct { - Value []uint64 - Err error + Value []uint64 `json:"value"` + Err error `json:"err"` } // UintSliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:56 2023/5/8 type UintSliceResult struct { - Value []uint - Err error + Value []uint `json:"value"` + Err error `json:"err"` } // BoolSliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:22 2023/5/8 type BoolSliceResult struct { - Value []bool - Err error + Value []bool `json:"value"` + Err error `json:"err"` } // Float32SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:24 2023/5/8 type Float32SliceResult struct { - Value []float32 - Err error + Value []float32 `json:"value"` + Err error `json:"err"` } // Float64SliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:24 2023/5/8 type Float64SliceResult struct { - Value []float64 - Err error + Value []float64 `json:"value"` + Err error `json:"err"` } // DurationResult 时间转换结果 -// -// Author : zhangdeman001@ke.com<张德满> -// -// Date : 20:32 2023/9/4 type DurationResult struct { - Value time.Duration - Err error + Value time.Duration `json:"value"` + Err error `json:"err"` } // StringSliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:11 2023/8/6 type StringSliceResult struct { - Value []string - Err error + Value []string `json:"value"` + Err error `json:"err"` } // MapResult 转map的结果 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:05 2023/8/10 type MapResult struct { - Value Map - Err error + Value Map `json:"value"` + Err error `json:"err"` } // AnySliceResult ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:28 2023/5/8 type AnySliceResult struct { - Value []any - Err error + Value []any `json:"value"` + Err error `json:"err"` } diff --git a/string.go b/string.go index d59aa5c..d916464 100644 --- a/string.go +++ b/string.go @@ -11,20 +11,17 @@ import ( "crypto/md5" "encoding/hex" "errors" - "git.zhangdeman.cn/zhangdeman/serialize" - "github.com/axgle/mahonia" - "github.com/spaolacci/murmur3" "io" "math/rand" "strings" "time" + + "git.zhangdeman.cn/zhangdeman/serialize" + "github.com/axgle/mahonia" + "github.com/spaolacci/murmur3" ) // StringFromRandom 从随机字符串生成String -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 14:50 2023/8/11 func StringFromRandom(length int, sourceCharList string) String { if length == 0 { return "" @@ -36,24 +33,16 @@ func StringFromRandom(length int, sourceCharList string) String { strByte := []byte(sourceCharList) var genStrByte = make([]byte, 0) r := rand.New(rand.NewSource(time.Now().UnixNano())) - for i := 0; i < int(length); i++ { + for i := 0; i < length; i++ { genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))]) } - return String(string(genStrByte)) + return String(genStrByte) } // String 字符串类型包装 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:07 2023/5/5 type String string // ToFloat32 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:26 2023/5/4 func (str String) ToFloat32() Float32Result { var ( res Float32Result @@ -63,10 +52,6 @@ func (str String) ToFloat32() Float32Result { } // ToFloat32Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:58 2023/5/16 func (str String) ToFloat32Ptr() Float32PtrResult { res := str.ToFloat32() if nil != res.Err { @@ -82,10 +67,6 @@ func (str String) ToFloat32Ptr() Float32PtrResult { } // ToFloat64 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:26 2023/5/4 func (str String) ToFloat64() Float64Result { var ( res Float64Result @@ -95,10 +76,6 @@ func (str String) ToFloat64() Float64Result { } // ToFloat64Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:59 2023/5/16 func (str String) ToFloat64Ptr() Float64PtrResult { res := str.ToFloat64() if nil != res.Err { @@ -114,46 +91,26 @@ func (str String) ToFloat64Ptr() Float64PtrResult { } // ToDouble 转double -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:33 2023/5/4 func (str String) ToDouble() Float64Result { return str.ToFloat64() } // ToDoublePtr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:59 2023/5/16 func (str String) ToDoublePtr() Float64PtrResult { return str.ToFloat64Ptr() } // ToNumber 转数字, 使用最高精度的float64 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:33 2023/5/4 func (str String) ToNumber() Float64Result { return str.ToFloat64() } // ToNumberPtr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:00 2023/5/16 func (str String) ToNumberPtr() Float64PtrResult { return str.ToFloat64Ptr() } // ToInt8 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToInt8() Int8Result { var ( res Int8Result @@ -163,10 +120,6 @@ func (str String) ToInt8() Int8Result { } // ToInt8Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:01 2023/5/16 func (str String) ToInt8Ptr() Int8PtrResult { res := str.ToInt8() if nil != res.Err { @@ -182,10 +135,6 @@ func (str String) ToInt8Ptr() Int8PtrResult { } // ToInt16 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToInt16() Int16Result { var ( res Int16Result @@ -195,10 +144,6 @@ func (str String) ToInt16() Int16Result { } // ToInt16Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:02 2023/5/16 func (str String) ToInt16Ptr() Int16PtrResult { res := str.ToInt16() if nil != res.Err { @@ -214,10 +159,6 @@ func (str String) ToInt16Ptr() Int16PtrResult { } // ToInt32 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToInt32() Int32Result { var ( res Int32Result @@ -227,10 +168,6 @@ func (str String) ToInt32() Int32Result { } // ToInt32Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:03 2023/5/16 func (str String) ToInt32Ptr() Int32PtrResult { res := str.ToInt32() if nil != res.Err { @@ -246,10 +183,6 @@ func (str String) ToInt32Ptr() Int32PtrResult { } // ToInt64 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToInt64() Int64Result { var ( res Int64Result @@ -259,10 +192,6 @@ func (str String) ToInt64() Int64Result { } // ToInt64Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:03 2023/5/16 func (str String) ToInt64Ptr() Int64PtrResult { res := str.ToInt64() if nil != res.Err { @@ -278,10 +207,6 @@ func (str String) ToInt64Ptr() Int64PtrResult { } // ToInt ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:47 2023/5/5 func (str String) ToInt() IntResult { var ( res IntResult @@ -310,10 +235,6 @@ func (str String) ToIntPtr() IntPtrResult { } // ToUint8 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:30 2023/5/4 func (str String) ToUint8() Uint8Result { var ( res Uint8Result @@ -323,10 +244,6 @@ func (str String) ToUint8() Uint8Result { } // ToUint8Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:05 2023/5/16 func (str String) ToUint8Ptr() Uint8PtrResult { res := str.ToUint8() if nil != res.Err { @@ -342,10 +259,6 @@ func (str String) ToUint8Ptr() Uint8PtrResult { } // ToUint16 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToUint16() Uint16Result { var ( res Uint16Result @@ -355,10 +268,6 @@ func (str String) ToUint16() Uint16Result { } // ToUint16Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:05 2023/5/16 func (str String) ToUint16Ptr() Uint16PtrResult { res := str.ToUint16() if nil != res.Err { @@ -374,10 +283,6 @@ func (str String) ToUint16Ptr() Uint16PtrResult { } // ToUint32 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToUint32() Uint32Result { var ( res Uint32Result @@ -387,10 +292,6 @@ func (str String) ToUint32() Uint32Result { } // ToUint32Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:05 2023/5/16 func (str String) ToUint32Ptr() Uint32PtrResult { res := str.ToUint32() if nil != res.Err { @@ -406,10 +307,6 @@ func (str String) ToUint32Ptr() Uint32PtrResult { } // ToUint64 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToUint64() Uint64Result { var ( res Uint64Result @@ -419,10 +316,6 @@ func (str String) ToUint64() Uint64Result { } // ToUint64Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:06 2023/5/16 func (str String) ToUint64Ptr() Uint64PtrResult { res := str.ToUint64() if nil != res.Err { @@ -438,10 +331,6 @@ func (str String) ToUint64Ptr() Uint64PtrResult { } // ToUint ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 func (str String) ToUint() UintResult { var ( res UintResult @@ -451,10 +340,6 @@ func (str String) ToUint() UintResult { } // ToUintPtr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:06 2023/5/16 func (str String) ToUintPtr() UintPtrResult { res := str.ToUint() if nil != res.Err { @@ -470,10 +355,6 @@ func (str String) ToUintPtr() UintPtrResult { } // ToBool ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:34 2023/5/4 func (str String) ToBool() BoolResult { var ( res BoolResult @@ -483,10 +364,6 @@ func (str String) ToBool() BoolResult { } // ToBoolPtr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:07 2023/5/16 func (str String) ToBoolPtr() BoolPtrResult { res := str.ToBool() if nil != res.Err { @@ -502,10 +379,6 @@ func (str String) ToBoolPtr() BoolPtrResult { } // ToStringPtr 转换成字符串指针 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:57 2023/7/3 func (str String) ToStringPtr() StringPtrResult { val := str.Value() return StringPtrResult{ @@ -531,10 +404,6 @@ func (str String) ToObject() ObjectResult { } // ToStruct ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:38 2023/5/4 func (str String) ToStruct(receiver any) error { if nil == receiver { return errors.New("receiver is nil") @@ -543,10 +412,6 @@ func (str String) ToStruct(receiver any) error { } // ToInt8Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:55 2023/5/4 func (str String) ToInt8Slice(splitChar ...string) Int8SliceResult { result := Int8SliceResult{ Value: []int8{}, @@ -575,10 +440,6 @@ func (str String) ToInt8Slice(splitChar ...string) Int8SliceResult { } // ToInt16Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:01 2023/5/4 func (str String) ToInt16Slice(splitChar ...string) Int16SliceResult { result := Int16SliceResult{ @@ -606,10 +467,6 @@ func (str String) ToInt16Slice(splitChar ...string) Int16SliceResult { } // ToInt32Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:03 2023/5/4 func (str String) ToInt32Slice(splitChar ...string) Int32SliceResult { result := Int32SliceResult{ Value: []int32{}, @@ -636,10 +493,6 @@ func (str String) ToInt32Slice(splitChar ...string) Int32SliceResult { } // ToInt64Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:04 2023/5/4 func (str String) ToInt64Slice(splitChar ...string) Int64SliceResult { result := Int64SliceResult{ Value: []int64{}, @@ -665,10 +518,6 @@ func (str String) ToInt64Slice(splitChar ...string) Int64SliceResult { } // ToIntSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:04 2023/5/4 func (str String) ToIntSlice(splitChar ...string) IntSliceResult { result := IntSliceResult{ Value: []int{}, @@ -695,10 +544,6 @@ func (str String) ToIntSlice(splitChar ...string) IntSliceResult { } // ToUint8Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:50 2023/5/5 func (str String) ToUint8Slice(splitChar ...string) Uint8SliceResult { result := Uint8SliceResult{ Value: []uint8{}, @@ -725,10 +570,6 @@ func (str String) ToUint8Slice(splitChar ...string) Uint8SliceResult { } // ToUint16Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:52 2023/5/5 func (str String) ToUint16Slice(splitChar ...string) Uint16SliceResult { result := Uint16SliceResult{ Value: []uint16{}, @@ -755,10 +596,6 @@ func (str String) ToUint16Slice(splitChar ...string) Uint16SliceResult { } // ToUint32Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:53 2023/5/5 func (str String) ToUint32Slice(splitChar ...string) Uint32SliceResult { result := Uint32SliceResult{ Value: []uint32{}, @@ -785,10 +622,6 @@ func (str String) ToUint32Slice(splitChar ...string) Uint32SliceResult { } // ToUint64Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:54 2023/5/5 func (str String) ToUint64Slice(splitChar ...string) Uint64SliceResult { result := Uint64SliceResult{ Value: []uint64{}, @@ -814,10 +647,6 @@ func (str String) ToUint64Slice(splitChar ...string) Uint64SliceResult { } // ToUintSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:54 2023/5/5 func (str String) ToUintSlice(splitChar ...string) UintSliceResult { result := UintSliceResult{ Value: []uint{}, @@ -844,10 +673,6 @@ func (str String) ToUintSlice(splitChar ...string) UintSliceResult { } // ToBoolSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:55 2023/5/5 func (str String) ToBoolSlice(splitChar ...string) BoolSliceResult { result := BoolSliceResult{ Value: []bool{}, @@ -874,10 +699,6 @@ func (str String) ToBoolSlice(splitChar ...string) BoolSliceResult { } // ToFloat32Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:56 2023/5/5 func (str String) ToFloat32Slice(splitChar ...string) Float32SliceResult { result := Float32SliceResult{ Value: []float32{}, @@ -903,10 +724,6 @@ func (str String) ToFloat32Slice(splitChar ...string) Float32SliceResult { } // ToFloat64Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:56 2023/5/5 func (str String) ToFloat64Slice(splitChar ...string) Float64SliceResult { result := Float64SliceResult{ Value: []float64{}, @@ -933,28 +750,16 @@ func (str String) ToFloat64Slice(splitChar ...string) Float64SliceResult { } // ToDoubleSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:57 2023/5/5 func (str String) ToDoubleSlice(splitChar ...string) Float64SliceResult { return str.ToFloat64Slice(splitChar...) } // ToNumberSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:57 2023/5/5 func (str String) ToNumberSlice(splitChar ...string) Float64SliceResult { return str.ToFloat64Slice(splitChar...) } // ToDuration 转换为时间格式 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 20:31 2023/9/4 func (str String) ToDuration(timeUnit time.Duration) DurationResult { int64Val := str.ToInt64() if nil != int64Val.Err { @@ -967,10 +772,6 @@ func (str String) ToDuration(timeUnit time.Duration) DurationResult { } // ToStringSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:11 2023/8/6 func (str String) ToStringSlice(splitChar ...string) StringSliceResult { result := StringSliceResult{ Value: []string{}, @@ -987,10 +788,6 @@ func (str String) ToStringSlice(splitChar ...string) StringSliceResult { } // ToAnySlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:01 2023/5/5 func (str String) ToAnySlice(splitCharList ...string) AnySliceResult { result := AnySliceResult{ Value: []any{}, @@ -1023,10 +820,6 @@ func (str String) ToAnySlice(splitCharList ...string) AnySliceResult { } // Md5 计算Md5值 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:08 2023/5/16 func (str String) Md5() StringResult { h := md5.New() _, err := io.WriteString(h, str.Value()) @@ -1043,19 +836,11 @@ func (str String) Md5() StringResult { } // Value ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:11 2023/5/16 func (str String) Value() string { return string(str) } // GetLetterList 获取字母列表 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 14:53 2023/8/11 func (str String) GetLetterList() []string { return []string{ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", @@ -1064,10 +849,6 @@ func (str String) GetLetterList() []string { } // SnakeCaseToCamel 蛇形字符串转换为驼峰 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 4:58 下午 2021/10/25 func (str String) SnakeCaseToCamel() string { if len(str) == 0 { return "" @@ -1097,10 +878,6 @@ func (str String) SnakeCaseToCamel() string { } // Convert 字符串编码转换 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 14:38 2022/7/9 func (str String) Convert(sourceCode string, targetCode string) String { sourceCoder := mahonia.NewDecoder(sourceCode) sourceResult := sourceCoder.ConvertString(str.Value()) @@ -1110,10 +887,6 @@ func (str String) Convert(sourceCode string, targetCode string) String { } // ClearChar 清理指定字符 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 14:59 2023/8/11 func (str String) ClearChar(charList ...string) String { if len(charList) == 0 { return str @@ -1126,10 +899,6 @@ func (str String) ClearChar(charList ...string) String { } // ReplaceChineseChar 替换常见的中文符号 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:59 2023/4/4 func (str String) ReplaceChineseChar() String { charTable := map[string]string{ "(": "(", @@ -1144,10 +913,6 @@ func (str String) ReplaceChineseChar() String { } // ReplaceChar 替换指定字符 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:01 2023/8/11 func (str String) ReplaceChar(charTable map[string]string) String { if len(charTable) == 0 { return str @@ -1160,10 +925,6 @@ func (str String) ReplaceChar(charTable map[string]string) String { } // HasSubStr 是否包含指定的子串 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:37 2024/4/19 func (str String) HasSubStr(subStrList []string) bool { if len(subStrList) == 0 { return true @@ -1178,10 +939,6 @@ func (str String) HasSubStr(subStrList []string) bool { } // 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())), -- 2.36.6 From 20d29122746ec7a9e229c6bfcaf7d001b0d75cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 11:40:08 +0800 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=E5=AD=97=E7=AC=A6=E4=B8=B2=20->=20?= =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2,=20=E4=BD=BF=E7=94=A8=E6=B3=9B=E5=9E=8B=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/result.go | 22 +++ go.mod | 9 +- go.sum | 8 +- map_test.go | 6 +- op_string/base.go | 50 ++++++ op_string/base_test.go | 32 ++++ tool/convert.go | 394 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 515 insertions(+), 6 deletions(-) create mode 100644 define/result.go create mode 100644 op_string/base.go create mode 100644 op_string/base_test.go create mode 100644 tool/convert.go diff --git a/define/result.go b/define/result.go new file mode 100644 index 0000000..0b38608 --- /dev/null +++ b/define/result.go @@ -0,0 +1,22 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 11:22 +package define + +import "git.zhangdeman.cn/zhangdeman/op_type" + +// BaseValueResult 基础类型转换结果 +type BaseValueResult[BaseType op_type.BaseType] struct { + Value BaseType `json:"result"` // 转换结果 + Err error `json:"err"` // 错误信息 +} + +// BaseValuePtrResult 基础类型指针转换结果 +type BaseValuePtrResult[BaseType op_type.BaseType] struct { + Value *BaseType `json:"result"` // 转换结果 + Err error `json:"err"` // 错误信息 +} diff --git a/go.mod b/go.mod index c57913c..de8c903 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.zhangdeman.cn/zhangdeman/wrapper -go 1.23.0 +go 1.24.0 require ( git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 @@ -8,6 +8,7 @@ require ( git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9 git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 + github.com/smartystreets/goconvey v1.8.1 github.com/spaolacci/murmur3 v1.1.0 github.com/stretchr/testify v1.11.1 github.com/tidwall/gjson v1.18.0 @@ -17,11 +18,17 @@ require ( github.com/BurntSushi/toml v1.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-ini/ini v1.67.0 // indirect + github.com/gopherjs/gopherjs v1.17.2 // indirect + github.com/jtolds/gls v4.20.0+incompatible // 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/sbabiv/xml2map v1.2.1 // indirect + github.com/smarty/assertions v1.15.0 // indirect github.com/tidwall/match v1.2.0 // indirect github.com/tidwall/pretty v1.2.1 // indirect + golang.org/x/mod v0.9.0 // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/tools v0.7.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index b2efcfc..e4de9ae 100644 --- a/go.sum +++ b/go.sum @@ -34,8 +34,6 @@ github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sS github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60= github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= @@ -46,6 +44,12 @@ github.com/tidwall/match v1.2.0/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT 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= +golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= +golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= +golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= 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= diff --git a/map_test.go b/map_test.go index 58652ee..68a9f3a 100644 --- a/map_test.go +++ b/map_test.go @@ -22,11 +22,11 @@ func TestMap_Exist(t *testing.T) { func TestMap_IsNil(t *testing.T) { var ( - m Map - m1 *Map + m Map + //m1 *Map ) fmt.Println(m.Set("a", 1)) - fmt.Println(m.IsNil(), m1.IsNil()) + //fmt.Println(m.IsNil(), m1.IsNil()) } func TestMap_IsMasher(t *testing.T) { diff --git a/op_string/base.go b/op_string/base.go new file mode 100644 index 0000000..1eb8d27 --- /dev/null +++ b/op_string/base.go @@ -0,0 +1,50 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 11:18 +package op_string + +import ( + "git.zhangdeman.cn/zhangdeman/op_type" + "git.zhangdeman.cn/zhangdeman/wrapper/define" + "git.zhangdeman.cn/zhangdeman/wrapper/tool" +) + +// ToBaseValue 转换为基础数据类型 +func ToBaseValue[BaseType op_type.BaseType](str string) define.BaseValueResult[BaseType] { + var ( + err error + target BaseType + ) + if err = tool.ConvertAssign(&target, str); nil != err { + return define.BaseValueResult[BaseType]{ + Value: target, + Err: err, + } + } + return define.BaseValueResult[BaseType]{ + Value: target, + Err: err, + } +} + +// ToBaseValuePtr 转换为基础数据类型指针 +func ToBaseValuePtr[BaseType op_type.BaseType](str string) define.BaseValuePtrResult[BaseType] { + var ( + err error + target BaseType + ) + if err = tool.ConvertAssign(&target, str); nil != err { + return define.BaseValuePtrResult[BaseType]{ + Value: nil, + Err: err, + } + } + return define.BaseValuePtrResult[BaseType]{ + Value: &target, + Err: err, + } +} diff --git a/op_string/base_test.go b/op_string/base_test.go new file mode 100644 index 0000000..6b8a55f --- /dev/null +++ b/op_string/base_test.go @@ -0,0 +1,32 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 11:28 +package op_string + +import ( + "reflect" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestToBaseValue(t *testing.T) { + Convey("测试ToBaseValue - uint64 转换成功", t, func() { + res := ToBaseValue[uint64]("12345") + So(res.Err, ShouldBeNil) + So(res.Value, ShouldEqual, uint64(12345)) + So(reflect.TypeOf(res.Value).Kind(), ShouldEqual, reflect.Uint64) + So(reflect.TypeOf(res.Value).Kind(), ShouldNotEqual, reflect.Uint32) + }) + Convey("测试ToBaseValue - uint64 转换失败", t, func() { + res := ToBaseValue[uint64]("s12345") + So(res.Err, ShouldNotBeNil) + So(res.Value, ShouldEqual, 0) + So(reflect.TypeOf(res.Value).Kind(), ShouldEqual, reflect.Uint64) + So(reflect.TypeOf(res.Value).Kind(), ShouldNotEqual, reflect.Uint32) + }) +} diff --git a/tool/convert.go b/tool/convert.go new file mode 100644 index 0000000..739e94e --- /dev/null +++ b/tool/convert.go @@ -0,0 +1,394 @@ +// Package tool ... +// +// Description : 任意类型之间的相互转换 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-02-23 10:23 下午 +package tool + +/* + Desc : 在处理一些参数的时候,可能需要将参数转换为各种类型,这里实现一个通用的转换函数,实现各种类型之间的相互转换。 + + 当然,如果源数据格式和目标数据类型不一致,是会返回错误的。例如将字符串“一二三”转换为数值类型则会报错,而将字符串“123”转换为数值类型则OK。 + + 这段代码实际抄自go自带的“database/sql”库,只是源代码作为内部函数无法在外面调用,可以单独把需要的功能拎出来使用: + + 代码中有一个Scaner接口,可以自行实现,然后通过"convertAssign()"函数,作为dst参数传入。 + + Author : zhangdeman001@ke.com<白茶清欢> + +*/ +import ( + "errors" + "fmt" + "reflect" + "strconv" + "time" +) + +// RawBytes is a byte slice that holds a reference to memory owned by +// the database itself. After a Scan into a RawBytes, the slice is only +// valid until the next call to Next, Scan, or Close. +type RawBytes []byte + +var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error + +// ConvertAssign ... +// 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 any) error { + // Common cases, without reflect. + switch s := src.(type) { + case string: + switch d := dest.(type) { + case *string: + if d == nil { + return errNilPtr + } + *d = s + return nil + case *[]byte: + if d == nil { + return errNilPtr + } + *d = []byte(s) + return nil + case *RawBytes: + if d == nil { + return errNilPtr + } + *d = append((*d)[:0], s...) + return nil + } + case []byte: + switch d := dest.(type) { + case *string: + if d == nil { + return errNilPtr + } + *d = string(s) + return nil + case *any: + if d == nil { + return errNilPtr + } + *d = cloneBytes(s) + return nil + case *[]byte: + if d == nil { + return errNilPtr + } + *d = cloneBytes(s) + return nil + case *RawBytes: + if d == nil { + return errNilPtr + } + *d = s + return nil + } + case time.Time: + switch d := dest.(type) { + case *time.Time: + *d = s + return nil + case *string: + *d = s.Format(time.RFC3339Nano) + return nil + case *[]byte: + if d == nil { + return errNilPtr + } + *d = []byte(s.Format(time.RFC3339Nano)) + return nil + case *RawBytes: + if d == nil { + return errNilPtr + } + *d = s.AppendFormat((*d)[:0], time.RFC3339Nano) + return nil + } + case nil: + switch d := dest.(type) { + case *any: + if d == nil { + return errNilPtr + } + *d = nil + return nil + case *[]byte: + if d == nil { + return errNilPtr + } + *d = nil + return nil + case *RawBytes: + if d == nil { + return errNilPtr + } + *d = nil + return nil + } + } + + var sv reflect.Value + + switch d := dest.(type) { + case *string: + sv = reflect.ValueOf(src) + switch sv.Kind() { + case reflect.Bool, + reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, + reflect.Float32, reflect.Float64: + *d = asString(src) + return nil + } + case *[]byte: + sv = reflect.ValueOf(src) + if b, ok := asBytes(nil, sv); ok { + *d = b + return nil + } + case *RawBytes: + sv = reflect.ValueOf(src) + if b, ok := asBytes([]byte(*d)[:0], sv); ok { + *d = RawBytes(b) + return nil + } + case *bool: + bv, err := Bool.ConvertValue(src) + if err == nil { + *d = bv.(bool) + } + return err + case *any: + *d = src + return nil + } + + if scanner, ok := dest.(Scanner); ok { + return scanner.Scan(src) + } + + dpv := reflect.ValueOf(dest) + if dpv.Kind() != reflect.Ptr { + return errors.New("destination not a pointer") + } + if dpv.IsNil() { + return errNilPtr + } + + if !sv.IsValid() { + sv = reflect.ValueOf(src) + } + + dv := reflect.Indirect(dpv) + if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) { + switch b := src.(type) { + case []byte: + dv.Set(reflect.ValueOf(cloneBytes(b))) + default: + dv.Set(sv) + } + return nil + } + + if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) { + dv.Set(sv.Convert(dv.Type())) + return nil + } + + // The following conversions use a string value as an intermediate representation + // to convert between various numeric types. + // + // This also allows scanning into user defined types such as "type Int int64". + // For symmetry, also check for string destination types. + switch dv.Kind() { + case reflect.Ptr: + if src == nil { + dv.Set(reflect.Zero(dv.Type())) + return nil + } + dv.Set(reflect.New(dv.Type().Elem())) + return ConvertAssign(dv.Interface(), src) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + s := asString(src) + i64, err := strconv.ParseInt(s, 10, dv.Type().Bits()) + if err != nil { + err = strconvErr(err) + return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) + } + dv.SetInt(i64) + return nil + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + s := asString(src) + u64, err := strconv.ParseUint(s, 10, dv.Type().Bits()) + if err != nil { + err = strconvErr(err) + return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) + } + dv.SetUint(u64) + return nil + case reflect.Float32, reflect.Float64: + s := asString(src) + f64, err := strconv.ParseFloat(s, dv.Type().Bits()) + if err != nil { + err = strconvErr(err) + return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) + } + dv.SetFloat(f64) + return nil + case reflect.String: + switch v := src.(type) { + case string: + dv.SetString(v) + return nil + case []byte: + dv.SetString(string(v)) + return nil + } + } + + return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest) +} + +func strconvErr(err error) error { + if ne, ok := err.(*strconv.NumError); ok { + return ne.Err + } + return err +} + +func cloneBytes(b []byte) []byte { + if b == nil { + return nil + } + c := make([]byte, len(b)) + copy(c, b) + return c +} + +func ToString(src any) string { + return asString(src) +} + +func asString(src any) string { + switch v := src.(type) { + case string: + return v + case []byte: + return string(v) + } + rv := reflect.ValueOf(src) + switch rv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return strconv.FormatInt(rv.Int(), 10) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return strconv.FormatUint(rv.Uint(), 10) + case reflect.Float64: + return strconv.FormatFloat(rv.Float(), 'g', -1, 64) + case reflect.Float32: + return strconv.FormatFloat(rv.Float(), 'g', -1, 32) + case reflect.Bool: + return strconv.FormatBool(rv.Bool()) + } + return fmt.Sprintf("%v", src) +} + +func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) { + switch rv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return strconv.AppendInt(buf, rv.Int(), 10), true + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return strconv.AppendUint(buf, rv.Uint(), 10), true + case reflect.Float32: + return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true + case reflect.Float64: + return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true + case reflect.Bool: + return strconv.AppendBool(buf, rv.Bool()), true + case reflect.String: + s := rv.String() + return append(buf, s...), true + } + return +} + +// Value is a value that drivers must be able to handle. +// It is either nil, a type handled by a database driver's NamedValueChecker +// interface, or an instance of one of these types: +// +// int64 +// float64 +// bool +// []byte +// string +// time.Time +type Value any + +type boolType struct{} + +var Bool boolType + +func (boolType) String() string { return "Bool" } +func (boolType) ConvertValue(src any) (Value, error) { + switch s := src.(type) { + case bool: + return s, nil + case string: + b, err := strconv.ParseBool(s) + if err != nil { + return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s) + } + return b, nil + case []byte: + b, err := strconv.ParseBool(string(s)) + if err != nil { + return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s) + } + return b, nil + } + + sv := reflect.ValueOf(src) + switch sv.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + iv := sv.Int() + if iv == 1 || iv == 0 { + return iv == 1, nil + } + return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", iv) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + uv := sv.Uint() + if uv == 1 || uv == 0 { + return uv == 1, nil + } + return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", uv) + } + + return nil, fmt.Errorf("sql/driver: couldn't convert %v (%T) into type bool", src, src) +} + +type Scanner interface { + // Scan assigns a value from a database driver. + // + // The src value will be of one of the following types: + // + // int64 + // float64 + // bool + // []byte + // string + // time.Time + // nil - for NULL values + // + // An error should be returned if the value cannot be stored + // without loss of information. + // + // 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 any) error +} -- 2.36.6 From 059830d87d74ef3d44fee9569649e3f6d1570fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 12:00:51 +0800 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20ToBasePtrValue=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- op_string/base_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/op_string/base_test.go b/op_string/base_test.go index 6b8a55f..578465e 100644 --- a/op_string/base_test.go +++ b/op_string/base_test.go @@ -30,3 +30,20 @@ func TestToBaseValue(t *testing.T) { So(reflect.TypeOf(res.Value).Kind(), ShouldNotEqual, reflect.Uint32) }) } + +func TestToBaseValuePtr(t *testing.T) { + Convey("测试ToBasePtrValue - uint64 转换成功", t, func() { + res := ToBaseValuePtr[uint64]("12345") + So(res.Err, ShouldBeNil) + So(*res.Value, ShouldEqual, uint64(12345)) + So(reflect.TypeOf(res.Value).Kind(), ShouldEqual, reflect.Ptr) + So(reflect.TypeOf(res.Value).Elem().Kind(), ShouldEqual, reflect.Uint64) + }) + Convey("测试ToBasePtrValue - uint64 转换失败", t, func() { + res := ToBaseValuePtr[uint64]("s12345") + So(res.Err, ShouldNotBeNil) + So(res.Value, ShouldBeNil) + So(reflect.TypeOf(res.Value).Kind(), ShouldEqual, reflect.Ptr) + So(reflect.TypeOf(res.Value).Elem().Kind(), ShouldEqual, reflect.Uint64) + }) +} -- 2.36.6 From 2100caa5e48984638784c8018413f518d60ac779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 12:52:01 +0800 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0map=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/result.go | 6 ++++++ go.mod | 4 ++-- go.sum | 4 ++++ op_string/map.go | 31 +++++++++++++++++++++++++++++++ op_string/map_test.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 op_string/map.go create mode 100644 op_string/map_test.go diff --git a/define/result.go b/define/result.go index 0b38608..b1d176c 100644 --- a/define/result.go +++ b/define/result.go @@ -20,3 +20,9 @@ type BaseValuePtrResult[BaseType op_type.BaseType] struct { Value *BaseType `json:"result"` // 转换结果 Err error `json:"err"` // 错误信息 } + +// MapValueResult map类型转换结果 +type MapValueResult[Key comparable, Value comparable] struct { + Value map[Key]Value `json:"result"` // 转换结果 + Err error `json:"err"` // 错误信息 +} diff --git a/go.mod b/go.mod index de8c903..2f5b853 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.24.0 require ( git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 - git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 - git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9 + git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42 + git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 github.com/smartystreets/goconvey v1.8.1 diff --git a/go.sum b/go.sum index e4de9ae..dfea902 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,12 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 h1:Yo1ur3 git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y= git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= +git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42 h1:VjYrb4adud7FHeiYS9XA0B/tOaJjfRejzQAlwimrrDc= +git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20251013024601-da007da2fb42/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9 h1:/GLQaFoLb+ciHOtAS2BIyPNnf4O5ME3AC5PUaJY9kfs= git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9/go.mod h1:ABJ655C5QenQNOzf7LjCe4sSB52CXvaWLX2Zg4uwDJY= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd h1:kTZOpR8iHx27sUufMWVYhDZx9Q4h80j7RWlaR8GIBiU= +git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd/go.mod h1:pLrQ63JICi81/3w2BrD26QZiu+IpddvEVfMJ6No3Xb4= 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.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= diff --git a/op_string/map.go b/op_string/map.go new file mode 100644 index 0000000..0c00720 --- /dev/null +++ b/op_string/map.go @@ -0,0 +1,31 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 12:18 +package op_string + +import ( + "git.zhangdeman.cn/zhangdeman/serialize" + "git.zhangdeman.cn/zhangdeman/wrapper/define" +) + +// ToMap 转换为map, 数据类型可比较, 即可作为 map key, 内置 any 类型无法作为key +func ToMap[Key comparable, Value comparable](str string) define.MapValueResult[Key, Value] { + var ( + err error + res map[Key]Value + ) + if err = serialize.JSON.UnmarshalWithNumberForString(str, &res); err != nil { + return define.MapValueResult[Key, Value]{ + Value: nil, + Err: err, + } + } + return define.MapValueResult[Key, Value]{ + Value: res, + Err: nil, + } +} diff --git a/op_string/map_test.go b/op_string/map_test.go new file mode 100644 index 0000000..9dcef82 --- /dev/null +++ b/op_string/map_test.go @@ -0,0 +1,30 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 12:21 +package op_string + +import ( + "testing" + + "encoding/json" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestToMap(t *testing.T) { + Convey("map[string]any转换成", t, func() { + testData := `{ + "name": "baicha", + "age": 18 + }` + res := ToMap[string, any](testData) + So(res.Err, ShouldBeNil) + So(res.Value, ShouldNotBeNil) + So(res.Value["name"], ShouldEqual, "baicha") + So(res.Value["age"], ShouldEqual, json.Number("18")) + }) +} -- 2.36.6 From efab8cb6d293a6c7fed63e47bb28abb15995a868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 14:20:40 +0800 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20str=E8=BD=ACstruct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/result.go | 8 +++++++- op_string/map.go | 19 ++++++++++++++++++- op_string/map_test.go | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/define/result.go b/define/result.go index b1d176c..7f26b04 100644 --- a/define/result.go +++ b/define/result.go @@ -22,7 +22,13 @@ type BaseValuePtrResult[BaseType op_type.BaseType] struct { } // MapValueResult map类型转换结果 -type MapValueResult[Key comparable, Value comparable] struct { +type MapValueResult[Key comparable, Value any] struct { Value map[Key]Value `json:"result"` // 转换结果 Err error `json:"err"` // 错误信息 } + +// StructValueResult struct类型转换结果 +type StructValueResult[Value any] struct { + Value Value `json:"result"` // 转换结果 + Err error `json:"err"` // 错误信息 +} diff --git a/op_string/map.go b/op_string/map.go index 0c00720..189cdb2 100644 --- a/op_string/map.go +++ b/op_string/map.go @@ -13,7 +13,7 @@ import ( ) // ToMap 转换为map, 数据类型可比较, 即可作为 map key, 内置 any 类型无法作为key -func ToMap[Key comparable, Value comparable](str string) define.MapValueResult[Key, Value] { +func ToMap[Key comparable, Value any](str string) define.MapValueResult[Key, Value] { var ( err error res map[Key]Value @@ -29,3 +29,20 @@ func ToMap[Key comparable, Value comparable](str string) define.MapValueResult[K Err: nil, } } + +// ToStruct 转换为结构体 +func ToStruct[Value any](str string, receiver Value) define.StructValueResult[Value] { + var ( + err error + ) + if err = serialize.JSON.UnmarshalWithNumberForString(str, receiver); err != nil { + return define.StructValueResult[Value]{ + Value: receiver, + Err: err, + } + } + return define.StructValueResult[Value]{ + Value: receiver, + Err: nil, + } +} diff --git a/op_string/map_test.go b/op_string/map_test.go index 9dcef82..d2b8441 100644 --- a/op_string/map_test.go +++ b/op_string/map_test.go @@ -28,3 +28,23 @@ func TestToMap(t *testing.T) { So(res.Value["age"], ShouldEqual, json.Number("18")) }) } + +func TestToStruct(t *testing.T) { + Convey("struct转换成", t, func() { + testData := `{ + "name": "baicha", + "age": 18 + }` + + type User struct { + Name string `json:"name"` + Age int `json:"age"` + } + var u User + res := ToStruct(testData, &u) + So(res.Err, ShouldBeNil) + So(res.Value, ShouldNotBeNil) + So(res.Value.Name, ShouldEqual, "baicha") + So(res.Value.Age, ShouldEqual, 18) + }) +} -- 2.36.6 From 033e013510719bee849735a23442a7e2d226539b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 14:42:54 +0800 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20=E5=9F=BA=E7=A1=80=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=B1=BB=E5=9E=8B=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=BD=AC?= =?UTF-8?q?=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/result.go | 22 ++++++++++++-------- op_string/slice.go | 43 +++++++++++++++++++++++++++++++++++++++ op_string/slice_test.go | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 op_string/slice.go create mode 100644 op_string/slice_test.go diff --git a/define/result.go b/define/result.go index 7f26b04..174a6de 100644 --- a/define/result.go +++ b/define/result.go @@ -11,24 +11,30 @@ import "git.zhangdeman.cn/zhangdeman/op_type" // BaseValueResult 基础类型转换结果 type BaseValueResult[BaseType op_type.BaseType] struct { - Value BaseType `json:"result"` // 转换结果 - Err error `json:"err"` // 错误信息 + Value BaseType `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 } // BaseValuePtrResult 基础类型指针转换结果 type BaseValuePtrResult[BaseType op_type.BaseType] struct { - Value *BaseType `json:"result"` // 转换结果 - Err error `json:"err"` // 错误信息 + Value *BaseType `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 } // MapValueResult map类型转换结果 type MapValueResult[Key comparable, Value any] struct { - Value map[Key]Value `json:"result"` // 转换结果 - Err error `json:"err"` // 错误信息 + Value map[Key]Value `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 } // StructValueResult struct类型转换结果 type StructValueResult[Value any] struct { - Value Value `json:"result"` // 转换结果 - Err error `json:"err"` // 错误信息 + Value Value `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 +} + +// BaseValueSliceResult 基础类型切片转换结果 +type BaseValueSliceResult[BaseType op_type.BaseType] struct { + Value []BaseType `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 } diff --git a/op_string/slice.go b/op_string/slice.go new file mode 100644 index 0000000..153f8d1 --- /dev/null +++ b/op_string/slice.go @@ -0,0 +1,43 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 14:21 +package op_string + +import ( + "strings" + + "git.zhangdeman.cn/zhangdeman/op_type" + "git.zhangdeman.cn/zhangdeman/serialize" + "git.zhangdeman.cn/zhangdeman/wrapper/define" +) + +// ToBaseTypeSlice 基础数据类型的列表 +// splitChar 没有用字符串表示的原因: 存在场景, 使用空字符串风格字符串, 空字符串是有意义的 +func ToBaseTypeSlice[BaseTpe op_type.BaseType](str string, splitChar ...string) define.BaseValueSliceResult[BaseTpe] { + var ( + err error + sliceValue []BaseTpe + ) + + if len(splitChar) == 0 { + // 序列化数组直接转换 + if err = serialize.JSON.UnmarshalWithNumberForString(str, &sliceValue); nil != err { + return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err} + } + return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil} + } + // 按照分隔符转换 + strArr := strings.Split(str, splitChar[0]) + for _, v := range strArr { + itemConvertRes := ToBaseValue[BaseTpe](v) + if nil != itemConvertRes.Err { + return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err} + } + sliceValue = append(sliceValue, itemConvertRes.Value) + } + return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil} +} diff --git a/op_string/slice_test.go b/op_string/slice_test.go new file mode 100644 index 0000000..0617e35 --- /dev/null +++ b/op_string/slice_test.go @@ -0,0 +1,45 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 14:36 +package op_string + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestToBaseTypeSlice(t *testing.T) { + Convey("序列化数据转数组成功", t, func() { + testData := `[1,2,3,4,5]` + res := ToBaseTypeSlice[uint](testData) + So(res.Value, ShouldNotBeNil) + So(res.Err, ShouldBeNil) + So(len(res.Value), ShouldEqual, 5) + }) + Convey("序列化数据转数组失败", t, func() { + testData := `[1,2,3,4,-5]` + res := ToBaseTypeSlice[uint](testData) + So(res.Value, ShouldNotBeNil) + So(len(res.Value), ShouldEqual, 0) + So(res.Err, ShouldNotBeNil) + }) + Convey("字符串转数组成功", t, func() { + testData := `1,2,3,4,5` + res := ToBaseTypeSlice[uint](testData, ",") + So(res.Value, ShouldNotBeNil) + So(res.Err, ShouldBeNil) + So(len(res.Value), ShouldEqual, 5) + }) + Convey("字符串转数组失败", t, func() { + testData := `1,2,3,4,-5` + res := ToBaseTypeSlice[uint](testData) + So(res.Value, ShouldNotBeNil) + So(len(res.Value), ShouldEqual, 0) + So(res.Err, ShouldNotBeNil) + }) +} -- 2.36.6 From 80b5e4e7cc08b30dcf54b42eea632c3379b5ab2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 15:21:33 +0800 Subject: [PATCH 7/8] =?UTF-8?q?feat:=20=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E8=BD=ACstruct=20slice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/result.go | 12 ++++++++++++ op_string/base.go | 2 +- op_string/map_test.go | 30 ++++++++++++++++++++++++++++-- op_string/slice.go | 28 +++++++++++++++++++++------- op_string/slice_test.go | 33 ++++++++++++++++++++++++++++++++- 5 files changed, 94 insertions(+), 11 deletions(-) diff --git a/define/result.go b/define/result.go index 174a6de..5e2c15f 100644 --- a/define/result.go +++ b/define/result.go @@ -38,3 +38,15 @@ type BaseValueSliceResult[BaseType op_type.BaseType] struct { Value []BaseType `json:"value"` // 转换结果 Err error `json:"err"` // 错误信息 } + +// MapValueSliceResult map类型切片转换结果 +type MapValueSliceResult[Key comparable, Value any] struct { + Value []map[Key]Value `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 +} + +// StructValueSliceResult struct类型切片转换结果 +type StructValueSliceResult[Value any] struct { + Value []Value `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 +} diff --git a/op_string/base.go b/op_string/base.go index 1eb8d27..3d56c5d 100644 --- a/op_string/base.go +++ b/op_string/base.go @@ -27,7 +27,7 @@ func ToBaseValue[BaseType op_type.BaseType](str string) define.BaseValueResult[B } return define.BaseValueResult[BaseType]{ Value: target, - Err: err, + Err: nil, } } diff --git a/op_string/map_test.go b/op_string/map_test.go index d2b8441..5def276 100644 --- a/op_string/map_test.go +++ b/op_string/map_test.go @@ -16,7 +16,7 @@ import ( ) func TestToMap(t *testing.T) { - Convey("map[string]any转换成", t, func() { + Convey("map[string]any转换成功", t, func() { testData := `{ "name": "baicha", "age": 18 @@ -27,10 +27,19 @@ func TestToMap(t *testing.T) { So(res.Value["name"], ShouldEqual, "baicha") So(res.Value["age"], ShouldEqual, json.Number("18")) }) + Convey("map[string]any转换失败", t, func() { + testData := ` + "name": "baicha", + "age": 18 + }` + res := ToMap[string, any](testData) + So(res.Err, ShouldNotBeNil) + So(res.Value, ShouldBeNil) + }) } func TestToStruct(t *testing.T) { - Convey("struct转换成", t, func() { + Convey("struct转换成功", t, func() { testData := `{ "name": "baicha", "age": 18 @@ -47,4 +56,21 @@ func TestToStruct(t *testing.T) { So(res.Value.Name, ShouldEqual, "baicha") So(res.Value.Age, ShouldEqual, 18) }) + Convey("struct转换失败", t, func() { + testData := ` + "name": "baicha", + "age": 18 + }` + + type User struct { + Name string `json:"name"` + Age int `json:"age"` + } + var u User + res := ToStruct(testData, &u) + So(res.Err, ShouldNotBeNil) + So(res.Value, ShouldNotBeNil) + So(res.Value.Name, ShouldEqual, "") + So(res.Value.Age, ShouldEqual, 0) + }) } diff --git a/op_string/slice.go b/op_string/slice.go index 153f8d1..19a0a5e 100644 --- a/op_string/slice.go +++ b/op_string/slice.go @@ -17,27 +17,41 @@ import ( // ToBaseTypeSlice 基础数据类型的列表 // splitChar 没有用字符串表示的原因: 存在场景, 使用空字符串风格字符串, 空字符串是有意义的 -func ToBaseTypeSlice[BaseTpe op_type.BaseType](str string, splitChar ...string) define.BaseValueSliceResult[BaseTpe] { +func ToBaseTypeSlice[BaseType op_type.BaseType](str string, splitChar ...string) define.BaseValueSliceResult[BaseType] { var ( err error - sliceValue []BaseTpe + sliceValue []BaseType ) if len(splitChar) == 0 { // 序列化数组直接转换 if err = serialize.JSON.UnmarshalWithNumberForString(str, &sliceValue); nil != err { - return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err} + return define.BaseValueSliceResult[BaseType]{Value: []BaseType{}, Err: err} } - return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil} + return define.BaseValueSliceResult[BaseType]{Value: sliceValue, Err: nil} } // 按照分隔符转换 strArr := strings.Split(str, splitChar[0]) for _, v := range strArr { - itemConvertRes := ToBaseValue[BaseTpe](v) + itemConvertRes := ToBaseValue[BaseType](v) if nil != itemConvertRes.Err { - return define.BaseValueSliceResult[BaseTpe]{Value: []BaseTpe{}, Err: err} + return define.BaseValueSliceResult[BaseType]{Value: []BaseType{}, Err: itemConvertRes.Err} } sliceValue = append(sliceValue, itemConvertRes.Value) } - return define.BaseValueSliceResult[BaseTpe]{Value: sliceValue, Err: nil} + return define.BaseValueSliceResult[BaseType]{Value: sliceValue, Err: nil} +} + +// ToMapSlice map类型的列表 +func ToMapSlice[Key comparable, Value any](str string) define.MapValueSliceResult[Key, Value] { + res := define.MapValueSliceResult[Key, Value]{Value: []map[Key]Value{}, Err: nil} + res.Err = serialize.JSON.UnmarshalWithNumberForString(str, &res.Value) + return res +} + +// ToStructSlice 结构体类型的列表 +func ToStructSlice[Value any](str string) define.StructValueSliceResult[Value] { + res := define.StructValueSliceResult[Value]{Value: []Value{}, Err: nil} + res.Err = serialize.JSON.UnmarshalWithNumberForString(str, &res.Value) + return res } diff --git a/op_string/slice_test.go b/op_string/slice_test.go index 0617e35..6ddd007 100644 --- a/op_string/slice_test.go +++ b/op_string/slice_test.go @@ -8,8 +8,10 @@ package op_string import ( + "encoding/json" "testing" + "git.zhangdeman.cn/zhangdeman/serialize" . "github.com/smartystreets/goconvey/convey" ) @@ -37,9 +39,38 @@ func TestToBaseTypeSlice(t *testing.T) { }) Convey("字符串转数组失败", t, func() { testData := `1,2,3,4,-5` - res := ToBaseTypeSlice[uint](testData) + res := ToBaseTypeSlice[uint](testData, ",") So(res.Value, ShouldNotBeNil) So(len(res.Value), ShouldEqual, 0) So(res.Err, ShouldNotBeNil) + serialize.JSON.ConsoleOutput(res) + }) +} + +func TestToMapSlice(t *testing.T) { + Convey("序列化数据转map数组成功", t, func() { + testData := `[{"name":"baicha", "age": 18}]` + res := ToMapSlice[string, any](testData) + So(res.Value, ShouldNotBeNil) + So(res.Err, ShouldBeNil) + So(len(res.Value), ShouldEqual, 1) + So(res.Value[0]["name"], ShouldEqual, "baicha") + So(res.Value[0]["age"], ShouldEqual, json.Number("18")) + }) +} + +func TestToStructSlice(t *testing.T) { + Convey("序列化数据转map数组成功", t, func() { + testData := `[{"name":"baicha", "age": 18}]` + type User struct { + Name string `json:"name"` + Age int `json:"age"` + } + res := ToStructSlice[User](testData) + So(res.Value, ShouldNotBeNil) + So(res.Err, ShouldBeNil) + So(len(res.Value), ShouldEqual, 1) + So(res.Value[0].Name, ShouldEqual, "baicha") + So(res.Value[0].Age, ShouldEqual, 18) }) } -- 2.36.6 From 50d2d6c7bbbddee3df854ff92a81b6bf20e0bff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 13 Oct 2025 17:05:00 +0800 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20=E7=9B=B8=E5=85=B3=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=8D=87=E7=BA=A7=E4=B8=BA=E6=B3=9B=E5=9E=8B=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {tool => convert}/convert.go | 4 +- define.go | 2 +- define/result.go | 6 + map_test.go | 47 -- any.go => op_any/any.go | 47 +- array.go => op_array/array.go | 101 ++-- easymap.go => op_map/easymap.go | 27 +- map.go => op_map/map.go | 15 +- op_string/base.go | 10 +- op_string/base_test.go | 4 +- op_string/slice.go | 2 +- op_string/tool.go | 148 +++++ struct.go => op_struct/struct.go | 35 +- op_ternary/ternary_operator.go | 79 +++ string.go | 947 ------------------------------- string_test.go | 31 - ternary_operator.go | 157 ----- tool/define/diff.go | 8 +- 18 files changed, 316 insertions(+), 1354 deletions(-) rename {tool => convert}/convert.go (99%) delete mode 100644 map_test.go rename any.go => op_any/any.go (70%) rename array.go => op_array/array.go (50%) rename easymap.go => op_map/easymap.go (79%) rename map.go => op_map/map.go (94%) create mode 100644 op_string/tool.go rename struct.go => op_struct/struct.go (58%) create mode 100644 op_ternary/ternary_operator.go delete mode 100644 string.go delete mode 100644 string_test.go delete mode 100644 ternary_operator.go diff --git a/tool/convert.go b/convert/convert.go similarity index 99% rename from tool/convert.go rename to convert/convert.go index 739e94e..495581c 100644 --- a/tool/convert.go +++ b/convert/convert.go @@ -1,11 +1,11 @@ -// Package tool ... +// Package convert ... // // Description : 任意类型之间的相互转换 // // Author : go_developer@163.com<白茶清欢> // // Date : 2021-02-23 10:23 下午 -package tool +package convert /* Desc : 在处理一些参数的时候,可能需要将参数转换为各种类型,这里实现一个通用的转换函数,实现各种类型之间的相互转换。 diff --git a/define.go b/define.go index 74d2660..9f1890f 100644 --- a/define.go +++ b/define.go @@ -291,7 +291,7 @@ type StringSliceResult struct { // MapResult 转map的结果 type MapResult struct { - Value Map `json:"value"` + Value any `json:"value"` Err error `json:"err"` } diff --git a/define/result.go b/define/result.go index 5e2c15f..f7b6ed3 100644 --- a/define/result.go +++ b/define/result.go @@ -50,3 +50,9 @@ type StructValueSliceResult[Value any] struct { Value []Value `json:"value"` // 转换结果 Err error `json:"err"` // 错误信息 } + +// StringResult ... +type StringResult struct { + Value string `json:"value"` + Err error `json:"err"` +} diff --git a/map_test.go b/map_test.go deleted file mode 100644 index 68a9f3a..0000000 --- a/map_test.go +++ /dev/null @@ -1,47 +0,0 @@ -// Package wrapper ... -// -// Description : wrapper ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2024-11-06 18:37 -package wrapper - -import ( - "fmt" - "testing" -) - -func TestMap_Exist(t *testing.T) { - testData := Map(map[string]any{ - "name": "zhang", - }) - fmt.Println(testData.Exist("name")) - fmt.Println(testData.Exist("age")) -} - -func TestMap_IsNil(t *testing.T) { - var ( - m Map - //m1 *Map - ) - fmt.Println(m.Set("a", 1)) - //fmt.Println(m.IsNil(), m1.IsNil()) -} - -func TestMap_IsMasher(t *testing.T) { - var ( - m Map - m1 = Map(map[string]any{ - "a": 1, - "b": m, - "c": Map(map[string]any{ - "name": "de", - }), - }) - ) - d, err := m.MarshalJSON() - fmt.Println(string(d), err) - d, err = m1.MarshalJSON() - fmt.Println(string(d), err) -} diff --git a/any.go b/op_any/any.go similarity index 70% rename from any.go rename to op_any/any.go index 600e6da..3a79783 100644 --- a/any.go +++ b/op_any/any.go @@ -1,24 +1,21 @@ -// Package wrapper ... +// Package op_any ... // // Description : wrapper ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2023-06-01 18:18 -package wrapper +package op_any import ( "fmt" + "reflect" + "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/serialize" - "reflect" ) // AnyDataType ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:19 2023/6/1 func AnyDataType(data any) *AnyType { at := &AnyType{ data: data, @@ -28,20 +25,12 @@ func AnyDataType(data any) *AnyType { } // AnyType ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:19 2023/6/1 type AnyType struct { data any dataType consts.DataType } // IsNil 是否为 nil -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:21 2023/6/1 func (at *AnyType) IsNil() bool { if at.data == nil { return true @@ -58,10 +47,6 @@ func (at *AnyType) IsNil() bool { } // Type 获取类型 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:22 2023/6/1 func (at *AnyType) Type() consts.DataType { if len(at.dataType) > 0 { // 已经处理过的,无需在处理 @@ -94,33 +79,29 @@ func (at *AnyType) Type() consts.DataType { } // ToString 转为字符串 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:32 2023/6/1 -func (at *AnyType) ToString() String { +func (at *AnyType) ToString() string { dataType := at.Type() switch dataType { case consts.DataTypeUnknown, consts.DataTypeNil: - return String("") + return "" case consts.DataTypeString: - return String(fmt.Sprintf("%v", at.data)) + return fmt.Sprintf("%v", at.data) case consts.DataTypeSliceAny: var val []any _ = serialize.JSON.Transition(at.data, &val) - return String(ArrayType[any](val).ToString().Value) + return ArrayType[any](val).ToString().Value case consts.DataTypeMapAnyAny: easyMap := EasyMap(at.data) - return String(easyMap.ToString()) + return easyMap.ToString() case consts.DataTypeInt: - return String(Int(at.data.(int64)).ToString().Value) + return Int(at.data.(int64)).ToString().Value case consts.DataTypeUint: - return String(Int(at.data.(uint)).ToString().Value) + return Int(at.data.(uint)).ToString().Value case consts.DataTypeFloat64: - return String(Float(at.data.(float64)).ToString().Value) + return Float(at.data.(float64)).ToString().Value case consts.DataTypeBool: - return String(fmt.Sprintf("%v", at.data)) + return fmt.Sprintf("%v", at.data) default: - return String(serialize.JSON.MarshalForStringIgnoreError(at.data)) + return serialize.JSON.MarshalForStringIgnoreError(at.data) } } diff --git a/array.go b/op_array/array.go similarity index 50% rename from array.go rename to op_array/array.go index 2793340..0e95d78 100644 --- a/array.go +++ b/op_array/array.go @@ -1,25 +1,24 @@ -// Package wrapper ... +// Package op_array ... // // Description : wrapper ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2023-06-11 21:02 -package wrapper +package op_array import ( "encoding/json" - "git.zhangdeman.cn/zhangdeman/op_type" - "github.com/tidwall/gjson" "reflect" "strings" + + "git.zhangdeman.cn/zhangdeman/op_type" + "git.zhangdeman.cn/zhangdeman/wrapper/define" + "git.zhangdeman.cn/zhangdeman/wrapper/op_any" + "github.com/tidwall/gjson" ) // ArrayType 数组实例 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:03 2023/6/11 func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] { at := &Array[Bt]{ value: value, @@ -28,10 +27,6 @@ func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] { } // Array ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:05 2023/6/11 type Array[Bt op_type.BaseType] struct { value []Bt convertErr error @@ -39,38 +34,26 @@ type Array[Bt op_type.BaseType] struct { } // IsNil 输入是否为nil -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:11 2023/6/11 -func (at *Array[Bt]) IsNil() bool { - return at.value == nil +func IsNil[BaseType op_type.BaseType](arr Array[BaseType]) bool { + return arr.value == nil } // ToStringSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:42 2024/4/22 -func (at *Array[Bt]) ToStringSlice() []string { +func ToStringSlice[BaseType op_type.BaseType](arr Array[BaseType]) []string { list := make([]string, 0) - for _, item := range at.value { - str := AnyDataType(item).ToString().Value() + for _, item := range arr.value { + str := op_any.AnyDataType(item).ToString() list = append(list, str) } return list } // Unique 对数据结果进行去重 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:43 2023/6/12 -func (at *Array[Bt]) Unique() []Bt { - result := make([]Bt, 0) +func Unique[BaseType op_type.BaseType](arr Array[BaseType]) []BaseType { + result := make([]BaseType, 0) dataTable := make(map[string]bool) - for _, item := range at.value { + for _, item := range arr.value { byteData, _ := json.Marshal(item) k := string(byteData) if strings.HasPrefix(k, "\"\"") && strings.HasSuffix(k, "\"\"") { @@ -87,17 +70,13 @@ func (at *Array[Bt]) Unique() []Bt { } // Has 查询一个值是否在列表里, 在的话, 返回首次出现的索引, 不在返回-1 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:28 2023/7/31 -func (at *Array[Bt]) Has(input Bt) int { - for idx := 0; idx < len(at.value); idx++ { - if reflect.TypeOf(at.value[idx]).String() != reflect.TypeOf(input).String() { +func Has[BaseType op_type.BaseType](arr Array[BaseType], input BaseType) int { + for idx := 0; idx < len(arr.value); idx++ { + if reflect.TypeOf(arr.value[idx]).String() != reflect.TypeOf(input).String() { // 类型不同 continue } - sourceByte, _ := json.Marshal(at.value[idx]) + sourceByte, _ := json.Marshal(arr.value[idx]) inputByte, _ := json.Marshal(input) if string(sourceByte) != string(inputByte) { continue @@ -108,52 +87,40 @@ func (at *Array[Bt]) Has(input Bt) int { } // ToString ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:57 2023/9/28 -func (at *Array[Bt]) ToString() StringResult { - if at.IsNil() { - return StringResult{ +func ToString[BaseType op_type.BaseType](arr Array[BaseType]) define.BaseValueResult[string] { + if IsNil(arr) { + return define.BaseValueResult[string]{ Value: "", Err: nil, } } - byteData, err := json.Marshal(at.value) - return StringResult{ + byteData, err := json.Marshal(arr.value) + return define.BaseValueResult[string]{ Value: string(byteData), Err: err, } } // ToStringWithSplit 数组按照指定分隔符转为字符串 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:42 2023/10/25 -func (at *Array[Bt]) ToStringWithSplit(split string) StringResult { - if at.IsNil() { - return StringResult{ +func ToStringWithSplit[BaseType op_type.BaseType](arr Array[BaseType], split string) define.BaseValueResult[string] { + if IsNil(arr) { + return define.BaseValueResult[string]{ Value: "", Err: nil, } } - return StringResult{ - Value: strings.Join(at.ToStringSlice(), split), + return define.BaseValueResult[string]{ + Value: strings.Join(ToStringSlice(arr), split), Err: nil, } } // ExtraField 提取[]map/[]struct 中的指定字段, 并以list形式返回 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:00 2024/10/13 -func (at *Array[Bt]) ExtraField(fieldName string) String { - if at.IsNil() { - return String("[]") +func ExtraField[BaseType op_type.BaseType](arr Array[BaseType], fieldName string) string { + if IsNil(arr) { + return "[]" } - byteData, _ := json.Marshal(at.value) + byteData, _ := json.Marshal(arr.value) res := make([]any, 0) list := gjson.ParseBytes(byteData).Array() for _, item := range list { @@ -162,5 +129,5 @@ func (at *Array[Bt]) ExtraField(fieldName string) String { res = append(res, itemValue.Value()) } } - return String(ArrayType(res).ToString().Value) + return ToString(arr).Value } diff --git a/easymap.go b/op_map/easymap.go similarity index 79% rename from easymap.go rename to op_map/easymap.go index a16d233..76d5710 100644 --- a/easymap.go +++ b/op_map/easymap.go @@ -1,35 +1,28 @@ -// Package wrapper ... +// Package op_map ... // // Description : wrapper ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2023-08-10 15:01 -package wrapper +package op_map import ( "encoding/json" "errors" + "reflect" + "git.zhangdeman.cn/zhangdeman/serialize" "github.com/tidwall/gjson" - "reflect" ) // EasyMap ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:02 2023/8/10 func EasyMap(mapData any) Map { m, _ := EasyMapWithError(mapData) return m } // EasyMapWithError 转换map,并带上转换的异常 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:06 2023/8/10 func EasyMapWithError(mapData any) (Map, error) { if nil == mapData { return map[string]any{}, nil @@ -52,29 +45,17 @@ func EasyMapWithError(mapData any) (Map, error) { } // EasyMapFromStruct 从struct转map -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:11 2023/8/10 func EasyMapFromStruct(data any) Map { byteData, _ := json.Marshal(data) return EasyMapFromByte(byteData) } // EasyMapFromString 从string转为Map -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:12 2023/8/10 func EasyMapFromString(data string) Map { return EasyMapFromByte([]byte(data)) } // EasyMapFromByte 从字节数组转为Map -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:12 2023/8/10 func EasyMapFromByte(data []byte) Map { res := Map(map[string]any{}) jsonRes := gjson.Parse(string(data)) diff --git a/map.go b/op_map/map.go similarity index 94% rename from map.go rename to op_map/map.go index c418fbe..768e2b7 100644 --- a/map.go +++ b/op_map/map.go @@ -1,17 +1,20 @@ -// Package wrapper ... +// Package op_map ... // // Description : wrapper ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2024-11-06 18:27 -package wrapper +package op_map import ( "errors" - "git.zhangdeman.cn/zhangdeman/serialize" "reflect" "sync" + + "git.zhangdeman.cn/zhangdeman/serialize" + "git.zhangdeman.cn/zhangdeman/wrapper/op_any" + "git.zhangdeman.cn/zhangdeman/wrapper/op_string" ) var mapLock = &sync.RWMutex{} @@ -248,7 +251,7 @@ func (m Map) GetString(field string) (string, error) { if nil != err { return "", err } - return AnyDataType(val).ToString().Value(), nil + return op_any.AnyDataType(val).ToString(), nil } // GetInt64 获取Int64值 @@ -264,7 +267,7 @@ func (m Map) GetInt64(field string) (int64, error) { if nil != err { return 0, err } - int64Res := AnyDataType(val).ToString().ToInt64() + int64Res := op_string.ToBaseTypeValue[int64](op_any.AnyDataType(val).ToString()) return int64Res.Value, int64Res.Err } @@ -281,6 +284,6 @@ func (m Map) GetFloat64(field string) (float64, error) { if nil != err { return 0, err } - float64Res := AnyDataType(val).ToString().ToFloat64() + float64Res := op_string.ToBaseTypeValue[float64](op_any.AnyDataType(val).ToString()) return float64Res.Value, float64Res.Err } diff --git a/op_string/base.go b/op_string/base.go index 3d56c5d..71cecdc 100644 --- a/op_string/base.go +++ b/op_string/base.go @@ -9,17 +9,17 @@ package op_string import ( "git.zhangdeman.cn/zhangdeman/op_type" + "git.zhangdeman.cn/zhangdeman/wrapper/convert" "git.zhangdeman.cn/zhangdeman/wrapper/define" - "git.zhangdeman.cn/zhangdeman/wrapper/tool" ) -// ToBaseValue 转换为基础数据类型 -func ToBaseValue[BaseType op_type.BaseType](str string) define.BaseValueResult[BaseType] { +// ToBaseTypeValue 转换为基础数据类型 +func ToBaseTypeValue[BaseType op_type.BaseType](str string) define.BaseValueResult[BaseType] { var ( err error target BaseType ) - if err = tool.ConvertAssign(&target, str); nil != err { + if err = convert.ConvertAssign(&target, str); nil != err { return define.BaseValueResult[BaseType]{ Value: target, Err: err, @@ -37,7 +37,7 @@ func ToBaseValuePtr[BaseType op_type.BaseType](str string) define.BaseValuePtrRe err error target BaseType ) - if err = tool.ConvertAssign(&target, str); nil != err { + if err = convert.ConvertAssign(&target, str); nil != err { return define.BaseValuePtrResult[BaseType]{ Value: nil, Err: err, diff --git a/op_string/base_test.go b/op_string/base_test.go index 578465e..cb28611 100644 --- a/op_string/base_test.go +++ b/op_string/base_test.go @@ -16,14 +16,14 @@ import ( func TestToBaseValue(t *testing.T) { Convey("测试ToBaseValue - uint64 转换成功", t, func() { - res := ToBaseValue[uint64]("12345") + res := ToBaseTypeValue[uint64]("12345") So(res.Err, ShouldBeNil) So(res.Value, ShouldEqual, uint64(12345)) So(reflect.TypeOf(res.Value).Kind(), ShouldEqual, reflect.Uint64) So(reflect.TypeOf(res.Value).Kind(), ShouldNotEqual, reflect.Uint32) }) Convey("测试ToBaseValue - uint64 转换失败", t, func() { - res := ToBaseValue[uint64]("s12345") + res := ToBaseTypeValue[uint64]("s12345") So(res.Err, ShouldNotBeNil) So(res.Value, ShouldEqual, 0) So(reflect.TypeOf(res.Value).Kind(), ShouldEqual, reflect.Uint64) diff --git a/op_string/slice.go b/op_string/slice.go index 19a0a5e..a3c381e 100644 --- a/op_string/slice.go +++ b/op_string/slice.go @@ -33,7 +33,7 @@ func ToBaseTypeSlice[BaseType op_type.BaseType](str string, splitChar ...string) // 按照分隔符转换 strArr := strings.Split(str, splitChar[0]) for _, v := range strArr { - itemConvertRes := ToBaseValue[BaseType](v) + itemConvertRes := ToBaseTypeValue[BaseType](v) if nil != itemConvertRes.Err { return define.BaseValueSliceResult[BaseType]{Value: []BaseType{}, Err: itemConvertRes.Err} } diff --git a/op_string/tool.go b/op_string/tool.go new file mode 100644 index 0000000..c57c893 --- /dev/null +++ b/op_string/tool.go @@ -0,0 +1,148 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 15:28 +package op_string + +import ( + "crypto/md5" + "encoding/hex" + "io" + "math/rand" + "strings" + "time" + + "git.zhangdeman.cn/zhangdeman/wrapper/define" + "github.com/spaolacci/murmur3" +) + +// GetLetterList 获取字符列表 +func GetLetterList() []string { + return []string{ + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", + "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", + } +} + +// SnakeCaseToCamel 蛇形转驼峰 +func SnakeCaseToCamel(str string) string { + if len(str) == 0 { + return "" + } + builder := strings.Builder{} + index := 0 + if str[0] >= 'a' && str[0] <= 'z' { + builder.WriteByte(str[0] - ('a' - 'A')) + index = 1 + } + for i := index; i < len(str); i++ { + if str[i] == '_' && i+1 < len(str) { + if str[i+1] >= 'a' && str[i+1] <= 'z' { + builder.WriteByte(str[i+1] - ('a' - 'A')) + i++ + continue + } + } + // 将ID转为大写 + if str[i] == 'd' && i-1 >= 0 && (str[i-1] == 'i' || str[i-1] == 'I') && (i+1 == len(str) || i+1 < len(str) && str[i+1] == '_') { + builder.WriteByte('d' - ('a' - 'A')) + continue + } + builder.WriteByte(str[i]) + } + return builder.String() +} + +// Md5 计算Md5值 +func Md5(str string) define.StringResult { + h := md5.New() + _, err := io.WriteString(h, str) + if nil != err { + return define.StringResult{ + Value: "", + Err: err, + } + } + return define.StringResult{ + Value: hex.EncodeToString(h.Sum(nil)), + Err: nil, + } +} + +// ClearChar 清理指定字符 +func ClearChar(str string, charList ...string) string { + if len(charList) == 0 { + return str + } + for _, item := range charList { + str = strings.ReplaceAll(str, item, "") + } + return str +} + +// ReplaceChineseChar 替换常见的中文符号 +func ReplaceChineseChar(str string) string { + charTable := map[string]string{ + "(": "(", + ")": ")", + ":": ":", + ",": ",", + "。": ".", + "【": "]", + "】": "]", + } + return ReplaceChar(str, charTable) +} + +// ReplaceChar 替换指定字符 +func ReplaceChar(str string, charTable map[string]string) string { + if len(charTable) == 0 { + return str + } + for k, v := range charTable { + str = strings.ReplaceAll(str, k, v) + } + return str +} + +// HasSubStr 是否包含指定的子串 +func HasSubStr(str string, subStrList []string) bool { + if len(subStrList) == 0 { + return true + } + for _, item := range subStrList { + if strings.Contains(str, item) { + return true + } + } + return false +} + +// HashNumber 生成字符串哈希值 +func HashNumber(str string) define.BaseValueResult[uint64] { + return define.BaseValueResult[uint64]{ + Value: murmur3.Sum64([]byte(str)), + Err: nil, + } +} + +// Random 生成随机字符串 +func Random(length int, sourceCharList string) string { + if length == 0 { + return "" + } + if len(sourceCharList) == 0 { + //字符串为空,默认字符源为如下(去除易混淆的i/l): + sourceCharList = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ" + } + strByte := []byte(sourceCharList) + var genStrByte = make([]byte, 0) + r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < length; i++ { + genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))]) + } + return string(genStrByte) +} diff --git a/struct.go b/op_struct/struct.go similarity index 58% rename from struct.go rename to op_struct/struct.go index f5f3785..4a5398b 100644 --- a/struct.go +++ b/op_struct/struct.go @@ -1,32 +1,26 @@ -// Package wrapper ... +// Package op_struct ... // // Description : wrapper ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2023-08-10 16:05 -package wrapper +package op_struct import ( "errors" "reflect" + + "git.zhangdeman.cn/zhangdeman/wrapper/op_map" ) // NewStruct 包装的数据类型 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:07 2023/8/10 func NewStruct(data any) *Struct { s, _ := NewStructWithError(data) return s } // NewStructWithError ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:17 2023/8/10 func NewStructWithError(data any) (*Struct, error) { if data == nil { return nil, errors.New("input data is nil") @@ -42,28 +36,11 @@ func NewStructWithError(data any) (*Struct, error) { } // Struct 结构体类型 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:05 2023/8/10 type Struct struct { data any } // ToMap 转为Map -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:08 2023/8/10 -func (s *Struct) ToMap() MapResult { - if nil == s.data { - return MapResult{ - Value: EasyMap(map[any]any{}), - Err: nil, - } - } - return MapResult{ - Value: EasyMapFromStruct(s.data), - Err: nil, - } +func ToMap(s *Struct) op_map.Map { + return op_map.EasyMap(s.data) } diff --git a/op_ternary/ternary_operator.go b/op_ternary/ternary_operator.go new file mode 100644 index 0000000..2a55cdf --- /dev/null +++ b/op_ternary/ternary_operator.go @@ -0,0 +1,79 @@ +// Package op_ternary ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-11-28 16:05 +package op_ternary + +import ( + "git.zhangdeman.cn/zhangdeman/op_type" + "git.zhangdeman.cn/zhangdeman/wrapper/op_any" + "git.zhangdeman.cn/zhangdeman/wrapper/op_map" +) + +var ( + // TernaryOperator 三元运算符操作实例 + TernaryOperator = &ternaryOperator{} +) + +// ternaryOperator ... +type ternaryOperator struct { +} + +// CondFunc ... +type CondFunc func() bool + +// defaultCondFunc ... +func defaultCondFunc() bool { + return false +} + +// BaseType ... +func BaseType[Bt op_type.BaseType](cond bool, trueVal Bt, falseVal Bt) Bt { + if cond { + return trueVal + } + return falseVal +} + +// BaseTypeWithFunc ... +func BaseTypeWithFunc[Bt op_type.BaseType](condFunc CondFunc, trueVal Bt, falseVal Bt) Bt { + if nil == condFunc { + condFunc = defaultCondFunc + } + return BaseType(condFunc(), trueVal, falseVal) +} + +// Map ... +func Map(cond bool, trueVal op_map.Map, falseVal op_map.Map) op_map.Map { + if cond { + return trueVal + } + return falseVal +} + +// MapWithFunc ... +func MapWithFunc(condFunc CondFunc, trueVal op_map.Map, falseVal op_map.Map) op_map.Map { + if nil == condFunc { + condFunc = defaultCondFunc + } + return Map(condFunc(), trueVal, falseVal) +} + +// Any ... +func Any(cond bool, trueVal *op_any.AnyType, falseVal *op_any.AnyType) *op_any.AnyType { + if cond { + return trueVal + } + return falseVal +} + +// AnyWithFunc ... +func AnyWithFunc(condFunc CondFunc, trueVal *op_any.AnyType, falseVal *op_any.AnyType) *op_any.AnyType { + if nil == condFunc { + condFunc = defaultCondFunc + } + return Any(condFunc(), trueVal, falseVal) +} diff --git a/string.go b/string.go deleted file mode 100644 index d916464..0000000 --- a/string.go +++ /dev/null @@ -1,947 +0,0 @@ -// Package wrapper ... -// -// Description : wrapper ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2023-05-05 11:59 -package wrapper - -import ( - "crypto/md5" - "encoding/hex" - "errors" - "io" - "math/rand" - "strings" - "time" - - "git.zhangdeman.cn/zhangdeman/serialize" - "github.com/axgle/mahonia" - "github.com/spaolacci/murmur3" -) - -// StringFromRandom 从随机字符串生成String -func StringFromRandom(length int, sourceCharList string) String { - if length == 0 { - return "" - } - if len(sourceCharList) == 0 { - //字符串为空,默认字符源为如下(去除易混淆的i/l): - sourceCharList = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ" - } - strByte := []byte(sourceCharList) - var genStrByte = make([]byte, 0) - r := rand.New(rand.NewSource(time.Now().UnixNano())) - for i := 0; i < length; i++ { - genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))]) - } - return String(genStrByte) -} - -// String 字符串类型包装 -type String string - -// ToFloat32 ... -func (str String) ToFloat32() Float32Result { - var ( - res Float32Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToFloat32Ptr ... -func (str String) ToFloat32Ptr() Float32PtrResult { - res := str.ToFloat32() - if nil != res.Err { - return Float32PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Float32PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToFloat64 ... -func (str String) ToFloat64() Float64Result { - var ( - res Float64Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToFloat64Ptr ... -func (str String) ToFloat64Ptr() Float64PtrResult { - res := str.ToFloat64() - if nil != res.Err { - return Float64PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Float64PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToDouble 转double -func (str String) ToDouble() Float64Result { - return str.ToFloat64() -} - -// ToDoublePtr ... -func (str String) ToDoublePtr() Float64PtrResult { - return str.ToFloat64Ptr() -} - -// ToNumber 转数字, 使用最高精度的float64 -func (str String) ToNumber() Float64Result { - return str.ToFloat64() -} - -// ToNumberPtr ... -func (str String) ToNumberPtr() Float64PtrResult { - return str.ToFloat64Ptr() -} - -// ToInt8 ... -func (str String) ToInt8() Int8Result { - var ( - res Int8Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt8Ptr ... -func (str String) ToInt8Ptr() Int8PtrResult { - res := str.ToInt8() - if nil != res.Err { - return Int8PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Int8PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToInt16 ... -func (str String) ToInt16() Int16Result { - var ( - res Int16Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt16Ptr ... -func (str String) ToInt16Ptr() Int16PtrResult { - res := str.ToInt16() - if nil != res.Err { - return Int16PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Int16PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToInt32 ... -func (str String) ToInt32() Int32Result { - var ( - res Int32Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt32Ptr ... -func (str String) ToInt32Ptr() Int32PtrResult { - res := str.ToInt32() - if nil != res.Err { - return Int32PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Int32PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToInt64 ... -func (str String) ToInt64() Int64Result { - var ( - res Int64Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt64Ptr ... -func (str String) ToInt64Ptr() Int64PtrResult { - res := str.ToInt64() - if nil != res.Err { - return Int64PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Int64PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToInt ... -func (str String) ToInt() IntResult { - var ( - res IntResult - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToIntPtr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:04 2023/5/16 -func (str String) ToIntPtr() IntPtrResult { - res := str.ToInt() - if nil != res.Err { - return IntPtrResult{ - Value: nil, - Err: res.Err, - } - } - return IntPtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToUint8 ... -func (str String) ToUint8() Uint8Result { - var ( - res Uint8Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint8Ptr ... -func (str String) ToUint8Ptr() Uint8PtrResult { - res := str.ToUint8() - if nil != res.Err { - return Uint8PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Uint8PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToUint16 ... -func (str String) ToUint16() Uint16Result { - var ( - res Uint16Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint16Ptr ... -func (str String) ToUint16Ptr() Uint16PtrResult { - res := str.ToUint16() - if nil != res.Err { - return Uint16PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Uint16PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToUint32 ... -func (str String) ToUint32() Uint32Result { - var ( - res Uint32Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint32Ptr ... -func (str String) ToUint32Ptr() Uint32PtrResult { - res := str.ToUint32() - if nil != res.Err { - return Uint32PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Uint32PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToUint64 ... -func (str String) ToUint64() Uint64Result { - var ( - res Uint64Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint64Ptr ... -func (str String) ToUint64Ptr() Uint64PtrResult { - res := str.ToUint64() - if nil != res.Err { - return Uint64PtrResult{ - Value: nil, - Err: res.Err, - } - } - return Uint64PtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToUint ... -func (str String) ToUint() UintResult { - var ( - res UintResult - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUintPtr ... -func (str String) ToUintPtr() UintPtrResult { - res := str.ToUint() - if nil != res.Err { - return UintPtrResult{ - Value: nil, - Err: res.Err, - } - } - return UintPtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToBool ... -func (str String) ToBool() BoolResult { - var ( - res BoolResult - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToBoolPtr ... -func (str String) ToBoolPtr() BoolPtrResult { - res := str.ToBool() - if nil != res.Err { - return BoolPtrResult{ - Value: nil, - Err: res.Err, - } - } - return BoolPtrResult{ - Value: &res.Value, - Err: nil, - } -} - -// ToStringPtr 转换成字符串指针 -func (str String) ToStringPtr() StringPtrResult { - val := str.Value() - return StringPtrResult{ - Value: &val, - Err: nil, - } -} - -// ToObject ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:35 2023/5/4 -func (str String) ToObject() ObjectResult { - var ( - res = ObjectResult{ - Value: map[string]any{}, - Err: nil, - } - ) - res.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &res.Value) - return res -} - -// ToStruct ... -func (str String) ToStruct(receiver any) error { - if nil == receiver { - return errors.New("receiver is nil") - } - return serialize.JSON.UnmarshalWithNumber([]byte(str), receiver) -} - -// ToInt8Slice ... -func (str String) ToInt8Slice(splitChar ...string) Int8SliceResult { - result := Int8SliceResult{ - Value: []int8{}, - Err: nil, - } - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - var ( - res = make([]int8, 0) - ) - - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToInt8(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToInt16Slice ... -func (str String) ToInt16Slice(splitChar ...string) Int16SliceResult { - - result := Int16SliceResult{ - Value: []int16{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]int16, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToInt16(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToInt32Slice ... -func (str String) ToInt32Slice(splitChar ...string) Int32SliceResult { - result := Int32SliceResult{ - Value: []int32{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]int32, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToInt32(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToInt64Slice ... -func (str String) ToInt64Slice(splitChar ...string) Int64SliceResult { - result := Int64SliceResult{ - Value: []int64{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]int64, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToInt64(); nil != itemVal.Err { - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToIntSlice ... -func (str String) ToIntSlice(splitChar ...string) IntSliceResult { - result := IntSliceResult{ - Value: []int{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]int, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToInt(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToUint8Slice ... -func (str String) ToUint8Slice(splitChar ...string) Uint8SliceResult { - result := Uint8SliceResult{ - Value: []uint8{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]uint8, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToUint8(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToUint16Slice ... -func (str String) ToUint16Slice(splitChar ...string) Uint16SliceResult { - result := Uint16SliceResult{ - Value: []uint16{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]uint16, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToUint16(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToUint32Slice ... -func (str String) ToUint32Slice(splitChar ...string) Uint32SliceResult { - result := Uint32SliceResult{ - Value: []uint32{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]uint32, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToUint32(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToUint64Slice ... -func (str String) ToUint64Slice(splitChar ...string) Uint64SliceResult { - result := Uint64SliceResult{ - Value: []uint64{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]uint64, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToUint64(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - return result -} - -// ToUintSlice ... -func (str String) ToUintSlice(splitChar ...string) UintSliceResult { - result := UintSliceResult{ - Value: []uint{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]uint, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToUint(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToBoolSlice ... -func (str String) ToBoolSlice(splitChar ...string) BoolSliceResult { - result := BoolSliceResult{ - Value: []bool{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]bool, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToBool(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToFloat32Slice ... -func (str String) ToFloat32Slice(splitChar ...string) Float32SliceResult { - result := Float32SliceResult{ - Value: []float32{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]float32, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToFloat32(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - return result -} - -// ToFloat64Slice ... -func (str String) ToFloat64Slice(splitChar ...string) Float64SliceResult { - result := Float64SliceResult{ - Value: []float64{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - res := make([]float64, 0) - arr := strings.Split(string(str), splitChar[0]) - for _, item := range arr { - if itemVal := String(item).ToFloat64(); nil != itemVal.Err { - result.Err = itemVal.Err - return result - } else { - res = append(res, itemVal.Value) - } - } - result.Value = res - return result -} - -// ToDoubleSlice ... -func (str String) ToDoubleSlice(splitChar ...string) Float64SliceResult { - return str.ToFloat64Slice(splitChar...) -} - -// ToNumberSlice ... -func (str String) ToNumberSlice(splitChar ...string) Float64SliceResult { - return str.ToFloat64Slice(splitChar...) -} - -// ToDuration 转换为时间格式 -func (str String) ToDuration(timeUnit time.Duration) DurationResult { - int64Val := str.ToInt64() - if nil != int64Val.Err { - return DurationResult{ - Value: 0, - Err: int64Val.Err, - } - } - return Int(int64Val.Value).ToDuration(timeUnit) -} - -// ToStringSlice ... -func (str String) ToStringSlice(splitChar ...string) StringSliceResult { - result := StringSliceResult{ - Value: []string{}, - Err: nil, - } - - isSplit := len(splitChar) > 0 - if !isSplit { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - result.Value = strings.Split(string(str), splitChar[0]) - return result -} - -// ToAnySlice ... -func (str String) ToAnySlice(splitCharList ...string) AnySliceResult { - result := AnySliceResult{ - Value: []any{}, - Err: nil, - } - - if len(splitCharList) == 0 { - result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) - return result - } - - valArr := strings.Split(str.Value(), splitCharList[0]) - valList := make([]any, 0) - for _, item := range valArr { - v := String(item) - if res := v.ToInt64(); nil == res.Err { - valList = append(valList, res.Value) - } else if res := v.ToUint64(); nil == res.Err { - valList = append(valList, res.Value) - } else if res := v.ToFloat64(); nil == res.Err { - valList = append(valList, res.Value) - } else if res := v.ToBool(); nil == res.Err { - valList = append(valList, res.Value) - } else { - valList = append(valList, item) - } - } - result.Value = valList - return result -} - -// Md5 计算Md5值 -func (str String) Md5() StringResult { - h := md5.New() - _, err := io.WriteString(h, str.Value()) - if nil != err { - return StringResult{ - Value: "", - Err: err, - } - } - return StringResult{ - Value: hex.EncodeToString(h.Sum(nil)), - Err: nil, - } -} - -// Value ... -func (str String) Value() string { - return string(str) -} - -// GetLetterList 获取字母列表 -func (str String) GetLetterList() []string { - return []string{ - "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", - "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", - } -} - -// SnakeCaseToCamel 蛇形字符串转换为驼峰 -func (str String) SnakeCaseToCamel() string { - if len(str) == 0 { - return "" - } - builder := strings.Builder{} - index := 0 - if str[0] >= 'a' && str[0] <= 'z' { - builder.WriteByte(str[0] - ('a' - 'A')) - index = 1 - } - for i := index; i < len(str); i++ { - if str[i] == '_' && i+1 < len(str) { - if str[i+1] >= 'a' && str[i+1] <= 'z' { - builder.WriteByte(str[i+1] - ('a' - 'A')) - i++ - continue - } - } - // 将ID转为大写 - if str[i] == 'd' && i-1 >= 0 && (str[i-1] == 'i' || str[i-1] == 'I') && (i+1 == len(str) || i+1 < len(str) && str[i+1] == '_') { - builder.WriteByte('d' - ('a' - 'A')) - continue - } - builder.WriteByte(str[i]) - } - return builder.String() -} - -// Convert 字符串编码转换 -func (str String) Convert(sourceCode string, targetCode string) String { - sourceCoder := mahonia.NewDecoder(sourceCode) - sourceResult := sourceCoder.ConvertString(str.Value()) - targetCoder := mahonia.NewDecoder(targetCode) - _, cdata, _ := targetCoder.Translate([]byte(sourceResult), true) - return String(string(cdata)) -} - -// ClearChar 清理指定字符 -func (str String) ClearChar(charList ...string) String { - if len(charList) == 0 { - return str - } - formatStr := str.Value() - for _, item := range charList { - formatStr = strings.ReplaceAll(formatStr, item, "") - } - return String(formatStr) -} - -// ReplaceChineseChar 替换常见的中文符号 -func (str String) ReplaceChineseChar() String { - charTable := map[string]string{ - "(": "(", - ")": ")", - ":": ":", - ",": ",", - "。": ".", - "【": "]", - "】": "]", - } - return str.ReplaceChar(charTable) -} - -// ReplaceChar 替换指定字符 -func (str String) ReplaceChar(charTable map[string]string) String { - if len(charTable) == 0 { - return str - } - formatStr := str.Value() - for k, v := range charTable { - formatStr = strings.ReplaceAll(formatStr, k, v) - } - return String(formatStr) -} - -// HasSubStr 是否包含指定的子串 -func (str String) HasSubStr(subStrList []string) bool { - if len(subStrList) == 0 { - return true - } - v := str.Value() - for _, item := range subStrList { - if strings.Contains(v, item) { - return true - } - } - return false -} - -// HashNumber ... -func (str String) HashNumber() Uint64Result { - return Uint64Result{ - Value: murmur3.Sum64([]byte(str.Value())), - Err: nil, - } -} diff --git a/string_test.go b/string_test.go deleted file mode 100644 index 76f1f98..0000000 --- a/string_test.go +++ /dev/null @@ -1,31 +0,0 @@ -// Package wrapper ... -// -// Description : wrapper ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2023-05-05 13:39 -package wrapper - -import ( - "fmt" - "testing" -) - -func TestString_ToFloat32(t *testing.T) { - var str String - str = "12345.123" - fmt.Println(str) - fmt.Println(str.ToFloat32()) - fmt.Println(str.ToFloat64()) - fmt.Println(str.ToNumber()) - fmt.Println(str.ToDouble()) - fmt.Println(str.ToInt()) - fmt.Println(str.ToUint()) -} - -func TestString_ToAnySlice(t *testing.T) { - str := "1,2,3" - r := String(str).ToAnySlice(",") - fmt.Println(r) -} diff --git a/ternary_operator.go b/ternary_operator.go deleted file mode 100644 index 2dd2fea..0000000 --- a/ternary_operator.go +++ /dev/null @@ -1,157 +0,0 @@ -// Package wrapper ... -// -// Description : wrapper ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2023-11-28 16:05 -package wrapper - -var ( - // TernaryOperator 三元运算符操作实例 - TernaryOperator = &ternaryOperator{} -) - -// ternaryOperator ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:06 2023/11/28 -type ternaryOperator struct { -} - -// CondFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:10 2023/11/28 -type CondFunc func() bool - -// defaultCondFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:11 2023/11/28 -func defaultCondFunc() bool { - return false -} - -// Int ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:10 2023/11/28 -func (to *ternaryOperator) Int(cond bool, trueVal Int, falseVal Int) Int { - if cond { - return trueVal - } - return falseVal -} - -// IntWithFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:11 2023/11/28 -func (to *ternaryOperator) IntWithFunc(condFunc CondFunc, trueVal Int, falseVal Int) Int { - if nil == condFunc { - condFunc = defaultCondFunc - } - return to.Int(condFunc(), trueVal, falseVal) -} - -// Float ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:10 2023/11/28 -func (to *ternaryOperator) Float(cond bool, trueVal Float, falseVal Float) Float { - if cond { - return trueVal - } - return falseVal -} - -// FloatWithFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:13 2023/11/28 -func (to *ternaryOperator) FloatWithFunc(condFunc CondFunc, trueVal Float, falseVal Float) Float { - if nil == condFunc { - condFunc = defaultCondFunc - } - return to.Float(condFunc(), trueVal, falseVal) -} - -// String ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:15 2023/11/28 -func (to *ternaryOperator) String(cond bool, trueVal String, falseVal String) String { - if cond { - return trueVal - } - return falseVal -} - -// StringWithFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:15 2023/11/28 -func (to *ternaryOperator) StringWithFunc(condFunc CondFunc, trueVal String, falseVal String) String { - if nil == condFunc { - condFunc = defaultCondFunc - } - return to.String(condFunc(), trueVal, falseVal) -} - -// Map ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:25 2023/11/28 -func (to *ternaryOperator) Map(cond bool, trueVal Map, falseVal Map) Map { - if cond { - return trueVal - } - return falseVal -} - -// MapWithFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:24 2023/11/28 -func (to *ternaryOperator) MapWithFunc(condFunc CondFunc, trueVal Map, falseVal Map) Map { - if nil == condFunc { - condFunc = defaultCondFunc - } - return falseVal -} - -// Any ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:26 2023/11/28 -func (to *ternaryOperator) Any(cond bool, trueVal *AnyType, falseVal *AnyType) *AnyType { - if cond { - return trueVal - } - return falseVal -} - -// AnyWithFunc ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:24 2023/11/28 -func (to *ternaryOperator) AnyWithFunc(condFunc CondFunc, trueVal *AnyType, falseVal *AnyType) *AnyType { - if nil == condFunc { - condFunc = defaultCondFunc - } - return to.Any(condFunc(), trueVal, falseVal) -} diff --git a/tool/define/diff.go b/tool/define/diff.go index 54b5547..2b20746 100644 --- a/tool/define/diff.go +++ b/tool/define/diff.go @@ -9,8 +9,10 @@ package define import ( "fmt" + "git.zhangdeman.cn/zhangdeman/util" - "git.zhangdeman.cn/zhangdeman/wrapper" + "git.zhangdeman.cn/zhangdeman/wrapper/op_map" + "reflect" "strings" ) @@ -61,7 +63,7 @@ func NewDiffOption() *DiffOption { // Author : go_developer@163.com<白茶清欢> // // Date : 11:06 2024/3/8 -type CustomDiffFunc func(field string, inputVal wrapper.Map, storageVal wrapper.Map, option *DiffOption) *DiffResult +type CustomDiffFunc func(field string, inputVal op_map.Map, storageVal op_map.Map, option *DiffOption) *DiffResult // DiffResult 对比结果 // @@ -122,7 +124,7 @@ func IsSupportValueType(kind reflect.Kind) bool { // Author : zhangdeman001@ke.com<张德满> // // Date : 12:05 2024/3/8 -func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map, option *DiffOption) *DiffResult { +func DefaultDiffFunc(field string, inputVal op_map.Map, storageVal op_map.Map, option *DiffOption) *DiffResult { if nil == option { option = NewDiffOption() } -- 2.36.6