3
0
Fork 0
forked from mirrors/nixpkgs

Merge pull request #24974 from Ericson2314/mapNullable

Introduce `mapNullable` into lib and use it in a few places
This commit is contained in:
John Ericson 2017-04-17 17:12:14 -04:00 committed by GitHub
commit 37e5e71fdf
7 changed files with 18 additions and 14 deletions

View file

@ -122,6 +122,9 @@ rec {
# Flip the order of the arguments of a binary function. # Flip the order of the arguments of a binary function.
flip = f: a: b: f b a; flip = f: a: b: f b a;
# Apply function if argument is non-null
mapNullable = f: a: if isNull a then a else f a;
# Pull in some builtins not included elsewhere. # Pull in some builtins not included elsewhere.
inherit (builtins) inherit (builtins)
pathExists readFile isBool isFunction pathExists readFile isBool isFunction

View file

@ -4,7 +4,7 @@ with lib;
let let
cfg = config.services.graphite; cfg = config.services.graphite;
writeTextOrNull = f: t: if t == null then null else pkgs.writeTextDir f t; writeTextOrNull = f: t: mapNullable (pkgs.writeTextDir f) t;
dataDir = cfg.dataDir; dataDir = cfg.dataDir;

View file

@ -141,7 +141,7 @@ let
}; };
}); });
default = null; default = null;
apply = x: if x == null then null else _filter x; apply = x: mapNullable _filter x;
description = '' description = ''
Optional http login credentials for metrics scraping. Optional http login credentials for metrics scraping.
''; '';

View file

@ -7,9 +7,10 @@
with stdenv.lib; with stdenv.lib;
let let
mkFlag = trueStr: falseStr: cond: name: val: mkFlag = trueStr: falseStr: cond: name: val: "--"
if cond == null then null else + (if cond then trueStr else falseStr)
"--${if cond != false then trueStr else falseStr}${name}${if val != null && cond != false then "=${val}" else ""}"; + name
+ optionalString (val != null && cond != false) "=${val}";
mkEnable = mkFlag "enable-" "disable-"; mkEnable = mkFlag "enable-" "disable-";
mkWith = mkFlag "with-" "without-"; mkWith = mkFlag "with-" "without-";
mkOther = mkFlag "" "" true; mkOther = mkFlag "" "" true;

View file

@ -85,7 +85,7 @@ let
withGtk2 = preferGtk2; withGtk2 = preferGtk2;
withGtk3 = !preferGtk2; withGtk3 = !preferGtk2;
}; };
persistenced = if persistencedSha256 == null then null else callPackage (import ./persistenced.nix self persistencedSha256) { }; persistenced = mapNullable (hash: callPackage (import ./persistenced.nix self hash) { }) persistencedSha256;
}; };
meta = with stdenv.lib; { meta = with stdenv.lib; {

View file

@ -6,9 +6,10 @@
}: }:
let let
mkFlag = trueStr: falseStr: cond: name: val: mkFlag = trueStr: falseStr: cond: name: val: "--"
if cond == null then null else + (if cond then trueStr else falseStr)
"--${if cond != false then trueStr else falseStr}${name}${if val != null && cond != false then "=${val}" else ""}"; + name
+ stdenv.lib.optionalString (val != null && cond != false) "=${val}";
mkEnable = mkFlag "enable-" "disable-"; mkEnable = mkFlag "enable-" "disable-";
mkWith = mkFlag "with-" "without-"; mkWith = mkFlag "with-" "without-";
mkOther = mkFlag "" "" true; mkOther = mkFlag "" "" true;

View file

@ -31,11 +31,10 @@ with stdenv;
with stdenv.lib; with stdenv.lib;
let let
inherit (python2Packages) python; inherit (python2Packages) python;
mkFlag = trueStr: falseStr: cond: name: val: mkFlag = trueStr: falseStr: cond: name: val: "--"
if cond == null then null else + (if cond then trueStr else falseStr)
"--${if cond != false then trueStr else falseStr}${name}" + name
+ "${if val != null && cond != false then "=${val}" else ""}"; + optionalString (val != null && cond != false) "=${val}";
mkEnable = mkFlag "enable-" "disable-"; mkEnable = mkFlag "enable-" "disable-";
mkWith = mkFlag "with-" "without-"; mkWith = mkFlag "with-" "without-";
mkOther = mkFlag "" "" true; mkOther = mkFlag "" "" true;