dynamic-struct/abstract.go
2025-03-23 17:51:13 +08:00

49 lines
1.4 KiB
Go

// Package dynamicstruct ...
//
// Description : dynamicstruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-03-23 13:34
package dynamicstruct
// IBuilder 运行时动态生成结构体的接口约束
type IBuilder interface {
// AddField 添加结构体字段
AddField(name string, pkg string, typ any, tag string, anonymous bool) IBuilder
// RemoveField 移除指定名称的结构体字段
RemoveField(name string) IBuilder
// HasField 检测指定名称的结构体字段是否存在
HasField(name string) bool
// GetField 根据名称获取结构体字段定义
GetField(name string) IFieldConfig
// Build 返回动态定义的结构体.
Build() IDynamicStruct
}
// IFieldConfig 结构体字段的定义.
type IFieldConfig interface {
// SetType 设置字段类型.
SetType(typ any) IFieldConfig
// SetTag 设置字段 tag.
SetTag(tag string) IFieldConfig
// GetType 获取类型
GetType() any
// GetTag 获取tag
GetTag() string
}
// IDynamicStruct contains defined dynamic struct.
// This definition can't be changed anymore, once is built.
// It provides a method for creating new instances of same defintion.
type IDynamicStruct interface {
// New 获取结构体实例, 所有字段值均为对应类型的初始零值
New() any
// NewSliceOfStructs slice实例化
NewSliceOfStructs() any
// NewMapOfStructs map 或者 struct实例化
NewMapOfStructs(key any) any
}