整理代码结构, 增加普通结构体单元测试
This commit is contained in:
31
dynamic_struct.go
Normal file
31
dynamic_struct.go
Normal 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()
|
||||
}
|
Reference in New Issue
Block a user