From 0732f2e03f40c81c94a9ba7226a77a9205c440ec Mon Sep 17 00:00:00 2001 From: AI Engineer Date: Thu, 4 Jun 2026 13:33:54 +0800 Subject: [PATCH] publish v1.5.1 --- CHANGELOG.md | 7 +++++++ config.go | 6 +++++- config_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 842d1e1..17cc14a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # 更新日志 (Changelog) +## [1.5.1] - 2026-06-04 +- **修复**: 优化 `config.Load` 加载逻辑,支持在 `env.yaml` 中通过 `name` 作为 key 查找并加载特定配置块(如 `service:`)。 +- **变更**: 补充 `TestConfig_EnvYamlWithPrefix` 测试用例验证前缀加载功能。 + +## [1.5.0] - 2026-05-10 +- **变更**: 基础设施全局对齐发布。 + ## [1.0.7] - 2026-05-09 - **修复**: 在 `resolveConfigPath` 中补充了对无后缀 `.yaml` 配置文件的支持。 - **变更**: 将 `apigo.cc/go/file` 的依赖升级至 `v1.0.7` 以对齐基础设施。 diff --git a/config.go b/config.go index 1ff6bef..e9e6348 100644 --- a/config.go +++ b/config.go @@ -60,7 +60,11 @@ func Load(conf interface{}, name string) error { if envFile != "" { envConf := map[string]interface{}{} if err := file.UnmarshalFile(envFile, &envConf); err == nil { - cast.Convert(conf, envConf) + if val, ok := envConf[name]; ok { + cast.Convert(conf, val) + } else { + cast.Convert(conf, envConf) + } } else { errs = append(errs, err) } diff --git a/config_test.go b/config_test.go index 3d13c0f..7f3627e 100644 --- a/config_test.go +++ b/config_test.go @@ -60,3 +60,31 @@ func TestConfig_ComplexStruct(t *testing.T) { t.Errorf("Duration failed, got %v", conf.Duration.TimeDuration()) } } + +func TestConfig_EnvYamlWithPrefix(t *testing.T) { + os.Clearenv() + // Create env.yaml with a prefixed block + os.WriteFile("env.yaml", []byte(` +service: + app: "emp" + listen: ":3000" +`), 0644) + defer os.Remove("env.yaml") + + type ServiceConfig struct { + App string + Listen string + } + + var conf ServiceConfig + if err := config.Load(&conf, "service"); err != nil { + t.Errorf("Load failed: %v", err) + } + + if conf.App != "emp" { + t.Errorf("App mismatch: expected emp, got %s", conf.App) + } + if conf.Listen != ":3000" { + t.Errorf("Listen mismatch: expected :3000, got %s", conf.Listen) + } +}