add attrset parsing example

This commit is contained in:
Emile Clark-Boman 2026-01-28 18:04:58 +10:00
parent 0a589f38fe
commit 7027a29692

View file

@ -1,9 +1,9 @@
# ❄ NixTypes (nt) ❄
<p align="center">Because Nix doesn't hold your hand here, it shoots your fingers off and spits out the world's longest stack trace...</p>
<p align="center">Because Nix never held your hand. It shot off your fingers off and spat out God's longest stack trace</p>
>[!WARNING]
> ✨ **Under Construction**
> NixTypes is quite a large project to do alone, but it's been staring at me for the last 12 months.
> NixTypes is **quite** a large project to do alone, but it's been staring at me for the last 12 months.
> If you're interested feel free to contact me and/or submit pull requests :yellow_heart::yellow_heart:
> **Be not afraid!** It's only a matter of time until NixTypes is ready for use!
@ -23,3 +23,49 @@ Some of the sweet sweet batteries included:
3. **Pretty Printing** (no more `builtins.toString` errors)
4. **A Module System** (say goodbye to managing all your `imports`)
5. **Types, Types, & More Types** (Maybe/Some/None, Monads, Tree, Rose, etc)
### ❄🎁 Parse the Parcel
*Close your eyes with me ok? MMmmmmmmmmm yes just like that...* **NOW** imagine
you're a moderately depressed and very sleepy programmer. You're reading a README.md
for a type system in Nix, I doubt it's that hard. Then **whoooshhh boooom**, Zeus
himself hath commended thee to fulfill his greatest ambition **OR DIE!!** You must
write a function taking an attribute set of the structure
```nix
{
a = { # optional
b = { # optional
c = ...; # optional (any type, default: null)
d = ...; # optional (string, default: "hola")
};
};
e = { # required
f = ...; # required (path)
g = ...; # optional (function, default: (x: x))
h = ...; # optional (string, default: "null")
};
}
```
Not having a great day now are you? It's doable and not overtly difficult,
but coming back in a couple months you'd have to decipher your solution.
**NOW BEHOLD:**
```nix
# nix made simple <3
f = attrs:
attrs
|> nt.projectOnto
{
a = {
b = {
c = null;
d = "hola";
};
};
e = {
f = nt.missing "error message...";
g = x: x;
h = nt.verify null isString;
};
};
|> ...; # your logic here...
```