抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
This commit is contained in:
commit
b4c3ac622c
55 files changed
+5862
No files matched your search
@@ -0,0 +1,67 @@
|
||||
// Package config 提供 YAML 配置读取,不发布应用配置或修改进程环境。
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
fault "git.zhangdeman.cn/github/ginframework/internal/failure"
|
||||
"github.com/goccy/go-yaml"
|
||||
)
|
||||
|
||||
const (
|
||||
// CodeTarget 表示配置接收对象无效。
|
||||
CodeTarget = 900001
|
||||
// CodeRead 表示读取配置文件失败。
|
||||
CodeRead = 900002
|
||||
// CodeDecode 表示配置内容解析失败。
|
||||
CodeDecode = 900003
|
||||
// CodeDocuments 表示配置包含多份 YAML 文档。
|
||||
CodeDocuments = 900004
|
||||
)
|
||||
|
||||
// Options 是配置读取行为;不会加载 .env 或执行应用校验。
|
||||
type Options struct {
|
||||
AllowUnknownFields bool `json:"allow_unknown_fields" dc:"是否接受未在接收结构体声明的配置项"`
|
||||
DisableEnvExpansion bool `json:"disable_env_expansion" dc:"是否禁用已有进程环境变量展开"`
|
||||
}
|
||||
|
||||
// Load 读取单份 YAML 到调用方结构体,默认拒绝未知字段。
|
||||
// 入参:path 为文件路径,target 为非空结构体指针,options 为解码选项。
|
||||
// 返回:读取或解析失败返回内部场景错误,不输出配置内容。
|
||||
func Load(path string, target any, options Options) error {
|
||||
value := reflect.ValueOf(target)
|
||||
if !value.IsValid() || value.Kind() != reflect.Ptr || value.IsNil() || value.Elem().Kind() != reflect.Struct {
|
||||
return fault.Wrap(CodeTarget, "validate configuration receiver failed", errors.New("configuration target must be a non-nil struct pointer"))
|
||||
}
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return fault.Wrap(CodeRead, "read configuration file failed", err)
|
||||
}
|
||||
if !options.DisableEnvExpansion {
|
||||
content = []byte(os.ExpandEnv(string(content)))
|
||||
}
|
||||
var (
|
||||
decodeOptions []yaml.DecodeOption
|
||||
)
|
||||
if !options.AllowUnknownFields {
|
||||
decodeOptions = append(decodeOptions, yaml.DisallowUnknownField())
|
||||
}
|
||||
decoder := yaml.NewDecoder(bytes.NewReader(content), decodeOptions...)
|
||||
if err = decoder.Decode(target); err != nil {
|
||||
return fault.Wrap(CodeDecode, "decode YAML configuration failed", err)
|
||||
}
|
||||
var (
|
||||
extra any
|
||||
)
|
||||
if err = decoder.Decode(&extra); !errors.Is(err, io.EOF) {
|
||||
if err == nil {
|
||||
err = errors.New("multiple YAML documents are not supported")
|
||||
}
|
||||
return fault.Wrap(CodeDocuments, "validate YAML document count failed", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/fault"
|
||||
)
|
||||
|
||||
func TestStrictEnvironmentAndOptionalValues(t *testing.T) {
|
||||
t.Setenv("GINFRAMEWORK_TEST_VALUE", "loaded")
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
if err := os.WriteFile(path, []byte("name: ${GINFRAMEWORK_TEST_VALUE}\ncount: 0\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var (
|
||||
target struct {
|
||||
Name string `yaml:"name"`
|
||||
Count *int `yaml:"count"`
|
||||
}
|
||||
)
|
||||
if err := Load(path, &target, Options{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if target.Name != "loaded" || target.Count == nil || *target.Count != 0 {
|
||||
t.Fatalf("configuration lost values: %+v", target)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("unknown: yes\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Load(path, &target, Options{}); !fault.Internal(err) {
|
||||
t.Fatalf("missing internal error: %v", err)
|
||||
}
|
||||
if err := Load(path, &target, Options{AllowUnknownFields: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadCauseAndMultipleDocuments(t *testing.T) {
|
||||
var (
|
||||
target struct {
|
||||
Name string `yaml:"name"`
|
||||
}
|
||||
)
|
||||
if err := Load(filepath.Join(t.TempDir(), "missing.yml"), &target, Options{}); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("cause lost: %v", err)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
if err := os.WriteFile(path, []byte("name: first\n---\nname: second\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := Load(path, &target, Options{}); err == nil {
|
||||
t.Fatal("accepted multiple configuration documents")
|
||||
}
|
||||
}
|
||||