Skip to content

Commit

Permalink
lib/types: init elemTypeFunctor for types.{attrsWith,listOf}
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed Dec 18, 2024
1 parent 08de518 commit 3f80731
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ let
let pos = builtins.unsafeGetAttrPos name v; in
if pos == null then "" else " at ${pos.file}:${toString pos.line}:${toString pos.column}";

# Internal functor to help for migrating functor.wrapped to functor.payload.elemType
# Note that individual attributes can be overriden if needed.
elemTypeFunctor = name: { elemType, ... }@payload: {
inherit name payload;
type = outer_types.types.${name};
binOp = a: b:
let
merged = a.elemType.typeMerge b.elemType.functor;
in
if merged == null
then
null
else
{ elemType = merged; };
wrappedDeprecationMessage = { loc }: lib.warn ''
The deprecated `type.functor.wrapped` attribute of the option `${showOption loc}` is accessed, use `type.nestedTypes.elemType` instead.
'' payload.elemType;
};


outer_types =
rec {
isType = type: x: (x._type or "") == type;
Expand Down Expand Up @@ -128,6 +148,7 @@ rec {
binOp = a: b: null;
};


isOptionType = isType "option-type";
mkOptionType =
{ # Human-readable representation of the type, should be equivalent to
Expand Down Expand Up @@ -580,7 +601,9 @@ rec {
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["*"]);
getSubModules = elemType.getSubModules;
substSubModules = m: listOf (elemType.substSubModules m);
functor = (defaultFunctor name) // { wrapped = elemType; };
functor = (elemTypeFunctor name { inherit elemType; }) // {
type = payload: types.listOf payload.elemType;
};
nestedTypes.elemType = elemType;
};

Expand Down Expand Up @@ -664,14 +687,10 @@ rec {
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<${placeholder}>"]);
getSubModules = elemType.getSubModules;
substSubModules = m: attrsWith { elemType = elemType.substSubModules m; inherit lazy placeholder; };
functor = defaultFunctor "attrsWith" // {
wrappedDeprecationMessage = { loc }: lib.warn ''
The deprecated `type.functor.wrapped` attribute of the option `${showOption loc}` is accessed, use `type.nestedTypes.elemType` instead.
'' elemType;
payload = {
# Important!: Add new function attributes here in case of future changes
functor = (elemTypeFunctor "attrsWith" {
inherit elemType lazy placeholder;
};
}) // {
# Custom type merging required because of the "placeholder" attribute
inherit binOp;
};
nestedTypes.elemType = elemType;
Expand Down

0 comments on commit 3f80731

Please sign in to comment.