36 Commits

Author SHA1 Message Date
309fcf134f Merge pull request 'feature/upgrade_t' (#11) from feature/upgrade_t into master
Reviewed-on: #11
2025-10-14 11:49:47 +08:00
7dc9cb7ce6 feat: update array && struct 2025-10-14 11:49:07 +08:00
eb6124e3f7 feat: 迁移数字、时间操作 2025-10-14 10:32:29 +08:00
bab05e7d6b migrate: 迁移 time 相关操作 2025-10-14 09:54:17 +08:00
fbc2c162d5 feat: 生成随机md5值 2025-10-14 09:12:28 +08:00
78605e0253 fix: 修复 op_arrat.Has 2025-10-13 23:15:43 +08:00
97afbebbe0 feat: 升级op_array 2025-10-13 23:12:06 +08:00
d57d32b103 fix: fix pkg tool 2025-10-13 17:41:28 +08:00
e162029506 fix: cycle import 2025-10-13 17:36:21 +08:00
e826fec633 fix: cycle import 2025-10-13 17:35:28 +08:00
1e7b658168 fix: op_any 2025-10-13 17:33:47 +08:00
fa9097dc09 Merge pull request '数据操作升级为基于泛型实现' (#10) from feature/support_genericity into master
Reviewed-on: #10
2025-10-13 17:10:53 +08:00
50d2d6c7bb feat: 相关操作升级为泛型实现 2025-10-13 17:05:00 +08:00
80b5e4e7cc feat: 字符串转struct slice 2025-10-13 15:21:33 +08:00
033e013510 feat: 基础数据类型字符串转数组 2025-10-13 14:42:54 +08:00
efab8cb6d2 feat: str转struct 2025-10-13 14:20:40 +08:00
2100caa5e4 feat: 增加map类型转换 2025-10-13 12:52:01 +08:00
059830d87d feat: ToBasePtrValue增加单元测试 2025-10-13 12:00:51 +08:00
20d2912274 feat: 字符串 -> 基础数据类型转换, 使用泛型实现 2025-10-13 11:40:08 +08:00
460672bcd0 feat: remove doc && update json tag 2025-10-13 11:17:53 +08:00
0dc9dba4c6 upgrade: update go mod 2025-10-13 10:33:32 +08:00
1cbfbe9597 Merge pull request 'feature/upgrade_dynamic_struct' (#9) from feature/upgrade_dynamic_struct into master
Reviewed-on: #9
2025-03-21 18:27:12 +08:00
8de0d9b942 升级consts pkg 2025-03-21 18:26:46 +08:00
2c73b9c034 修复map数据key错误 + 获取map数据方法 2025-03-18 18:20:59 +08:00
beb8c74e8a update go mod 2025-03-18 16:44:03 +08:00
3167ea0f50 merge master 2025-03-18 16:43:43 +08:00
c1588abcb4 Merge pull request '优化数组字符串处理' (#8) from feature/fix_arr into master
Reviewed-on: #8
2025-03-02 21:34:17 +08:00
34c8cf04fa update go mod 2025-03-02 21:33:55 +08:00
d5622fe0ca 优化数组字符串处理 2025-03-02 21:33:16 +08:00
743acb53f2 优化动态结构体生成 2025-02-07 12:20:52 +08:00
981ff94388 动态结构体增加SetMapData方法 2025-01-24 18:51:16 +08:00
febf5a63ad 动态结构体增加自定义序列化方法 2025-01-24 18:48:24 +08:00
bbf5184314 update 2025-01-24 18:23:01 +08:00
c503187022 动态结构体支持赋值 2025-01-24 18:06:29 +08:00
c757e551a8 update go mod 2025-01-24 17:16:20 +08:00
7b56590b57 修复flot.ToString的BUg 2024-12-23 12:08:03 +08:00
35 changed files with 886 additions and 3227 deletions

View File

@@ -1,18 +0,0 @@
// 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{"1", 1, 1, "1", 2, 3}).Unique())
fmt.Println(ArrayType([]int{1, 1, 2, 3}).Unique())
}

View File

@@ -1,11 +1,11 @@
// Package wrapper ...
// Package bigint ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-19 16:33
package wrapper
package bigint
import (
"database/sql/driver"

View File

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

482
define.go
View File

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

70
define/result.go Normal file
View File

@@ -0,0 +1,70 @@
// 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"`
}

View File

@@ -1,198 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-21 16:43
package wrapper
import (
"fmt"
"reflect"
)
func NewDynamic() *DynamicStruct {
return &DynamicStruct{
structFieldList: make([]reflect.StructField, 0),
}
}
// DynamicStruct 动态生成数据结构
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:48 2024/8/21
type DynamicStruct struct {
structFieldList []reflect.StructField // 结构体字段列表
}
// AddInt 添加int字段统一Int64
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2024/8/21
func (ds *DynamicStruct) AddInt(fieldName string, fieldTag string, pkgPath string) {
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf(int64(0)),
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
})
}
// AddUint 添加uint字段, 统一 uint64
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:50 2024/8/21
func (ds *DynamicStruct) AddUint(fieldName string, fieldTag string, pkgPath string) {
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf(uint64(0)),
Tag: reflect.StructTag(fieldTag),
})
}
// AddString 添加字符串字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2024/8/21
func (ds *DynamicStruct) AddString(fieldName string, fieldTag string, pkgPath string) {
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf(""),
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
})
}
// AddBool 添加bool字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:49 2024/8/21
func (ds *DynamicStruct) AddBool(fieldName string, fieldTag string, pkgPath string) {
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf(true),
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
})
}
// AddFloat 添加float字段, 统一 float64
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:48 2024/8/21
func (ds *DynamicStruct) AddFloat(fieldName string, fieldTag string, pkgPath string) {
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf(float64(0)),
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
})
}
// AddSlice 添加slice
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:47 2024/8/21
func (ds *DynamicStruct) AddSlice(fieldName string, fieldTag string, pkgPath string) {
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf([]any{}),
Tag: reflect.StructTag(fieldTag),
})
}
// AddMap 添加map字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:43 2024/8/21
func (ds *DynamicStruct) AddMap(fieldName string, fieldTag string, pkgPath string) {
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf(map[string]any{}),
Tag: reflect.StructTag(fieldTag),
})
}
// AddAny 添加任意类型字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:52 2024/8/21
func (ds *DynamicStruct) AddAny(fieldName string, fieldTag string, pkgPath string, value any) {
if nil == value {
// 不能是空指针
return
}
ds.AddStructField(reflect.StructField{
Name: fieldName,
PkgPath: pkgPath,
Type: reflect.TypeOf(value),
Tag: reflect.StructTag(fieldTag),
})
}
// AddStructField 添加结构体字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:42 2024/8/21
func (ds *DynamicStruct) AddStructField(field reflect.StructField) {
if field.Tag == "" {
field.Tag = reflect.StructTag(fmt.Sprintf(`json:"%v"`, field.Name))
}
field.Name = String(field.Name).SnakeCaseToCamel() // 转成大驼峰, 保证对外可访问
ds.structFieldList = append(ds.structFieldList, field)
}
// GetStructType 获取结构体的类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:58 2024/8/21
func (ds *DynamicStruct) GetStructType() reflect.Type {
return reflect.StructOf(ds.structFieldList)
}
// ToStructDefaultValue 获取结构体的值, 并采用对应类型默认值填充相关字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:56 2024/8/21
func (ds *DynamicStruct) ToStructDefaultValue() any {
defer ds.Clear()
defaultValue := reflect.New(ds.GetStructType()).Elem().Interface()
return defaultValue
}
// ToStructDefaultSliceValue 自动生成结构体列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:04 2024/8/21
func (ds *DynamicStruct) ToStructDefaultSliceValue() any {
defer ds.Clear()
tSlice := reflect.MakeSlice(reflect.SliceOf(ds.GetStructType()), 0, 0)
return reflect.New(tSlice.Type()).Elem().Interface()
}
// Clear 清理
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:08 2024/8/21
func (ds *DynamicStruct) Clear() {
// 清理掉已设置的字段, 不然实例复用会互相影响
ds.structFieldList = make([]reflect.StructField, 0)
}

View File

@@ -1,27 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-08-21 17:56
package wrapper
import (
"fmt"
"github.com/mitchellh/mapstructure"
"testing"
)
func TestNewDynamic(t *testing.T) {
instance := NewDynamic()
instance.AddInt("Age", "age", "")
instance.AddString("Name", "name", "")
defaultVal := instance.ToStructDefaultValue()
testMap := map[string]any{
"name": "白茶",
"age": 18,
}
_ = mapstructure.Decode(testMap, &defaultVal)
fmt.Println(defaultVal)
}

132
float.go
View File

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

25
go.mod
View File

@@ -1,29 +1,34 @@
module git.zhangdeman.cn/zhangdeman/wrapper
go 1.21
toolchain go1.21.4
go 1.24.0
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc
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
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/mitchellh/mapstructure v1.5.0
github.com/smartystreets/goconvey v1.8.1
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.11.1
github.com/tidwall/gjson v1.18.0
)
require (
github.com/BurntSushi/toml v1.4.0 // indirect
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/tidwall/match v1.1.1 // 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
)

49
go.sum
View File

@@ -1,44 +1,59 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 h1:BiAlBJ+DuRs/xD7nDQD2JT8Oc+V+0Uwt36qZwdXGvzI=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9 h1:TP/M3WnGsxh0Vr6YuS1i28hw1oV//YbdCoI46PUBIA0=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
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/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/serialize v0.0.0-20241104092308-ecb02113459e h1:A045F67AMSqFKGD9kk2uLa+6c/zpmW8vjjSRmSsdjPs=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
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.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
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/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/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
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.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=
github.com/tidwall/gjson v1.18.0/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
View File

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

150
object.go
View File

@@ -1,150 +0,0 @@
// 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 {
if ot.source == nil {
return true
}
return reflect.ValueOf(ot.source).IsNil()
}
// 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,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 {
// 已经处理过的,无需在处理
@@ -87,40 +72,29 @@ func (at *AnyType) Type() consts.DataType {
case reflect.Bool:
return consts.DataTypeBool
case reflect.Float32, reflect.Float64:
return consts.DataTypeFloat
return consts.DataTypeFloat64
default:
return consts.DataTypeUnknown
}
}
// 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))
case consts.DataTypeSliceAny:
var val []any
_ = serialize.JSON.Transition(at.data, &val)
return String(ArrayType[any](val).ToString().Value)
case consts.DataTypeMapAnyAny:
easyMap := EasyMap(at.data)
return String(easyMap.ToString())
return fmt.Sprintf("%v", at.data)
case consts.DataTypeInt:
return String(Int(at.data.(int64)).ToString().Value)
fallthrough
case consts.DataTypeUint:
return String(Int(at.data.(uint)).ToString().Value)
case consts.DataTypeFloat:
return String(Float(at.data.(float64)).ToString().Value)
fallthrough
case consts.DataTypeFloat64:
fallthrough
case consts.DataTypeBool:
return String(fmt.Sprintf("%v", at.data))
return fmt.Sprintf("%v", at.data)
default:
return String(serialize.JSON.MarshalForStringIgnoreError(at.data))
return serialize.JSON.MarshalForStringIgnoreError(at.data)
}
}

View File

@@ -1,25 +1,24 @@
// Package wrapper ...
// Package op_array ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-11 21:02
package wrapper
package op_array
import (
"encoding/json"
"git.zhangdeman.cn/zhangdeman/op_type"
"github.com/tidwall/gjson"
"reflect"
"strings"
"git.zhangdeman.cn/zhangdeman/op_type"
"git.zhangdeman.cn/zhangdeman/wrapper/define"
"git.zhangdeman.cn/zhangdeman/wrapper/op_any"
"github.com/tidwall/gjson"
)
// ArrayType 数组实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:03 2023/6/11
func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
at := &Array[Bt]{
value: value,
@@ -28,10 +27,6 @@ func ArrayType[Bt op_type.BaseType](value []Bt) *Array[Bt] {
}
// Array ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:05 2023/6/11
type Array[Bt op_type.BaseType] struct {
value []Bt
convertErr error
@@ -39,38 +34,26 @@ type Array[Bt op_type.BaseType] struct {
}
// IsNil 输入是否为nil
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:11 2023/6/11
func (at *Array[Bt]) IsNil() bool {
return at.value == nil
func (a *Array[Bt]) IsNil() bool {
return a.value == nil
}
// ToStringSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:42 2024/4/22
func (at *Array[Bt]) ToStringSlice() []string {
func (a *Array[Bt]) ToStringSlice() []string {
list := make([]string, 0)
for _, item := range at.value {
byteData, _ := json.Marshal(item)
list = append(list, strings.Trim(string(byteData), "\""))
for _, item := range a.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 {
func (a *Array[Bt]) Unique() []Bt {
result := make([]Bt, 0)
dataTable := make(map[string]bool)
for _, item := range at.value {
for _, item := range a.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 (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(at.value[idx])
sourceByte, _ := json.Marshal(a.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 (a *Array[Bt]) ToString() define.BaseValueResult[string] {
if a.IsNil() {
return define.BaseValueResult[string]{
Value: "",
Err: nil,
}
}
byteData, err := json.Marshal(at.value)
return StringResult{
byteData, err := json.Marshal(a.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 (a *Array[Bt]) ToStringWithSplit(split string) define.BaseValueResult[string] {
if a.IsNil() {
return define.BaseValueResult[string]{
Value: "",
Err: nil,
}
}
return StringResult{
Value: strings.Join(at.ToStringSlice(), split),
return define.BaseValueResult[string]{
Value: strings.Join(a.ToStringSlice(), 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 (a *Array[Bt]) ExtraField(fieldName string) string {
if a.IsNil() {
return "[]"
}
byteData, _ := json.Marshal(at.value)
byteData, _ := json.Marshal(a.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 a.ToString().Value
}

View File

@@ -1,35 +1,28 @@
// Package wrapper ...
// Package op_map ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-08-10 15:01
package wrapper
package op_map
import (
"encoding/json"
"errors"
"reflect"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/tidwall/gjson"
"reflect"
)
// EasyMap ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:02 2023/8/10
func EasyMap(mapData any) Map {
m, _ := EasyMapWithError(mapData)
return m
}
// EasyMapWithError 转换map并带上转换的异常
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:06 2023/8/10
func EasyMapWithError(mapData any) (Map, error) {
if nil == mapData {
return map[string]any{}, nil
@@ -52,29 +45,17 @@ func EasyMapWithError(mapData any) (Map, error) {
}
// EasyMapFromStruct 从struct转map
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:11 2023/8/10
func EasyMapFromStruct(data any) Map {
byteData, _ := json.Marshal(data)
return EasyMapFromByte(byteData)
}
// EasyMapFromString 从string转为Map
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:12 2023/8/10
func EasyMapFromString(data string) Map {
return EasyMapFromByte([]byte(data))
}
// EasyMapFromByte 从字节数组转为Map
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:12 2023/8/10
func EasyMapFromByte(data []byte) Map {
res := Map(map[string]any{})
jsonRes := gjson.Parse(string(data))

View File

@@ -1,17 +1,20 @@
// Package wrapper ...
// Package op_map ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-06 18:27
package wrapper
package op_map
import (
"errors"
"git.zhangdeman.cn/zhangdeman/serialize"
"reflect"
"sync"
"git.zhangdeman.cn/zhangdeman/serialize"
"git.zhangdeman.cn/zhangdeman/wrapper/op_any"
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
)
var mapLock = &sync.RWMutex{}
@@ -248,7 +251,7 @@ func (m Map) GetString(field string) (string, error) {
if nil != err {
return "", err
}
return AnyDataType(val).ToString().Value(), nil
return op_any.AnyDataType(val).ToString(), nil
}
// GetInt64 获取Int64值
@@ -264,7 +267,7 @@ func (m Map) GetInt64(field string) (int64, error) {
if nil != err {
return 0, err
}
int64Res := AnyDataType(val).ToString().ToInt64()
int64Res := op_string.ToBaseTypeValue[int64](op_any.AnyDataType(val).ToString())
return int64Res.Value, int64Res.Err
}
@@ -281,6 +284,6 @@ func (m Map) GetFloat64(field string) (float64, error) {
if nil != err {
return 0, err
}
float64Res := AnyDataType(val).ToString().ToFloat64()
float64Res := op_string.ToBaseTypeValue[float64](op_any.AnyDataType(val).ToString())
return float64Res.Value, float64Res.Err
}

86
op_number/number.go Normal file
View File

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

50
op_string/base.go Normal file
View File

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

49
op_string/base_test.go Normal file
View File

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

48
op_string/map.go Normal file
View File

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

76
op_string/map_test.go Normal file
View File

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

57
op_string/slice.go Normal file
View File

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

76
op_string/slice_test.go Normal file
View File

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

154
op_string/tool.go Normal file
View File

@@ -0,0 +1,154 @@
// 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,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 (s *Struct) ToMap() op_map.Map {
return op_map.EasyMap(s.data)
}

View File

@@ -0,0 +1,79 @@
// Package op_ternary ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-11-28 16:05
package op_ternary
import (
"git.zhangdeman.cn/zhangdeman/op_type"
"git.zhangdeman.cn/zhangdeman/wrapper/op_any"
"git.zhangdeman.cn/zhangdeman/wrapper/op_map"
)
var (
// TernaryOperator 三元运算符操作实例
TernaryOperator = &ternaryOperator{}
)
// ternaryOperator ...
type ternaryOperator struct {
}
// CondFunc ...
type CondFunc func() bool
// defaultCondFunc ...
func defaultCondFunc() bool {
return false
}
// BaseType ...
func BaseType[Bt op_type.BaseType](cond bool, trueVal Bt, falseVal Bt) Bt {
if cond {
return trueVal
}
return falseVal
}
// BaseTypeWithFunc ...
func BaseTypeWithFunc[Bt op_type.BaseType](condFunc CondFunc, trueVal Bt, falseVal Bt) Bt {
if nil == condFunc {
condFunc = defaultCondFunc
}
return BaseType(condFunc(), trueVal, falseVal)
}
// Map ...
func Map(cond bool, trueVal op_map.Map, falseVal op_map.Map) op_map.Map {
if cond {
return trueVal
}
return falseVal
}
// MapWithFunc ...
func MapWithFunc(condFunc CondFunc, trueVal op_map.Map, falseVal op_map.Map) op_map.Map {
if nil == condFunc {
condFunc = defaultCondFunc
}
return Map(condFunc(), trueVal, falseVal)
}
// Any ...
func Any(cond bool, trueVal *op_any.AnyType, falseVal *op_any.AnyType) *op_any.AnyType {
if cond {
return trueVal
}
return falseVal
}
// AnyWithFunc ...
func AnyWithFunc(condFunc CondFunc, trueVal *op_any.AnyType, falseVal *op_any.AnyType) *op_any.AnyType {
if nil == condFunc {
condFunc = defaultCondFunc
}
return Any(condFunc(), trueVal, falseVal)
}

View File

@@ -1,11 +1,11 @@
// Package wrapper ...
// Package op_time ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-08-09 18:22
package wrapper
package op_time
import (
"fmt"
@@ -13,17 +13,11 @@ 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
@@ -31,59 +25,35 @@ 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
@@ -98,66 +68,38 @@ 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(StandTimeFormat, layout...))
return time.Now().In(time.Local).Format(t.getTimeFormat(time.DateTime, 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(StandTimeFormat, layout...)) + fmt.Sprintf(".%v", t.UnixMilli()%1000)
Format(t.getTimeFormat(time.DateTime, 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(StandTimeFormat, layout...))
return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(time.DateTime, 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]

1190
string.go

File diff suppressed because it is too large Load Diff

View File

@@ -1,31 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 13:39
package wrapper
import (
"fmt"
"testing"
)
func TestString_ToFloat32(t *testing.T) {
var str String
str = "12345.123"
fmt.Println(str)
fmt.Println(str.ToFloat32())
fmt.Println(str.ToFloat64())
fmt.Println(str.ToNumber())
fmt.Println(str.ToDouble())
fmt.Println(str.ToInt())
fmt.Println(str.ToUint())
}
func TestString_ToAnySlice(t *testing.T) {
str := "1,2,3"
r := String(str).ToAnySlice(",")
fmt.Println(r)
}

View File

@@ -1,157 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-11-28 16:05
package wrapper
var (
// TernaryOperator 三元运算符操作实例
TernaryOperator = &ternaryOperator{}
)
// ternaryOperator ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:06 2023/11/28
type ternaryOperator struct {
}
// CondFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:10 2023/11/28
type CondFunc func() bool
// defaultCondFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:11 2023/11/28
func defaultCondFunc() bool {
return false
}
// Int ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:10 2023/11/28
func (to *ternaryOperator) Int(cond bool, trueVal Int, falseVal Int) Int {
if cond {
return trueVal
}
return falseVal
}
// IntWithFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:11 2023/11/28
func (to *ternaryOperator) IntWithFunc(condFunc CondFunc, trueVal Int, falseVal Int) Int {
if nil == condFunc {
condFunc = defaultCondFunc
}
return to.Int(condFunc(), trueVal, falseVal)
}
// Float ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:10 2023/11/28
func (to *ternaryOperator) Float(cond bool, trueVal Float, falseVal Float) Float {
if cond {
return trueVal
}
return falseVal
}
// FloatWithFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:13 2023/11/28
func (to *ternaryOperator) FloatWithFunc(condFunc CondFunc, trueVal Float, falseVal Float) Float {
if nil == condFunc {
condFunc = defaultCondFunc
}
return to.Float(condFunc(), trueVal, falseVal)
}
// String ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:15 2023/11/28
func (to *ternaryOperator) String(cond bool, trueVal String, falseVal String) String {
if cond {
return trueVal
}
return falseVal
}
// StringWithFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:15 2023/11/28
func (to *ternaryOperator) StringWithFunc(condFunc CondFunc, trueVal String, falseVal String) String {
if nil == condFunc {
condFunc = defaultCondFunc
}
return to.String(condFunc(), trueVal, falseVal)
}
// Map ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:25 2023/11/28
func (to *ternaryOperator) Map(cond bool, trueVal Map, falseVal Map) Map {
if cond {
return trueVal
}
return falseVal
}
// MapWithFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:24 2023/11/28
func (to *ternaryOperator) MapWithFunc(condFunc CondFunc, trueVal Map, falseVal Map) Map {
if nil == condFunc {
condFunc = defaultCondFunc
}
return falseVal
}
// Any ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:26 2023/11/28
func (to *ternaryOperator) Any(cond bool, trueVal *AnyType, falseVal *AnyType) *AnyType {
if cond {
return trueVal
}
return falseVal
}
// AnyWithFunc ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:24 2023/11/28
func (to *ternaryOperator) AnyWithFunc(condFunc CondFunc, trueVal *AnyType, falseVal *AnyType) *AnyType {
if nil == condFunc {
condFunc = defaultCondFunc
}
return to.Any(condFunc(), trueVal, falseVal)
}

View File

@@ -9,8 +9,10 @@ package define
import (
"fmt"
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/wrapper/op_map"
"reflect"
"strings"
)
@@ -61,7 +63,7 @@ func NewDiffOption() *DiffOption {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:06 2024/3/8
type CustomDiffFunc func(field string, inputVal wrapper.Map, storageVal wrapper.Map, option *DiffOption) *DiffResult
type CustomDiffFunc func(field string, inputVal op_map.Map, storageVal op_map.Map, option *DiffOption) *DiffResult
// DiffResult 对比结果
//
@@ -122,7 +124,7 @@ func IsSupportValueType(kind reflect.Kind) bool {
// Author : zhangdeman001@ke.com<张德满>
//
// Date : 12:05 2024/3/8
func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map, option *DiffOption) *DiffResult {
func DefaultDiffFunc(field string, inputVal op_map.Map, storageVal op_map.Map, option *DiffOption) *DiffResult {
if nil == option {
option = NewDiffOption()
}

View File

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

View File

@@ -8,7 +8,8 @@
package tool
import (
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
"git.zhangdeman.cn/zhangdeman/wrapper/op_ternary"
"git.zhangdeman.cn/zhangdeman/wrapper/tool/define"
)
@@ -20,13 +21,9 @@ 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 := wrapper.String(versionOne).ToInt64Slice(".")
twoWrapper := wrapper.String(versionTwo).ToInt64Slice(".")
oneWrapper := op_string.ToBaseTypeSlice[int64](versionOne, ".")
twoWrapper := op_string.ToBaseTypeSlice[int64](versionTwo, ".")
if oneWrapper.Err != nil {
return nil, nil, oneWrapper.Err
}
@@ -38,11 +35,7 @@ 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 没有小版本号
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:07 2023/12/27
// - strictMode 严格模式, 非严格模式下, 2.4 == 2.4.0 , 开启严格模式, 则认为 2.4 < 2.4.0 , 因为 2.4 没有小版本号
func (v *version) Compare(versionOne string, versionTwo string, strictMode bool) (int, error) {
oneVersionArr, twoVersionArr, err := v.getVersionArr(versionOne, versionTwo)
if nil != err {
@@ -80,18 +73,14 @@ func (v *version) Compare(versionOne string, versionTwo string, strictMode bool)
if !strictMode || oneVersionLength == twoVersionLength {
return define.VersionEqual, nil
}
return wrapper.TernaryOperator.Int(
return op_ternary.BaseType[int](
oneVersionLength > twoVersionLength,
wrapper.Int(define.VersionOneMax),
wrapper.Int(define.VersionTwoMax),
).ToInt().Value, nil
define.VersionOneMax,
define.VersionTwoMax,
), 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
View File

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