refactor: Gain back a few more lines

This commit is contained in:
Vincent Ambo 2019-08-08 13:24:05 +01:00
parent 2acdbb5009
commit 2e576e10ab

View file

@ -25,23 +25,18 @@ with builtins; let
"${n}" = t1: t2: typedef "${n}<${t1.name},${t2.name}>" (c t1 t2); "${n}" = t1: t2: typedef "${n}<${t1.name},${t2.name}>" (c t1 t2);
}; };
ofType = t: x: isAttrs x && x ? "type" && x.type == t;
typeSet = foldl' (s: t: s // (if t ? "name" then { "${t.name}" = t; } else t)) {}; typeSet = foldl' (s: t: s // (if t ? "name" then { "${t.name}" = t; } else t)) {};
# Struct checks performed: # Struct implementation. Checks that all fields match their declared
# # types, no optional fields are missing and no unexpected fields
# 1. All existing fields match their types # occur in the struct.
# 2. No non-optional fields are missing.
# 3. No unexpected fields are in the struct.
# #
# Anonymous structs are supported (e.g. for nesting) by omitting the # Anonymous structs are supported (e.g. for nesting) by omitting the
# name. # name.
checkField = def: value: current: field: checkField = def: value: current: field:
let let fieldVal = if hasAttr field value then value."${field}" else null;
fieldVal = if hasAttr field value then value."${field}" else null; type = def."${field}";
type = def."${field}"; checked = type.check fieldVal;
checked = type.check fieldVal;
in if checked then (current && true) in if checked then (current && true)
else (throw "Field ${field} is of type ${typeOf fieldVal}, but expected ${type.name}"); else (throw "Field ${field} is of type ${typeOf fieldVal}, but expected ${type.name}");
@ -64,9 +59,8 @@ with builtins; let
else (throw "Expected '${self.name}'-struct, but ${toPretty value} is of type ${typeOf value}"); else (throw "Expected '${self.name}'-struct, but ${toPretty value} is of type ${typeOf value}");
}; };
struct = arg: struct = arg: if isString arg then (struct' arg)
if isString arg then (struct' arg) else (struct' "anonymous" arg);
else (struct' "anonymous" arg);
in (typeSet [ in (typeSet [
# Primitive types # Primitive types
(typedef "any" (_: true)) (typedef "any" (_: true))