Compare commits
11 Commits
feature/su
...
3167ea0f50
Author | SHA1 | Date | |
---|---|---|---|
3167ea0f50 | |||
c1588abcb4 | |||
34c8cf04fa | |||
d5622fe0ca | |||
743acb53f2 | |||
981ff94388 | |||
febf5a63ad | |||
bbf5184314 | |||
c503187022 | |||
c757e551a8 | |||
7b56590b57 |
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,13 +8,31 @@
|
||||
package wrapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// NewMap2DynamicStruct 通过map生成动态结构体
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:48 2025/1/24
|
||||
func NewMap2DynamicStruct(mapData map[string]any, tagTable map[string]string) *DynamicStruct {
|
||||
d := NewDynamic()
|
||||
d.mapData = mapData
|
||||
for k, v := range mapData {
|
||||
d.AddAny(String(k).SnakeCaseToCamel(), tagTable[k], "dynamic_struct", v)
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func NewDynamic() *DynamicStruct {
|
||||
return &DynamicStruct{
|
||||
structFieldList: make([]reflect.StructField, 0),
|
||||
mapData: make(map[string]any),
|
||||
l: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,6 +43,19 @@ func NewDynamic() *DynamicStruct {
|
||||
// Date : 16:48 2024/8/21
|
||||
type DynamicStruct struct {
|
||||
structFieldList []reflect.StructField // 结构体字段列表
|
||||
mapData map[string]any
|
||||
l *sync.RWMutex
|
||||
}
|
||||
|
||||
// SetMapData 设置map值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:50 2025/1/24
|
||||
func (ds *DynamicStruct) SetMapData(field string, value any) {
|
||||
ds.l.Lock()
|
||||
defer ds.l.Unlock()
|
||||
ds.mapData[field] = value
|
||||
}
|
||||
|
||||
// AddInt 添加int字段统一Int64
|
||||
@ -32,13 +63,13 @@ type DynamicStruct struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2024/8/21
|
||||
func (ds *DynamicStruct) AddInt(fieldName string, fieldTag string, pkgPath string) {
|
||||
func (ds *DynamicStruct) AddInt(fieldName string, fieldTag string, pkgPath string, value int64) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(int64(0)),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
}, value)
|
||||
}
|
||||
|
||||
// AddUint 添加uint字段, 统一 uint64
|
||||
@ -46,13 +77,13 @@ func (ds *DynamicStruct) AddInt(fieldName string, fieldTag string, pkgPath strin
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:50 2024/8/21
|
||||
func (ds *DynamicStruct) AddUint(fieldName string, fieldTag string, pkgPath string) {
|
||||
func (ds *DynamicStruct) AddUint(fieldName string, fieldTag string, pkgPath string, val uint64) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(uint64(0)),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}, val)
|
||||
}
|
||||
|
||||
// AddString 添加字符串字段
|
||||
@ -60,13 +91,13 @@ func (ds *DynamicStruct) AddUint(fieldName string, fieldTag string, pkgPath stri
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:49 2024/8/21
|
||||
func (ds *DynamicStruct) AddString(fieldName string, fieldTag string, pkgPath string) {
|
||||
func (ds *DynamicStruct) AddString(fieldName string, fieldTag string, pkgPath string, val string) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(""),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
}, val)
|
||||
}
|
||||
|
||||
// AddBool 添加bool字段
|
||||
@ -74,13 +105,13 @@ func (ds *DynamicStruct) AddString(fieldName string, fieldTag string, pkgPath st
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:49 2024/8/21
|
||||
func (ds *DynamicStruct) AddBool(fieldName string, fieldTag string, pkgPath string) {
|
||||
func (ds *DynamicStruct) AddBool(fieldName string, fieldTag string, pkgPath string, val bool) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(true),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
}, val)
|
||||
}
|
||||
|
||||
// AddFloat 添加float字段, 统一 float64
|
||||
@ -88,13 +119,13 @@ func (ds *DynamicStruct) AddBool(fieldName string, fieldTag string, pkgPath stri
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:48 2024/8/21
|
||||
func (ds *DynamicStruct) AddFloat(fieldName string, fieldTag string, pkgPath string) {
|
||||
func (ds *DynamicStruct) AddFloat(fieldName string, fieldTag string, pkgPath string, val float64) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(float64(0)),
|
||||
Tag: reflect.StructTag(fmt.Sprintf(`json:"%v"`, fieldTag)),
|
||||
})
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
}, val)
|
||||
}
|
||||
|
||||
// AddSlice 添加slice
|
||||
@ -102,13 +133,13 @@ func (ds *DynamicStruct) AddFloat(fieldName string, fieldTag string, pkgPath str
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:47 2024/8/21
|
||||
func (ds *DynamicStruct) AddSlice(fieldName string, fieldTag string, pkgPath string) {
|
||||
func (ds *DynamicStruct) AddSlice(fieldName string, fieldTag string, pkgPath string, val any) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf([]any{}),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}, val)
|
||||
}
|
||||
|
||||
// AddMap 添加map字段
|
||||
@ -116,13 +147,13 @@ func (ds *DynamicStruct) AddSlice(fieldName string, fieldTag string, pkgPath str
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:43 2024/8/21
|
||||
func (ds *DynamicStruct) AddMap(fieldName string, fieldTag string, pkgPath string) {
|
||||
func (ds *DynamicStruct) AddMap(fieldName string, fieldTag string, pkgPath string, val any) {
|
||||
ds.AddStructField(reflect.StructField{
|
||||
Name: fieldName,
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(map[string]any{}),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}, val)
|
||||
}
|
||||
|
||||
// AddAny 添加任意类型字段
|
||||
@ -140,7 +171,7 @@ func (ds *DynamicStruct) AddAny(fieldName string, fieldTag string, pkgPath strin
|
||||
PkgPath: pkgPath,
|
||||
Type: reflect.TypeOf(value),
|
||||
Tag: reflect.StructTag(fieldTag),
|
||||
})
|
||||
}, value)
|
||||
}
|
||||
|
||||
// AddStructField 添加结构体字段
|
||||
@ -148,12 +179,13 @@ func (ds *DynamicStruct) AddAny(fieldName string, fieldTag string, pkgPath strin
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:42 2024/8/21
|
||||
func (ds *DynamicStruct) AddStructField(field reflect.StructField) {
|
||||
func (ds *DynamicStruct) AddStructField(field reflect.StructField, fieldValue any) {
|
||||
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)
|
||||
ds.SetMapData(field.Name, fieldValue)
|
||||
}
|
||||
|
||||
// GetStructType 获取结构体的类型
|
||||
@ -172,7 +204,18 @@ func (ds *DynamicStruct) GetStructType() reflect.Type {
|
||||
// Date : 16:56 2024/8/21
|
||||
func (ds *DynamicStruct) ToStructDefaultValue() any {
|
||||
defer ds.Clear()
|
||||
defaultValue := reflect.New(ds.GetStructType()).Elem().Interface()
|
||||
reflectValue := reflect.New(ds.GetStructType()).Elem()
|
||||
if len(ds.mapData) > 0 {
|
||||
// 开始赋值
|
||||
for field, val := range ds.mapData {
|
||||
realField := String(field).SnakeCaseToCamel()
|
||||
findFieldValue := reflectValue.FieldByName(realField)
|
||||
if findFieldValue.CanSet() {
|
||||
findFieldValue.Set(reflect.ValueOf(val))
|
||||
}
|
||||
}
|
||||
}
|
||||
defaultValue := reflectValue.Interface()
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
@ -196,3 +239,12 @@ func (ds *DynamicStruct) Clear() {
|
||||
// 清理掉已设置的字段, 不然实例复用会互相影响
|
||||
ds.structFieldList = make([]reflect.StructField, 0)
|
||||
}
|
||||
|
||||
// MarshalJSON 自定义的序列化方法
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:47 2025/1/24
|
||||
func (ds *DynamicStruct) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(ds.mapData)
|
||||
}
|
||||
|
@ -15,8 +15,8 @@ import (
|
||||
|
||||
func TestNewDynamic(t *testing.T) {
|
||||
instance := NewDynamic()
|
||||
instance.AddInt("Age", "age", "")
|
||||
instance.AddString("Name", "name", "")
|
||||
instance.AddInt("Age", "age", "", 0)
|
||||
instance.AddString("Name", "name", "", "test")
|
||||
defaultVal := instance.ToStructDefaultValue()
|
||||
testMap := map[string]any{
|
||||
"name": "白茶",
|
||||
|
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,
|
||||
}
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -5,9 +5,9 @@ go 1.21
|
||||
toolchain go1.21.4
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250227040546-863c03f34bb8
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc
|
||||
git.zhangdeman.cn/zhangdeman/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
|
||||
|
10
go.sum
10
go.sum
@ -4,12 +4,22 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIy
|
||||
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/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e h1:A045F67AMSqFKGD9kk2uLa+6c/zpmW8vjjSRmSsdjPs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241104092308-ecb02113459e/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
|
||||
git.zhangdeman.cn/zhangdeman/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.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
|
Reference in New Issue
Block a user