lib/generators: print paths without quotes & move function down

This commit is contained in:
Profpatsch 2018-04-25 15:04:30 +02:00
parent 4340a1582f
commit c84dad316a
2 changed files with 20 additions and 14 deletions

View file

@ -143,18 +143,13 @@ rec {
(This means fn is type Val -> String.) */ (This means fn is type Val -> String.) */
allowPrettyValues ? false allowPrettyValues ? false
}@args: v: with builtins; }@args: v: with builtins;
if isInt v then toString v let isPath = v: typeOf v == "path";
in if isInt v then toString v
else if isString v then ''"${libStr.escape [''"''] v}"'' else if isString v then ''"${libStr.escape [''"''] v}"''
else if true == v then "true" else if true == v then "true"
else if false == v then "false" else if false == v then "false"
else if null == v then "null" else if null == v then "null"
else if isFunction v then else if isPath v then toString v
let fna = lib.functionArgs v;
showFnas = concatStringsSep "," (libAttr.mapAttrsToList
(name: hasDefVal: if hasDefVal then "(${name})" else name)
fna);
in if fna == {} then "<λ>"
else "<λ:{${showFnas}}>"
else if isList v then "[ " else if isList v then "[ "
+ libStr.concatMapStringsSep " " (toPretty args) v + libStr.concatMapStringsSep " " (toPretty args) v
+ " ]" + " ]"
@ -163,12 +158,21 @@ rec {
if attrNames v == [ "__pretty" "val" ] && allowPrettyValues if attrNames v == [ "__pretty" "val" ] && allowPrettyValues
then v.__pretty v.val then v.__pretty v.val
# TODO: there is probably a better representation? # TODO: there is probably a better representation?
else if v ? type && v.type == "derivation" then "<δ>" else if v ? type && v.type == "derivation" then
"<δ:${v.name}>"
# "<δ:${concatStringsSep "," (builtins.attrNames v)}>"
else "{ " else "{ "
+ libStr.concatStringsSep " " (libAttr.mapAttrsToList + libStr.concatStringsSep " " (libAttr.mapAttrsToList
(name: value: (name: value:
"${toPretty args name} = ${toPretty args value};") v) "${toPretty args name} = ${toPretty args value};") v)
+ " }" + " }"
else abort "generators.toPretty: should never happen (v = ${v})"; else if isFunction v then
let fna = lib.functionArgs v;
showFnas = concatStringsSep "," (libAttr.mapAttrsToList
(name: hasDefVal: if hasDefVal then "(${name})" else name)
fna);
in if fna == {} then "<λ>"
else "<λ:{${showFnas}}>"
else abort "toPretty: should never happen (v = ${v})";
} }

View file

@ -317,7 +317,8 @@ runTests {
expr = mapAttrs (const (generators.toPretty {})) rec { expr = mapAttrs (const (generators.toPretty {})) rec {
int = 42; int = 42;
bool = true; bool = true;
string = "fnord"; string = ''fno"rd'';
path = /. + "/foo"; # toPath returns a string
null_ = null; null_ = null;
function = x: x; function = x: x;
functionArgs = { arg ? 4, foo }: arg; functionArgs = { arg ? 4, foo }: arg;
@ -328,13 +329,14 @@ runTests {
expected = rec { expected = rec {
int = "42"; int = "42";
bool = "true"; bool = "true";
string = "\"fnord\""; string = ''"fno\"rd"'';
path = "/foo";
null_ = "null"; null_ = "null";
function = "<λ>"; function = "<λ>";
functionArgs = "<λ:{(arg),foo}>"; functionArgs = "<λ:{(arg),foo}>";
list = "[ 3 4 ${function} [ false ] ]"; list = "[ 3 4 ${function} [ false ] ]";
attrs = "{ \"foo\" = null; \"foo bar\" = \"baz\"; }"; attrs = "{ \"foo\" = null; \"foo bar\" = \"baz\"; }";
drv = "<δ>"; drv = "<δ:test>";
}; };
}; };