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:
Jeremy Wall 2019-02-18 18:47:52 -06:00
parent d71182f017
commit 9acd72571d
2 changed files with 25 additions and 4 deletions

View File

@ -74,14 +74,20 @@ let shaped = module {
},
};
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),
int = simple_handler(mod.val, mod.shape),
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),
};

View File

@ -97,7 +97,7 @@ 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",
};
@ -105,3 +105,18 @@ 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",
};