From fd2ac6079b5ea3f7ebe7d666dc9b4d4169a34433 Mon Sep 17 00:00:00 2001 From: AI Engineer Date: Sat, 9 May 2026 13:22:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20=E8=A1=A5=E5=85=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=97=A0=E5=90=8E=E7=BC=80=20.yaml=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E5=8A=A0=E8=BD=BD=E5=B9=B6=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=20file=20=E4=BE=9D=E8=B5=96=E8=87=B3=20v1.0.7=20(by?= =?UTF-8?q?=20AI)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 8 ++++++++ TEST.md | 5 +++-- config.go | 5 ++++- go.mod | 4 ++-- yaml_test.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 yaml_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 003a40c..842d1e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # 更新日志 (Changelog) +## [1.0.7] - 2026-05-09 +- **修复**: 在 `resolveConfigPath` 中补充了对无后缀 `.yaml` 配置文件的支持。 +- **变更**: 将 `apigo.cc/go/file` 的依赖升级至 `v1.0.7` 以对齐基础设施。 +- **变更**: 补充针对无后缀 `.yaml` 文件的回归测试 `TestForYaml_Support`。 + +## [1.0.6] - 2026-05-06 +- **变更**: 修复与基础设施的兼容性。 + ## [1.0.5] - 2026-05-05 - **变更**: 基础设施对齐与生态系统同步发布。 diff --git a/TEST.md b/TEST.md index bab64db..a98ff5d 100644 --- a/TEST.md +++ b/TEST.md @@ -10,6 +10,7 @@ - `TestForMap_Regression`: 验证基础的基于 Map 的配置加载。 - `TestForStruct_Regression`: 验证基于结构体的映射及环境变量覆盖。 - `TestForYml_Regression`: 检查 YAML 配置解析,包括接口切片和映射。 + - `TestForYaml_Support`: 验证针对无后缀 `.yaml` 文件的默认解析支持。 ## 基准测试结果 @@ -17,8 +18,8 @@ goos: darwin goarch: amd64 pkg: apigo.cc/go/config -BenchmarkLoad-16 9104 123326 ns/op 5136 B/op 104 allocs/op -BenchmarkApplyEnvOverrides-16 260586 4700 ns/op 2657 B/op 64 allocs/op +BenchmarkLoad-16 6255 164111 ns/op +BenchmarkApplyEnvOverrides-16 294292 3592 ns/op ``` *注:基准测试反映了在 Intel i9-9980HK 上的性能表现。* diff --git a/config.go b/config.go index 6e791d3..1ff6bef 100644 --- a/config.go +++ b/config.go @@ -89,10 +89,13 @@ func resolveConfigPath(name string) string { return "" } - // Otherwise, check for .yml then .json + // Otherwise, check for .yml, .yaml, then .json if file.Exists(name + ".yml") { return name + ".yml" } + if file.Exists(name + ".yaml") { + return name + ".yaml" + } if file.Exists(name + ".json") { return name + ".json" } diff --git a/go.mod b/go.mod index 1d5fff2..a9f3bf4 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module apigo.cc/go/config go 1.25.0 require ( - apigo.cc/go/cast v1.2.7 - apigo.cc/go/file v1.0.6 + apigo.cc/go/cast v1.2.8 + apigo.cc/go/file v1.0.7 ) require ( diff --git a/yaml_test.go b/yaml_test.go new file mode 100644 index 0000000..34a4e38 --- /dev/null +++ b/yaml_test.go @@ -0,0 +1,30 @@ +package config_test + +import ( + "os" + "testing" + + "apigo.cc/go/config" +) + +func TestForYaml_Support(t *testing.T) { + os.Clearenv() + // create a test.yaml without extension in the load call + os.WriteFile("test3.yaml", []byte(` +name: yaml-config +sets: [4, 5, 6] +`), 0644) + defer os.Remove("test3.yaml") + + var testConf RegressionConf + if err := config.Load(&testConf, "test3"); err != nil { + t.Errorf("Load failed: %v", err) + } + + if testConf.Name != "yaml-config" { + t.Errorf("Expected name yaml-config, got %v", testConf.Name) + } + if len(testConf.Sets) != 3 || testConf.Sets[0] != 4 { + t.Errorf("Sets failed: %v", testConf.Sets) + } +}