完成v1版本动态结构体创建

This commit is contained in:
白茶清欢 2024-08-21 18:16:21 +08:00
parent e4cef0855e
commit 18351f3b29
4 changed files with 145 additions and 16 deletions

View File

@ -7,7 +7,10 @@
// Date : 2024-08-21 16:43
package wrapper
import "reflect"
import (
"fmt"
"reflect"
)
func NewDynamic() *DynamicStruct {
return &DynamicStruct{
@ -24,36 +27,132 @@ type DynamicStruct struct {
structFieldList []reflect.StructField // 结构体字段列表
}
func (ds *DynamicStruct) AddInt() {
// 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)),
})
}
func (ds *DynamicStruct) AddUint() {
// 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),
})
}
func (ds *DynamicStruct) AddString() {
// 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)),
})
}
func (ds *DynamicStruct) AddBool() {
// 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)),
})
}
func (ds *DynamicStruct) AddFloat() {
// 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)),
})
}
func (ds *DynamicStruct) AddSlice() {
// 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),
})
}
func (ds *DynamicStruct) AddMap() {
// 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),
})
}
func (ds *DynamicStruct) AddStructField() {
// 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 获取结构体的类型

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)
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=