diff --git a/dynamic_struct.go b/dynamic_struct.go new file mode 100644 index 0000000..904fe09 --- /dev/null +++ b/dynamic_struct.go @@ -0,0 +1,197 @@ +// 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)) + } + 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 new file mode 100644 index 0000000..af3cfec --- /dev/null +++ b/dynamic_struct_test.go @@ -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) +} diff --git a/go.mod b/go.mod index dcc9a44..e21ee1b 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( 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 diff --git a/go.sum b/go.sum index 4a51eac..edc9cc6 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ 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/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 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=