Documentation
¶
Index ¶
- Variables
- func GetAll() (map[string]any, error)
- func GetAs[T any](key string, defaults ...any) (T, error)
- func GetMany(keys map[string]any) (map[string]any, error)
- func GetManyAs[T any]() (*T, error)
- func IsChangedT[T any](e ConfigEvent, key string) (bool, T)
- func Set(key string, value any) error
- func SetMany(cst map[string]any) error
- func Subscribe(subscriber ConfigSubscriber)
- type ConfigEvent
- type ConfigItem
- type ConfigSubscriber
Constants ¶
This section is empty.
Variables ¶
View Source
var ( SetDb = func(gdb *gorm.DB) { db = gdb if err := db.AutoMigrate(&ConfigItem{}); err != nil { panic("failed to migrate config item table: " + err.Error()) } } )
Functions ¶
func GetMany ¶
GetMany 获取多个配置项,keys 为 map[key]defaultValue 如果 defaultValue 为 nil,则数据库不存在时不写入 如果 defaultValue 不为 nil,则数据库不存在时写入默认值
func GetManyAs ¶
GetManyAs 将多个配置项映射到一个结构体中,json tag 作为 Key 支持 default tag 作为默认值,如果数据库中不存在且有 default tag 则写入数据库 没有 default tag 的字段使用零值,不写入数据库
func IsChangedT ¶
func IsChangedT[T any](e ConfigEvent, key string) (bool, T)
func Subscribe ¶
func Subscribe(subscriber ConfigSubscriber)
Subscribe registers a subscriber for all config events.
Types ¶
type ConfigEvent ¶
func (ConfigEvent) IsChanged ¶
func (e ConfigEvent) IsChanged(key string) bool
type ConfigItem ¶
type ConfigItem struct {
Key string `gorm:"primaryKey;column:key;type:text"`
Value string `gorm:"column:value;type:text"` // 存 JSON 字符串
}
func (ConfigItem) TableName ¶
func (ConfigItem) TableName() string
type ConfigSubscriber ¶
type ConfigSubscriber func(event ConfigEvent)
ConfigSubscriber handles config events
Click to show internal directories.
Click to hide internal directories.