From c84dad316a8d1eb0b5c2af0bd037c169096683ca Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Wed, 25 Apr 2018 15:04:30 +0200 Subject: [PATCH] lib/generators: print paths without quotes & move function down --- lib/generators.nix | 26 +++++++++++++++----------- lib/tests/misc.nix | 8 +++++--- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index d1a8f6bf8dc..c09384c00f5 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -143,18 +143,13 @@ rec { (This means fn is type Val -> String.) */ allowPrettyValues ? false }@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 true == v then "true" else if false == v then "false" - else if null == v then "null" - 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 if null == v then "null" + else if isPath v then toString v else if isList v then "[ " + libStr.concatMapStringsSep " " (toPretty args) v + " ]" @@ -163,12 +158,21 @@ rec { if attrNames v == [ "__pretty" "val" ] && allowPrettyValues then v.__pretty v.val # 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 "{ " + libStr.concatStringsSep " " (libAttr.mapAttrsToList (name: value: "${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})"; } diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 5f19dd63f2d..c683df7d7ca 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -317,7 +317,8 @@ runTests { expr = mapAttrs (const (generators.toPretty {})) rec { int = 42; bool = true; - string = "fnord"; + string = ''fno"rd''; + path = /. + "/foo"; # toPath returns a string null_ = null; function = x: x; functionArgs = { arg ? 4, foo }: arg; @@ -328,13 +329,14 @@ runTests { expected = rec { int = "42"; bool = "true"; - string = "\"fnord\""; + string = ''"fno\"rd"''; + path = "/foo"; null_ = "null"; function = "<λ>"; functionArgs = "<λ:{(arg),foo}>"; list = "[ 3 4 ${function} [ false ] ]"; attrs = "{ \"foo\" = null; \"foo bar\" = \"baz\"; }"; - drv = "<δ>"; + drv = "<δ:test>"; }; };