more accurate support for EitherCodec

This commit is contained in:
Tom Sydney Kerckhove 2024-08-22 01:22:32 +02:00
parent 7b67f8c468
commit f19887d136
9 changed files with 99 additions and 48 deletions

View File

@ -1,51 +1,63 @@
{ lib }:
{
1 = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
1old = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2 = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2old = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3 = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3old = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
new = lib.mkOption {
default = null;
description = "new key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
newer = lib.mkOption {
default = null;
description = "newer key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
newest = lib.mkOption {
default = null;
description = "newest key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
old = lib.mkOption {
default = null;
description = "old key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
older = lib.mkOption {
default = null;
description = "older key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
oldest = lib.mkOption {
default = null;
description = "oldest key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
}

View File

@ -2,52 +2,64 @@
lib.types.submodule {
options = {
1 = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
1old = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2 = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2old = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3 = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3old = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
new = lib.mkOption {
default = null;
description = "new key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
newer = lib.mkOption {
default = null;
description = "newer key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
newest = lib.mkOption {
default = null;
description = "newest key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
old = lib.mkOption {
default = null;
description = "old key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
older = lib.mkOption {
default = null;
description = "older key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
oldest = lib.mkOption {
default = null;
description = "oldest key";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
};
}

View File

@ -1,27 +1,33 @@
{ lib }:
{
1 = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
1old = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2 = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2old = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3 = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3old = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
}

View File

@ -2,28 +2,34 @@
lib.types.submodule {
options = {
1 = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
1old = lib.mkOption {
default = null;
description = "text 1";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2 = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
2old = lib.mkOption {
default = null;
description = "text 2";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3 = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
3old = lib.mkOption {
default = null;
description = "text 3";
type = lib.types.str;
type = lib.types.nullOr lib.types.str;
};
};
}

View File

@ -1,5 +1,11 @@
# Changelog
## [0.0.1.4] - 2024-08-22
### Changed
* More accurate support for `EitherCodec` in `ObjectCodec`s.
## [0.0.1.3] - 2024-08-22
### Changed

View File

@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack
name: autodocodec-nix
version: 0.0.1.3
version: 0.0.1.4
synopsis: Autodocodec interpreters for nix
homepage: https://github.com/NorfairKing/autodocodec#readme
bug-reports: https://github.com/NorfairKing/autodocodec/issues

View File

@ -3,7 +3,7 @@
}:
mkDerivation {
pname = "autodocodec-nix";
version = "0.0.1.3";
version = "0.0.1.4";
src = ./.;
libraryHaskellDepends = [
aeson autodocodec base containers scientific text

View File

@ -1,5 +1,5 @@
name: autodocodec-nix
version: 0.0.1.3
version: 0.0.1.4
github: "NorfairKing/autodocodec"
license: MIT
author: "Tom Sydney Kerckhove"

View File

@ -106,10 +106,11 @@ valueCodecNixOptionType = fmap simplifyOptionType . go
-- If Nix options ever figure out how to do optional fields, we'll use that
-- instead.
objectCodecNixOptions :: ObjectCodec input output -> Map Text Option
objectCodecNixOptions = simplifyOptions . go
objectCodecNixOptions = simplifyOptions . go False
where
go :: ObjectCodec input output -> Map Text Option
go = \case
-- The bool means 'force optional'
go :: Bool -> ObjectCodec input output -> Map Text Option
go b = \case
DiscriminatedUnionCodec k _ m ->
M.insert
k
@ -127,14 +128,22 @@ objectCodecNixOptions = simplifyOptions . go
optionDefault = Nothing
}
)
$ map (go . snd)
$ map (go b . snd)
$ HM.elems m
RequiredKeyCodec key o mDesc ->
M.singleton key $
Option
{ optionType = valueCodecNixOptionType o,
{ optionType =
( if b
then fmap OptionTypeNullOr
else id
)
$ valueCodecNixOptionType o,
optionDescription = mDesc,
optionDefault = Nothing -- [ref:NixOptionNullable]
optionDefault =
if b
then Just JSON.Null
else Nothing -- [ref:NixOptionNullable]
}
OptionalKeyCodec key o mDesc ->
M.singleton key $
@ -160,9 +169,9 @@ objectCodecNixOptions = simplifyOptions . go
optionDefault = Just $ toJSONVia c defaultValue
}
PureCodec _ -> M.empty
ApCodec c1 c2 -> M.union (go c1) (go c2)
BimapCodec _ _ c -> go c
EitherCodec _ c1 c2 -> M.union (go c1) (go c2) -- TODO use an or?
ApCodec c1 c2 -> M.union (go b c1) (go b c2)
BimapCodec _ _ c -> go b c
EitherCodec _ c1 c2 -> M.union (go True c1) (go True c2) -- TODO use a more accurate or?
data Option = Option
{ optionType :: !(Maybe OptionType),