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] =?UTF-8?q?feat:=20=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E4=B8=BA=E6=B3=9B=E5=9E=8B=E5=AE=9E=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() }