rename LoadConfig to Load(by AI)
This commit is contained in:
parent
f29b426c2d
commit
254aff7873
@ -1,7 +1,7 @@
|
||||
# 更新日志 (Changelog)
|
||||
|
||||
## [1.0.4] - 2026-05-02
|
||||
- **变更**: 重构了 `LoadConfig`,使用 `errors.Join` 改进了错误处理。
|
||||
- **变更**: 重构了 `Load`,使用 `errors.Join` 改进了错误处理。
|
||||
- **变更**: 将 `findFile` 重命名为 `resolveConfigFilePath` 以提高代码清晰度。
|
||||
- **变更**: 更新了测试套件以验证错误返回,并移除了重复的类型定义。
|
||||
- **变更**: 新增 `bench_test.go` 用于性能评估。
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
`config` 模块负责从文件(JSON/YAML)加载配置,并支持通过环境变量进行覆盖。
|
||||
|
||||
## API 指南
|
||||
### `LoadConfig(name string, conf interface{}) error`
|
||||
### `Load(name string, conf interface{}) error`
|
||||
从当前目录、可执行文件目录或用户主目录中加载 `name.json` 或 `name.yml`。随后,它会尝试加载 `env.json` 或 `env.yml` 并使用环境变量进行最终覆盖。
|
||||
|
||||
## 环境变量覆盖规则
|
||||
|
||||
2
TEST.md
2
TEST.md
@ -17,7 +17,7 @@
|
||||
goos: darwin
|
||||
goarch: amd64
|
||||
pkg: apigo.cc/go/config
|
||||
BenchmarkLoadConfig-16 9104 123326 ns/op 5136 B/op 104 allocs/op
|
||||
BenchmarkLoad-16 9104 123326 ns/op 5136 B/op 104 allocs/op
|
||||
BenchmarkApplyEnvOverrides-16 260586 4700 ns/op 2657 B/op 64 allocs/op
|
||||
```
|
||||
|
||||
|
||||
@ -5,18 +5,18 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func BenchmarkLoadConfig(b *testing.B) {
|
||||
func BenchmarkLoad(b *testing.B) {
|
||||
os.WriteFile("bench.json", []byte(`{"name":"bench-test","sets":[1,2,3]}`), 0644)
|
||||
defer os.Remove("bench.json")
|
||||
|
||||
|
||||
var conf struct {
|
||||
Name string
|
||||
Sets []int
|
||||
}
|
||||
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = LoadConfig("bench", &conf)
|
||||
_ = Load("bench", &conf)
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ func BenchmarkApplyEnvOverrides(b *testing.B) {
|
||||
os.Setenv("APP_DB_HOST", "localhost")
|
||||
os.Setenv("APP_DB_PORT", "5432")
|
||||
os.Setenv("APP_SERVER_TIMEOUT", "30s")
|
||||
|
||||
|
||||
var conf struct {
|
||||
App struct {
|
||||
Db struct {
|
||||
@ -36,7 +36,7 @@ func BenchmarkApplyEnvOverrides(b *testing.B) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
applyEnvOverrides(&conf)
|
||||
|
||||
@ -43,9 +43,9 @@ func (d Duration) TimeDuration() time.Duration {
|
||||
return time.Duration(d)
|
||||
}
|
||||
|
||||
// LoadConfig loads configuration from a file, then applies env.json/yaml if exists,
|
||||
// Load loads configuration from a file, then applies env.json/yaml if exists,
|
||||
// and finally overrides with environment variables.
|
||||
func LoadConfig(name string, conf interface{}) error {
|
||||
func Load(name string, conf interface{}) error {
|
||||
var errs []error
|
||||
|
||||
// 1. Load base file
|
||||
@ -118,7 +118,7 @@ func applyEnvOverrides(conf interface{}) {
|
||||
if len(pair) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Convert DB_HOST to {"db": {"host": value}}
|
||||
parts := strings.Split(strings.ToLower(pair[0]), "_")
|
||||
curr := envMap
|
||||
|
||||
@ -38,8 +38,8 @@ func TestConfig_ComplexStruct(t *testing.T) {
|
||||
os.Setenv("LIST_BBB", "{\"name\":\"xxx\"}")
|
||||
|
||||
var conf testConfType
|
||||
if err := LoadConfig("complex", &conf); err != nil {
|
||||
t.Errorf("LoadConfig failed: %v", err)
|
||||
if err := Load("complex", &conf); err != nil {
|
||||
t.Errorf("Load failed: %v", err)
|
||||
}
|
||||
|
||||
if conf.Name != "test-config" {
|
||||
|
||||
@ -20,13 +20,13 @@ func TestForMap_Regression(t *testing.T) {
|
||||
os.Clearenv()
|
||||
os.WriteFile("test.json", []byte(`{"name":"test-config"}`), 0644)
|
||||
defer os.Remove("test.json")
|
||||
|
||||
|
||||
os.Setenv("TEST_LIST_CCC", "333")
|
||||
|
||||
|
||||
testConf := map[string]interface{}{}
|
||||
err := LoadConfig("test", &testConf)
|
||||
err := Load("test", &testConf)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig failed: %v", err)
|
||||
t.Fatalf("Load failed: %v", err)
|
||||
}
|
||||
if testConf["name"] != "test-config" {
|
||||
t.Errorf("Expected name test-config, got %v", testConf["name"])
|
||||
@ -42,13 +42,13 @@ func TestForStruct_Regression(t *testing.T) {
|
||||
"duration": "100s"
|
||||
}`), 0644)
|
||||
defer os.Remove("test.json")
|
||||
|
||||
|
||||
os.Setenv("LIST_CCC", "{\"name\":\"333\"}")
|
||||
os.Setenv("LIST_DDD", "{\"name\":\"444\"}")
|
||||
|
||||
var testConf RegressionConf
|
||||
if err := LoadConfig("test", &testConf); err != nil {
|
||||
t.Errorf("LoadConfig failed: %v", err)
|
||||
if err := Load("test", &testConf); err != nil {
|
||||
t.Errorf("Load failed: %v", err)
|
||||
}
|
||||
|
||||
if testConf.Name != "test-config" || len(testConf.Sets) != 3 || testConf.Sets[1] != 2 {
|
||||
@ -79,8 +79,8 @@ extList: ["a", "222", {val: "111"}]
|
||||
defer os.Remove("test2.yml")
|
||||
|
||||
var testConf RegressionConf
|
||||
if err := LoadConfig("test2", &testConf); err != nil {
|
||||
t.Errorf("LoadConfig failed: %v", err)
|
||||
if err := Load("test2", &testConf); err != nil {
|
||||
t.Errorf("Load failed: %v", err)
|
||||
}
|
||||
|
||||
if testConf.Name != "test-config" || len(testConf.List2) != 2 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user