lib.fetchers.normalizeHash: more implementation comment and clearer variable names

This commit is contained in:
nicoo 2024-09-17 14:48:44 +00:00
parent 0aa5242829
commit 09eb3c64e8

View File

@ -2,8 +2,8 @@
{ lib }: { lib }:
let let
commonH = hashTypes: rec { commonH = hashTypes: rec {
hNames = [ "hash" ] ++ hashTypes; hashNames = [ "hash" ] ++ hashTypes;
hAttrs = lib.genAttrs hNames (lib.const {}); hashSet = lib.genAttrs hashNames (lib.const {});
}; };
fakeH = { fakeH = {
@ -85,7 +85,7 @@ in rec {
inherit (lib) concatMapStringsSep head tail throwIf; inherit (lib) concatMapStringsSep head tail throwIf;
inherit (lib.attrsets) attrsToList intersectAttrs removeAttrs optionalAttrs; inherit (lib.attrsets) attrsToList intersectAttrs removeAttrs optionalAttrs;
inherit (commonH hashTypes) hAttrs hNames; inherit (commonH hashTypes) hashNames hashSet;
in in
args: args:
if args ? "outputHash" then if args ? "outputHash" then
@ -94,16 +94,17 @@ in rec {
let let
# The argument hash, as a {name, value} pair # The argument hash, as a {name, value} pair
h = h =
let _h = attrsToList (intersectAttrs hAttrs args); in # All hashes passed in arguments (possibly 0 or >1) as a list of {name, value} pairs
if _h == [] then let hashesAsNVPairs = attrsToList (intersectAttrs hashSet args); in
if hashesAsNVPairs == [] then
throwIf required "fetcher called without `hash`" null throwIf required "fetcher called without `hash`" null
else if tail _h != [] then else if tail hashesAsNVPairs != [] then
throw "fetcher called with mutually-incompatible arguments: ${concatMapStringsSep ", " (a: a.name) _h}" throw "fetcher called with mutually-incompatible arguments: ${concatMapStringsSep ", " (a: a.name) hashesAsNVPairs}"
else else
head _h head hashesAsNVPairs
; ;
in in
removeAttrs args hNames // (optionalAttrs (h != null) { removeAttrs args hashNames // (optionalAttrs (h != null) {
outputHashAlgo = if h.name == "hash" then null else h.name; outputHashAlgo = if h.name == "hash" then null else h.name;
outputHash = outputHash =
if h.value == "" then if h.value == "" then
@ -170,7 +171,7 @@ in rec {
inherit (lib.attrsets) genAttrs intersectAttrs removeAttrs; inherit (lib.attrsets) genAttrs intersectAttrs removeAttrs;
inherit (lib.trivial) const functionArgs setFunctionArgs; inherit (lib.trivial) const functionArgs setFunctionArgs;
inherit (commonH hashTypes) hAttrs; inherit (commonH hashTypes) hashSet;
fArgs = functionArgs fetcher; fArgs = functionArgs fetcher;
normalize = normalizeHash { normalize = normalizeHash {
@ -180,7 +181,7 @@ in rec {
in in
# The o.g. fetcher must *only* accept outputHash and outputHashAlgo # The o.g. fetcher must *only* accept outputHash and outputHashAlgo
assert fArgs ? outputHash && fArgs ? outputHashAlgo; assert fArgs ? outputHash && fArgs ? outputHashAlgo;
assert intersectAttrs fArgs hAttrs == {}; assert intersectAttrs fArgs hashSet == {};
setFunctionArgs setFunctionArgs
(args: fetcher (normalize args)) (args: fetcher (normalize args))