-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(gcfg): add Configurator with automatic struct binding and config watching (like Spring Boot @ConfigurationProperties) #4575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LanceAdd
wants to merge
16
commits into
gogf:master
Choose a base branch
from
LanceAdd:feature/gprop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+652
−22
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 实现了 Configurator 泛型结构体用于配置绑定 - 支持从配置文件或内容字符串创建配置器 - 提供配置加载、监听和管理功能 - 实现了线程安全的操作机制 - 支持自定义转换器和错误处理函数 - 提供配置变更回调功能 - 添加了完整的单元测试覆盖 - 包含详细的使用文档和示例代码
- 实现了 GetPointer 方法用于获取当前配置结构体的指针 - 该方法是线程安全的,支持读操作但不应被修改 - 添加了相应的注释文档说明方法用途和安全性
Member
|
建议在example中补充一下示例操作代码,谢谢 @LanceAdd |
Member
Author
是这个example吗?https://github.com/gogf/examples |
Member
是的 |
- 添加 watcherName 字段用于跟踪监视器名称 - 在 Watch 方法中验证监视器名称不为空 - 添加 StopWatch 方法用于停止配置变更监视 - 添加 IsWatching 方法用于检查当前是否正在监视 - 添加相关单元测试验证监视器启停功能
- 将所有配置客户端的 AddWatcher 方法参数类型从 func(ctx context.Context) 修改为 gcfg.WatcherFunc - 在 gcfg 包中定义 WatcherFunc 类型别名,提高代码类型安全性 - 修改 WatcherRegistry 内部存储结构,使用泛型 KVMap 替代 StrAnyMap - 简化 WatcherRegistry.Notify 方法中的类型断言和迭代逻辑 - 移除多余的类型转换,使代码更加简洁和类型安全
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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