Compare commits
24 Commits
ddeb444afb
...
master
Author | SHA1 | Date | |
---|---|---|---|
1cbfbe9597 | |||
8de0d9b942 | |||
2c73b9c034 | |||
beb8c74e8a | |||
3167ea0f50 | |||
c1588abcb4 | |||
34c8cf04fa | |||
d5622fe0ca | |||
743acb53f2 | |||
981ff94388 | |||
febf5a63ad | |||
bbf5184314 | |||
c503187022 | |||
c757e551a8 | |||
7b56590b57 | |||
2f87fe0cd9 | |||
91423fb146 | |||
b435398a94 | |||
e2c6fb3e91 | |||
59aeb88808 | |||
4196d342f7 | |||
3bb36680ad | |||
b7890f7fd0 | |||
a56574ff65 |
11
any.go
11
any.go
@ -34,7 +34,7 @@ func AnyDataType(data any) *AnyType {
|
||||
// Date : 18:19 2023/6/1
|
||||
type AnyType struct {
|
||||
data any
|
||||
dataType string
|
||||
dataType consts.DataType
|
||||
}
|
||||
|
||||
// IsNil 是否为 nil
|
||||
@ -62,7 +62,7 @@ func (at *AnyType) IsNil() bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:22 2023/6/1
|
||||
func (at *AnyType) Type() string {
|
||||
func (at *AnyType) Type() consts.DataType {
|
||||
if len(at.dataType) > 0 {
|
||||
// 已经处理过的,无需在处理
|
||||
return at.dataType
|
||||
@ -87,7 +87,7 @@ func (at *AnyType) Type() string {
|
||||
case reflect.Bool:
|
||||
return consts.DataTypeBool
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return consts.DataTypeFloat
|
||||
return consts.DataTypeFloat64
|
||||
default:
|
||||
return consts.DataTypeUnknown
|
||||
}
|
||||
@ -110,12 +110,13 @@ func (at *AnyType) ToString() String {
|
||||
_ = serialize.JSON.Transition(at.data, &val)
|
||||
return String(ArrayType[any](val).ToString().Value)
|
||||
case consts.DataTypeMapAnyAny:
|
||||
return String(EasyMap(at.data).ToString())
|
||||
easyMap := EasyMap(at.data)
|
||||
return String(easyMap.ToString())
|
||||
case consts.DataTypeInt:
|
||||
return String(Int(at.data.(int64)).ToString().Value)
|
||||
case consts.DataTypeUint:
|
||||
return String(Int(at.data.(uint)).ToString().Value)
|
||||
case consts.DataTypeFloat:
|
||||
case consts.DataTypeFloat64:
|
||||
return String(Float(at.data.(float64)).ToString().Value)
|
||||
case consts.DataTypeBool:
|
||||
return String(fmt.Sprintf("%v", at.data))
|
||||
|
4
array.go
4
array.go
@ -55,8 +55,8 @@ func (at *Array[Bt]) IsNil() bool {
|
||||
func (at *Array[Bt]) ToStringSlice() []string {
|
||||
list := make([]string, 0)
|
||||
for _, item := range at.value {
|
||||
byteData, _ := json.Marshal(item)
|
||||
list = append(list, strings.Trim(string(byteData), "\""))
|
||||
str := AnyDataType(item).ToString().Value()
|
||||
list = append(list, str)
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
@ -8,7 +8,6 @@
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/easymap"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -468,7 +467,7 @@ type StringSliceResult struct {
|
||||
//
|
||||
// Date : 16:05 2023/8/10
|
||||
type MapResult struct {
|
||||
Value easymap.EasyMap
|
||||
Value Map
|
||||
Err error
|
||||
}
|
||||
|
||||
|
@ -1,198 +0,0 @@
|
||||
// Package wrapper ...
|
||||
//
|
||||
// Description : wrapper ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-08-21 16:43
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func NewDynamic() *DynamicStruct {
|
||||
return &DynamicStruct{
|
||||
structFieldList: make([]reflect.StructField, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// DynamicStruct 动态生成数据结构
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:48 2024/8/21
|
||||
type DynamicStruct struct {
|
||||
structFieldList []reflect.StructField // 结构体字段列表
|
||||
}
|
||||
|
||||
// AddInt 添加int字段统一Int64
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2024/8/21
|
||||
func (ds *DynamicStruct) AddInt(fieldName string, fieldTag string, pkgPath string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(int64(0)),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
}
|
||||
|
||||
// AddUint 添加uint字段, 统一 uint64
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2024/8/21
|
||||
func (ds *DynamicStruct) AddUint(fieldName string, fieldTag string, pkgPath string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(uint64(0)),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}
|
||||
|
||||
// AddString 添加字符串字段
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:49 2024/8/21
|
||||
func (ds *DynamicStruct) AddString(fieldName string, fieldTag string, pkgPath string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(""),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
}
|
||||
|
||||
// AddBool 添加bool字段
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:49 2024/8/21
|
||||
func (ds *DynamicStruct) AddBool(fieldName string, fieldTag string, pkgPath string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(true),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
}
|
||||
|
||||
// AddFloat 添加float字段, 统一 float64
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:48 2024/8/21
|
||||
func (ds *DynamicStruct) AddFloat(fieldName string, fieldTag string, pkgPath string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(float64(0)),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
}
|
||||
|
||||
// AddSlice 添加slice
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:47 2024/8/21
|
||||
func (ds *DynamicStruct) AddSlice(fieldName string, fieldTag string, pkgPath string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf([]any{}),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}
|
||||
|
||||
// AddMap 添加map字段
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:43 2024/8/21
|
||||
func (ds *DynamicStruct) AddMap(fieldName string, fieldTag string, pkgPath string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(map[string]any{}),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}
|
||||
|
||||
// AddAny 添加任意类型字段
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:52 2024/8/21
|
||||
func (ds *DynamicStruct) AddAny(fieldName string, fieldTag string, pkgPath string, value any) {
|
||||
if nil == value {
|
||||
// 不能是空指针
|
||||
return
|
||||
}
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(value),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}
|
||||
|
||||
// AddStructField 添加结构体字段
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:42 2024/8/21
|
||||
func (ds *DynamicStruct) AddStructField(field reflect.StructField) {
|
||||
if field.Tag == "" {
|
||||
field.Tag = reflect.StructTag(fmt.Sprintf(`json:"%v"`, field.Name))
|
||||
}
|
||||
field.Name = String(field.Name).SnakeCaseToCamel() // 转成大驼峰, 保证对外可访问
|
||||
ds.structFieldList = append(ds.structFieldList, field)
|
||||
}
|
||||
|
||||
// GetStructType 获取结构体的类型
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:58 2024/8/21
|
||||
func (ds *DynamicStruct) GetStructType() reflect.Type {
|
||||
return reflect.StructOf(ds.structFieldList)
|
||||
}
|
||||
|
||||
// ToStructDefaultValue 获取结构体的值, 并采用对应类型默认值填充相关字段
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:56 2024/8/21
|
||||
func (ds *DynamicStruct) ToStructDefaultValue() any {
|
||||
defer ds.Clear()
|
||||
defaultValue := reflect.New(ds.GetStructType()).Elem().Interface()
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// ToStructDefaultSliceValue 自动生成结构体列表
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:04 2024/8/21
|
||||
func (ds *DynamicStruct) ToStructDefaultSliceValue() any {
|
||||
defer ds.Clear()
|
||||
tSlice := reflect.MakeSlice(reflect.SliceOf(ds.GetStructType()), 0, 0)
|
||||
return reflect.New(tSlice.Type()).Elem().Interface()
|
||||
}
|
||||
|
||||
// Clear 清理
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:08 2024/8/21
|
||||
func (ds *DynamicStruct) Clear() {
|
||||
// 清理掉已设置的字段, 不然实例复用会互相影响
|
||||
ds.structFieldList = make([]reflect.StructField, 0)
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
// Package wrapper ...
|
||||
//
|
||||
// Description : wrapper ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2024-08-21 17:56
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewDynamic(t *testing.T) {
|
||||
instance := NewDynamic()
|
||||
instance.AddInt("Age", "age", "")
|
||||
instance.AddString("Name", "name", "")
|
||||
defaultVal := instance.ToStructDefaultValue()
|
||||
testMap := map[string]any{
|
||||
"name": "白茶",
|
||||
"age": 18,
|
||||
}
|
||||
_ = mapstructure.Decode(testMap, &defaultVal)
|
||||
fmt.Println(defaultVal)
|
||||
}
|
26
easymap.go
26
easymap.go
@ -10,7 +10,6 @@ package wrapper
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/easymap"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"reflect"
|
||||
@ -21,7 +20,7 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:02 2023/8/10
|
||||
func EasyMap(mapData any) easymap.EasyMap {
|
||||
func EasyMap(mapData any) Map {
|
||||
m, _ := EasyMapWithError(mapData)
|
||||
return m
|
||||
}
|
||||
@ -31,24 +30,23 @@ func EasyMap(mapData any) easymap.EasyMap {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:06 2023/8/10
|
||||
func EasyMapWithError(mapData any) (easymap.EasyMap, error) {
|
||||
func EasyMapWithError(mapData any) (Map, error) {
|
||||
if nil == mapData {
|
||||
return easymap.NewNormal(), nil
|
||||
return map[string]any{}, 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 m, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
|
||||
return mapFormatData, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
|
||||
}
|
||||
mapData = mapFormatData
|
||||
return mapFormatData, nil
|
||||
}
|
||||
|
||||
m := Map(map[string]any{})
|
||||
reflectValue := reflect.ValueOf(mapData).MapRange()
|
||||
for reflectValue.Next() {
|
||||
// 循环提取相关值
|
||||
m.Set(reflectValue.Key().Interface(), reflectValue.Value().Interface())
|
||||
_ = m.Set(reflectValue.Key().String(), reflectValue.Value().Interface())
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@ -58,7 +56,7 @@ func EasyMapWithError(mapData any) (easymap.EasyMap, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:11 2023/8/10
|
||||
func EasyMapFromStruct(data any) easymap.EasyMap {
|
||||
func EasyMapFromStruct(data any) Map {
|
||||
byteData, _ := json.Marshal(data)
|
||||
return EasyMapFromByte(byteData)
|
||||
}
|
||||
@ -68,7 +66,7 @@ func EasyMapFromStruct(data any) easymap.EasyMap {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:12 2023/8/10
|
||||
func EasyMapFromString(data string) easymap.EasyMap {
|
||||
func EasyMapFromString(data string) Map {
|
||||
return EasyMapFromByte([]byte(data))
|
||||
}
|
||||
|
||||
@ -77,11 +75,11 @@ func EasyMapFromString(data string) easymap.EasyMap {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:12 2023/8/10
|
||||
func EasyMapFromByte(data []byte) easymap.EasyMap {
|
||||
res := easymap.NewNormal()
|
||||
func EasyMapFromByte(data []byte) Map {
|
||||
res := Map(map[string]any{})
|
||||
jsonRes := gjson.Parse(string(data))
|
||||
jsonRes.ForEach(func(key, value gjson.Result) bool {
|
||||
res.Set(key.Value(), value.Value())
|
||||
_ = res.Set(key.String(), value.Value())
|
||||
return true
|
||||
})
|
||||
return res
|
||||
|
2
float.go
2
float.go
@ -107,7 +107,7 @@ func (f Float) ToString() StringResult {
|
||||
}
|
||||
}
|
||||
return StringResult{
|
||||
Value: fmt.Sprintf("%v", floatVal),
|
||||
Value: fmt.Sprintf("%v", floatVal.Value),
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
11
go.mod
11
go.mod
@ -5,23 +5,22 @@ go 1.21
|
||||
toolchain go1.21.4
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250321102241-d6e86b64f7ca
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/spaolacci/murmur3 v1.1.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
)
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e // indirect
|
||||
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||
github.com/BurntSushi/toml v1.5.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/mozillazg/go-pinyin v0.20.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
|
61
go.sum
61
go.sum
@ -1,49 +1,33 @@
|
||||
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/consts v0.0.0-20240823041145-d4df71cf37e5 h1:pmIHln0gWW+5xAB762h3WDsRkZuYLUDndvJDsGMKoOY=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240823041145-d4df71cf37e5/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdTKKUjgxtCnK38d1Tb4teyQp1C7wYHPdR32yZtM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 h1:BiAlBJ+DuRs/xD7nDQD2JT8Oc+V+0Uwt36qZwdXGvzI=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/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/easymap v0.0.0-20241024134352-ce2d84d282ba h1:hgajrPZGoDY9P+x6iqcS06pnu5t+N7DOfpmRwb+TZ4s=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241024134352-ce2d84d282ba/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9 h1:TP/M3WnGsxh0Vr6YuS1i28hw1oV//YbdCoI46PUBIA0=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701 h1:G+lGQmjMOBWGspZfijZvenGUAKpjBBrkRXLg3+GZp0U=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250122075709-5ecf3edb4a00 h1:obyJF0CXVR93TOnOtzN5xXxxSLpw1UFMBc4niWiyoQI=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250122075709-5ecf3edb4a00/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250227040546-863c03f34bb8 h1:VEifPc+vkpEQoX9rj7zxmT1m+IA81XjOxe7+Z1aqWNM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250227040546-863c03f34bb8/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250321102241-d6e86b64f7ca h1:uxjzbY5fDozjyK6jkoQtuQouVTcVfXjbe3chARYSjRM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250321102241-d6e86b64f7ca/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-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/serialize v0.0.0-20241023104258-2e0a298aa558 h1:ZVJyyDKfYeA3TsN8UOi4IprkouK4wIIfCKe+F9byLWA=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241023104258-2e0a298aa558/go.mod h1:/9eicss/Dt9tp2jwZ/4cXDqDKo/Dez+HuT5/NGdqW+s=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e h1:A045F67AMSqFKGD9kk2uLa+6c/zpmW8vjjSRmSsdjPs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
|
||||
git.zhangdeman.cn/zhangdeman/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/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125105403-cb92be844edc h1:rYjlMH5Yy0G8OQgXA8qrV+fqObnB99v+6s8nbiLhzQs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125105403-cb92be844edc/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7GG14qgXKB4MEXQFOe7/UYebsqMfPaSX80TcPdOosI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs=
|
||||
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.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/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
@ -60,11 +44,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
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.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
|
||||
github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
|
107
map.go
107
map.go
@ -18,19 +18,19 @@ var mapLock = &sync.RWMutex{}
|
||||
|
||||
type Map map[string]any
|
||||
|
||||
func (m *Map) lock() {
|
||||
func (m Map) lock() {
|
||||
mapLock.Lock()
|
||||
}
|
||||
|
||||
func (m *Map) unlock() {
|
||||
func (m Map) unlock() {
|
||||
mapLock.Unlock()
|
||||
}
|
||||
|
||||
func (m *Map) rLock() {
|
||||
func (m Map) rLock() {
|
||||
mapLock.RLock()
|
||||
}
|
||||
|
||||
func (m *Map) rUnlock() {
|
||||
func (m Map) rUnlock() {
|
||||
mapLock.RUnlock()
|
||||
}
|
||||
|
||||
@ -39,14 +39,13 @@ func (m *Map) rUnlock() {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:34 2024/11/6
|
||||
func (m *Map) Exist(key string) bool {
|
||||
func (m Map) Exist(key string) bool {
|
||||
if m.IsNil() {
|
||||
return false
|
||||
}
|
||||
m.rLock()
|
||||
defer m.rUnlock()
|
||||
v := *m
|
||||
_, exist := v[key]
|
||||
_, exist := m[key]
|
||||
return exist
|
||||
}
|
||||
|
||||
@ -59,13 +58,13 @@ func (m *Map) Exist(key string) bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:16 2024/11/19
|
||||
func (m *Map) Set(field string, value any) error {
|
||||
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
|
||||
m[field] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -74,13 +73,13 @@ func (m *Map) Set(field string, value any) error {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:21 2024/11/19
|
||||
func (m *Map) Del(field string) {
|
||||
func (m Map) Del(field string) {
|
||||
if m.IsNil() {
|
||||
return
|
||||
}
|
||||
m.lock()
|
||||
defer m.unlock()
|
||||
delete(*m, field)
|
||||
delete(m, field)
|
||||
}
|
||||
|
||||
// IsNil 判断map是否为nil
|
||||
@ -88,11 +87,11 @@ func (m *Map) Del(field string) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:22 2024/11/19
|
||||
func (m *Map) IsNil() bool {
|
||||
func (m Map) IsNil() bool {
|
||||
if nil == m {
|
||||
return true
|
||||
}
|
||||
return reflect.ValueOf(*m).IsNil()
|
||||
return reflect.ValueOf(m).IsNil()
|
||||
}
|
||||
|
||||
// Get ...
|
||||
@ -100,14 +99,13 @@ func (m *Map) IsNil() bool {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:41 2024/11/19
|
||||
func (m *Map) Get(field string) (any, error) {
|
||||
func (m Map) Get(field string) (any, error) {
|
||||
if m.IsNil() {
|
||||
return nil, errors.New("map is nil")
|
||||
}
|
||||
m.rLock()
|
||||
defer m.rUnlock()
|
||||
v := *m
|
||||
val, exist := v[field]
|
||||
val, exist := m[field]
|
||||
if !exist {
|
||||
return nil, errors.New(field + " : field not found")
|
||||
}
|
||||
@ -124,14 +122,13 @@ func (m *Map) Get(field string) (any, error) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:59 2024/11/19
|
||||
func (m *Map) GetDefault(field string, defaultValue any, allowNil bool) any {
|
||||
func (m Map) GetDefault(field string, defaultValue any, allowNil bool) any {
|
||||
if m.IsNil() {
|
||||
return defaultValue
|
||||
}
|
||||
m.rLock()
|
||||
defer m.rUnlock()
|
||||
v := *m
|
||||
val, exist := v[field]
|
||||
val, exist := m[field]
|
||||
if !exist {
|
||||
return defaultValue
|
||||
}
|
||||
@ -149,11 +146,11 @@ func (m *Map) GetDefault(field string, defaultValue any, allowNil bool) any {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:39 2024/11/6
|
||||
func (m *Map) Value() map[string]any {
|
||||
func (m Map) Value() map[string]any {
|
||||
if m.IsNil() {
|
||||
return nil
|
||||
}
|
||||
return *m
|
||||
return m
|
||||
}
|
||||
|
||||
// Clone 克隆数据
|
||||
@ -161,7 +158,7 @@ func (m *Map) Value() map[string]any {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:40 2024/11/6
|
||||
func (m *Map) Clone() Map {
|
||||
func (m Map) Clone() Map {
|
||||
newData := map[string]any{}
|
||||
if m.IsNil() {
|
||||
return newData
|
||||
@ -184,7 +181,7 @@ func (m *Map) Clone() Map {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:40 2024/11/19
|
||||
func (m *Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, error) {
|
||||
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 {
|
||||
@ -207,7 +204,7 @@ func (m *Map) Filter(fieldList []string, ignoreNotFound bool) (map[string]any, e
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:07 2024/11/19
|
||||
func (m *Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]any {
|
||||
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)
|
||||
@ -220,10 +217,70 @@ func (m *Map) FilterDefault(fieldMap map[string]any, allowNil bool) map[string]a
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:35 2024/11/19
|
||||
func (m *Map) MarshalJSON() ([]byte, error) {
|
||||
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 AnyDataType(val).ToString().Value(), 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 := AnyDataType(val).ToString().ToInt64()
|
||||
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 := AnyDataType(val).ToString().ToFloat64()
|
||||
return float64Res.Value, float64Res.Err
|
||||
}
|
||||
|
@ -136,21 +136,21 @@ func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map,
|
||||
}
|
||||
var (
|
||||
inputFieldVal any
|
||||
inputFieldValExist bool
|
||||
inputFieldValExist error
|
||||
storageFieldVal any
|
||||
storageFieldValExist bool
|
||||
storageFieldValExist error
|
||||
)
|
||||
|
||||
inputFieldVal, inputFieldValExist = inputVal.Get(field)
|
||||
storageFieldVal, storageFieldValExist = storageVal.Get(field)
|
||||
// 字段在输入数据和存储数据中均不存在
|
||||
if !inputFieldValExist && !storageFieldValExist {
|
||||
if nil != inputFieldValExist && nil != storageFieldValExist {
|
||||
// 输入和存储都没这个字段
|
||||
return result
|
||||
}
|
||||
|
||||
// 判断输入字段是否存在
|
||||
if !inputFieldValExist {
|
||||
if nil != inputFieldValExist {
|
||||
if option.IgnoreNotFoundField {
|
||||
// 忽略不存在的字段
|
||||
return result
|
||||
@ -162,7 +162,7 @@ func DefaultDiffFunc(field string, inputVal wrapper.Map, storageVal wrapper.Map,
|
||||
return result
|
||||
}
|
||||
// 判断存储字段是否存在
|
||||
if !storageFieldValExist {
|
||||
if nil != storageFieldValExist {
|
||||
result.IsSame = false
|
||||
result.DiffReason = DiffReasonStorageFieldNotFound
|
||||
result.NewVal = inputFieldVal
|
||||
|
Reference in New Issue
Block a user