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
@ -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, {
|
let result =select schema.base_type_of(mod.val), false, {
|
||||||
str = simple_handler(mod.val, mod.shape),
|
str = simple_handler(mod.val, mod.shape),
|
||||||
int = simple_handler(mod.val, mod.shape),
|
int = simple_handler(mod.val, mod.shape),
|
||||||
float = simple_handler(mod.val, mod.shape),
|
float = simple_handler(mod.val, mod.shape),
|
||||||
bool = simple_handler(mod.val, mod.shape),
|
bool = simple_handler(mod.val, mod.shape),
|
||||||
null = 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,
|
tuple = reduce(tuple_handler, {shape=mod.shape, ok=false}, mod.val).ok,
|
||||||
list = simple_handler(mod.val, mod.shape),
|
list = select mod.shape == [], true, {
|
||||||
|
false = reduce(list_handler, {shape=mod.shape, ok=false}, mod.val).ok,
|
||||||
|
},
|
||||||
func = simple_handler(mod.val, mod.shape),
|
func = simple_handler(mod.val, mod.shape),
|
||||||
module = simple_handler(mod.val, mod.shape),
|
module = simple_handler(mod.val, mod.shape),
|
||||||
};
|
};
|
||||||
|
@ -97,7 +97,7 @@ assert t.ok{
|
|||||||
};
|
};
|
||||||
|
|
||||||
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",
|
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,
|
test = schema.shaped{val={inner={foo="bar"}}, shape={inner={foo=1}}}.result,
|
||||||
desc = "shaped fails when the shape doesn't match",
|
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