summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Schmidt <os@flyingcircus.io>2023-09-10 17:56:29 +0200
committerOliver Schmidt <os@flyingcircus.io>2023-10-10 16:11:01 +0200
commitd70633f91cb27d9314940d3a6e9385f89bf7f007 (patch)
tree7cefb07ef73c3912105e7cb1d2bdf8d96446156a
parent27eedb56010219cd7f779e87b9a00c7504f76125 (diff)
downloadnixpkgs-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar
nixpkgs-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.gz
nixpkgs-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.bz2
nixpkgs-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.lz
nixpkgs-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.xz
nixpkgs-d70633f91cb27d9314940d3a6e9385f89bf7f007.tar.zst
nixpkgs-d70633f91cb27d9314940d3a6e9385f89bf7f007.zip
lib.attrsets.attrsToList: add function
For transforming back between lists and attrsets, it makes sense to have
a quasi-inverse of `builtins.listToAttrs` available as a library
function.

Co-authored-by: Silvan Mosberger <github@infinisil.com>
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
-rw-r--r--lib/attrsets.nix30
-rw-r--r--lib/default.nix4
-rw-r--r--lib/tests/misc.nix24
3 files changed, 56 insertions, 2 deletions
diff --git a/lib/attrsets.nix b/lib/attrsets.nix
index 77e36d3271f..09a2efa40a2 100644
--- a/lib/attrsets.nix
+++ b/lib/attrsets.nix
@@ -542,6 +542,36 @@ rec {
     attrs:
     map (name: f name attrs.${name}) (attrNames attrs);
 
+  /*
+    Deconstruct an attrset to a list of name-value pairs as expected by [`builtins.listToAttrs`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-listToAttrs).
+    Each element of the resulting list is an attribute set with these attributes:
+    - `name` (string): The name of the attribute
+    - `value` (any): The value of the attribute
+
+    The following is always true:
+    ```nix
+    builtins.listToAttrs (attrsToList attrs) == attrs
+    ```
+
+    :::{.warning}
+    The opposite is not always true. In general expect that
+    ```nix
+    attrsToList (builtins.listToAttrs list) != list
+    ```
+
+    This is because the `listToAttrs` removes duplicate names and doesn't preserve the order of the list.
+    :::
+
+    Example:
+      attrsToList { foo = 1; bar = "asdf"; }
+      => [ { name = "bar"; value = "asdf"; } { name = "foo"; value = 1; } ]
+
+    Type:
+      attrsToList :: AttrSet -> [ { name :: String; value :: Any; } ]
+
+  */
+  attrsToList = mapAttrsToList nameValuePair;
+
 
   /* Like `mapAttrs`, except that it recursively applies itself to
      the *leaf* attributes of a potentially-nested attribute set:
diff --git a/lib/default.nix b/lib/default.nix
index e4bf45aac3b..282aa8f61e0 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -81,8 +81,8 @@ let
     inherit (self.attrsets) attrByPath hasAttrByPath setAttrByPath
       getAttrFromPath attrVals attrValues getAttrs catAttrs filterAttrs
       filterAttrsRecursive foldlAttrs foldAttrs collect nameValuePair mapAttrs
-      mapAttrs' mapAttrsToList concatMapAttrs mapAttrsRecursive mapAttrsRecursiveCond
-      genAttrs isDerivation toDerivation optionalAttrs
+      mapAttrs' mapAttrsToList attrsToList concatMapAttrs mapAttrsRecursive
+      mapAttrsRecursiveCond genAttrs isDerivation toDerivation optionalAttrs
       zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
       recursiveUpdate matchAttrs overrideExisting showAttrPath getOutput getBin
       getLib getDev getMan chooseDevOutputs zipWithNames zip
diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix
index 6d55ae68477..702be9529b4 100644
--- a/lib/tests/misc.nix
+++ b/lib/tests/misc.nix
@@ -20,6 +20,10 @@ let
     expr = (builtins.tryEval (builtins.seq expr "didn't throw"));
     expected = { success = false; value = false; };
   };
+  testingEval = expr: {
+    expr = (builtins.tryEval expr).success;
+    expected = true;
+  };
   testingDeepThrow = expr: testingThrow (builtins.deepSeq expr expr);
 
   testSanitizeDerivationName = { name, expected }:
@@ -784,6 +788,26 @@ runTests {
     expected = { a = 1; b = 2; };
   };
 
+  testListAttrsReverse = let
+    exampleAttrs = {foo=1; bar="asdf"; baz = [1 3 3 7]; fnord=null;};
+    exampleSingletonList = [{name="foo"; value=1;}];
+  in {
+    expr = {
+      isReverseToListToAttrs = builtins.listToAttrs (attrsToList exampleAttrs) == exampleAttrs;
+      isReverseToAttrsToList = attrsToList (builtins.listToAttrs exampleSingletonList) == exampleSingletonList;
+      testDuplicatePruningBehaviour = attrsToList (builtins.listToAttrs [{name="a"; value=2;} {name="a"; value=1;}]);
+    };
+    expected = {
+      isReverseToAttrsToList = true;
+      isReverseToListToAttrs = true;
+      testDuplicatePruningBehaviour = [{name="a"; value=2;}];
+    };
+  };
+
+  testAttrsToListsCanDealWithFunctions = testingEval (
+    attrsToList { someFunc= a: a + 1;}
+  );
+
 # GENERATORS
 # these tests assume attributes are converted to lists
 # in alphabetical order