Documentation
¶
Overview ¶
Package kconf 是一个通用的多格式配置文件读取库。
核心能力:
- 自动识别文件格式(json / json5 / jsonc / yaml / toml / hcl)
- 支持注册自定义格式
- 读取到 map[string]any(自由结构)
- 读取到任意结构体
- 纯数据读取,不做任何业务假设
Index ¶
- func DecodeMapToStruct(src map[string]any, dst any) error
- func Detect(filePath string) (string, error)
- func Read(filePath string, opts ...Option) (map[string]any, error)
- func ReadRaw(filePath string) ([]byte, error)
- func ReadTo(filePath string, target any, opts ...Option) error
- func Register(ext string, fn UnmarshalFunc)
- func RegisterMarshal(ext string, fn MarshalFunc)
- func Save(filePath string, v any) error
- type Duration
- type MarshalFunc
- type Option
- type ProcessFunc
- type Processor
- type UnmarshalFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeMapToStruct ¶
DecodeMapToStruct 将 map[string]any 解码到指定的目标结构体中。 对应你项目中 process() 里 mapstructure.Decode(anyMap, &xxxConf) 的用法。
典型场景(Processor 内使用):
kconf.WithProcessor(kconf.ProcessFunc(func(v any) error {
cfg := v.(*AppConfig)
for _, db := range cfg.Database {
if m, ok := db.Item.(map[string]any); ok {
switch db.DBType {
case "redis":
var redisConf RedisConfig
if err := kconf.DecodeMapToStruct(m, &redisConf); err != nil {
return err
}
db.Item = &redisConf
}
}
}
return nil
}))
func Read ¶
Read 读取配置文件并以 map[string]any 的形式返回。 配置内容不限定结构,适用于"配置内容不固定"的场景。
cfg, err := kconf.Read("config.yaml")
host := cfg["host"].(string)
// 带后处理
cfg, err := kconf.Read("config.yaml",
kconf.WithProcessor(myProcessor))
func ReadTo ¶
ReadTo 读取配置文件并解码到指定的目标结构体中。 适合已知配置结构的场景。target 必须是一个指针。
var cfg MyConfig
err := kconf.ReadTo("config.yaml", &cfg)
// 带后处理
err := kconf.ReadTo("config.yaml", &cfg,
kconf.WithProcessor(myProcessor))
func Register ¶
func Register(ext string, fn UnmarshalFunc)
Register 注册自定义文件后缀对应的反序列化函数。 注册后会覆盖后缀相同的内置处理器。
ext: 文件后缀,例如 ".properties" / ".conf" fn: 反序列化函数
func RegisterMarshal ¶
func RegisterMarshal(ext string, fn MarshalFunc)
RegisterMarshal 注册自定义文件后缀对应的序列化函数。
Types ¶
type Duration ¶ added in v0.1.1
Duration 时长配置类型, 底层为纳秒数。
支持两种写法(各格式通用):
- 带单位字符串: "2s" "500ms" "100us" "50ns" (time.ParseDuration 支持的格式)
- 纯数字: 1000 (一律视为毫秒)
判断依据是目标字段的声明类型: 声明为 kconf.Duration 的字段 才应用时长语义, 普通 int / string 字段不参与任何转换。
json / json5 / jsonc / yaml / toml 通过 Unmarshal 接口解析; hcl 及 DecodeMapToStruct 通过 durationDecodeHook 解析。
func ParseDuration ¶ added in v0.1.1
ParseDuration 解析时长字符串: 带单位直接解析, 纯数字按毫秒处理。
func (*Duration) UnmarshalJSON ¶ added in v0.1.1
UnmarshalJSON 实现 json / json5 / jsonc 格式的反序列化。
func (*Duration) UnmarshalTOML ¶ added in v0.1.1
UnmarshalTOML 实现 toml 格式的反序列化。
type MarshalFunc ¶
MarshalFunc 序列化函数。签名与 encoding/json.Marshal 一致。
type Option ¶
type Option func(*options)
Option 是 Read / ReadTo 的可选参数。
func WithProcessor ¶
WithProcessor 设置一个 Processor,在反序列化完成后调用。 processor.Process 接收的 v 与 Read/ReadTo 返回的值是同一个对象, 可以直接修改它。
Read("config.yaml", WithProcessor(ProcessFunc(func(v any) error {
m := v.(map[string]any)
if m["port"] == nil { m["port"] = 8080 }
return nil
})))
type ProcessFunc ¶
ProcessFunc 将普通函数适配为 Processor 接口。
func (ProcessFunc) Process ¶
func (f ProcessFunc) Process(v any) error
type UnmarshalFunc ¶
UnmarshalFunc 反序列化函数。签名与 encoding/json.Unmarshal 一致。