feat: 相关操作升级为泛型实现

This commit is contained in:
2025-10-13 17:05:00 +08:00
parent 80b5e4e7cc
commit 50d2d6c7bb
18 changed files with 316 additions and 1354 deletions

View File

@ -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 : 在处理一些参数的时候可能需要将参数转换为各种类型这里实现一个通用的转换函数实现各种类型之间的相互转换

View File

@ -291,7 +291,7 @@ type StringSliceResult struct {
// MapResult 转map的结果
type MapResult struct {
Value Map `json:"value"`
Value any `json:"value"`
Err error `json:"err"`
}

View File

@ -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"`
}

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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))

View File

@ -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
}

View File

@ -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,

View File

@ -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)

View File

@ -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}
}

148
op_string/tool.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

947
string.go
View File

@ -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,
}
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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()
}