~aasg/nixexprs

279d1c6d388b666ceadeb385cd9de800a1bb3000 — Aluísio Augusto Silva Gonçalves 18 days ago 1286f4f
lib: Add attrsets.concatMapAttrs'
3 files changed, 28 insertions(+), 2 deletions(-)

M lib/attrsets.nix
M lib/default.nix
M lib/tests.nix
M lib/attrsets.nix => lib/attrsets.nix +11 -2
@@ 1,6 1,6 @@
{ lib, aasgLib }:
let
  inherit (builtins) attrNames concatStringsSep intersectAttrs isAttrs;
  inherit (builtins) attrNames concatMap concatStringsSep intersectAttrs isAttrs listToAttrs;
  inherit (lib) isDerivation;
in
rec {


@@ 12,7 12,16 @@ rec {
  capitalizeAttrNames = /*attrs:*/
    lib.mapAttrs' (name: value: lib.nameValuePair (aasgLib.capitalize name) value);

  /* copyAttrsByPath :: [[string] -> set -> set
  /* concatMapAttrs' :: set -> (string -> any -> [NameValuePair]) -> set
   *
   * What `concatMap` is to `map`, this is to `mapAttrs'`.  Namely,
   * call a function to produce multiple attributes for each attribute
   * in the passed set.
   */
  concatMapAttrs' = mapper: attrs:
    listToAttrs (concatMap (name: mapper name attrs.${name}) (attrNames attrs));

  /* copyAttrsByPath :: [string] -> set -> set
   *
   * Recreate attrs recursively with only the attributes listed in
   * paths.

M lib/default.nix => lib/default.nix +1 -0
@@ 5,6 5,7 @@ let
    attrsets = callLibs ./attrsets.nix;
    inherit (attrsets)
      capitalizeAttrNames
      concatMapAttrs'
      copyAttrsByPath
      recurseIntoAttrsRecursive
      updateNew

M lib/tests.nix => lib/tests.nix +16 -0
@@ 39,6 39,22 @@ runTestsOrDieTrying {
    expected = { ServiceConfig = { execStart = "/bin/false"; }; };
  };

  testConcatMapAttrs1 = {
    expr = concatMapAttrs' (n: v: singleton nameValuePair v n) { };
    expected = { };
  };

  testConcatMapAttrs2 = {
    expr = concatMapAttrs' (n: v: singleton (nameValuePair (toString v) n)) { x = 1; y = 2; };
    expected = { "1" = "x"; "2" = "y"; };
  };

  # TODO: what is the expected behavior here?
  #testConcatMapAttrs3 = {
  #  expr = concatMapAttrs' (n: v: singleton (nameValuePair (toString v) n)) { z = 1; y = 1; };
  #  expected = { "1" = "x"; };
  #};

  testUpdateNew1 = {
    expr = updateNew { } { };
    expected = { };