From 9acd72571d6ac88fc3a1210f4b529947a4c7b831 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Mon, 18 Feb 2019 18:47:52 -0600 Subject: [PATCH] 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. --- std/schema.ucg | 10 ++++++++-- std/tests/schema_test.ucg | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/std/schema.ucg b/std/schema.ucg index 4e74af8..94a9e26 100644 --- a/std/schema.ucg +++ b/std/schema.ucg @@ -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), }; diff --git a/std/tests/schema_test.ucg b/std/tests/schema_test.ucg index 2da3498..5a15eb6 100644 --- a/std/tests/schema_test.ucg +++ b/std/tests/schema_test.ucg @@ -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", -}; \ No newline at end of file +}; + +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", +};