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

lib.gvariant: fix rendering of empty non-string arrays

Before, empty arrays would always be rendered as an empty string
array.
This commit is contained in:
Robert Helgesson 2021-08-13 01:50:09 +02:00
parent a3d691c053
commit a965b097b1
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
3 changed files with 18 additions and 7 deletions

View file

@ -71,6 +71,8 @@ in rec {
inherit type typeOf;
isGVariant = v: v._type or "" == "gvariant";
isArray = hasPrefix "a";
isMaybe = hasPrefix "m";
isTuple = hasPrefix "(";

View file

@ -70,7 +70,11 @@ in rec {
check = v: gvar.mkValue v != null;
merge = loc: defs:
let
vdefs = map (d: d // { value = gvar.mkValue d.value; }) defs;
vdefs = map (d:
d // {
value =
if gvar.isGVariant d.value then d.value else gvar.mkValue d.value;
}) defs;
vals = map (d: d.value) vdefs;
defTypes = map (x: x.type) vals;
sameOrNull = x: y: if x == y then y else null;
@ -82,8 +86,10 @@ in rec {
+ " mismatched GVariant types given in"
+ " ${showFiles (getFiles defs)}.")
else if gvar.isArray sharedDefType && allChecked then
(types.listOf gvariant).merge loc
(map (d: d // { value = d.value.value; }) vdefs)
gvar.mkValue ((types.listOf gvariant).merge loc
(map (d: d // { value = d.value.value; }) vdefs)) // {
type = sharedDefType;
}
else if gvar.isTuple sharedDefType && allChecked then
mergeOneOption loc defs
else if gvar.isMaybe sharedDefType && allChecked then

View file

@ -33,8 +33,10 @@ in {
{ uint64 = mkUint64 42; }
{ uint64 = mkUint64 42; }
{ list = [ "one" ]; }
{ list = mkArray type.string [ "two" ]; }
{ array1 = [ "one" ]; }
{ array1 = mkArray type.string [ "two" ]; }
{ array2 = mkArray type.uint32 [ 1 ]; }
{ array2 = mkArray type.uint32 [ 2 ]; }
{ emptyArray1 = [ ]; }
{ emptyArray2 = mkEmptyArray type.uint32; }
@ -63,15 +65,16 @@ in {
home-files/result.txt \
${
pkgs.writeText "expected.txt" ''
array1 = @as ['one','two']
array2 = @au [1,2]
bool = true
emptyArray1 = @as []
emptyArray2 = @as []
emptyArray2 = @au []
escapedString = '\'\\\n'
float = 3.140000
int = -42
int16 = @n -42
int64 = @x -42
list = @as ['one','two']
maybe1 = @ms nothing
maybe2 = just @u 4
string = 'foo'