1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-05 02:39: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.uint64`
- `hm.gvariant.type.double` - `hm.gvariant.type.double`
- `hm.gvariant.type.arrayOf type` - `hm.gvariant.type.arrayOf type`
- `hm.gvariant.type.maybeOf type`
- `hm.gvariant.type.tupleOf types` - `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`::: `hm.gvariant.mkEmptyArray type`:::
An alias of `hm.gvariant.mkArray 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`::: `hm.gvariant.mkTuple elements`:::
Builds a GVariant tuple containing the given list of elements, where each element is a GVariant value. 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 = { type = {
arrayOf = t: "a${t}"; arrayOf = t: "a${t}";
maybeOf = t: "m${t}";
tupleOf = ts: "(${concatStrings ts})"; tupleOf = ts: "(${concatStrings ts})";
string = "s"; string = "s";
boolean = "b"; boolean = "b";
@ -57,11 +58,21 @@ let
else 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 { in rec {
inherit type typeOf; inherit type typeOf;
isArray = hasPrefix "a"; isArray = hasPrefix "a";
isMaybe = hasPrefix "m";
isTuple = hasPrefix "("; isTuple = hasPrefix "(";
# Returns the GVariant value that most closely matches the given Nix # Returns the GVariant value that most closely matches the given Nix
@ -92,6 +103,10 @@ in rec {
mkEmptyArray = elemType: mkArray elemType [ ]; mkEmptyArray = elemType: mkArray elemType [ ];
mkNothing = elemType: mkMaybe elemType null;
mkJust = elem: let gvarElem = mkValue elem; in mkMaybe gvarElem.type gvarElem;
mkTuple = elems: mkTuple = elems:
let let
gvarElems = map mkValue elems; gvarElems = map mkValue elems;

View file

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

View file

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