A pure MoonBit reader and writer for a documented subset of libconfig 1.8.2
moon add Ryan-1125/moonlibconfig@0.3.0import { "Ryan-1125/moonlibconfig" @config } for "test"///|
test "target interpretation and normalized readback" {
let doc = @config.parse_target(b"mask=0xFFFFFFFFL; bits=0x100000000L; port=9000;")
assert_eq(doc.get_int64("mask"), -1L)
assert_eq(doc.get_int64("bits"), 4294967296L)
let root = doc.root().replaced(@config.parse_path("port"), @config.Integer(8080))
assert_eq(@config.parse_target_text(@config.stringify(root)).root(), root)
}///|
test "edit original text without reinterpreting unrelated values" {
let original = b"mask=0xFFFFFFFFL; port=9000; title=\"moon\";"
let edited = @config.replace_source_value(original, "port", @config.Integer(8080))
assert_eq(edited, b"mask=0xFFFFFFFFL; port=8080; title=\"moon\";")
assert_eq(@config.parse_target(edited).get_int64("mask"), -1L)
let changed_mask = @config.replace_source_value(edited, "mask", @config.Integer64(4294967295L))
assert_eq(@config.parse_target(changed_mask).get_int64("mask"), 4294967295L)
}///|
test "resolve files and locate original child values" {
let root : @config.IncludeFile = {
id: "root.cfg",
content: b"service={\n@include \"child.cfg\"\n}\n",
}
let doc = @config.parse_with_includes(root, (parent, requested) => {
if parent != "root.cfg" || requested != "child.cfg" {
raise @config.resolver_error("unknown source")
}
{ id: "child.cfg", content: b"port=9000; mask=0xFFFFFFFFL;" }
})
assert_eq(doc.root().lookup("service.port").as_int(), 9000)
assert_eq(doc.root().lookup("service.mask").as_int64(), -1L)
guard doc.origin("service.port") is Some(origin) else { fail("missing origin") }
assert_eq(origin.source, "child.cfg")
assert_eq((origin.position.line, origin.position.column), (1, 6))
}///|
test "edit containers validate and read defaults" {
let root = @config.parse("service={port=9000} peers=(\"one\",\"two\")")
let peers = root.lookup("peers").inserted(1, @config.Text(b"backup")).removed_at(0)
let updated = root.replaced(@config.parse_path("peers"), peers)
assert_eq(@config.validate(updated), [])
assert_eq(updated.lookup_or("workers", @config.Integer(4)), @config.Integer(4))
assert_eq(updated.lookup_optional("absent"), None)
assert_eq(@config.parse(@config.stringify(updated)), updated)
}| 能力 | 接口 |
|---|---|
| 目标应用解析 | parse_target、parse_with_includes |
| 严格可移植解析 | parse、parse_bytes、parse_document、parse_document_bytes |
| 读取与默认值 | lookup、get_path、get_member、at、lookup_optional、lookup_or |
| 树编辑 | replaced、with_member、without_member、inserted、appended、removed_at |
| 输出及校验 | stringify、replace_source_value、validate |
| 类型读取 | as_int、as_int64、as_double、as_bool、as_bytes、as_string |
| 定位 | Document.location、Document.get_*、IncludedDocument.origin |
| 层次 | 测试契约 |
|---|---|
| 目标模式读取 | 固定140个输入,要求接受/拒绝和类型/值与真实v1.8.2一致 |
| 目标模式写出 | 116个输入双向读写;单独记录1个非有限浮点不可规范化写出的用例 |
| 旧模式回归 | 原140个输入及8项明确规则保留,保护既有行为 |
| 原应用编辑回读 | 8个场景,仅指定字段变化,其他值与类型逐字段相等 |
| 多文件工作流 | 递归展开、子文件编辑、原应用重读、来源、循环、总量及失败测试 |
| 工程化 | 三后端测试、ZIP/注册表独立消费、种子模糊回归及双平台CI |
git clone https://github.com/Ryan-1125/moonlibconfig.git
cd moonlibconfig
moon update
moon fmt --check
moon check --deny-warn
moon test --target wasm-gc --deny-warn
moon test --target js --deny-warn
moon test --target native --deny-warn
moon run examples/service --target wasm-gcnode tools/differential.mjs
node tools/differential.mjs --target-profile
node tools/semantic-edit.mjspub struct IncludedDocument {
document : Document
chunks : Array[OriginChunk]
sources : Map[String, Bytes]
}fn IncludedDocument::origin(self : IncludedDocument, path : String) -> SourceOrigin? raise ConfigErrorfn parse_with_includes(root : IncludeFile, resolve : (String, String) -> IncludeFile raise ConfigError, limits? : Limits, max_include_depth? : Int) -> IncludedDocument raise ConfigErrorfn replace_source_value(input : Bytes, path : String, replacement : Value, limits? : Limits) -> Bytes raise ConfigErrorInstall
Download zipA pure MoonBit reader and writer for a documented subset of libconfig 1.8.2