Compare commits
1 Commits
fbc2c162d5
...
feature/ge
| Author | SHA1 | Date | |
|---|---|---|---|
| 50aa5ae7f8 |
@@ -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
190
array.go
Normal 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
|
||||
}
|
||||
@@ -13,6 +13,24 @@ import (
|
||||
)
|
||||
|
||||
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())
|
||||
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"))
|
||||
}
|
||||
|
||||
118
bigint.go
118
bigint.go
@@ -1,118 +0,0 @@
|
||||
// Package wrapper ...
|
||||
//
|
||||
// Description : wrapper ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-11-19 16:33
|
||||
package wrapper
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -1,394 +0,0 @@
|
||||
// Package convert ...
|
||||
//
|
||||
// Description : 任意类型之间的相互转换
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-02-23 10:23 下午
|
||||
package convert
|
||||
|
||||
/*
|
||||
Desc : 在处理一些参数的时候,可能需要将参数转换为各种类型,这里实现一个通用的转换函数,实现各种类型之间的相互转换。
|
||||
|
||||
当然,如果源数据格式和目标数据类型不一致,是会返回错误的。例如将字符串“一二三”转换为数值类型则会报错,而将字符串“123”转换为数值类型则OK。
|
||||
|
||||
这段代码实际抄自go自带的“database/sql”库,只是源代码作为内部函数无法在外面调用,可以单独把需要的功能拎出来使用:
|
||||
|
||||
代码中有一个Scaner接口,可以自行实现,然后通过"convertAssign()"函数,作为dst参数传入。
|
||||
|
||||
Author : zhangdeman001@ke.com<白茶清欢>
|
||||
|
||||
*/
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RawBytes is a byte slice that holds a reference to memory owned by
|
||||
// the database itself. After a Scan into a RawBytes, the slice is only
|
||||
// valid until the next call to Next, Scan, or Close.
|
||||
type RawBytes []byte
|
||||
|
||||
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
|
||||
|
||||
// ConvertAssign ...
|
||||
// convertAssign copies to dest the value in src, converting it if possible.
|
||||
// An error is returned if the copy would result in loss of information.
|
||||
// dest should be a pointer type.
|
||||
func ConvertAssign(dest, src any) error {
|
||||
// Common cases, without reflect.
|
||||
switch s := src.(type) {
|
||||
case string:
|
||||
switch d := dest.(type) {
|
||||
case *string:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = s
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = []byte(s)
|
||||
return nil
|
||||
case *RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = append((*d)[:0], s...)
|
||||
return nil
|
||||
}
|
||||
case []byte:
|
||||
switch d := dest.(type) {
|
||||
case *string:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = string(s)
|
||||
return nil
|
||||
case *any:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = cloneBytes(s)
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = cloneBytes(s)
|
||||
return nil
|
||||
case *RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = s
|
||||
return nil
|
||||
}
|
||||
case time.Time:
|
||||
switch d := dest.(type) {
|
||||
case *time.Time:
|
||||
*d = s
|
||||
return nil
|
||||
case *string:
|
||||
*d = s.Format(time.RFC3339Nano)
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = []byte(s.Format(time.RFC3339Nano))
|
||||
return nil
|
||||
case *RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = s.AppendFormat((*d)[:0], time.RFC3339Nano)
|
||||
return nil
|
||||
}
|
||||
case nil:
|
||||
switch d := dest.(type) {
|
||||
case *any:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = nil
|
||||
return nil
|
||||
case *[]byte:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = nil
|
||||
return nil
|
||||
case *RawBytes:
|
||||
if d == nil {
|
||||
return errNilPtr
|
||||
}
|
||||
*d = nil
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var sv reflect.Value
|
||||
|
||||
switch d := dest.(type) {
|
||||
case *string:
|
||||
sv = reflect.ValueOf(src)
|
||||
switch sv.Kind() {
|
||||
case reflect.Bool,
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
*d = asString(src)
|
||||
return nil
|
||||
}
|
||||
case *[]byte:
|
||||
sv = reflect.ValueOf(src)
|
||||
if b, ok := asBytes(nil, sv); ok {
|
||||
*d = b
|
||||
return nil
|
||||
}
|
||||
case *RawBytes:
|
||||
sv = reflect.ValueOf(src)
|
||||
if b, ok := asBytes([]byte(*d)[:0], sv); ok {
|
||||
*d = RawBytes(b)
|
||||
return nil
|
||||
}
|
||||
case *bool:
|
||||
bv, err := Bool.ConvertValue(src)
|
||||
if err == nil {
|
||||
*d = bv.(bool)
|
||||
}
|
||||
return err
|
||||
case *any:
|
||||
*d = src
|
||||
return nil
|
||||
}
|
||||
|
||||
if scanner, ok := dest.(Scanner); ok {
|
||||
return scanner.Scan(src)
|
||||
}
|
||||
|
||||
dpv := reflect.ValueOf(dest)
|
||||
if dpv.Kind() != reflect.Ptr {
|
||||
return errors.New("destination not a pointer")
|
||||
}
|
||||
if dpv.IsNil() {
|
||||
return errNilPtr
|
||||
}
|
||||
|
||||
if !sv.IsValid() {
|
||||
sv = reflect.ValueOf(src)
|
||||
}
|
||||
|
||||
dv := reflect.Indirect(dpv)
|
||||
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
|
||||
switch b := src.(type) {
|
||||
case []byte:
|
||||
dv.Set(reflect.ValueOf(cloneBytes(b)))
|
||||
default:
|
||||
dv.Set(sv)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
|
||||
dv.Set(sv.Convert(dv.Type()))
|
||||
return nil
|
||||
}
|
||||
|
||||
// The following conversions use a string value as an intermediate representation
|
||||
// to convert between various numeric types.
|
||||
//
|
||||
// This also allows scanning into user defined types such as "type Int int64".
|
||||
// For symmetry, also check for string destination types.
|
||||
switch dv.Kind() {
|
||||
case reflect.Ptr:
|
||||
if src == nil {
|
||||
dv.Set(reflect.Zero(dv.Type()))
|
||||
return nil
|
||||
}
|
||||
dv.Set(reflect.New(dv.Type().Elem()))
|
||||
return ConvertAssign(dv.Interface(), src)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
s := asString(src)
|
||||
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
|
||||
if err != nil {
|
||||
err = strconvErr(err)
|
||||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
|
||||
}
|
||||
dv.SetInt(i64)
|
||||
return nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
s := asString(src)
|
||||
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
|
||||
if err != nil {
|
||||
err = strconvErr(err)
|
||||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
|
||||
}
|
||||
dv.SetUint(u64)
|
||||
return nil
|
||||
case reflect.Float32, reflect.Float64:
|
||||
s := asString(src)
|
||||
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
|
||||
if err != nil {
|
||||
err = strconvErr(err)
|
||||
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
|
||||
}
|
||||
dv.SetFloat(f64)
|
||||
return nil
|
||||
case reflect.String:
|
||||
switch v := src.(type) {
|
||||
case string:
|
||||
dv.SetString(v)
|
||||
return nil
|
||||
case []byte:
|
||||
dv.SetString(string(v))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
|
||||
}
|
||||
|
||||
func strconvErr(err error) error {
|
||||
if ne, ok := err.(*strconv.NumError); ok {
|
||||
return ne.Err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func cloneBytes(b []byte) []byte {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
c := make([]byte, len(b))
|
||||
copy(c, b)
|
||||
return c
|
||||
}
|
||||
|
||||
func ToString(src any) string {
|
||||
return asString(src)
|
||||
}
|
||||
|
||||
func asString(src any) string {
|
||||
switch v := src.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
}
|
||||
rv := reflect.ValueOf(src)
|
||||
switch rv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return strconv.FormatInt(rv.Int(), 10)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return strconv.FormatUint(rv.Uint(), 10)
|
||||
case reflect.Float64:
|
||||
return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
|
||||
case reflect.Float32:
|
||||
return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
|
||||
case reflect.Bool:
|
||||
return strconv.FormatBool(rv.Bool())
|
||||
}
|
||||
return fmt.Sprintf("%v", src)
|
||||
}
|
||||
|
||||
func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
|
||||
switch rv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return strconv.AppendInt(buf, rv.Int(), 10), true
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
return strconv.AppendUint(buf, rv.Uint(), 10), true
|
||||
case reflect.Float32:
|
||||
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
|
||||
case reflect.Float64:
|
||||
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
|
||||
case reflect.Bool:
|
||||
return strconv.AppendBool(buf, rv.Bool()), true
|
||||
case reflect.String:
|
||||
s := rv.String()
|
||||
return append(buf, s...), true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Value is a value that drivers must be able to handle.
|
||||
// It is either nil, a type handled by a database driver's NamedValueChecker
|
||||
// interface, or an instance of one of these types:
|
||||
//
|
||||
// int64
|
||||
// float64
|
||||
// bool
|
||||
// []byte
|
||||
// string
|
||||
// time.Time
|
||||
type Value any
|
||||
|
||||
type boolType struct{}
|
||||
|
||||
var Bool boolType
|
||||
|
||||
func (boolType) String() string { return "Bool" }
|
||||
func (boolType) ConvertValue(src any) (Value, error) {
|
||||
switch s := src.(type) {
|
||||
case bool:
|
||||
return s, nil
|
||||
case string:
|
||||
b, err := strconv.ParseBool(s)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
|
||||
}
|
||||
return b, nil
|
||||
case []byte:
|
||||
b, err := strconv.ParseBool(string(s))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
sv := reflect.ValueOf(src)
|
||||
switch sv.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
iv := sv.Int()
|
||||
if iv == 1 || iv == 0 {
|
||||
return iv == 1, nil
|
||||
}
|
||||
return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", iv)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
uv := sv.Uint()
|
||||
if uv == 1 || uv == 0 {
|
||||
return uv == 1, nil
|
||||
}
|
||||
return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", uv)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("sql/driver: couldn't convert %v (%T) into type bool", src, src)
|
||||
}
|
||||
|
||||
type Scanner interface {
|
||||
// Scan assigns a value from a database driver.
|
||||
//
|
||||
// The src value will be of one of the following types:
|
||||
//
|
||||
// int64
|
||||
// float64
|
||||
// bool
|
||||
// []byte
|
||||
// string
|
||||
// time.Time
|
||||
// nil - for NULL values
|
||||
//
|
||||
// An error should be returned if the value cannot be stored
|
||||
// without loss of information.
|
||||
//
|
||||
// Reference types such as []byte are only valid until the next call to Scan
|
||||
// and should not be retained. Their underlying memory is owned by the driver.
|
||||
// If retention is necessary, copy their values before the next call to Scan.
|
||||
Scan(src any) error
|
||||
}
|
||||
388
define.go
388
define.go
@@ -7,296 +7,474 @@
|
||||
// Date : 2023-05-05 14:44
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/op_type"
|
||||
)
|
||||
|
||||
// BaseTypeValueResult 基础类型结果
|
||||
type BaseTypeValueResult[BaseType op_type.BaseType] struct {
|
||||
Value BaseType `json:"value"`
|
||||
Err error `json:"err"`
|
||||
}
|
||||
import "time"
|
||||
|
||||
// Int8Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:25 2023/5/8
|
||||
type Int8Result struct {
|
||||
Value int8 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value int8
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int8PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:05 2023/5/15
|
||||
type Int8PtrResult struct {
|
||||
Value *int8 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *int8
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int16Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:26 2023/5/8
|
||||
type Int16Result struct {
|
||||
Value int16 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value int16
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int16PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:05 2023/5/15
|
||||
type Int16PtrResult struct {
|
||||
Value *int16 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *int16
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int32Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:26 2023/5/8
|
||||
type Int32Result struct {
|
||||
Value int32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value int32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int32PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:04 2023/5/15
|
||||
type Int32PtrResult struct {
|
||||
Value *int32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *int32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int64Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:26 2023/5/8
|
||||
type Int64Result struct {
|
||||
Value int64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value int64
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int64PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:04 2023/5/15
|
||||
type Int64PtrResult struct {
|
||||
Value *int64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *int64
|
||||
Err error
|
||||
}
|
||||
|
||||
// IntResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:26 2023/5/8
|
||||
type IntResult struct {
|
||||
Value int `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value int
|
||||
Err error
|
||||
}
|
||||
|
||||
// IntPtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:51 2023/5/15
|
||||
type IntPtrResult struct {
|
||||
Value *int `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *int
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint8Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:28 2023/5/8
|
||||
type Uint8Result struct {
|
||||
Value uint8 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value uint8
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint8PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:49 2023/5/16
|
||||
type Uint8PtrResult struct {
|
||||
Value *uint8 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *uint8
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint16Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:28 2023/5/8
|
||||
type Uint16Result struct {
|
||||
Value uint16 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value uint16
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint16PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:49 2023/5/16
|
||||
type Uint16PtrResult struct {
|
||||
Value *uint16 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *uint16
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint32Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:28 2023/5/8
|
||||
type Uint32Result struct {
|
||||
Value uint32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value uint32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint32PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:49 2023/5/16
|
||||
type Uint32PtrResult struct {
|
||||
Value *uint32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *uint32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint64Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:28 2023/5/8
|
||||
type Uint64Result struct {
|
||||
Value uint64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value uint64
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint64PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:50 2023/5/16
|
||||
type Uint64PtrResult struct {
|
||||
Value *uint64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *uint64
|
||||
Err error
|
||||
}
|
||||
|
||||
// UintResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:29 2023/5/8
|
||||
type UintResult struct {
|
||||
Value uint `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value uint
|
||||
Err error
|
||||
}
|
||||
|
||||
// UintPtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:51 2023/5/16
|
||||
type UintPtrResult struct {
|
||||
Value *uint `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *uint
|
||||
Err error
|
||||
}
|
||||
|
||||
// Float32Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:31 2023/5/8
|
||||
type Float32Result struct {
|
||||
Value float32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value float32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Float32PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:10 2023/5/16
|
||||
type Float32PtrResult struct {
|
||||
Value *float32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *float32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Float64Result ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:32 2023/5/8
|
||||
type Float64Result struct {
|
||||
Value float64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value float64
|
||||
Err error
|
||||
}
|
||||
|
||||
// Float64PtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:52 2023/5/16
|
||||
type Float64PtrResult struct {
|
||||
Value *float64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *float64
|
||||
Err error
|
||||
}
|
||||
|
||||
// Any ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:40 2023/5/8
|
||||
type Any struct {
|
||||
Value any `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value any
|
||||
Err error
|
||||
}
|
||||
|
||||
// BoolResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:36 2023/5/8
|
||||
type BoolResult struct {
|
||||
Value bool `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value bool
|
||||
Err error
|
||||
}
|
||||
|
||||
// BoolPtrResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:53 2023/5/16
|
||||
type BoolPtrResult struct {
|
||||
Value *bool `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *bool
|
||||
Err error
|
||||
}
|
||||
|
||||
// ObjectResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:38 2023/5/8
|
||||
type ObjectResult struct {
|
||||
Value map[string]any `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value map[string]any
|
||||
Err error
|
||||
}
|
||||
|
||||
// StringResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:57 2023/5/8
|
||||
type StringResult struct {
|
||||
Value string `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value string
|
||||
Err error
|
||||
}
|
||||
|
||||
// StringPtrResult 字符串指针
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:02 2023/5/15
|
||||
type StringPtrResult struct {
|
||||
Value *string `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value *string
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int8SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:49 2023/5/8
|
||||
type Int8SliceResult struct {
|
||||
Value []int8 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []int8
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int16SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:49 2023/5/8
|
||||
type Int16SliceResult struct {
|
||||
Value []int16 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []int16
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int32SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2023/5/8
|
||||
type Int32SliceResult struct {
|
||||
Value []int32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []int32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Int64SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2023/5/8
|
||||
type Int64SliceResult struct {
|
||||
Value []int64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []int64
|
||||
Err error
|
||||
}
|
||||
|
||||
// IntSliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2023/5/8
|
||||
type IntSliceResult struct {
|
||||
Value []int `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []int
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint8SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:55 2023/5/8
|
||||
type Uint8SliceResult struct {
|
||||
Value []uint8 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []uint8
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint16SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:55 2023/5/8
|
||||
type Uint16SliceResult struct {
|
||||
Value []uint16 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []uint16
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint32SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:55 2023/5/8
|
||||
type Uint32SliceResult struct {
|
||||
Value []uint32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []uint32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Uint64SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:55 2023/5/8
|
||||
type Uint64SliceResult struct {
|
||||
Value []uint64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []uint64
|
||||
Err error
|
||||
}
|
||||
|
||||
// UintSliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:56 2023/5/8
|
||||
type UintSliceResult struct {
|
||||
Value []uint `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []uint
|
||||
Err error
|
||||
}
|
||||
|
||||
// BoolSliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:22 2023/5/8
|
||||
type BoolSliceResult struct {
|
||||
Value []bool `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []bool
|
||||
Err error
|
||||
}
|
||||
|
||||
// Float32SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:24 2023/5/8
|
||||
type Float32SliceResult struct {
|
||||
Value []float32 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []float32
|
||||
Err error
|
||||
}
|
||||
|
||||
// Float64SliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:24 2023/5/8
|
||||
type Float64SliceResult struct {
|
||||
Value []float64 `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []float64
|
||||
Err error
|
||||
}
|
||||
|
||||
// DurationResult 时间转换结果
|
||||
//
|
||||
// Author : zhangdeman001@ke.com<张德满>
|
||||
//
|
||||
// Date : 20:32 2023/9/4
|
||||
type DurationResult struct {
|
||||
Value time.Duration `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value time.Duration
|
||||
Err error
|
||||
}
|
||||
|
||||
// StringSliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:11 2023/8/6
|
||||
type StringSliceResult struct {
|
||||
Value []string `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []string
|
||||
Err error
|
||||
}
|
||||
|
||||
// MapResult 转map的结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:05 2023/8/10
|
||||
type MapResult struct {
|
||||
Value any `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value Map
|
||||
Err error
|
||||
}
|
||||
|
||||
// AnySliceResult ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:28 2023/5/8
|
||||
type AnySliceResult struct {
|
||||
Value []any `json:"value"`
|
||||
Err error `json:"err"`
|
||||
Value []any
|
||||
Err error
|
||||
}
|
||||
|
||||
@@ -1,58 +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"`
|
||||
}
|
||||
2
float.go
2
float.go
@@ -107,7 +107,7 @@ func (f Float) ToString() StringResult {
|
||||
}
|
||||
}
|
||||
return StringResult{
|
||||
Value: fmt.Sprintf("%v", floatVal.Value),
|
||||
Value: fmt.Sprintf("%v", floatVal),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
29
go.mod
29
go.mod
@@ -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
67
go.sum
@@ -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=
|
||||
|
||||
@@ -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
|
||||
@@ -71,10 +71,7 @@ func (ot *ObjectType) IsValid() bool {
|
||||
//
|
||||
// Date : 18:50 2023/6/1
|
||||
func (ot *ObjectType) IsNil() bool {
|
||||
if ot.source == nil {
|
||||
return true
|
||||
}
|
||||
return reflect.ValueOf(ot.source).IsNil()
|
||||
return ot.source == nil
|
||||
}
|
||||
|
||||
// ToString 转字符串
|
||||
|
||||
@@ -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(arr Array[Bt]) []Bt {
|
||||
result := make([]Bt, 0)
|
||||
dataTable := make(map[string]bool)
|
||||
|
||||
for _, item := range arr.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(arr Array[Bt], 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
|
||||
}
|
||||
289
op_map/map.go
289
op_map/map.go
@@ -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
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
@@ -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.StringResult {
|
||||
h := md5.New()
|
||||
_, err := io.WriteString(h, str)
|
||||
if nil != err {
|
||||
return define.StringResult{
|
||||
Value: "",
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return define.StringResult{
|
||||
Value: hex.EncodeToString(h.Sum(nil)),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// RandomMd5 生成随机字符串MD%值
|
||||
func RandomMd5() define.StringResult {
|
||||
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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
31
string_test.go
Normal file
31
string_test.go
Normal 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)
|
||||
}
|
||||
@@ -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 ToMap(s *Struct) 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
157
ternary_operator.go
Normal 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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
20
tool/diff.go
20
tool/diff.go
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user