diff --git a/any.go b/any.go index 641f832..600e6da 100644 --- a/any.go +++ b/any.go @@ -87,7 +87,7 @@ func (at *AnyType) Type() consts.DataType { case reflect.Bool: return consts.DataTypeBool case reflect.Float32, reflect.Float64: - return consts.DataTypeFloat + return consts.DataTypeFloat64 default: return consts.DataTypeUnknown } @@ -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.DataTypeFloat: + case consts.DataTypeFloat64: return String(Float(at.data.(float64)).ToString().Value) case consts.DataTypeBool: return String(fmt.Sprintf("%v", at.data)) diff --git a/dynamic_struct.go b/dynamic_struct.go deleted file mode 100644 index 8e89604..0000000 --- a/dynamic_struct.go +++ /dev/null @@ -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) -} diff --git a/dynamic_struct_test.go b/dynamic_struct_test.go deleted file mode 100644 index af3cfec..0000000 --- a/dynamic_struct_test.go +++ /dev/null @@ -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) -} diff --git a/go.mod b/go.mod index 70f06d7..30b05c5 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 toolchain go1.21.4 require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250227040546-863c03f34bb8 + 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-20241223084948-de2e49144fcd git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e @@ -17,7 +17,7 @@ require ( ) require ( - github.com/BurntSushi/toml v1.4.0 // indirect + github.com/BurntSushi/toml v1.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect diff --git a/go.sum b/go.sum index fe7f78d..2fc674e 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250122075709-5ecf3edb4a00 h1:obyJF0 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= @@ -24,6 +26,8 @@ git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6Cc 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=