mirror of
https://github.com/nix-community/home-manager.git
synced 2025-11-08 19:46:05 +01:00
lib/deprecations: add ignore parameter to remapAttrsRecursive
Needed to allow exclusionary keys easily without complex predicates. Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
parent
650a38ebe8
commit
36c57c6a1d
1 changed files with 19 additions and 6 deletions
|
|
@ -51,6 +51,12 @@
|
||||||
/*
|
/*
|
||||||
Recursively transforms attribute set keys, issuing a warning for each transformation.
|
Recursively transforms attribute set keys, issuing a warning for each transformation.
|
||||||
|
|
||||||
|
The function takes an attribute set with the following keys:
|
||||||
|
- pred: (str -> bool) Predicate to detect which keys to transform.
|
||||||
|
- transform: (str -> str) Function to transform the key.
|
||||||
|
- ignore: (list of str) Optional. A list of keys to never transform,
|
||||||
|
even if they match `pred`.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
let
|
let
|
||||||
# Renames camelCase keys to snake_case.
|
# Renames camelCase keys to snake_case.
|
||||||
|
|
@ -59,15 +65,22 @@
|
||||||
pred = key: builtins.match ".*[a-z][A-Z].*" key != null;
|
pred = key: builtins.match ".*[a-z][A-Z].*" key != null;
|
||||||
# The transformation to apply.
|
# The transformation to apply.
|
||||||
transform = lib.hm.strings.toSnakeCase;
|
transform = lib.hm.strings.toSnakeCase;
|
||||||
|
# Keys we will not rename
|
||||||
|
ignore = [ "allowThisOne" ];
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
# { my-key = { fooBar = 1; }; }
|
migrateCamelCase "programs.mymodule.settings" {
|
||||||
migrateCamelCase "programs.mymodule.settings" { my-key = { fooBar = 1; }; }
|
someSetting = 1; # will be renamed
|
||||||
# => { my-key = { foo_bar = 1; }; }
|
allowThisOne = 2; # will be ignored
|
||||||
# (with a warning about the fooBar -> foo_bar conversion)
|
}
|
||||||
|
# => { some_setting = 1; allowThisOne = 2; }
|
||||||
*/
|
*/
|
||||||
remapAttrsRecursive =
|
remapAttrsRecursive =
|
||||||
{ pred, transform }:
|
{
|
||||||
|
pred,
|
||||||
|
transform,
|
||||||
|
ignore ? [ ],
|
||||||
|
}:
|
||||||
let
|
let
|
||||||
migrate =
|
migrate =
|
||||||
path: value:
|
path: value:
|
||||||
|
|
@ -75,7 +88,7 @@
|
||||||
lib.mapAttrs' (
|
lib.mapAttrs' (
|
||||||
name: val:
|
name: val:
|
||||||
let
|
let
|
||||||
newName = if pred name then transform name else name;
|
newName = if pred name && !(builtins.elem name ignore) then transform name else name;
|
||||||
|
|
||||||
warnOrId =
|
warnOrId =
|
||||||
if newName != name then
|
if newName != name then
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue