Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.generators.toINI: allow lists of name-value pairs as section contents #340726

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/generators.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let
addErrorContext
assertMsg
attrNames
attrsToList
concatLists
concatMapStringsSep
concatStrings
Expand Down Expand Up @@ -177,9 +178,9 @@ in rec {
}:
let mkLine = k: v: indent + mkKeyValue k v + "\n";
mkLines = if listsAsDuplicateKeys
then k: v: map (mkLine k) (if isList v then v else [v])
else k: v: [ (mkLine k v) ];
in attrs: concatStrings (concatLists (mapAttrsToList mkLines attrs));
then {name, value}: map (mkLine name) (if isList value then value else [value])
else {name, value}: [ (mkLine name value) ];
in attrs: concatStrings (concatLists (map mkLines (if isAttrs attrs then attrsToList attrs else attrs)));


/**
Expand Down
23 changes: 23 additions & 0 deletions lib/tests/misc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,29 @@ runTests {
'';
};

testToINIOrdered = {
expr = generators.toINI {} {
bar = [
{ name = "foo"; value = 1;}
{ name = "qux"; value = 2;}
];
baz = [
{ name = "qux"; value = 2;}
{ name = "foo"; value = 1;}
];
};
expected = ''
[bar]
foo=1
qux=2

[baz]
qux=2
foo=1
'';
};


testToINIWithGlobalSectionEmpty = {
expr = generators.toINIWithGlobalSection {} {
globalSection = {
Expand Down