feature/upgrade_dynamic_struct #9

Merged
zhangdeman merged 9 commits from feature/upgrade_dynamic_struct into master 2025-03-21 18:27:12 +08:00
Showing only changes of commit 981ff94388 - Show all commits

View File

@ -11,8 +11,14 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"reflect" "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 { func NewMap2DynamicStruct(mapData map[string]any, tagTable map[string]string) *DynamicStruct {
d := NewDynamic() d := NewDynamic()
d.mapData = mapData d.mapData = mapData
@ -26,6 +32,7 @@ func NewDynamic() *DynamicStruct {
return &DynamicStruct{ return &DynamicStruct{
structFieldList: make([]reflect.StructField, 0), structFieldList: make([]reflect.StructField, 0),
mapData: make(map[string]any), mapData: make(map[string]any),
l: &sync.RWMutex{},
} }
} }
@ -37,6 +44,18 @@ func NewDynamic() *DynamicStruct {
type DynamicStruct struct { type DynamicStruct struct {
structFieldList []reflect.StructField // 结构体字段列表 structFieldList []reflect.StructField // 结构体字段列表
mapData map[string]any 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 // AddInt 添加int字段统一Int64
@ -44,7 +63,7 @@ type DynamicStruct struct {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:50 2024/8/21 // 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{ ds.AddStructField(reflect.StructField{
Name: fieldName, Name: fieldName,
PkgPath: pkgPath, PkgPath: pkgPath,