feat(gcfg): add Configurator with automatic struct binding and config watching (like Spring Boot @ConfigurationProperties) #4575
+498
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Configurator 配置管理器
Configurator 是一个通用的配置管理器,提供了类似于 Spring Boot 的
@ConfigurationProperties的配置加载、监控、更新和管理功能。功能特性
安装
使用示例
1. 基本用法
用法一
用法二
2. 配置监控
3. 自定义转换器
4. 便捷方法
API 参考
NewConfigurator创建一个新的 Configurator 实例。
参数:
config: 配置实例,用于监控变化propertyKey: 监控的属性键模式(使用 "" 或 "." 监控所有配置)targetStruct: 接收配置值的结构体指针(可选)NewConfiguratorWithAdapter使用适配器创建一个新的 Configurator 实例。
Load从配置实例加载数据并绑定到目标结构体。
MustLoad与 Load 类似,但出错时会 panic。
Watch开始监控配置变化并自动更新目标结构体。
MustWatch与 Watch 类似,但出错时会 panic。
MustLoadAndWatch便捷方法,调用 MustLoad 和 MustWatch。
Get返回当前配置结构体。
GetPointer() *T返回指向当前配置结构体的指针。
OnChange设置配置变化时调用的回调函数。
SetConverter设置在 Load 操作期间使用的自定义转换函数。
SetLoadErrorHandler设置 Load 操作失败时调用的错误处理函数。
SetReuseTargetStruct设置是否在更新时重用相同的目标结构体或创建新结构体。
高级用法
监控特定配置键
使用默认值
错误处理
Configurator 提供了灵活的错误处理机制:
许可证
MIT License