Add record support to lib.generators.toPreserves
This commit is contained in:
parent
079be99183
commit
bf64c36c22
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
overlay = final: prev:
|
overlay = final: prev:
|
||||||
with final; {
|
with final; {
|
||||||
lib = lib.extend libOverlay;
|
lib = prev.lib.extend libOverlay;
|
||||||
syndicate-rs = callPackage ./syndicate-rs {
|
syndicate-rs = callPackage ./syndicate-rs {
|
||||||
rust = rust-bin.nightly.latest.default;
|
rust = rust-bin.nightly.latest.default;
|
||||||
};
|
};
|
||||||
|
|
24
lib.nix
24
lib.nix
|
@ -4,20 +4,32 @@ in with lib; {
|
||||||
|
|
||||||
generators = with final.generators;
|
generators = with final.generators;
|
||||||
prev.generators // {
|
prev.generators // {
|
||||||
|
|
||||||
|
# Generates Preserves from an arbitrary value.
|
||||||
|
# Records are generated for functions that take
|
||||||
|
# a single formal argument `toRecord` and return
|
||||||
|
# a list of values.
|
||||||
toPreserves = { }@args:
|
toPreserves = { }@args:
|
||||||
v:
|
let
|
||||||
let concatItems = lib.strings.concatStringsSep " ";
|
toPreserves' = toPreserves args;
|
||||||
in if isAttrs v then
|
concatItems = toString;
|
||||||
|
in v:
|
||||||
|
if isAttrs v then
|
||||||
"{ ${
|
"{ ${
|
||||||
concatItems (lib.attrsets.mapAttrsToList
|
concatItems (lib.attrsets.mapAttrsToList
|
||||||
(key: val: "${key}: ${toPreserves args val}") v)
|
(key: val: "${key}: ${toPreserves' val}") v)
|
||||||
} }"
|
} }"
|
||||||
else if isList v then
|
else if isList v then
|
||||||
"[ ${concatItems (map (toPreserves args) v)} ]"
|
"[ ${concatItems (map toPreserves' v)} ]"
|
||||||
else if isBool v then
|
else if isBool v then
|
||||||
(if v then "#t" else "#f")
|
(if v then "#t" else "#f")
|
||||||
else if isFunction v then
|
else if isFunction v then
|
||||||
abort "generators.toPreserves: cannot convert a function to Preserves"
|
(if (lib.functionArgs v) == { toRecord = false; } then
|
||||||
|
let items = v { toRecord = null; };
|
||||||
|
in "<${head items} ${toString (map toPreserves' (tail items))}>"
|
||||||
|
else
|
||||||
|
abort
|
||||||
|
"generators.toPreserves: cannot convert a function to Preserves")
|
||||||
else if isNull v then
|
else if isNull v then
|
||||||
"null"
|
"null"
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue