Skip to content

Commit

Permalink
lib.generators.toPlist: Make escaping optional as per PR comment
Browse files Browse the repository at this point in the history
As discussed, this patch makes the following changes:

1. Escaping is only done when called with `{ escape = true; }`.
2. A warning is emitted if `{ escape = false; }`
3. Escaping is disabled by default, retaining backwards compatibility.

I have also added a test case for both the escaped and non-escaped
usage.
  • Loading branch information
linnnus committed Nov 16, 2024
1 parent a5ba49f commit 78b2f18
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
21 changes: 15 additions & 6 deletions lib/generators.nix
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,17 @@ in rec {
# Inputs
Options
: Empty set, there may be configuration options in the future
Structured function argument
: escape (optional, default: `false`)
: If this option is true, XML special characters are escaped in string values and keys
Value
: The value to be converted to Plist
*/
toPlist = {}: v: let
toPlist = {
escape ? false
}: v: let
expr = ind: x:
if x == null then "" else
if isBool x then bool ind x else
Expand All @@ -569,10 +573,12 @@ in rec {

literal = ind: x: ind + x;

maybeEscapeXML = if escape then escapeXML else x: x;

bool = ind: x: literal ind (if x then "<true/>" else "<false/>");
int = ind: x: literal ind "<integer>${toString x}</integer>";
str = ind: x: literal ind "<string>${escapeXML x}</string>";
key = ind: x: literal ind "<key>${escapeXML x}</key>";
str = ind: x: literal ind "<string>${maybeEscapeXML x}</string>";
key = ind: x: literal ind "<key>${maybeEscapeXML x}</key>";
float = ind: x: literal ind "<real>${toString x}</real>";

indent = ind: expr "\t${ind}";
Expand All @@ -598,7 +604,10 @@ in rec {
(expr "\t${ind}" value)
]) x));

in ''<?xml version="1.0" encoding="UTF-8"?>
in
# TODO: As discussed in #356502, we should do the actual deprecation in a future release cycle.
lib.warnIf (!escape) "Using `lib.generators.toPlist` without escape = true; is deprecated"
''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
${expr "" v}
Expand Down
27 changes: 25 additions & 2 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1635,8 +1635,31 @@ runTests {
expected = "«foo»";
};

testToPlist = {
testToPlistUnescaped = {
expr = mapAttrs (const (generators.toPlist { })) {
value = {
nested.values = {
int = 42;
float = 0.1337;
bool = true;
emptystring = "";
string = "fn\${o}\"r\\d";
newlinestring = "\n";
path = /. + "/foo";
null_ = null;
list = [ 3 4 "test" ];
emptylist = [];
attrs = { foo = null; "foo b/ar" = "baz"; };
emptyattrs = {};
"keys are not <escaped>" = "and < neither are string values";
};
};
};
expected = { value = builtins.readFile ./test-to-plist-unescaped-expected.plist; };
};

testToPlistEscaped = {
expr = mapAttrs (const (generators.toPlist { escape = true; })) {
value = {
nested.values = {
int = 42;
Expand All @@ -1655,7 +1678,7 @@ runTests {
};
};
};
expected = { value = builtins.readFile ./test-to-plist-expected.plist; };
expected = { value = builtins.readFile ./test-to-plist-escaped-expected.plist; };
};

testToLuaEmptyAttrSet = {
Expand Down
File renamed without changes.
48 changes: 48 additions & 0 deletions lib/tests/test-to-plist-unescaped-expected.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>nested</key>
<dict>
<key>values</key>
<dict>
<key>attrs</key>
<dict>
<key>foo b/ar</key>
<string>baz</string>
</dict>
<key>bool</key>
<true/>
<key>emptyattrs</key>
<dict>

</dict>
<key>emptylist</key>
<array>

</array>
<key>emptystring</key>
<string></string>
<key>float</key>
<real>0.133700</real>
<key>int</key>
<integer>42</integer>
<key>keys are not <escaped></key>
<string>and < neither are string values</string>
<key>list</key>
<array>
<integer>3</integer>
<integer>4</integer>
<string>test</string>
</array>
<key>newlinestring</key>
<string>
</string>
<key>path</key>
<string>/foo</string>
<key>string</key>
<string>fn${o}"r\d</string>
</dict>
</dict>
</dict>
</plist>

0 comments on commit 78b2f18

Please sign in to comment.