2024-08-21 17:09:00 +08:00
|
|
|
// Package wrapper ...
|
|
|
|
//
|
|
|
|
// Description : wrapper ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2024-08-21 16:43
|
|
|
|
package wrapper
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
2024-08-21 17:09:00 +08:00
|
|
|
|
|
|
|
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 // 结构体字段列表
|
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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)),
|
|
|
|
})
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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),
|
|
|
|
})
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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)),
|
|
|
|
})
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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)),
|
|
|
|
})
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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)),
|
|
|
|
})
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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),
|
|
|
|
})
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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),
|
|
|
|
})
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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),
|
|
|
|
})
|
|
|
|
}
|
2024-08-21 17:09:00 +08:00
|
|
|
|
2024-08-21 18:16:21 +08:00
|
|
|
// 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)
|
2024-08-21 17:09:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|