99 lines
2.0 KiB
Go
99 lines
2.0 KiB
Go
|
// Package wrapper ...
|
||
|
//
|
||
|
// Description : wrapper ...
|
||
|
//
|
||
|
// Author : go_developer@163.com<白茶清欢>
|
||
|
//
|
||
|
// Date : 2024-08-21 16:43
|
||
|
package wrapper
|
||
|
|
||
|
import "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 // 结构体字段列表
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddInt() {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddUint() {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddString() {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddBool() {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddFloat() {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddSlice() {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddMap() {
|
||
|
|
||
|
}
|
||
|
|
||
|
func (ds *DynamicStruct) AddStructField() {
|
||
|
|
||
|
}
|
||
|
|
||
|
// 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)
|
||
|
}
|