mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-22 18:19:54 -04:00
FEATURE: Typed Lists in schema lib.
Schemas now allow list shapes to hold a list of the allowed types in the list. More work toward #6 and #32.
This commit is contained in:
parent
d71182f017
commit
9acd72571d
@ -73,6 +73,10 @@ let shaped = module {
|
||||
true = schema.shaped{val=value, shape=acc.shape.(name)}.result,
|
||||
},
|
||||
};
|
||||
|
||||
let list_handler = func(acc, value) => acc{
|
||||
ok = false || schema.any{val=value, types=acc.shape}.result,
|
||||
};
|
||||
|
||||
let result =select schema.base_type_of(mod.val), false, {
|
||||
str = simple_handler(mod.val, mod.shape),
|
||||
@ -80,8 +84,10 @@ let shaped = module {
|
||||
float = simple_handler(mod.val, mod.shape),
|
||||
bool = simple_handler(mod.val, mod.shape),
|
||||
null = simple_handler(mod.val, mod.shape),
|
||||
tuple = reduce(tuple_handler, {shape=mod.shape, ok=false}, (mod.val)).ok,
|
||||
list = simple_handler(mod.val, mod.shape),
|
||||
tuple = reduce(tuple_handler, {shape=mod.shape, ok=false}, mod.val).ok,
|
||||
list = select mod.shape == [], true, {
|
||||
false = reduce(list_handler, {shape=mod.shape, ok=false}, mod.val).ok,
|
||||
},
|
||||
func = simple_handler(mod.val, mod.shape),
|
||||
module = simple_handler(mod.val, mod.shape),
|
||||
};
|
||||
|
@ -97,11 +97,26 @@ assert t.ok{
|
||||
};
|
||||
|
||||
assert t.ok{
|
||||
test = schema.shaped{val={foo="bar", inner=[1, 2]}, shape={foo="", inner=[]}}.result,
|
||||
test = schema.shaped{val={foo="bar", inner=[1, 2]}, shape={foo="", inner=[0]}}.result,
|
||||
desc = "shaped for nested list in tuple works",
|
||||
};
|
||||
|
||||
assert t.not_ok{
|
||||
test = schema.shaped{val={inner={foo="bar"}}, shape={inner={foo=1}}}.result,
|
||||
desc = "shaped fails when the shape doesn't match",
|
||||
};
|
||||
};
|
||||
|
||||
assert t.ok{
|
||||
test = schema.shaped{val={list=[1, "foo"]}, shape={list=[0, ""]}}.result,
|
||||
desc="inner list with valid types matches shape",
|
||||
};
|
||||
|
||||
assert t.not_ok{
|
||||
test = schema.shaped{val={list=[1, "foo", true]}, shape={list=[0, ""]}}.result,
|
||||
desc="inner list with invalid types does not match shape",
|
||||
};
|
||||
|
||||
assert t.ok{
|
||||
test = schema.shaped{val={list=[1, "foo"]}, shape={list=[]}}.result,
|
||||
desc="inner list with valid types matches empty list shape",
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user