From c50318702237805ac4c068eaed13f0396d5f3e90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 24 Jan 2025 18:06:29 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=BD=93=E6=94=AF=E6=8C=81=E8=B5=8B=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dynamic_struct.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/dynamic_struct.go b/dynamic_struct.go index 8e89604..428f00d 100644 --- a/dynamic_struct.go +++ b/dynamic_struct.go @@ -12,6 +12,15 @@ import ( "reflect" ) +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], "", v) + } + return d +} + func NewDynamic() *DynamicStruct { return &DynamicStruct{ structFieldList: make([]reflect.StructField, 0), @@ -25,6 +34,7 @@ func NewDynamic() *DynamicStruct { // Date : 16:48 2024/8/21 type DynamicStruct struct { structFieldList []reflect.StructField // 结构体字段列表 + mapData map[string]any } // AddInt 添加int字段统一Int64 @@ -172,7 +182,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 } From bbf5184314a3c49149558e458cacff9228ef1b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 24 Jan 2025 18:23:01 +0800 Subject: [PATCH 2/8] update --- dynamic_struct.go | 1 + 1 file changed, 1 insertion(+) diff --git a/dynamic_struct.go b/dynamic_struct.go index 428f00d..b0961de 100644 --- a/dynamic_struct.go +++ b/dynamic_struct.go @@ -24,6 +24,7 @@ func NewMap2DynamicStruct(mapData map[string]any, tagTable map[string]string) *D func NewDynamic() *DynamicStruct { return &DynamicStruct{ structFieldList: make([]reflect.StructField, 0), + mapData: make(map[string]any), } } From febf5a63ad1c94aa879f5da71b34420288388275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 24 Jan 2025 18:48:24 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=BD=93=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dynamic_struct.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dynamic_struct.go b/dynamic_struct.go index b0961de..db99de2 100644 --- a/dynamic_struct.go +++ b/dynamic_struct.go @@ -8,6 +8,7 @@ package wrapper import ( + "encoding/json" "fmt" "reflect" ) @@ -218,3 +219,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) +} From 981ff94388dd13381259ca2acdcb51b02f4497e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 24 Jan 2025 18:51:16 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=BD=93=E5=A2=9E=E5=8A=A0SetMapData=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dynamic_struct.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/dynamic_struct.go b/dynamic_struct.go index db99de2..bd5746e 100644 --- a/dynamic_struct.go +++ b/dynamic_struct.go @@ -11,8 +11,14 @@ 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 @@ -26,6 +32,7 @@ func NewDynamic() *DynamicStruct { return &DynamicStruct{ structFieldList: make([]reflect.StructField, 0), mapData: make(map[string]any), + l: &sync.RWMutex{}, } } @@ -37,6 +44,18 @@ func NewDynamic() *DynamicStruct { 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 @@ -44,7 +63,7 @@ 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 int) { ds.AddStructField(reflect.StructField{ Name: fieldName, PkgPath: pkgPath, From 743acb53f20715c0d6a33077f0d33d53695487ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 7 Feb 2025 12:20:52 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BD=93=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dynamic_struct.go | 41 +++++++++++++++++++++-------------------- dynamic_struct_test.go | 4 ++-- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/dynamic_struct.go b/dynamic_struct.go index bd5746e..29d9d65 100644 --- a/dynamic_struct.go +++ b/dynamic_struct.go @@ -23,7 +23,7 @@ func NewMap2DynamicStruct(mapData map[string]any, tagTable map[string]string) *D d := NewDynamic() d.mapData = mapData for k, v := range mapData { - d.AddAny(String(k).SnakeCaseToCamel(), tagTable[k], "", v) + d.AddAny(String(k).SnakeCaseToCamel(), tagTable[k], "dynamic_struct", v) } return d } @@ -63,13 +63,13 @@ func (ds *DynamicStruct) SetMapData(field string, value any) { // Author : go_developer@163.com<白茶清欢> // // Date : 17:50 2024/8/21 -func (ds *DynamicStruct) AddInt(fieldName string, fieldTag string, pkgPath string, value int) { +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 @@ -77,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 添加字符串字段 @@ -91,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字段 @@ -105,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 @@ -119,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 @@ -133,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字段 @@ -147,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 添加任意类型字段 @@ -171,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 添加结构体字段 @@ -179,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 获取结构体的类型 diff --git a/dynamic_struct_test.go b/dynamic_struct_test.go index af3cfec..2b3193a 100644 --- a/dynamic_struct_test.go +++ b/dynamic_struct_test.go @@ -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": "白茶", From beb8c74e8a0f0e24feb378fb1f2030c9bd3c28ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 18 Mar 2025 16:44:03 +0800 Subject: [PATCH 6/8] update go mod --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 70f06d7..5ca3dae 100644 --- a/go.mod +++ b/go.mod @@ -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..6518d6a 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,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= From 2c73b9c034188ed73c8d0a59702fe8599ff7ad17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 18 Mar 2025 18:20:59 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmap=E6=95=B0=E6=8D=AEkey?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20+=20=E8=8E=B7=E5=8F=96map=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dynamic_struct.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dynamic_struct.go b/dynamic_struct.go index 29d9d65..f3e81cf 100644 --- a/dynamic_struct.go +++ b/dynamic_struct.go @@ -183,9 +183,9 @@ func (ds *DynamicStruct) AddStructField(field reflect.StructField, fieldValue an if field.Tag == "" { field.Tag = reflect.StructTag(fmt.Sprintf(`json:"%v"`, field.Name)) } + ds.SetMapData(field.Name, fieldValue) field.Name = String(field.Name).SnakeCaseToCamel() // 转成大驼峰, 保证对外可访问 ds.structFieldList = append(ds.structFieldList, field) - ds.SetMapData(field.Name, fieldValue) } // GetStructType 获取结构体的类型 @@ -248,3 +248,8 @@ func (ds *DynamicStruct) Clear() { func (ds *DynamicStruct) MarshalJSON() ([]byte, error) { return json.Marshal(ds.mapData) } + +// MapData map数据 +func (ds *DynamicStruct) MapData() (map[string]any) { + return ds.mapData +} From 8de0d9b94281ecf2f97a1a34d3630ffc1f2ebd98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 21 Mar 2025 18:26:46 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=8D=87=E7=BA=A7consts=20pkg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- any.go | 4 +- dynamic_struct.go | 255 ----------------------------------------- dynamic_struct_test.go | 27 ----- go.mod | 2 +- go.sum | 2 + 5 files changed, 5 insertions(+), 285 deletions(-) delete mode 100644 dynamic_struct.go delete mode 100644 dynamic_struct_test.go 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 f3e81cf..0000000 --- a/dynamic_struct.go +++ /dev/null @@ -1,255 +0,0 @@ -// Package wrapper ... -// -// Description : wrapper ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2024-08-21 16:43 -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{}, - } -} - -// DynamicStruct 动态生成数据结构 -// -// Author : go_developer@163.com<白茶清欢> -// -// 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 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:50 2024/8/21 -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(fieldTag), - }, value) -} - -// AddUint 添加uint字段, 统一 uint64 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:50 2024/8/21 -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 添加字符串字段 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:49 2024/8/21 -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(fieldTag), - }, val) -} - -// AddBool 添加bool字段 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:49 2024/8/21 -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 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:48 2024/8/21 -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(fieldTag), - }, val) -} - -// AddSlice 添加slice -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:47 2024/8/21 -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字段 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:43 2024/8/21 -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 添加任意类型字段 -// -// 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), - }, value) -} - -// AddStructField 添加结构体字段 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:42 2024/8/21 -func (ds *DynamicStruct) AddStructField(field reflect.StructField, fieldValue any) { - if field.Tag == "" { - field.Tag = reflect.StructTag(fmt.Sprintf(`json:"%v"`, field.Name)) - } - ds.SetMapData(field.Name, fieldValue) - 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() - 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 -} - -// 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) -} - -// MarshalJSON 自定义的序列化方法 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:47 2025/1/24 -func (ds *DynamicStruct) MarshalJSON() ([]byte, error) { - return json.Marshal(ds.mapData) -} - -// MapData map数据 -func (ds *DynamicStruct) MapData() (map[string]any) { - return ds.mapData -} diff --git a/dynamic_struct_test.go b/dynamic_struct_test.go deleted file mode 100644 index 2b3193a..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", "", 0) - instance.AddString("Name", "name", "", "test") - 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 5ca3dae..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 diff --git a/go.sum b/go.sum index 6518d6a..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=