deriving via example

This commit is contained in:
Tom Sydney Kerckhove 2021-11-01 18:42:32 +01:00
parent ae46d36ef4
commit 312149899a
12 changed files with 89 additions and 2 deletions

View File

@ -24,6 +24,7 @@ library
exposed-modules:
Autodocodec.Aeson
Autodocodec.Aeson.Decode
Autodocodec.Aeson.DerivingVia
Autodocodec.Aeson.Document
Autodocodec.Aeson.Encode
other-modules:

View File

@ -1,11 +1,15 @@
{-# OPTIONS_GHC -fno-warn-dodgy-exports #-}
module Autodocodec.Aeson
( -- * To makes sure we definitely export everything.
module Autodocodec.Aeson.Decode,
module Autodocodec.Aeson.DerivingVia,
module Autodocodec.Aeson.Document,
module Autodocodec.Aeson.Encode,
)
where
import Autodocodec.Aeson.Decode
import Autodocodec.Aeson.DerivingVia ()
import Autodocodec.Aeson.Document
import Autodocodec.Aeson.Encode

View File

@ -0,0 +1,14 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Autodocodec.Aeson.DerivingVia where
import Autodocodec
import Autodocodec.Aeson.Decode
import Autodocodec.Aeson.Encode
import Data.Aeson
instance HasCodec a => ToJSON (Autodocodec a) where
toJSON = toJSONViaCodec . unAutodocodec
instance HasCodec a => FromJSON (Autodocodec a) where
parseJSON = fmap Autodocodec <$> parseJSONViaCodec

View File

@ -1,4 +1,5 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
@ -10,6 +11,7 @@
module Autodocodec.Usage where
import Autodocodec
import Autodocodec.Aeson ()
import Control.Applicative
import Data.Aeson (FromJSON (..), ToJSON (..))
import qualified Data.Aeson as JSON
@ -32,8 +34,8 @@ data Fruit
instance Validity Fruit
instance GenValid Fruit where
genValid = genValidStructurally
shrinkValid = shrinkValidStructurally
genValid = genValidStructurallyWithoutExtraChecking
shrinkValid = shrinkValidStructurallyWithoutExtraFiltering
instance HasCodec Fruit where
codec = shownBoundedEnumCodec
@ -149,3 +151,20 @@ instance HasCodec Recursive where
eitherCodec
(codec @Int <?> "base case")
(object "Recurse" $ requiredField "recurse" "recursive case")
data Via = Via {viaOne :: !Text, viaTwo :: !Text}
deriving stock (Show, Eq, Generic)
deriving (FromJSON, ToJSON) via (Autodocodec Via)
instance Validity Via
instance GenValid Via where
genValid = genValidStructurallyWithoutExtraChecking
shrinkValid = shrinkValidStructurallyWithoutExtraFiltering
instance HasCodec Via where
codec =
object "Via" $
Via
<$> requiredField "one" "first field" .= viaOne
<*> requiredField "two" "second field" .= viaTwo

View File

@ -55,6 +55,7 @@ spec = do
jsonSchemaSpec @[Text] "list-text"
jsonSchemaSpec @Example "example"
jsonSchemaSpec @Recursive "recursive"
jsonSchemaSpec @Via "via"
describe "JSONSchema" $ do
genValidSpec @JSONSchema
it "roundtrips through json and back" $

View File

@ -53,6 +53,7 @@ spec = do
aesonCodecSpec @Fruit
aesonCodecSpec @Example
aesonCodecSpec @Recursive
aesonCodecSpec @Via
aesonCodecSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, ToJSON a, FromJSON a, HasCodec a) => Spec
aesonCodecSpec =

View File

@ -50,6 +50,7 @@ spec = do
yamlSchemaSpec @Fruit "fruit"
yamlSchemaSpec @Example "example"
yamlSchemaSpec @Recursive "recursive"
yamlSchemaSpec @Via "via"
yamlSchemaSpec :: forall a. (Show a, Eq a, Typeable a, GenValid a, HasCodec a) => FilePath -> Spec
yamlSchemaSpec filePath = do

View File

@ -0,0 +1,16 @@
{
"$comment": "Via",
"required": [
"one",
"two"
],
"type": "object",
"properties": {
"two": {
"type": "string"
},
"one": {
"type": "string"
}
}
}

View File

@ -0,0 +1,7 @@
# Via
one: # required
# first field
<string>
two: # required
# second field
<string>

View File

@ -25,6 +25,7 @@ library
Autodocodec
Autodocodec.Class
Autodocodec.Codec
Autodocodec.DerivingVia
other-modules:
Paths_autodocodec
hs-source-dirs:

View File

@ -68,14 +68,19 @@ module Autodocodec
pureCodec,
apCodec,
-- * Deriving Via
Autodocodec (..),
-- ** Internals you most likely don't need
showCodecABit,
-- * To make sure we definitely export everything
module Autodocodec.Class,
module Autodocodec.DerivingVia,
module Autodocodec.Codec,
)
where
import Autodocodec.Class
import Autodocodec.Codec
import Autodocodec.DerivingVia

View File

@ -0,0 +1,17 @@
module Autodocodec.DerivingVia where
-- | 'Autodocodec' is a wrapper to provide codec-based deriving strategies.
--
-- === Example usage
--
-- > data Via = Via {viaOne :: !Text, viaTwo :: !Text}
-- > deriving stock (Show, Eq, Generic)
-- > deriving (FromJSON, ToJSON) via (Autodocodec Via)
-- >
-- > instance HasCodec Via where
-- > codec =
-- > object "Via" $
-- > Via
-- > <$> requiredField "one" "first field" .= viaOne
-- > <*> requiredField "two" "second field" .= viaTwo
newtype Autodocodec a = Autodocodec {unAutodocodec :: a}