nonempty list instance

This commit is contained in:
Tom Sydney Kerckhove 2021-11-05 16:52:18 +01:00
parent 5a26606d59
commit 4de5e5f844
12 changed files with 61 additions and 0 deletions

View File

@ -56,6 +56,7 @@ spec = do
jsonSchemaSpec @(Either Bool Text) "either-bool-text"
jsonSchemaSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text"
jsonSchemaSpec @[Text] "list-text"
jsonSchemaSpec @(NonEmpty Text) "nonempty-text"
jsonSchemaSpec @Example "example"
jsonSchemaSpec @Recursive "recursive"
jsonSchemaSpec @Via "via"

View File

@ -17,6 +17,7 @@ import Data.GenValidity.Aeson ()
import Data.GenValidity.Scientific ()
import Data.GenValidity.Text ()
import Data.Int
import Data.List.NonEmpty (NonEmpty)
import Data.Scientific
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
@ -51,6 +52,7 @@ spec = do
aesonCodecSpec @(Either Bool Text)
aesonCodecSpec @(Either (Either Bool Scientific) Text)
aesonCodecSpec @[Text]
aesonCodecSpec @(NonEmpty Text)
aesonCodecSpec @Fruit
aesonCodecSpec @Example
aesonCodecSpec @Recursive

View File

@ -18,6 +18,7 @@ import Data.GenValidity.Containers ()
import Data.GenValidity.Scientific ()
import Data.GenValidity.Text ()
import Data.Int
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe
import Data.OpenApi (Components (..), OpenApi (..))
import qualified Data.OpenApi as OpenAPI
@ -57,6 +58,7 @@ spec = do
openAPISchemaSpec @(Either Bool Text) "either-bool-text"
openAPISchemaSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text"
openAPISchemaSpec @[Text] "list-text"
openAPISchemaSpec @(NonEmpty Text) "nonempty-text"
openAPISchemaSpec @Fruit "fruit"
openAPISchemaSpec @Example "example"
openAPISchemaSpec @Recursive "recursive"

View File

@ -14,6 +14,7 @@ import Data.GenValidity.Aeson ()
import Data.GenValidity.Scientific ()
import Data.GenValidity.Text ()
import Data.Int
import Data.List.NonEmpty (NonEmpty)
import Data.Scientific
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
@ -47,6 +48,7 @@ spec = do
showCodecSpec @(Either Bool Text) "either-bool-text"
showCodecSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text"
showCodecSpec @[Text] "list-text"
showCodecSpec @(NonEmpty Text) "nonempty-text"
showCodecSpec @Fruit "fruit"
showCodecSpec @Example "example"
showCodecSpec @Recursive "recursive"

View File

@ -18,6 +18,7 @@ import Data.GenValidity.Containers ()
import Data.GenValidity.Scientific ()
import Data.GenValidity.Text ()
import Data.Int
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe
import Data.Scientific
import Data.Swagger (Swagger (..))
@ -57,6 +58,7 @@ spec = do
swaggerSchemaSpec @(Either Bool Text) "either-bool-text"
swaggerSchemaSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text"
swaggerSchemaSpec @[Text] "list-text"
swaggerSchemaSpec @(NonEmpty Text) "nonempty-text"
swaggerSchemaSpec @Fruit "fruit"
swaggerSchemaSpec @Example "example"
swaggerSchemaSpec @Recursive "recursive"

View File

@ -15,6 +15,7 @@ import Data.GenValidity.Aeson ()
import Data.GenValidity.Scientific ()
import Data.GenValidity.Text ()
import Data.Int
import Data.List.NonEmpty (NonEmpty)
import Data.Scientific
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
@ -48,6 +49,7 @@ spec = do
yamlSchemaSpec @(Either Bool Text) "either-bool-text"
yamlSchemaSpec @(Either (Either Bool Scientific) Text) "either-either-bool-scientific-text"
yamlSchemaSpec @[Text] "list-text"
yamlSchemaSpec @(NonEmpty Text) "nonempty-text"
yamlSchemaSpec @Fruit "fruit"
yamlSchemaSpec @Example "example"
yamlSchemaSpec @Recursive "recursive"

View File

@ -0,0 +1,6 @@
{
"items": {
"type": "string"
},
"type": "array"
}

View File

@ -0,0 +1,18 @@
{
"components": {
"schemas": {
"(NonEmpty Text)": {
"items": {
"type": "string"
},
"type": "array"
}
}
},
"openapi": "3.0.0",
"info": {
"version": "",
"title": ""
},
"paths": {}
}

View File

@ -0,0 +1 @@
MapCodec _ _ (MapCodec _ _ (ArrayOfCodec Nothing (StringCodec Nothing)))

View File

@ -0,0 +1,15 @@
{
"swagger": "2.0",
"info": {
"version": "",
"title": ""
},
"definitions": {
"(NonEmpty Text)": {
"items": {
"type": "string"
},
"type": "array"
}
}
}

View File

@ -0,0 +1 @@
- <string>

View File

@ -9,6 +9,8 @@ module Autodocodec.Class where
import Autodocodec.Codec
import qualified Data.Aeson as JSON
import Data.Int
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.List.NonEmpty as NE
import Data.Scientific
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
@ -100,6 +102,13 @@ instance (HasCodec l, HasCodec r) => HasCodec (Either l r) where
instance HasCodec a => HasCodec [a] where
codec = listCodecForStringCompatibility
instance HasCodec a => HasCodec (NonEmpty a) where
codec = bimapCodec parseNonEmptyList NE.toList codec
where
parseNonEmptyList l = case NE.nonEmpty l of
Nothing -> Left "Expected a nonempty list, but got an empty list."
Just ne -> Right ne
-- | A required field
--
-- During decoding, the field must be in the object.