1 Commits

Author SHA1 Message Date
50aa5ae7f8 数组支持提取指定路径字段, 转换成list 2024-08-06 16:03:12 +08:00
34 changed files with 3125 additions and 1420 deletions

View File

@ -1,21 +1,24 @@
// Package op_any ...
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-01 18:18
package op_any
package wrapper
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,
@ -25,29 +28,30 @@ 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
dataType string
}
// IsNil 是否为 nil
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:21 2023/6/1
func (at *AnyType) IsNil() bool {
if at.data == nil {
return true
}
reflectValue := reflect.ValueOf(at.data)
switch reflectValue.Kind() {
case reflect.Chan, reflect.Func, reflect.Map,
reflect.Pointer, reflect.UnsafePointer,
reflect.Interface, reflect.Slice:
return reflectValue.IsNil()
default:
return false
}
return at.data == nil
}
// Type 获取类型
func (at *AnyType) Type() consts.DataType {
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:22 2023/6/1
func (at *AnyType) Type() string {
if len(at.dataType) > 0 {
// 已经处理过的,无需在处理
return at.dataType
@ -72,29 +76,39 @@ func (at *AnyType) Type() consts.DataType {
case reflect.Bool:
return consts.DataTypeBool
case reflect.Float32, reflect.Float64:
return consts.DataTypeFloat64
return consts.DataTypeFloat
default:
return consts.DataTypeUnknown
}
}
// ToString 转为字符串
func (at *AnyType) ToString() string {
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:32 2023/6/1
func (at *AnyType) ToString() String {
dataType := at.Type()
switch dataType {
case consts.DataTypeUnknown, consts.DataTypeNil:
return ""
return String("")
case consts.DataTypeString:
return fmt.Sprintf("%v", at.data)
return String(fmt.Sprintf("%v", at.data))
case consts.DataTypeSliceAny:
var val []any
_ = serialize.JSON.Transition(at.data, &val)
return String(ArrayType[any, any](val).ToString().Value)
case consts.DataTypeMapAnyAny:
return String(EasyMap(at.data).ToString())
case consts.DataTypeInt:
fallthrough
return String(Int(at.data.(int64)).ToString().Value)
case consts.DataTypeUint:
fallthrough
case consts.DataTypeFloat64:
fallthrough
return String(Int(at.data.(uint)).ToString().Value)
case consts.DataTypeFloat:
return String(Float(at.data.(float64)).ToString().Value)
case consts.DataTypeBool:
return fmt.Sprintf("%v", at.data)
return String(fmt.Sprintf("%v", at.data))
default:
return serialize.JSON.MarshalForStringIgnoreError(at.data)
return String(serialize.JSON.MarshalForString(at.data))
}
}

190
array.go Normal file
View File

@ -0,0 +1,190 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-11 21:02
package wrapper
import (
"encoding/json"
"errors"
"git.zhangdeman.cn/zhangdeman/op_type"
"github.com/tidwall/gjson"
"reflect"
"strings"
)
// ArrayType 数组实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:03 2023/6/11
func ArrayType[Bt op_type.BaseType, ExtractDataType op_type.BaseType](value []Bt) *Array[Bt, ExtractDataType] {
at := &Array[Bt, ExtractDataType]{
value: value,
}
return at
}
// Array ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:05 2023/6/11
type Array[Bt op_type.BaseType, ExtractDataType op_type.BaseType] struct {
value []Bt
convertErr error
itemType reflect.Kind // 简单list场景下, 每一项的数据类型
}
// IsNil 输入是否为nil
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:11 2023/6/11
func (at *Array[Bt, ExtractDataType]) IsNil() bool {
return at.value == nil
}
// ToStringSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:42 2024/4/22
func (at *Array[Bt, ExtractDataType]) ToStringSlice() []string {
list := make([]string, 0)
for _, item := range at.value {
byteData, _ := json.Marshal(item)
list = append(list, strings.Trim(string(byteData), "\""))
}
return list
}
// Unique 对数据结果进行去重
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:43 2023/6/12
func (at *Array[Bt, ExtractDataType]) Unique() []Bt {
result := make([]Bt, 0)
dataTable := make(map[string]bool)
for _, item := range at.value {
byteData, _ := json.Marshal(item)
k := string(byteData)
if strings.HasPrefix(k, "\"\"") && strings.HasSuffix(k, "\"\"") {
k = string(byteData[1 : len(byteData)-1])
}
if _, exist := dataTable[k]; exist {
continue
}
dataTable[k] = true
result = append(result, item)
}
return result
}
// Has 查询一个值是否在列表里, 在的话, 返回首次出现的索引, 不在返回-1
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:28 2023/7/31
func (at *Array[Bt, ExtractDataType]) Has(input Bt) int {
for idx := 0; idx < len(at.value); idx++ {
if reflect.TypeOf(at.value[idx]).String() != reflect.TypeOf(input).String() {
// 类型不同
continue
}
sourceByte, _ := json.Marshal(at.value[idx])
inputByte, _ := json.Marshal(input)
if string(sourceByte) != string(inputByte) {
continue
}
return idx
}
return -1
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:57 2023/9/28
func (at *Array[Bt, ExtractDataType]) ToString() StringResult {
if at.IsNil() {
return StringResult{
Value: "",
Err: nil,
}
}
byteData, err := json.Marshal(at.value)
return StringResult{
Value: string(byteData),
Err: err,
}
}
// ToStringWithSplit 数组按照指定分隔符转为字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:42 2023/10/25
func (at *Array[Bt, ExtractDataType]) ToStringWithSplit(split string) StringResult {
if at.IsNil() {
return StringResult{
Value: "",
Err: nil,
}
}
return StringResult{
Value: strings.Join(at.ToStringSlice(), split),
Err: nil,
}
}
// ExtractField 提取指定字段, 转换成数组
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:24 2024/8/6
func (at *Array[Bt, ExtractDataType]) ExtractField(fieldPath string) ([]ExtractDataType, error) {
strValResult := at.ToString()
if nil != strValResult.Err {
return make([]ExtractDataType, 0), nil
}
gjsonResult := gjson.Parse(strValResult.Value)
if !gjsonResult.IsArray() {
return make([]ExtractDataType, 0), errors.New("input value is not slice")
}
arrList := gjsonResult.Array()
if len(arrList) == 0 {
return make([]ExtractDataType, 0), nil
}
res := make([]ExtractDataType, 0)
for _, item := range arrList {
valueResult := item.Get(fieldPath)
if !valueResult.Exists() {
// 不存在
continue
}
var val ExtractDataType
if err := ConvertAssign(&val, valueResult.String()); nil != err {
return nil, err
}
res = append(res, val)
}
return res, nil
}
// ExtractFieldIgnoreError 提取指定字段并忽略异常
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:28 2024/8/6
func (at *Array[Bt, ExtractDataType]) ExtractFieldIgnoreError(field string) []ExtractDataType {
res, _ := at.ExtractField(field)
return res
}

36
array_test.go Normal file
View File

@ -0,0 +1,36 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-06 下午2:48
package wrapper
import (
"fmt"
"testing"
)
func TestArray_Unique(t *testing.T) {
fmt.Println(ArrayType[any, any]([]any{"1", 1, 1, "1", 2, 3}).Unique())
fmt.Println(ArrayType[int, any]([]int{1, 1, 2, 3}).Unique())
}
func TestArray_ExtractField(t *testing.T) {
testMap := []any{
map[string]any{
"age": 18,
"name": "baicha",
},
map[string]any{
"age": 20,
"name": "qinghuan",
},
map[string]any{
"foo": "bar",
},
}
fmt.Println(ArrayType[any, int](testMap).ExtractField("age"))
fmt.Println(ArrayType[any, string](testMap).ExtractField("name"))
}

View File

@ -1,118 +0,0 @@
// Package bigint ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-19 16:33
package bigint
import (
"database/sql/driver"
"fmt"
"reflect"
"strconv"
)
// BigInt 是一个自定义类型用于在JSON编码时将int64转换为字符串。
// 建议只在Output中使用它Input时直接使用int64。
type BigInt string
var EmptyBigInt BigInt = "0"
func (f *BigInt) IsNil() bool {
if nil == f {
return true
}
return reflect.ValueOf(*f).IsNil()
}
// MarshalJSON 自定义的JSON编码逻辑。
// @implements json.Marshaler
func (f *BigInt) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%d"`, f.Int64())), nil
}
// ToString 返回BigId的字符串表示。
func (f *BigInt) String() string {
if f.IsNil() {
return string(EmptyBigInt)
}
return string(*f)
}
// Value 在orm写入数据库时会调用它进行格式转换.
// @implements database/sql/driver.Valuer
func (f *BigInt) Value() (driver.Value, error) {
if f.IsNil() {
return 0, nil
}
return f.Uint64(), nil
}
// Int64 返回BigId的int64表示。
// @implements github.com/gogf/gf/v2/util/gconv.iInt64
func (f *BigInt) Int64() int64 {
if f.IsNil() {
return 0
}
i, err := strconv.ParseInt(string(*f), 10, 64)
if err != nil || i <= 0 {
return 0
}
return i
}
// Uint64 返回BigId的uint64表示。
// @implements github.com/gogf/gf/v2/util/gconv.iUint64
func (f *BigInt) Uint64() uint64 {
if f.IsNil() {
return 0
}
return uint64(f.Int64())
}
// IsEmpty 判断BigId是否为空
func (f *BigInt) IsEmpty() bool {
if f.IsNil() {
return true
}
return *f == "" || *f == EmptyBigInt || f.Int64() == 0
}
// UnmarshalJSON converts a json byte array of a big ID into an BigInt type.
// @implements json.Unmarshaler
func (f *BigInt) UnmarshalJSON(b []byte) error {
var (
s = string(b)
)
// 如果是null设置为0
if s == "null" || s == "" || s == "0" {
*f = EmptyBigInt
return nil
}
// 如果是字符串,去掉引号
if len(b) >= 3 && b[0] == '"' && b[len(b)-1] == '"' {
s = string(b[1 : len(b)-1])
}
*f = StrToBigInt(s)
return nil
}
// ToBigInt 将int转换为BigInt
func ToBigInt(id int64) BigInt {
if id <= 0 {
return EmptyBigInt
}
return BigInt(fmt.Sprintf("%d", id))
}
// StrToBigInt 将str转换为BigInt类型
func StrToBigInt(id string) BigInt {
i, err := strconv.ParseInt(id, 10, 64)
if err != nil || i <= 0 {
return EmptyBigInt
}
return BigInt(id)
}

View File

@ -1,11 +1,11 @@
// Package convert ...
// Package wrapper ...
//
// Description : 任意类型之间的相互转换
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-02-23 10:23 下午
package convert
package wrapper
/*
Desc : 在处理一些参数的时候可能需要将参数转换为各种类型这里实现一个通用的转换函数实现各种类型之间的相互转换

480
define.go Normal file
View File

@ -0,0 +1,480 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 14:44
package wrapper
import "time"
// Int8Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:25 2023/5/8
type Int8Result struct {
Value int8
Err error
}
// Int8PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:05 2023/5/15
type Int8PtrResult struct {
Value *int8
Err error
}
// Int16Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:26 2023/5/8
type Int16Result struct {
Value int16
Err error
}
// Int16PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:05 2023/5/15
type Int16PtrResult struct {
Value *int16
Err error
}
// Int32Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:26 2023/5/8
type Int32Result struct {
Value int32
Err error
}
// Int32PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:04 2023/5/15
type Int32PtrResult struct {
Value *int32
Err error
}
// Int64Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:26 2023/5/8
type Int64Result struct {
Value int64
Err error
}
// Int64PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:04 2023/5/15
type Int64PtrResult struct {
Value *int64
Err error
}
// IntResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:26 2023/5/8
type IntResult struct {
Value int
Err error
}
// IntPtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:51 2023/5/15
type IntPtrResult struct {
Value *int
Err error
}
// Uint8Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:28 2023/5/8
type Uint8Result struct {
Value uint8
Err error
}
// Uint8PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:49 2023/5/16
type Uint8PtrResult struct {
Value *uint8
Err error
}
// Uint16Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:28 2023/5/8
type Uint16Result struct {
Value uint16
Err error
}
// Uint16PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:49 2023/5/16
type Uint16PtrResult struct {
Value *uint16
Err error
}
// Uint32Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:28 2023/5/8
type Uint32Result struct {
Value uint32
Err error
}
// Uint32PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:49 2023/5/16
type Uint32PtrResult struct {
Value *uint32
Err error
}
// Uint64Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:28 2023/5/8
type Uint64Result struct {
Value uint64
Err error
}
// Uint64PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:50 2023/5/16
type Uint64PtrResult struct {
Value *uint64
Err error
}
// UintResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:29 2023/5/8
type UintResult struct {
Value uint
Err error
}
// UintPtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:51 2023/5/16
type UintPtrResult struct {
Value *uint
Err error
}
// Float32Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:31 2023/5/8
type Float32Result struct {
Value float32
Err error
}
// Float32PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:10 2023/5/16
type Float32PtrResult struct {
Value *float32
Err error
}
// Float64Result ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:32 2023/5/8
type Float64Result struct {
Value float64
Err error
}
// Float64PtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:52 2023/5/16
type Float64PtrResult struct {
Value *float64
Err error
}
// Any ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:40 2023/5/8
type Any struct {
Value any
Err error
}
// BoolResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:36 2023/5/8
type BoolResult struct {
Value bool
Err error
}
// BoolPtrResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 2023/5/16
type BoolPtrResult struct {
Value *bool
Err error
}
// ObjectResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:38 2023/5/8
type ObjectResult struct {
Value map[string]any
Err error
}
// StringResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:57 2023/5/8
type StringResult struct {
Value string
Err error
}
// StringPtrResult 字符串指针
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2023/5/15
type StringPtrResult struct {
Value *string
Err error
}
// Int8SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2023/5/8
type Int8SliceResult struct {
Value []int8
Err error
}
// Int16SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2023/5/8
type Int16SliceResult struct {
Value []int16
Err error
}
// Int32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type Int32SliceResult struct {
Value []int32
Err error
}
// Int64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type Int64SliceResult struct {
Value []int64
Err error
}
// IntSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2023/5/8
type IntSliceResult struct {
Value []int
Err error
}
// Uint8SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint8SliceResult struct {
Value []uint8
Err error
}
// Uint16SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint16SliceResult struct {
Value []uint16
Err error
}
// Uint32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint32SliceResult struct {
Value []uint32
Err error
}
// Uint64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:55 2023/5/8
type Uint64SliceResult struct {
Value []uint64
Err error
}
// UintSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:56 2023/5/8
type UintSliceResult struct {
Value []uint
Err error
}
// BoolSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:22 2023/5/8
type BoolSliceResult struct {
Value []bool
Err error
}
// Float32SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:24 2023/5/8
type Float32SliceResult struct {
Value []float32
Err error
}
// Float64SliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:24 2023/5/8
type Float64SliceResult struct {
Value []float64
Err error
}
// DurationResult 时间转换结果
//
// Author : zhangdeman001@ke.com<张德满>
//
// Date : 20:32 2023/9/4
type DurationResult struct {
Value time.Duration
Err error
}
// StringSliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:11 2023/8/6
type StringSliceResult struct {
Value []string
Err error
}
// MapResult 转map的结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:05 2023/8/10
type MapResult struct {
Value Map
Err error
}
// AnySliceResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:28 2023/5/8
type AnySliceResult struct {
Value []any
Err error
}

View File

@ -1,70 +0,0 @@
// 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"`
}*/
// NumberResult 数字转换结果
type NumberResult[ResultType op_type.Number] struct {
Value ResultType `json:"value"`
Err error `json:"err"`
}
// NumberPtrResult 数字指针转换结果
type NumberPtrResult[ResultType op_type.Number] struct {
Value *ResultType `json:"value"`
Err error `json:"err"`
}

132
float.go Normal file
View File

@ -0,0 +1,132 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 14:33
package wrapper
import (
"fmt"
"math"
)
// Float ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:33 2023/5/5
type Float float64
// ToFloat32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:36 2023/5/5
func (f Float) ToFloat32() Float32Result {
res := Float32Result{
Value: 0,
Err: nil,
}
if f > math.MaxFloat32 || f < math.SmallestNonzeroFloat32 {
res.Err = fmt.Errorf("float32 should between %v and %v", float32(math.SmallestNonzeroFloat32), float32(math.MaxFloat32))
return res
}
res.Value = float32(f)
return res
}
// ToFloat32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:10 2023/5/16
func (f Float) ToFloat32Ptr() Float32PtrResult {
res := f.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 : 14:36 2023/5/5
func (f Float) ToFloat64() Float64Result {
res := Float64Result{
Value: 0,
Err: nil,
}
if f > math.MaxFloat64 || f < math.SmallestNonzeroFloat64 {
res.Err = fmt.Errorf("float32 should between %v and %v", float64(math.SmallestNonzeroFloat64), float64(math.MaxFloat64))
return res
}
res.Value = float64(f)
return res
}
// ToFloat64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:11 2023/5/16
func (f Float) ToFloat64Ptr() Float64PtrResult {
res := f.ToFloat64()
if nil != res.Err {
return Float64PtrResult{
Value: nil,
Err: res.Err,
}
}
return Float64PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:38 2023/5/5
func (f Float) ToString() StringResult {
floatVal := f.ToFloat64()
if nil != floatVal.Err {
return StringResult{
Value: "",
Err: floatVal.Err,
}
}
return StringResult{
Value: fmt.Sprintf("%v", floatVal),
Err: nil,
}
}
// ToStringPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:56 2023/5/16
func (f Float) ToStringPtr() StringPtrResult {
res := f.ToString()
if nil != res.Err {
return StringPtrResult{
Value: nil,
Err: res.Err,
}
}
return StringPtrResult{
Value: &res.Value,
Err: nil,
}
}

29
go.mod
View File

@ -1,34 +1,29 @@
module git.zhangdeman.cn/zhangdeman/wrapper
go 1.24.0
go 1.21
toolchain go1.21.4
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772
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
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/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
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.17.3
)
require (
github.com/BurntSushi/toml v1.5.0 // indirect
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/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/match v1.1.1 // 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
)

67
go.sum
View File

@ -1,59 +1,58 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 h1:Yo1ur3LnDF5s7F7tpJsNrdUSF8LwYKnN9TdQU32F3eU=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240104123641-b3f23974e5d6 h1:ytpXTP3oxp480BAZQoOzqlBP4XP73NcpMplZ1/fA1lQ=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240104123641-b3f23974e5d6/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240419080457-9d9562469008 h1:6z99+X/B/G9sCZ+aTLYGWk3YLVVODzevA4wjWj9jvq0=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240419080457-9d9562469008/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc h1:kPz9xiUVruM8kwbUUVpxyCTX8pGgyKt60K5zX77oyC4=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608124542-4d97bd80dc68 h1:AaWKU0bKHnNot24OMhaOCBKtpfhz4o05DKHrRFgYd8M=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608124542-4d97bd80dc68/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMXOTy0NFKdodsGKA4gTNHJMfob3yRFYMXrZj7ek=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4 h1:mibnyzYbZullK0aTHVASHl3UeoVr8IgytQZsuyv+yEM=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240726024939-e424db29c5c4/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4 h1:93JYY8JLbFcrlq37q/uKyxs2r2e3modsjvfSbnZQ/UI=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
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/serialize v0.0.0-20240110090803-399e964daa0c h1:k7VCn9GfRGTilvdF/TcTFVMDBfKLe3VeGAtMTiDSnS0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240110090803-399e964daa0c/go.mod h1:w7kG4zyTJ1uPFaTWhze+OQuaUBINT2XnDxpyiM6ctc0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sbabiv/xml2map v1.2.1 h1:1lT7t0hhUvXZCkdxqtq4n8/ZCnwLWGq4rDuDv5XOoFE=
github.com/sbabiv/xml2map v1.2.1/go.mod h1:2TPoAfcaM7+Sd4iriPvzyntb2mx7GY+kkQpB/GQa/eo=
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
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.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=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/match v1.2.0 h1:0pt8FlkOwjN2fPt4bIl4BoNxb98gGHN2ObFEDkrfZnM=
github.com/tidwall/match v1.2.0/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
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=

255
int.go Normal file
View File

@ -0,0 +1,255 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 13:56
package wrapper
import (
"fmt"
"math"
"time"
)
// Int int类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:57 2023/5/5
type Int int64
// ToDuration ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:33 2023/9/4
func (i Int) ToDuration(timeUnit time.Duration) DurationResult {
return DurationResult{
Value: time.Duration(i.ToInt64().Value) * timeUnit,
Err: nil,
}
}
// ToInt8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:57 2023/5/5
func (i Int) ToInt8() Int8Result {
res := Int8Result{
Value: 0,
Err: nil,
}
if math.MaxInt8 < int64(i) || math.MinInt8 > int64(i) {
res.Err = fmt.Errorf("int8 value should between %v and %v ", int8(math.MinInt8), int8(math.MaxInt8))
return res
}
res.Value = int8(i)
return res
}
// ToInt8Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:08 2023/5/15
func (i Int) ToInt8Ptr() Int8PtrResult {
res := i.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 : 14:05 2023/5/5
func (i Int) ToInt16() Int16Result {
res := Int16Result{
Value: 0,
Err: nil,
}
if math.MaxInt16 < int64(i) || math.MinInt16 > int64(i) {
res.Err = fmt.Errorf("int16 value should between %v and %v ", int16(math.MinInt16), int16(math.MaxInt16))
return res
}
res.Value = int16(i)
return res
}
// ToInt16Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:07 2023/5/15
func (i Int) ToInt16Ptr() Int16PtrResult {
res := i.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 : 14:05 2023/5/5
func (i Int) ToInt32() Int32Result {
res := Int32Result{
Value: 0,
Err: nil,
}
if math.MaxInt32 < int64(i) || math.MinInt32 > int64(i) {
res.Err = fmt.Errorf("int32 value should between %v and %v ", int32(math.MinInt32), int32(math.MaxInt32))
return res
}
res.Value = int32(i)
return res
}
// ToInt32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:07 2023/5/15
func (i Int) ToInt32Ptr() Int32PtrResult {
res := i.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 : 14:06 2023/5/5
func (i Int) ToInt64() Int64Result {
res := Int64Result{
Value: 0,
Err: nil,
}
if math.MaxInt64 < i || math.MinInt64 > i {
res.Err = fmt.Errorf("int64 value should between %v and %v ", int64(math.MinInt64), int64(math.MaxInt64))
return res
}
res.Value = int64(i)
return res
}
// ToInt64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:05 2023/5/15
func (i Int) ToInt64Ptr() Int64PtrResult {
res := i.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 : 14:07 2023/5/5
func (i Int) ToInt() IntResult {
res := IntResult{
Value: 0,
Err: nil,
}
if math.MaxInt < i || math.MinInt > i {
res.Err = fmt.Errorf("int value should between %v and %v ", int(math.MinInt), int(math.MaxInt))
}
res.Value = int(i)
return res
}
// ToIntPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:52 2023/5/15
func (i Int) ToIntPtr() IntPtrResult {
intRes := i.ToInt()
if nil != intRes.Err {
return IntPtrResult{
Value: nil,
Err: intRes.Err,
}
}
return IntPtrResult{
Value: &intRes.Value,
Err: nil,
}
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:07 2023/5/5
func (i Int) ToString() StringResult {
result := i.ToInt64()
if nil != result.Err {
return StringResult{
Value: "",
Err: result.Err,
}
}
return StringResult{
Value: fmt.Sprintf("%v", result.Value),
Err: nil,
}
}
// ToStringPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2023/5/15
func (i Int) ToStringPtr() StringPtrResult {
result := i.ToString()
if nil != result.Err {
return StringPtrResult{
Value: nil,
Err: result.Err,
}
}
return StringPtrResult{
Value: &result.Value,
Err: nil,
}
}

View File

@ -1,8 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-10-14 11:52
package wrapper

View File

@ -1,67 +1,95 @@
// Package op_map ...
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-08-10 15:01
package op_map
package wrapper
import (
"encoding/json"
"errors"
"reflect"
"git.zhangdeman.cn/zhangdeman/easymap"
"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
return easymap.NewNormal(), nil
}
m := easymap.NewNormal()
reflectType := reflect.TypeOf(mapData)
if reflectType.Kind() != reflect.Map {
mapFormatData := make(map[string]any)
if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByteIgnoreError(mapData), &mapFormatData); nil != err {
return mapFormatData, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByte(mapData), &mapFormatData); nil != err {
return m, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
}
return mapFormatData, nil
mapData = mapFormatData
}
m := Map(map[string]any{})
reflectValue := reflect.ValueOf(mapData).MapRange()
for reflectValue.Next() {
// 循环提取相关值
_ = m.Set(reflectValue.Key().String(), reflectValue.Value().Interface())
m.Set(reflectValue.Key().Interface(), reflectValue.Value().Interface())
}
return m, nil
}
// 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{})
res := easymap.NewNormal()
jsonRes := gjson.Parse(string(data))
jsonRes.ForEach(func(key, value gjson.Result) bool {
_ = res.Set(key.String(), value.Value())
res.Set(key.Value(), value.Value())
return true
})
return res
}
// Map ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:14 2023/8/10
type Map easymap.EasyMap

147
object.go Normal file
View File

@ -0,0 +1,147 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-01 18:33
package wrapper
import (
"encoding/json"
"errors"
"git.zhangdeman.cn/zhangdeman/serialize"
"reflect"
)
// ObjectData 对象类型, 支持 nil / Map / Struct
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:36 2023/6/1
func ObjectData(data any) *ObjectType {
ot := &ObjectType{
source: data,
data: map[any]any{},
byteData: []byte{},
isValid: true,
invalidErr: errors.New("data is invalid"),
}
if nil == ot {
return ot
}
reflectType := reflect.TypeOf(data)
switch reflectType.Kind() {
case reflect.Map:
fallthrough
case reflect.Struct:
ot.byteData, _ = json.Marshal(ot.source)
default:
// 数据类型不是 nil / map / struct 之一
ot.isValid = false
}
return ot
}
// ObjectType ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:38 2023/6/1
type ObjectType struct {
source any
data map[any]any
byteData []byte
isValid bool
invalidErr error
}
// IsValid 是否有效对象数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:49 2023/6/1
func (ot *ObjectType) IsValid() bool {
return ot.isValid
}
// IsNil 是否为nil
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:50 2023/6/1
func (ot *ObjectType) IsNil() bool {
return ot.source == nil
}
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:51 2023/6/1
func (ot *ObjectType) ToString() StringResult {
if ot.IsNil() {
return StringResult{
Value: "nil",
Err: nil,
}
}
if !ot.IsValid() {
// 非法对象数据
return StringResult{
Value: "",
Err: errors.New("data is invalid"),
}
}
return StringResult{
Value: string(ot.byteData),
Err: nil,
}
}
// ToMapStringAny ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:17 2023/6/2
func (ot *ObjectType) ToMapStringAny() ObjectResult {
res := ObjectResult{
Value: map[string]any{},
Err: nil,
}
if ot.IsNil() {
return res
}
res.Err = serialize.JSON.UnmarshalWithNumber(ot.byteData, &res.Value)
return res
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:41 2023/6/2
func (ot *ObjectType) ToStruct(receiver any) error {
if nil == receiver {
return errors.New("receiver is nil")
}
if ot.IsNil() {
return errors.New("data is nil")
}
return serialize.JSON.UnmarshalWithNumber(ot.byteData, receiver)
}
// ToStructIgnoreErr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:42 2023/6/2
func (ot *ObjectType) ToStructIgnoreErr(receiver any) {
if nil == receiver {
return
}
if ot.IsNil() {
return
}
_ = serialize.JSON.UnmarshalWithNumber(ot.byteData, receiver)
}

View File

@ -1,133 +0,0 @@
// Package op_array ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-11 21:02
package op_array
import (
"encoding/json"
"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 数组实例
func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
at := &Array[Bt]{
value: value,
}
return at
}
// Array ...
type Array[Bt op_type.BaseType] struct {
value []Bt
convertErr error
itemType reflect.Kind // 简单list场景下, 每一项的数据类型
}
// IsNil 输入是否为nil
func (a *Array[Bt]) IsNil() bool {
return a.value == nil
}
// ToStringSlice ...
func (a *Array[Bt]) ToStringSlice() []string {
list := make([]string, 0)
for _, item := range a.value {
str := op_any.AnyDataType(item).ToString()
list = append(list, str)
}
return list
}
// Unique 对数据结果进行去重
func (a *Array[Bt]) Unique() []Bt {
result := make([]Bt, 0)
dataTable := make(map[string]bool)
for _, item := range a.value {
byteData, _ := json.Marshal(item)
k := string(byteData)
if strings.HasPrefix(k, "\"\"") && strings.HasSuffix(k, "\"\"") {
k = string(byteData[1 : len(byteData)-1])
}
if _, exist := dataTable[k]; exist {
continue
}
dataTable[k] = true
result = append(result, item)
}
return result
}
// Has 查询一个值是否在列表里, 在的话, 返回首次出现的索引, 不在返回-1
func (a *Array[Bt]) Has(input Bt) int {
for idx := 0; idx < len(a.value); idx++ {
if reflect.TypeOf(a.value[idx]).String() != reflect.TypeOf(input).String() {
// 类型不同
continue
}
sourceByte, _ := json.Marshal(a.value[idx])
inputByte, _ := json.Marshal(input)
if string(sourceByte) != string(inputByte) {
continue
}
return idx
}
return -1
}
// ToString ...
func (a *Array[Bt]) ToString() define.BaseValueResult[string] {
if a.IsNil() {
return define.BaseValueResult[string]{
Value: "",
Err: nil,
}
}
byteData, err := json.Marshal(a.value)
return define.BaseValueResult[string]{
Value: string(byteData),
Err: err,
}
}
// ToStringWithSplit 数组按照指定分隔符转为字符串
func (a *Array[Bt]) ToStringWithSplit(split string) define.BaseValueResult[string] {
if a.IsNil() {
return define.BaseValueResult[string]{
Value: "",
Err: nil,
}
}
return define.BaseValueResult[string]{
Value: strings.Join(a.ToStringSlice(), split),
Err: nil,
}
}
// ExtraField 提取[]map/[]struct 中的指定字段, 并以list形式返回
func (a *Array[Bt]) ExtraField(fieldName string) string {
if a.IsNil() {
return "[]"
}
byteData, _ := json.Marshal(a.value)
res := make([]any, 0)
list := gjson.ParseBytes(byteData).Array()
for _, item := range list {
itemValue := item.Get(fieldName)
if itemValue.Exists() {
res = append(res, itemValue.Value())
}
}
return a.ToString().Value
}

View File

@ -1,289 +0,0 @@
// Package op_map ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-06 18:27
package op_map
import (
"errors"
"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{}
type Map map[string]any
func (m Map) lock() {
mapLock.Lock()
}
func (m Map) unlock() {
mapLock.Unlock()
}
func (m Map) rLock() {
mapLock.RLock()
}
func (m Map) rUnlock() {
mapLock.RUnlock()
}
// Exist key是否存在
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:34 2024/11/6
func (m Map) Exist(key string) bool {
if m.IsNil() {
return false
}
m.rLock()
defer m.rUnlock()
_, exist := m[key]
return exist
}
// Set 设置map的值, 字段如果已存在, 会覆盖
//
// 参数说明:
// - field : 摇摆存的字段
// - value : 字段对应的值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:16 2024/11/19
func (m Map) Set(field string, value any) error {
if m.IsNil() {
return errors.New("Map is nil")
}
m.lock()
defer m.unlock()
m[field] = value
return nil
}
// Del 删除指定的字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:21 2024/11/19
func (m Map) Del(field string) {
if m.IsNil() {
return
}
m.lock()
defer m.unlock()
delete(m, field)
}
// IsNil 判断map是否为nil
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:22 2024/11/19
func (m Map) IsNil() bool {
if nil == m {
return true
}
return reflect.ValueOf(m).IsNil()
}
// Get ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:41 2024/11/19
func (m Map) Get(field string) (any, error) {
if m.IsNil() {
return nil, errors.New("map is nil")
}
m.rLock()
defer m.rUnlock()
val, exist := m[field]
if !exist {
return nil, errors.New(field + " : field not found")
}
return val, nil
}
// GetDefault 获取指定字段, 不存在则设置默认值
//
// 参数说明:
// - field : 要读取的字段
// - defaultValue : 字段不存在返回的默认值
// - allowNil : 字段存在, 但是值为Nil, 是否是一个合法值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:59 2024/11/19
func (m Map) GetDefault(field string, defaultValue any, allowNil bool) any {
if m.IsNil() {
return defaultValue
}
m.rLock()
defer m.rUnlock()
val, exist := m[field]
if !exist {
return defaultValue
}
if nil == val {
if allowNil {
return val
}
return defaultValue
}
return val
}
// Value 获取数据值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:39 2024/11/6
func (m Map) Value() map[string]any {
if m.IsNil() {
return nil
}
return m
}
// Clone 克隆数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:40 2024/11/6
func (m Map) Clone() Map {
newData := map[string]any{}
if m.IsNil() {
return newData
}
m.rLock()
defer m.rUnlock()
mapValue := m.Value()
for k, v := range mapValue {
newData[k] = v
}
return newData
}
// Filter 过滤指定字段
//
// 参数说明:
// - fieldList : 要保留的字段列表
// - ignoreNotFound : 指定字段不存在是否忽略,如不忽略, 字段不存在, 将会报错
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:40 2024/11/19
func (m Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, error) {
res := make(map[string]any)
for _, itemField := range fieldList {
if val, err := m.Get(itemField); err == nil {
res[itemField] = val
} else {
if !ignoreNotFound {
return nil, err
}
}
}
return res, nil
}
// FilterDefault 过滤指定字段, 字段不存储在则用默认值填充
//
// 参数说明:
// - fieldMap : 查询字段表, key为要查询的字段, value为 字段不存在时返回的默认值
// - allowNil : 字段存在, 三只值为你来是否是一个合法值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:07 2024/11/19
func (m Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]any {
res := make(map[string]any)
for itemField, fieldDefaultValue := range fieldMap {
res[itemField] = m.GetDefault(itemField, fieldDefaultValue, allowNil)
}
return res
}
// MarshalJSON Map序列化
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:35 2024/11/19
func (m Map) MarshalJSON() ([]byte, error) {
mapData := m.Value()
if nil == mapData {
return nil, nil
}
return serialize.JSON.MarshalForByte(mapData)
}
// ToString 序列化成字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:24 2024/11/21
func (m Map) ToString() string {
byteData, _ := m.MarshalJSON()
return string(byteData)
}
// GetString 获取字符串结果
//
// 参数说明:
// - field : 要查找的字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:15 2024/11/21
func (m Map) GetString(field string) (string, error) {
val, err := m.Get(field)
if nil != err {
return "", err
}
return op_any.AnyDataType(val).ToString(), nil
}
// GetInt64 获取Int64值
//
// 参数说明:
// - field : 要查找的字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:18 2024/11/21
func (m Map) GetInt64(field string) (int64, error) {
val, err := m.Get(field)
if nil != err {
return 0, err
}
int64Res := op_string.ToBaseTypeValue[int64](op_any.AnyDataType(val).ToString())
return int64Res.Value, int64Res.Err
}
// GetFloat64 获取float64值
//
// 参数说明:
// - field : 要查找的字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:18 2024/11/21
func (m Map) GetFloat64(field string) (float64, error) {
val, err := m.Get(field)
if nil != err {
return 0, err
}
float64Res := op_string.ToBaseTypeValue[float64](op_any.AnyDataType(val).ToString())
return float64Res.Value, float64Res.Err
}

View File

@ -1,86 +0,0 @@
// Package op_number ...
//
// Description : op_number ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-10-14 10:14
package op_number
import (
"git.zhangdeman.cn/zhangdeman/op_type"
"git.zhangdeman.cn/zhangdeman/wrapper/convert"
"git.zhangdeman.cn/zhangdeman/wrapper/define"
)
// ToNumber ...
func ToNumber[InputType op_type.BaseType, ResultType op_type.Number](in InputType) define.NumberResult[ResultType] {
var (
res ResultType
err error
)
if err = convert.ConvertAssign(&res, in); err != nil {
return define.NumberResult[ResultType]{
Err: err,
Value: res,
}
}
return define.NumberResult[ResultType]{
Err: nil,
Value: res,
}
}
// ToNumberPtr 数字指针
func ToNumberPtr[InputType op_type.BaseType, ResultType op_type.Number](in InputType) define.NumberPtrResult[ResultType] {
var (
res define.NumberResult[ResultType]
)
res = ToNumber[InputType, ResultType](in)
if nil != res.Err {
return define.NumberPtrResult[ResultType]{
Err: res.Err,
Value: nil,
}
}
return define.NumberPtrResult[ResultType]{
Err: nil,
Value: &res.Value,
}
}
// ToString 转换为字符串
func ToString[InputType op_type.Number](in InputType) define.BaseValueResult[string] {
var (
err error
res string
)
if err = convert.ConvertAssign(&res, in); err != nil {
return define.BaseValueResult[string]{
Value: res,
Err: err,
}
}
return define.BaseValueResult[string]{
Value: res,
Err: nil,
}
}
// ToStringPtr 字符串指针
func ToStringPtr[InputType op_type.Number](in InputType) define.BaseValuePtrResult[string] {
var (
res define.BaseValueResult[string]
)
res = ToString[InputType](in)
if nil != res.Err {
return define.BaseValuePtrResult[string]{
Err: res.Err,
Value: nil,
}
}
return define.BaseValuePtrResult[string]{
Err: nil,
Value: &res.Value,
}
}

View File

@ -1,50 +0,0 @@
// 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,
}
}

View File

@ -1,49 +0,0 @@
// 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)
})
}

View File

@ -1,48 +0,0 @@
// 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,
}
}

View File

@ -1,76 +0,0 @@
// 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)
})
}

View File

@ -1,57 +0,0 @@
// 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
}

View File

@ -1,76 +0,0 @@
// 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)
})
}

View File

@ -1,154 +0,0 @@
// 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.BaseValueResult[string] {
h := md5.New()
_, err := io.WriteString(h, str)
if nil != err {
return define.BaseValueResult[string]{
Value: "",
Err: err,
}
}
return define.BaseValueResult[string]{
Value: hex.EncodeToString(h.Sum(nil)),
Err: nil,
}
}
// RandomMd5 生成随机字符串MD%值
func RandomMd5() define.BaseValueResult[string] {
str := Random(64, "")
return Md5(str)
}
// 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,79 +0,0 @@
// 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)
}

1190
string.go Normal file

File diff suppressed because it is too large Load Diff

31
string_test.go Normal file
View File

@ -0,0 +1,31 @@
// 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,26 +1,32 @@
// Package op_struct ...
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-08-10 16:05
package op_struct
package wrapper
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")
@ -36,11 +42,28 @@ 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
func (s *Struct) ToMap() op_map.Map {
return op_map.EasyMap(s.data)
//
// 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,
}
}

157
ternary_operator.go Normal file
View File

@ -0,0 +1,157 @@
// 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

@ -1,11 +1,11 @@
// Package op_time ...
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-08-09 18:22
package op_time
package wrapper
import (
"fmt"
@ -13,11 +13,17 @@ import (
)
const (
// StandTimeFormat 标准时间格式
StandTimeFormat = "2006-01-02 15:04:05"
// StandISO8601Time 标准ISO时间格式
StandISO8601Time = "2006-01-02T15:04:05+08:00"
)
// OwnTime ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:28 2023/8/9
func OwnTime(inputTime time.Time) *Time {
instance := &Time{}
instance.Time = inputTime
@ -25,35 +31,59 @@ func OwnTime(inputTime time.Time) *Time {
}
// OwnTimeFromNow 从当前时间获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:44 2023/8/10
func OwnTimeFromNow() *Time {
return OwnTime(time.Now())
}
// OwnTimeFromSecond 从秒获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:47 2023/8/9
func OwnTimeFromSecond(second int64) *Time {
t := time.UnixMilli(second * 1000)
return OwnTime(t)
}
// OwnTimeFromMilli 从ms获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:56 2023/8/9
func OwnTimeFromMilli(milli int64) *Time {
t := time.UnixMilli(milli)
return OwnTime(t)
}
// OwnTimeFromMicro 从微秒获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:00 2023/8/9
func OwnTimeFromMicro(micro int64) *Time {
t := time.UnixMicro(micro)
return OwnTime(t)
}
// OwnTimeFromNano 从纳秒获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:00 2023/8/9
func OwnTimeFromNano(nano int64) *Time {
t := time.Unix(nano/1e9, nano%1e9)
return OwnTime(t)
}
// OwnTimeFromISO8601Time ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:02 2023/8/10
func OwnTimeFromISO8601Time(parseTime string, layout ...string) *Time {
var (
err error
@ -68,38 +98,66 @@ func OwnTimeFromISO8601Time(parseTime string, layout ...string) *Time {
}
// Time 时间类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:23 2023/8/9
type Time struct {
time.Time
}
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 1:34 上午 2021/10/7
func (t *Time) GetCurrentFormatTime(layout ...string) string {
return time.Now().In(time.Local).Format(t.getTimeFormat(time.DateTime, layout...))
return time.Now().In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
}
// FormatUnixNano 格式化纳秒时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/14
func (t *Time) FormatUnixNano(layout ...string) string {
nano := t.UnixNano() % 1e6
return t.FormatUnixMilli(layout...) + fmt.Sprintf(" %v", nano)
}
// FormatUnixMilli 格式化毫秒时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:55 2022/7/14
func (t *Time) FormatUnixMilli(layout ...string) string {
return time.UnixMilli(t.UnixMilli()).In(time.Local).
Format(t.getTimeFormat(time.DateTime, layout...)) + fmt.Sprintf(".%v", t.UnixMilli()%1000)
Format(t.getTimeFormat(StandTimeFormat, layout...)) + fmt.Sprintf(".%v", t.UnixMilli()%1000)
}
// FormatUnixSec 格式化秒级时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:06 2022/7/14
func (t *Time) FormatUnixSec(layout ...string) string {
return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(time.DateTime, layout...))
return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
}
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:16 2023/8/10
func (t *Time) ToString(layout ...string) string {
return t.FormatUnixSec(layout...)
}
// getTimeFormat 获取时间格式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:37 2023/8/9
func (t *Time) getTimeFormat(defaultFormat string, layout ...string) string {
if len(layout) > 0 && len(layout[0]) > 0 {
return layout[0]

View File

@ -9,10 +9,8 @@ package define
import (
"fmt"
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/wrapper/op_map"
"git.zhangdeman.cn/zhangdeman/wrapper"
"reflect"
"strings"
)
@ -63,7 +61,7 @@ func NewDiffOption() *DiffOption {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:06 2024/3/8
type CustomDiffFunc func(field string, inputVal op_map.Map, storageVal op_map.Map, option *DiffOption) *DiffResult
type CustomDiffFunc func(field string, inputVal wrapper.Map, storageVal wrapper.Map, option *DiffOption) *DiffResult
// DiffResult 对比结果
//
@ -124,7 +122,7 @@ func IsSupportValueType(kind reflect.Kind) bool {
// Author : zhangdeman001@ke.com<张德满>
//
// Date : 12:05 2024/3/8
func DefaultDiffFunc(field string, inputVal op_map.Map, storageVal op_map.Map, option *DiffOption) *DiffResult {
func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map, option *DiffOption) *DiffResult {
if nil == option {
option = NewDiffOption()
}
@ -138,21 +136,21 @@ func DefaultDiffFunc(field string, inputVal op_map.Map, storageVal op_map.Map, o
}
var (
inputFieldVal any
inputFieldValExist error
inputFieldValExist bool
storageFieldVal any
storageFieldValExist error
storageFieldValExist bool
)
inputFieldVal, inputFieldValExist = inputVal.Get(field)
storageFieldVal, storageFieldValExist = storageVal.Get(field)
// 字段在输入数据和存储数据中均不存在
if nil != inputFieldValExist && nil != storageFieldValExist {
if !inputFieldValExist && !storageFieldValExist {
// 输入和存储都没这个字段
return result
}
// 判断输入字段是否存在
if nil != inputFieldValExist {
if !inputFieldValExist {
if option.IgnoreNotFoundField {
// 忽略不存在的字段
return result
@ -164,7 +162,7 @@ func DefaultDiffFunc(field string, inputVal op_map.Map, storageVal op_map.Map, o
return result
}
// 判断存储字段是否存在
if nil != storageFieldValExist {
if !storageFieldValExist {
result.IsSame = false
result.DiffReason = DiffReasonStorageFieldNotFound
result.NewVal = inputFieldVal

View File

@ -8,7 +8,7 @@
package tool
import (
"git.zhangdeman.cn/zhangdeman/wrapper/op_map"
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/wrapper/tool/define"
)
@ -20,7 +20,11 @@ type diff struct {
}
// Compare 比较两个数据源的指定字段
func (d *diff) Compare(fieldList []string, input op_map.Map, storage op_map.Map, option *define.DiffOption) map[string]*define.DiffResult {
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:53 2024/3/8
func (d *diff) Compare(fieldList []string, input wrapper.Map, storage wrapper.Map, option *define.DiffOption) map[string]*define.DiffResult {
if nil == option {
option = define.NewDiffOption()
}
@ -32,7 +36,11 @@ func (d *diff) Compare(fieldList []string, input op_map.Map, storage op_map.Map,
}
// CompareSingle 比较一个字段
func (d *diff) CompareSingle(field string, input op_map.Map, storage op_map.Map, option *define.DiffOption) *define.DiffResult {
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:57 2024/3/8
func (d *diff) CompareSingle(field string, input wrapper.Map, storage wrapper.Map, option *define.DiffOption) *define.DiffResult {
if nil == option {
option = define.NewDiffOption()
}
@ -44,7 +52,11 @@ func (d *diff) CompareSingle(field string, input op_map.Map, storage op_map.Map,
}
// IsSame 判断连个数据是否一致
func (d *diff) IsSame(fieldList []string, input op_map.Map, storage op_map.Map, option *define.DiffOption) bool {
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:16 2024/3/11
func (d *diff) IsSame(fieldList []string, input wrapper.Map, storage wrapper.Map, option *define.DiffOption) bool {
res := d.Compare(fieldList, input, storage, option)
for _, item := range res {
if item.IsSame {

View File

@ -8,8 +8,7 @@
package tool
import (
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
"git.zhangdeman.cn/zhangdeman/wrapper/op_ternary"
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/wrapper/tool/define"
)
@ -21,9 +20,13 @@ type version struct {
}
// getVersionArr 解析版本号
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:42 2023/12/27
func (v *version) getVersionArr(versionOne string, versionTwo string) ([]int64, []int64, error) {
oneWrapper := op_string.ToBaseTypeSlice[int64](versionOne, ".")
twoWrapper := op_string.ToBaseTypeSlice[int64](versionTwo, ".")
oneWrapper := wrapper.String(versionOne).ToInt64Slice(".")
twoWrapper := wrapper.String(versionTwo).ToInt64Slice(".")
if oneWrapper.Err != nil {
return nil, nil, oneWrapper.Err
}
@ -35,7 +38,11 @@ func (v *version) getVersionArr(versionOne string, versionTwo string) ([]int64,
// Compare 比较版本号的大小, 版本号格式必须是 x.y.z 的形式,几个 . 不限制, x、y、z 必须是是数字
//
// - strictMode 严格模式, 非严格模式下, 2.4 == 2.4.0 , 开启严格模式, 则认为 2.4 < 2.4.0 , 因为 2.4 没有小版本号
// strictMode 严格模式, 非严格模式下, 2.4 == 2.4.0 , 开启严格模式, 则认为 2.4 < 2.4.0 , 因为 2.4 没有小版本号
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:07 2023/12/27
func (v *version) Compare(versionOne string, versionTwo string, strictMode bool) (int, error) {
oneVersionArr, twoVersionArr, err := v.getVersionArr(versionOne, versionTwo)
if nil != err {
@ -73,14 +80,18 @@ func (v *version) Compare(versionOne string, versionTwo string, strictMode bool)
if !strictMode || oneVersionLength == twoVersionLength {
return define.VersionEqual, nil
}
return op_ternary.BaseType[int](
return wrapper.TernaryOperator.Int(
oneVersionLength > twoVersionLength,
define.VersionOneMax,
define.VersionTwoMax,
), nil
wrapper.Int(define.VersionOneMax),
wrapper.Int(define.VersionTwoMax),
).ToInt().Value, nil
}
// CompareIgnoreError 忽略error
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:20 2023/12/27
func (v *version) CompareIgnoreError(versionOne string, versionTwo string, strictMode bool) int {
res, _ := v.Compare(versionOne, versionTwo, strictMode)
return res

242
uint.go Normal file
View File

@ -0,0 +1,242 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 14:20
package wrapper
import (
"fmt"
"math"
)
// Uint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:20 2023/5/5
type Uint uint64
// ToUint8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:21 2023/5/5
func (ui Uint) ToUint8() Uint8Result {
res := Uint8Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint8 || ui < 0 {
res.Err = fmt.Errorf("uint8 should between 0 and %v", uint8(math.MaxUint8))
return res
}
res.Value = uint8(ui)
return res
}
// ToUint8Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/16
func (ui Uint) ToUint8Ptr() Uint8PtrResult {
res := ui.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 : 14:25 2023/5/5
func (ui Uint) ToUint16() Uint16Result {
res := Uint16Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint16 || ui < 0 {
res.Err = fmt.Errorf("uint16 should between 0 and %v", uint16(math.MaxUint16))
return res
}
res.Value = uint16(ui)
return res
}
// ToUint16Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/16
func (ui Uint) ToUint16Ptr() Uint16PtrResult {
res := ui.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 : 14:25 2023/5/5
func (ui Uint) ToUint32() Uint32Result {
res := Uint32Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint32 || ui < 0 {
res.Err = fmt.Errorf("uint32 should between 0 and %v", uint32(math.MaxUint32))
return res
}
res.Value = uint32(ui)
return res
}
// ToUint32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/16
func (ui Uint) ToUint32Ptr() Uint32PtrResult {
res := ui.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 : 14:30 2023/5/5
func (ui Uint) ToUint64() Uint64Result {
res := Uint64Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint64 || ui < 0 {
res.Err = fmt.Errorf("uint64 should between 0 and %v", uint64(math.MaxUint64))
}
res.Value = uint64(ui)
return res
}
// ToUint64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/16
func (ui Uint) ToUint64Ptr() Uint64PtrResult {
res := ui.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 : 14:31 2023/5/5
func (ui Uint) ToUint() UintResult {
res := UintResult{
Value: 0,
Err: nil,
}
if ui > math.MaxUint || ui < 0 {
res.Err = fmt.Errorf("uint should between 0 and %v", uint(math.MaxUint))
return res
}
res.Value = uint(ui)
return res
}
// ToUintPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/16
func (ui Uint) ToUintPtr() UintPtrResult {
res := ui.ToUint()
if nil != res.Err {
return UintPtrResult{
Value: nil,
Err: res.Err,
}
}
return UintPtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:32 2023/5/5
func (ui Uint) ToString() StringResult {
uint64Val := ui.ToUint64()
if nil != uint64Val.Err {
return StringResult{
Value: "",
Err: uint64Val.Err,
}
}
return StringResult{
Value: fmt.Sprintf("%v", uint64Val),
Err: nil,
}
}
// ToStringPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:59 2023/5/16
func (ui Uint) ToStringPtr() StringPtrResult {
res := ui.ToString()
if nil != res.Err {
return StringPtrResult{
Value: nil,
Err: res.Err,
}
}
return StringPtrResult{
Value: &res.Value,
Err: nil,
}
}