Compare commits

..

No commits in common. "master" and "feature/support_map_any" have entirely different histories.

7 changed files with 233 additions and 22 deletions

4
any.go
View File

@ -87,7 +87,7 @@ 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
}
@ -116,7 +116,7 @@ func (at *AnyType) ToString() String {
return String(Int(at.data.(int64)).ToString().Value)
case consts.DataTypeUint:
return String(Int(at.data.(uint)).ToString().Value)
case consts.DataTypeFloat64:
case consts.DataTypeFloat:
return String(Float(at.data.(float64)).ToString().Value)
case consts.DataTypeBool:
return String(fmt.Sprintf("%v", at.data))

View File

@ -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 {
str := AnyDataType(item).ToString().Value()
list = append(list, str)
byteData, _ := json.Marshal(item)
list = append(list, strings.Trim(string(byteData), "\""))
}
return list
}

198
dynamic_struct.go Normal file
View File

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

27
dynamic_struct_test.go Normal file
View File

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

View File

@ -107,7 +107,7 @@ func (f Float) ToString() StringResult {
}
}
return StringResult{
Value: fmt.Sprintf("%v", floatVal.Value),
Value: fmt.Sprintf("%v", floatVal),
Err: nil,
}
}

6
go.mod
View File

@ -5,9 +5,9 @@ go 1.21
toolchain go1.21.4
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250321102241-d6e86b64f7ca
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc
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
@ -17,7 +17,7 @@ require (
)
require (
github.com/BurntSushi/toml v1.5.0 // 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/mitchellh/go-homedir v1.1.0 // indirect

14
go.sum
View File

@ -4,30 +4,16 @@ 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/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-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=
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=