helpers: add rawKeysAttrs

This commit is contained in:
Gaetan Lepage 2024-05-31 10:14:48 +02:00 committed by Gaétan Lepage
parent 03c5f5eb74
commit 0ba2ea5416
2 changed files with 56 additions and 0 deletions

View file

@ -40,3 +40,18 @@ A certain number of helpers are defined that can be useful:
- `helpers.enableExceptInTests`: Evaluates to `true`, except in `mkTestDerivationFromNixvimModule`
where it evaluates to `false`. This allows to skip instantiating plugins that can't be run in tests.
- `helpers.toRawKeys attrs`: Convert the keys of the given `attrs` to raw lua.
```nix
toRawKeys {foo = 1; bar = "hi";}
```
will translate in lua to:
```lua
{[foo] = 1, [bar] = 2,}
```
Otherwise, the keys of a regular `attrs` will be interpreted as lua string:
```lua
{['foo'] = 1, ['bar'] = 2,}
-- which is the same as
{foo = 1, bar = 2,}
```

View file

@ -11,6 +11,47 @@ with lib;
"__empty" = null;
};
/**
Turn all the keys of an attrs into raw lua.
# Example
```nix
toRawKeys { foo = 1; bar = 2; }
=> { __rawKey__foo = 1; __rawKey__bar = 2; }
```
# Type
```
toRawKeys :: AttrSet -> AttrSet
```
*/
toRawKeys = mapAttrs' (n: v: nameValuePair "__rawKey__${n}" v);
/**
Create a 1-element attrs with a raw lua key.
# Example
```nix
mkRawKey "foo" 1
=> { __rawKey__foo = 1; }
```
# Type
```
mkRawKey :: String -> String -> AttrSet
```
# Arguments
- [n] The attribute name (raw lua)
- [v] The attribute value
*/
mkRawKey = n: v: toRawKeys { "${n}" = v; };
/*
Convert a string from camelCase to snake_case
Type: string -> string