Fixed the type representation of null and nullOr

This commit is contained in:
Tom Sydney Kerckhove 2024-08-21 22:34:36 +02:00
parent d34f34418c
commit d3cbdbc07b
16 changed files with 49 additions and 20 deletions

View File

@ -126,6 +126,9 @@ extra-source-files:
test_resources/nix/lists-example-type.nix
test_resources/nix/local-time-type.nix
test_resources/nix/map-text-ind-type.nix
test_resources/nix/maybe-either-bool-text-type.nix
test_resources/nix/maybe-maybe-maybe-text-type.nix
test_resources/nix/maybe-maybe-text-type.nix
test_resources/nix/maybe-text-type.nix
test_resources/nix/monoid-first-type.nix
test_resources/nix/monoid-last-type.nix

View File

@ -57,8 +57,11 @@ spec = do
nixOptionTypeSpec @Word64 "word64"
nixOptionTypeSpec @Natural "natural"
nixOptionTypeSpec @(Maybe Text) "maybe-text"
nixOptionTypeSpec @(Maybe (Maybe Text)) "maybe-maybe-text"
nixOptionTypeSpec @(Maybe (Maybe (Maybe Text))) "maybe-maybe-maybe-text"
nixOptionTypeSpec @(Either Bool Text) "either-bool-text"
nixOptionTypeSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text"
nixOptionTypeSpec @(Maybe (Either Bool Text)) "maybe-either-bool-text"
nixOptionTypeSpec @[Text] "list-text"
nixOptionTypeSpec @(DList Text) "dlist-text"
nixOptionTypeSpec @(NonEmpty Text) "nonempty-text"

View File

@ -0,0 +1,19 @@
{ lib }:
lib.types.nullOr (lib.types.oneOf [
(lib.types.submodule {
options = {
Left = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.bool;
};
};
})
(lib.types.submodule {
options = {
Right = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.str;
};
};
})
])

View File

@ -0,0 +1,2 @@
{ lib }:
lib.types.nullOr lib.types.str

View File

@ -0,0 +1,2 @@
{ lib }:
lib.types.nullOr lib.types.str

View File

@ -1,5 +1,2 @@
{ lib }:
lib.types.oneOf [
lib.types.null
lib.types.str
]
lib.types.nullOr lib.types.str

View File

@ -1,5 +1,2 @@
{ lib }:
lib.types.oneOf [
lib.types.null
lib.types.str
]
lib.types.nullOr lib.types.str

View File

@ -1,5 +1,2 @@
{ lib }:
lib.types.oneOf [
lib.types.null
lib.types.str
]
lib.types.nullOr lib.types.str

View File

@ -1,2 +1,2 @@
{ lib }:
lib.types.null
lib.types.enum [null]

View File

@ -1,2 +1,2 @@
{ lib }:
lib.types.null
lib.types.enum [null]

View File

@ -1,5 +1,10 @@
# Changelog
## [0.0.1.2] - 2024-08-20
* Fixed the nix type that corresponds to `null`.
* Fixed that some nix types were not being simplified enough.
## [0.0.1.1] - 2024-08-20
### Changed

View File

@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack
name: autodocodec-nix
version: 0.0.1.1
version: 0.0.1.2
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.1";
version = "0.0.1.2";
src = ./.;
libraryHaskellDepends = [
aeson autodocodec base containers scientific text

View File

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

View File

@ -209,7 +209,12 @@ simplifyOptionType = go
OptionTypeAttrsOf o -> OptionTypeAttrsOf $ go o
OptionTypeOneOf os -> case goEnums $ nubOrd $ concatMap goOr os of
[ot] -> ot
os' -> OptionTypeOneOf os'
os' ->
if OptionTypeNull `elem` os'
then go $ OptionTypeNullOr $ case filter (/= OptionTypeNull) os' of
[t] -> t
ts' -> OptionTypeOneOf ts'
else OptionTypeOneOf os'
OptionTypeSubmodule m -> OptionTypeSubmodule $ M.map goOpt m
goEnums :: [OptionType] -> [OptionType]
@ -270,7 +275,7 @@ optionTypeExpr :: OptionType -> Expr
optionTypeExpr = go
where
go = \case
OptionTypeNull -> ExprVar "lib.types.null"
OptionTypeNull -> ExprAp (ExprVar "lib.types.enum") (ExprLitList [ExprNull])
OptionTypeSimple s -> ExprVar s
OptionTypeEnum es -> ExprAp (ExprVar "lib.types.enum") (ExprLitList es)
OptionTypeNullOr ot -> ExprAp (ExprVar "lib.types.nullOr") (go ot)

View File

@ -9,7 +9,6 @@
* `optionalFieldOrNullWithDefault'`
* `optionalFieldOrNullWithDefaultWith'`
## [0.4.2.0] - 2024-08-06
### Added