MoonBit 原生配置库:一处声明,多来源汇入。
moon add MuVirus/moonfigimport {
"MuVirus/moonfig/core" @config,
"MuVirus/moonfig/core/format/json_format" @jsonfmt,
}git clone https://github.com/MuVirus/moonfig.git
cd moonfig
moon update
moon test
moon run examples/multi_source -- --log-level=debug// @config and @jsonfmt are the aliases from the moon.pkg import block above.
let cfg = @config.Config::new()
.add(@config.StringSource::new("{\"db\":{\"host\":\"localhost\",\"port\":5432}}", @jsonfmt.JsonFormat::new()))
.add(@config.StringSource::new("{\"db\":{\"port\":6543}}", @jsonfmt.JsonFormat::new()))
.build()
cfg.get_string("db.host") // "localhost" ← from the first source, not overridden
cfg.get_int("db.port") // 6543 ← overridden by the seconddb.host = localhost
db.port = 6543| Source | How to add |
|---|---|
| In-memory defaults | MemorySource::new(value) |
| Config file | FileSource::new("config/app.json", JsonFormat::new()) |
| Environment variables | EnvSource::new("APP_") |
| Command line | CliSource::new() |
| Status | Direction | Items |
|---|---|---|
| ✅ | Core | One value type for configuration, merge by key, read by key, read by struct, error handling... |
| ✅ | Many formats | Seven formats: JSON, INI, dotenv, YAML, TOML, XML and CBOR |
| ✅ | External sources | Configuration files, environment variables, command-line flags, any combination of sources |
| ✅ | Source tracking | Which layer a key finally came from |
| ✅ | Validation | Required keys, value ranges, custom rules |
| ✅ | Format detection | By suffix, by content, an error when neither decides |
| ✅ | Hot reload | Poll for file changes, re-read and merge, notify listeners, keep the old config when a new one is bad |
| ⬜ | Remote configuration | Fetch configuration files over HTTP/URL, with timeouts, retries and a local fallback. |
| ⬜ | Encrypted storage | Parse and securely store encrypted values for sensitive configuration fields. |
| ⬜ | Preprocessing | Transform existing configuration text or data without changing the source. |
| Document | Description |
|---|---|
| Documentation | Reading paths, core concepts, and task guides |
| moon doc | Generate the API reference into _build/doc/ |
| moon doc --serve | Start a local preview (http://127.0.0.1:3000 by default) |
| moon coverage analyze -- -f summary | Run the tests and print a coverage summary |
| Examples | Runnable examples for each capability |
| Design document | Core data model, plus the design and trade-offs of sources, extraction, validation and hot reload |
| Error reference | Every ConfigError variant and its exact message |
| Roadmap | Planned directions and progress |
English is authoritative. Chinese copies live next to the English files: .zh.md under docs/, and README_ZH.md / AGENTS_ZH.md at the root.
Install
Download zipMoonBit 原生配置库:一处声明,多来源汇入。