-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add openStructOption & structOption #5
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{self, typedef', typeError}: | ||
{ | ||
self, | ||
typedef', | ||
typeError, | ||
}: | ||
# Struct checking is more involved than the simpler types above. | ||
# To make the actual type definition more readable, several | ||
# helpers are defined below. | ||
|
@@ -73,6 +77,66 @@ with builtins; let | |
else "" | ||
); | ||
}; | ||
|
||
# Modified checkStruct that takes into account the ignored flag | ||
checkStruct' = ignored: def: value: let | ||
init = { | ||
ok = true; | ||
err = ""; | ||
}; | ||
# Function to check if a field should be ignored | ||
shouldIgnore = n: ignored || hasAttr n def; | ||
# Check fields based on the ignored flag | ||
checkedFields = map ( | ||
n: let | ||
v = | ||
if hasAttr n value | ||
then value."${n}" | ||
else null; | ||
in | ||
if shouldIgnore n | ||
then | ||
if hasAttr n def | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is somewhat redundant, because it's already checked in one branch of Maybe it's just the naming that confuses me here 😊 Ideally, the redundant branches could be unified. Edit: for clarity I suggest you do a plain 3-branch case (if else) statement (ignored/hasattr/else) |
||
then checkField def."${n}" n v | ||
else { | ||
ok = true; | ||
err = ""; | ||
} # Skip check if field is not in definition and ignored is true | ||
else { | ||
ok = false; | ||
err = "field '${n}' is not among the types in [${concatStringsSep "," (attrNames def)}]\n"; | ||
} # Fail check if ignored is false and field is not in definition | ||
) (attrNames value); | ||
|
||
combined = | ||
foldl' ( | ||
acc: res: { | ||
ok = acc.ok && res.ok; | ||
err = | ||
if !res.ok | ||
then acc.err + res.err | ||
else acc.err; | ||
} | ||
) | ||
init | ||
checkedFields; | ||
in | ||
combined; | ||
|
||
openStructCheck = name: ignored: def: | ||
typedef' { | ||
inherit name def; | ||
checkType = value: | ||
if isAttrs value | ||
then (checkStruct' ignored (self.attrs self.type def) value) | ||
else { | ||
ok = false; | ||
err = typeError name value; | ||
}; | ||
toError = _: result: | ||
"expected '${name}'-struct, but found:\n" + result.err; | ||
}; | ||
|
||
struct' = name: isClosed: def: | ||
typedef' { | ||
inherit name def; | ||
|
@@ -96,4 +160,37 @@ in { | |
if isString arg | ||
then struct' arg false | ||
else struct' "anon" false arg; | ||
|
||
/* | ||
(structOption "option" {name = string; inputs = attrs any;}) {b = "s";} | ||
=> expected 'option'-struct, but found: | ||
field 'b' is not among the types in [inputs,name] | ||
|
||
(structOption "option" {name = string; inputs = attrs any;}) {inputs = {};} | ||
=> | ||
{ Inputs = { ... }; } | ||
*/ | ||
structOption = arg: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I parse this name as an "option for a struct", but it rather is a "struct with all elements being optional". |
||
if isString arg | ||
then openStructCheck arg false | ||
else openStructCheck "anon" false arg; | ||
|
||
/* | ||
(openStructOption "option" {name = string; inputs = attrs any;}) {b= "5";} | ||
=> | ||
{ b = "5"; } | ||
|
||
(openStructOption "option" {name = string; inputs = attrs any;}) {b= "5"; name = 5;} | ||
=> | ||
expected 'option'-struct, but found: | ||
field 'name': expected type 'string', but value '5' is of type 'int' | ||
|
||
(openStructOption "option" {name = string; inputs = attrs any;}) {b= "5"; name = "5";} | ||
=> | ||
{ b = "5"; name = "5"; } | ||
*/ | ||
openStructOption = arg: | ||
if isString arg | ||
then openStructCheck arg true | ||
else openStructCheck "anon" true arg; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fail to grok how
hasAttr n def
translates to the concept of "should ignore". Could you find a more suitable name?