lib/generators: introduce a sane default for `mkValueString`
So far, `mkValueString` defaulted to `toString`, which is a bad match for most configuration file formats, especially because how booleans are formatted. This also improves error messages for unsupported types. Add a test to codify the formatting.
This commit is contained in:
parent
a7e45fdd8e
commit
fa71407f36
|
@ -28,6 +28,30 @@ rec {
|
||||||
|
|
||||||
## -- HELPER FUNCTIONS & DEFAULTS --
|
## -- HELPER FUNCTIONS & DEFAULTS --
|
||||||
|
|
||||||
|
/* Convert a value to a sensible default string representation.
|
||||||
|
* The builtin `toString` function has some strange defaults,
|
||||||
|
* suitable for bash scripts but not much else.
|
||||||
|
*/
|
||||||
|
mkValueStringDefault = {}: v: with builtins;
|
||||||
|
let err = t: v: abort
|
||||||
|
("generators.mkValueStringDefault: " +
|
||||||
|
"${t} not supported: ${toPretty {} v}");
|
||||||
|
in if isInt v then toString v
|
||||||
|
# we default to not quoting strings
|
||||||
|
else if isString v then v
|
||||||
|
# isString returns "1", which is not a good default
|
||||||
|
else if true == v then "true"
|
||||||
|
# here it returns to "", which is even less of a good default
|
||||||
|
else if false == v then "false"
|
||||||
|
else if null == v then "null"
|
||||||
|
# if you have lists you probably want to replace this
|
||||||
|
else if isList v then err "lists" v
|
||||||
|
# same as for lists, might want to replace
|
||||||
|
else if isAttrs v then err "attrsets" v
|
||||||
|
else if isFunction v then err "functions" v
|
||||||
|
else err "this value is" (toString v);
|
||||||
|
|
||||||
|
|
||||||
/* Generate a line of key k and value v, separated by
|
/* Generate a line of key k and value v, separated by
|
||||||
* character sep. If sep appears in k, it is escaped.
|
* character sep. If sep appears in k, it is escaped.
|
||||||
* Helper for synaxes with different separators.
|
* Helper for synaxes with different separators.
|
||||||
|
@ -38,7 +62,7 @@ rec {
|
||||||
* > "f\:oo:bar"
|
* > "f\:oo:bar"
|
||||||
*/
|
*/
|
||||||
mkKeyValueDefault = {
|
mkKeyValueDefault = {
|
||||||
mkValueString ? toString
|
mkValueString ? mkValueStringDefault {}
|
||||||
}: sep: k: v:
|
}: sep: k: v:
|
||||||
"${libStr.escape [sep] k}${sep}${mkValueString v}";
|
"${libStr.escape [sep] k}${sep}${mkValueString v}";
|
||||||
|
|
||||||
|
|
|
@ -207,6 +207,29 @@ runTests {
|
||||||
expected = ''f\:oo:bar'';
|
expected = ''f\:oo:bar'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
testMkValueString = {
|
||||||
|
expr = let
|
||||||
|
vals = {
|
||||||
|
int = 42;
|
||||||
|
string = ''fo"o'';
|
||||||
|
bool = true;
|
||||||
|
bool2 = false;
|
||||||
|
null = null;
|
||||||
|
# float = 42.23; # floats are strange
|
||||||
|
};
|
||||||
|
in mapAttrs
|
||||||
|
(const (generators.mkValueStringDefault {}))
|
||||||
|
vals;
|
||||||
|
expected = {
|
||||||
|
int = "42";
|
||||||
|
string = ''fo"o'';
|
||||||
|
bool = "true";
|
||||||
|
bool2 = "false";
|
||||||
|
null = "null";
|
||||||
|
# float = "42.23" true false [ "bar" ] ]'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
testToKeyValue = {
|
testToKeyValue = {
|
||||||
expr = generators.toKeyValue {} {
|
expr = generators.toKeyValue {} {
|
||||||
key = "value";
|
key = "value";
|
||||||
|
@ -249,6 +272,8 @@ runTests {
|
||||||
"section 1" = {
|
"section 1" = {
|
||||||
attribute1 = 5;
|
attribute1 = 5;
|
||||||
x = "Me-se JarJar Binx";
|
x = "Me-se JarJar Binx";
|
||||||
|
# booleans are converted verbatim by default
|
||||||
|
boolean = false;
|
||||||
};
|
};
|
||||||
"foo[]" = {
|
"foo[]" = {
|
||||||
"he\\h=he" = "this is okay";
|
"he\\h=he" = "this is okay";
|
||||||
|
@ -260,6 +285,7 @@ runTests {
|
||||||
|
|
||||||
[section 1]
|
[section 1]
|
||||||
attribute1=5
|
attribute1=5
|
||||||
|
boolean=false
|
||||||
x=Me-se JarJar Binx
|
x=Me-se JarJar Binx
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue