整理代码结构, 增加普通结构体单元测试

This commit is contained in:
2025-03-23 14:20:49 +08:00
parent 7b830c8a9e
commit 9ef4123f38
7 changed files with 210 additions and 108 deletions

31
dynamic_struct.go Normal file
View File

@ -0,0 +1,31 @@
// Package dynamicstruct ...
//
// Description : dynamicstruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-03-23 14:14
package dynamicstruct
import "reflect"
type dynamicStructImpl struct {
structFields []reflect.StructField
definition reflect.Type
}
// New 创建动态结构体实例
func (ds *dynamicStructImpl) New() any {
// 不要指针,全部解引用
return reflect.New(ds.definition).Interface()
}
// NewSliceOfStructs 创建动态结构体切片实例
func (ds *dynamicStructImpl) NewSliceOfStructs() any {
return reflect.New(reflect.SliceOf(ds.definition)).Interface()
}
// NewMapOfStructs 创建动态结构体map实例
func (ds *dynamicStructImpl) NewMapOfStructs(key any) any {
return reflect.New(reflect.MapOf(reflect.Indirect(reflect.ValueOf(key)).Type(), ds.definition)).Interface()
}