Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,28 @@
};
};
eitherN = tn:
typedef "either<${concatStringsSep ", " (map (x: x.name) tn)}>" (x: any (t: (self.type t).check x) tn);
let
combine = a: b: if a == null then b else "[${a}]: ${b}";
logContexts = map (t: t.logContext or null) tn;
# Function to pair two lists, combining their corresponding elements
pairList = listA: listB: lib.lists.zipListsWith combine listA listB;
in
_typedef' null rec {
name = "either<${concatStringsSep ", " (map (x: x.name) tn)}>";
checkType = x: let
results = map (t: t.checkType x) tn;
allErrors = filter (r: !r.ok) results;
errorMsgs = map (r: r.err) allErrors;
formatError = err: "expected type '${name}', but found:\n" + err;
in
if any (r: r.ok) results
then { ok = true; }
else {
ok = false;
err = concatStringsSep "\n" (pairList logContexts (map formatError errorMsgs));
};
};

either = t1: t2: self.eitherN [t1 t2];
list = t:
typedef' rec {
Expand Down Expand Up @@ -211,7 +232,7 @@
# Anonymous structs are supported (e.g. for nesting) by omitting the
# name.
#
inherit (import ./struct.nix {inherit self typedef' typeError;}) struct openStruct;
inherit (import ./struct.nix {inherit self typedef' typeError;}) struct openStruct structOption openStructOption;

enum = let
plain = name: def:
Expand Down
99 changes: 98 additions & 1 deletion struct.nix
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.
Expand Down Expand Up @@ -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;
Copy link
Collaborator

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?

# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 shouldIgnore.

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;
Expand All @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}