diff --git a/convert/convert.go b/convert/convert.go new file mode 100644 index 0000000..495581c --- /dev/null +++ b/convert/convert.go @@ -0,0 +1,394 @@ +// Package convert ... +// +// Description : 任意类型之间的相互转换 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-02-23 10:23 下午 +package convert + +/* + 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 +} diff --git a/define.go b/define.go index c7fb631..9f1890f 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 any `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/define/result.go b/define/result.go new file mode 100644 index 0000000..f7b6ed3 --- /dev/null +++ b/define/result.go @@ -0,0 +1,58 @@ +// 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:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 +} + +// BaseValuePtrResult 基础类型指针转换结果 +type BaseValuePtrResult[BaseType op_type.BaseType] struct { + Value *BaseType `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 +} + +// MapValueResult map类型转换结果 +type MapValueResult[Key comparable, Value any] struct { + Value map[Key]Value `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 +} + +// StructValueResult struct类型转换结果 +type StructValueResult[Value any] struct { + Value Value `json:"value"` // 转换结果 + Err error `json:"err"` // 错误信息 +} + +// BaseValueSliceResult 基础类型切片转换结果 +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"` // 错误信息 +} + +// StringResult ... +type StringResult struct { + Value string `json:"value"` + Err error `json:"err"` +} diff --git a/go.mod b/go.mod index c57913c..2f5b853 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,14 @@ 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 - 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 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..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= @@ -34,8 +38,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 +48,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 deleted file mode 100644 index 58652ee..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 new file mode 100644 index 0000000..71cecdc --- /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/convert" + "git.zhangdeman.cn/zhangdeman/wrapper/define" +) + +// ToBaseTypeValue 转换为基础数据类型 +func ToBaseTypeValue[BaseType op_type.BaseType](str string) define.BaseValueResult[BaseType] { + var ( + err error + target BaseType + ) + if err = convert.ConvertAssign(&target, str); nil != err { + return define.BaseValueResult[BaseType]{ + Value: target, + Err: err, + } + } + return define.BaseValueResult[BaseType]{ + Value: target, + Err: nil, + } +} + +// ToBaseValuePtr 转换为基础数据类型指针 +func ToBaseValuePtr[BaseType op_type.BaseType](str string) define.BaseValuePtrResult[BaseType] { + var ( + err error + target BaseType + ) + if err = convert.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..cb28611 --- /dev/null +++ b/op_string/base_test.go @@ -0,0 +1,49 @@ +// 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 := 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 := ToBaseTypeValue[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) + }) +} + +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) + }) +} diff --git a/op_string/map.go b/op_string/map.go new file mode 100644 index 0000000..189cdb2 --- /dev/null +++ b/op_string/map.go @@ -0,0 +1,48 @@ +// 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 any](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, + } +} + +// 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 new file mode 100644 index 0000000..5def276 --- /dev/null +++ b/op_string/map_test.go @@ -0,0 +1,76 @@ +// 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")) + }) + 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() { + 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) + }) + 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 new file mode 100644 index 0000000..a3c381e --- /dev/null +++ b/op_string/slice.go @@ -0,0 +1,57 @@ +// 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[BaseType op_type.BaseType](str string, splitChar ...string) define.BaseValueSliceResult[BaseType] { + var ( + err error + sliceValue []BaseType + ) + + if len(splitChar) == 0 { + // 序列化数组直接转换 + if err = serialize.JSON.UnmarshalWithNumberForString(str, &sliceValue); nil != err { + return define.BaseValueSliceResult[BaseType]{Value: []BaseType{}, Err: err} + } + return define.BaseValueSliceResult[BaseType]{Value: sliceValue, Err: nil} + } + // 按照分隔符转换 + strArr := strings.Split(str, splitChar[0]) + for _, v := range strArr { + itemConvertRes := ToBaseTypeValue[BaseType](v) + if nil != itemConvertRes.Err { + return define.BaseValueSliceResult[BaseType]{Value: []BaseType{}, Err: itemConvertRes.Err} + } + sliceValue = append(sliceValue, itemConvertRes.Value) + } + 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 new file mode 100644 index 0000000..6ddd007 --- /dev/null +++ b/op_string/slice_test.go @@ -0,0 +1,76 @@ +// Package op_string ... +// +// Description : op_string ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-13 14:36 +package op_string + +import ( + "encoding/json" + "testing" + + "git.zhangdeman.cn/zhangdeman/serialize" + . "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) + 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) + }) +} 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 d59aa5c..0000000 --- a/string.go +++ /dev/null @@ -1,1190 +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" - "git.zhangdeman.cn/zhangdeman/serialize" - "github.com/axgle/mahonia" - "github.com/spaolacci/murmur3" - "io" - "math/rand" - "strings" - "time" -) - -// StringFromRandom 从随机字符串生成String -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 14:50 2023/8/11 -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 < int(length); i++ { - genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))]) - } - return String(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 - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToFloat32Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:58 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:26 2023/5/4 -func (str String) ToFloat64() Float64Result { - var ( - res Float64Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToFloat64Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:59 2023/5/16 -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 -// -// 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 - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt8Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:01 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 -func (str String) ToInt16() Int16Result { - var ( - res Int16Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt16Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:02 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 -func (str String) ToInt32() Int32Result { - var ( - res Int32Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt32Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:03 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 -func (str String) ToInt64() Int64Result { - var ( - res Int64Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToInt64Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:03 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:47 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:30 2023/5/4 -func (str String) ToUint8() Uint8Result { - var ( - res Uint8Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint8Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:05 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 -func (str String) ToUint16() Uint16Result { - var ( - res Uint16Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint16Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:05 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 -func (str String) ToUint32() Uint32Result { - var ( - res Uint32Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint32Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:05 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 -func (str String) ToUint64() Uint64Result { - var ( - res Uint64Result - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUint64Ptr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:06 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:29 2023/5/4 -func (str String) ToUint() UintResult { - var ( - res UintResult - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToUintPtr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:06 2023/5/16 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:34 2023/5/4 -func (str String) ToBool() BoolResult { - var ( - res BoolResult - ) - res.Err = ConvertAssign(&res.Value, str) - return res -} - -// ToBoolPtr ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 12:07 2023/5/16 -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 转换成字符串指针 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:57 2023/7/3 -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 ... -// -// 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") - } - return serialize.JSON.UnmarshalWithNumber([]byte(str), receiver) -} - -// ToInt8Slice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:55 2023/5/4 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:01 2023/5/4 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:03 2023/5/4 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:04 2023/5/4 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 19:04 2023/5/4 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:50 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:52 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:53 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:54 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:54 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:55 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:56 2023/5/5 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 10:56 2023/5/5 -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 ... -// -// 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 { - return DurationResult{ - Value: 0, - Err: int64Val.Err, - } - } - return Int(int64Val.Value).ToDuration(timeUnit) -} - -// ToStringSlice ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:11 2023/8/6 -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 ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:01 2023/5/5 -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值 -// -// 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()) - if nil != err { - return StringResult{ - Value: "", - Err: err, - } - } - return StringResult{ - Value: hex.EncodeToString(h.Sum(nil)), - Err: nil, - } -} - -// 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", - "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", - } -} - -// SnakeCaseToCamel 蛇形字符串转换为驼峰 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 4:58 下午 2021/10/25 -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 字符串编码转换 -// -// 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()) - targetCoder := mahonia.NewDecoder(targetCode) - _, cdata, _ := targetCoder.Translate([]byte(sourceResult), true) - return String(string(cdata)) -} - -// 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 - } - formatStr := str.Value() - for _, item := range charList { - formatStr = strings.ReplaceAll(formatStr, item, "") - } - return String(formatStr) -} - -// ReplaceChineseChar 替换常见的中文符号 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:59 2023/4/4 -func (str String) ReplaceChineseChar() String { - charTable := map[string]string{ - "(": "(", - ")": ")", - ":": ":", - ",": ",", - "。": ".", - "【": "]", - "】": "]", - } - return str.ReplaceChar(charTable) -} - -// 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 - } - formatStr := str.Value() - for k, v := range charTable { - formatStr = strings.ReplaceAll(formatStr, k, v) - } - return String(formatStr) -} - -// 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 - } - v := str.Value() - for _, item := range subStrList { - if strings.Contains(v, item) { - return true - } - } - return false -} - -// HashNumber ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 11:05 2024/6/27 -func (str String) HashNumber() Uint64Result { - return Uint64Result{ - Value: murmur3.Sum64([]byte(str.Value())), - Err: nil, - } -} 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() }