FEATURE: The begnnings of some schema definition utilities.

Also allow failure messages to be generated from any expression.
This commit is contained in:
Jeremy Wall 2019-01-20 08:32:05 -06:00
parent 7959391b90
commit 6321828006
3 changed files with 28 additions and 1 deletions

View File

@ -663,7 +663,7 @@ make_fn!(
do_each!(
pos => pos,
_ => word!("fail"),
msg => must!(wrap_err!(either!(format_expression, string_expression), "Expected failure message")),
msg => must!(wrap_err!(expression, "Expected failure message")),
(Expression::Fail(FailDef{
pos: pos,
message: Box::new(msg),

15
std/schema.ucg Normal file
View File

@ -0,0 +1,15 @@
let one_of = module {
val=NULL,
types=[],
} => {
let reducer = macro(acc, t) => acc{
ok = acc.ok || (acc.val is t),
};
let any = macro(val, types) => reduce reducer {ok=false, val=val}, types;
let result = any(mod.val, mod.types).ok;
};
let must = macro(m, msg) => select m.result, fail msg, {
true = m.result,
};

12
std/tests/schema_test.ucg Normal file
View File

@ -0,0 +1,12 @@
let t = import "std/testing.ucg".asserts{};
let schema = import "std/schema.ucg";
assert t.ok{
test = schema.must(schema.one_of{val=1, types=["float", "int"]}, "Must be a float or an int"),
desc = "1 is an int",
};
assert t.not_ok{
test = schema.one_of{val=1, types=["float", "str"]}.result,
desc = "1 is not a float or string",
};