Which of two settings wins, and what to do when they disagree: the precedence every moonbitstack library shares. Not a configuration loader; that is moonconf.
library preset < the record passed in < this call's argument| clash | what the call answers | what it says |
|---|---|---|
| Ignore | what wins chose | nothing |
| Warn(f) | what wins chose | one line to f |
| Panic | nothing: the program stops | the message |
| Handle(f) | what f returns | f gets the names, the base and the merge |
struct Options {
timeout : Int32
retries : Int32
}
let options : Options = { timeout: 30, retries: 3 }
fn connect(
options~ : Options = options,
timeout? : Int32,
retries? : Int32,
wins~ : @moonknob.Wins = Extra,
clash~ : @moonknob.OnClash[Options] = Ignore,
) -> Options {
let merge = @moonknob.Merge::new(wins)
let merged = {
timeout: merge.pick("timeout", options.timeout, timeout),
retries: merge.pick("retries", options.retries, retries),
}
merge.settle(clash, options, merged, "connect: options and arguments")
}let mine = { timeout: 5, retries: 1, }
let by_default = connect(options=mine, timeout=10)
let by_record = connect(options=mine, timeout=10, wins=Base)
assert_eq(by_default.timeout, 10)
assert_eq(by_record.timeout, 5)let said : Array[String] = []
let lines = connect(options=mine, timeout=10, clash=Warn(line => said.push(line)))
assert_eq(lines.timeout, 10)
assert_eq(said, ["connect: options and arguments: timeout given twice with different values"])let keep_retries : @moonknob.OnClash[Options] = Handle(sides => sides.base)
assert_eq(connect(options=mine, timeout=10, clash=keep_retries).timeout, 5)let merge = @moonknob.Merge::new(Extra)
let base = { "alg": "none", "typ": "JWT" }
let extra = { "typ": "JOSE", "kid": "k1" }
let heads = merge.union(base, extra)
assert_eq(heads.to_array(), [
("alg", "none"),
("typ", "JOSE"),
("kid", "k1"),
])type Mergetest {
let merge = @moonknob.Merge::new(Extra)
let timeout = merge.pick("timeout", 30, Some(10))
assert_eq(timeout, 10)
assert_eq(merge.names().to_owned(), ["timeout"])
}Install
Download zipWhich of two settings wins, and what to do when they disagree: the precedence every moonbitstack library shares. Not a configuration loader; that is moonconf.