MoonBit 原生配置库:多来源合并成一份配置,按 key 取值或提取成你自己的类型。
Dependencies
import {
"MuVirus/moonfig/core" @config,
"MuVirus/moonfig/core/source/json_source" @jsonsrc,
}别名显式写出来。 省略别名时 MoonBit 按目录名绑定,核心会变成 @core,实测能编译; 本项目统一写 @config / @jsonsrc,短,且目录再调整时用户的写法不用跟着改。
using @config { type Config }
using @jsonsrc { type JsonSource }
let cfg = Config::new()
.add(JsonSource::from_str("{\"db\":{\"host\":\"localhost\",\"port\":5432}}"))
.add(JsonSource::from_str("{\"db\":{\"port\":6543}}"))
.build()
cfg.get_string("db.host") // "localhost" ← 第一个来源提供,未被覆盖
cfg.get_int("db.port") // 6543 ← 第二个来源覆盖///|
using @jsonsrc {extract_from}
///|
struct DbCfg {
host : String
port : Int64
} derive(FromJson)
///|
let db : DbCfg = extract_from(cfg, "db")| 状态 | 方向 | 条目 |
|---|---|---|
| 🚧 | 核心 | 统一的配置值类型、按键合并、按键取值、按结构体取值、错误处理 |
| 🚧 | 多格式 | CBOR、让别的格式接进来 |
| ⬜ | 外部来源 | 配置文件、环境变量、多个来源任意组合 |
| ⬜ | 校验 | 必填项、取值范围、自定义规则 |
| ⬜ | 自动发现格式 | 按后缀判断、按内容判断、无法判断时报错 |
| ⬜ | 热更新 | 监听文件变化、重新读取并合并、回调通知、失败时保留旧配置 |
Install
Download zipMoonBit 原生配置库:多来源合并成一份配置,按 key 取值或提取成你自己的类型。
Dependencies