1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-26 21:19:45 +01:00

lib: support gvariant maybe type

Fixes #1397
This commit is contained in:
Robert Helgesson 2020-07-24 16:31:56 +02:00
parent 8ad5580021
commit 83301ca787
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
4 changed files with 30 additions and 1 deletions

View file

@ -168,13 +168,20 @@ Builds a GVariant array containing the given list of elements, where each elemen
- `hm.gvariant.type.uint64`
- `hm.gvariant.type.double`
- `hm.gvariant.type.arrayOf type`
- `hm.gvariant.type.maybeOf type`
- `hm.gvariant.type.tupleOf types`
--
+
where `type` and `types` are themselve a type and list of types, respectively.
where `type` and `types` are themselves a type and list of types, respectively.
+
`hm.gvariant.mkEmptyArray type`:::
An alias of `hm.gvariant.mkArray type []`.
+
`hm.gvariant.mkNothing type`:::
Builds a GVariant maybe value whose (non-existent) element is of the given type. The `type` value is constructed as described for the `mkArray` function above.
+
`hm.gvariant.mkJust element`:::
Builds a GVariant maybe value containing the given GVariant element.
+
`hm.gvariant.mkTuple elements`:::
Builds a GVariant tuple containing the given list of elements, where each element is a GVariant value.

View file

@ -18,6 +18,7 @@ let
type = {
arrayOf = t: "a${t}";
maybeOf = t: "m${t}";
tupleOf = ts: "(${concatStrings ts})";
string = "s";
boolean = "b";
@ -57,11 +58,21 @@ let
else
"";
mkMaybe = elemType: elem:
mkPrimitive (type.maybeOf elemType) elem // {
__toString = self:
if self.value == null then
"@${self.type} nothing"
else
"just ${toString self.value}";
};
in rec {
inherit type typeOf;
isArray = hasPrefix "a";
isMaybe = hasPrefix "m";
isTuple = hasPrefix "(";
# Returns the GVariant value that most closely matches the given Nix
@ -92,6 +103,10 @@ in rec {
mkEmptyArray = elemType: mkArray elemType [ ];
mkNothing = elemType: mkMaybe elemType null;
mkJust = elem: let gvarElem = mkValue elem; in mkMaybe gvarElem.type gvarElem;
mkTuple = elems:
let
gvarElems = map mkValue elems;

View file

@ -77,6 +77,8 @@ in rec {
(map (d: d // { value = d.value.value; }) vdefs)
else if gvar.isTuple sharedDefType && allChecked then
mergeOneOption loc defs
else if gvar.isMaybe sharedDefType && allChecked then
mergeOneOption loc defs
else if gvar.type.string == sharedDefType && allChecked then
types.str.merge loc defs
else if gvar.type.double == sharedDefType && allChecked then

View file

@ -28,6 +28,9 @@ in {
{ string = "foo"; }
{ tuple = mkTuple [ 1 [ "foo" ] ]; }
{ maybe1 = mkNothing type.string; }
{ maybe2 = mkJust (mkUint32 4); }
];
home.file."result.txt".text = let
@ -46,6 +49,8 @@ in {
float = 3.140000
int = 42
list = @as ['one','two']
maybe1 = @ms nothing
maybe2 = just @u 4
string = 'foo'
tuple = @(ias) (1,@as ['foo'])
''