mirror of
https://github.com/NorfairKing/autodocodec.git
synced 2024-11-30 03:22:52 +03:00
optionalFieldOrNullWithOmittedDefault
This commit is contained in:
parent
3af2f4b216
commit
9cb3e6995c
@ -9,7 +9,6 @@ module Autodocodec.Aeson.Decode where
|
||||
import Autodocodec.Class
|
||||
import Autodocodec.Codec
|
||||
import Autodocodec.DerivingVia
|
||||
import Control.Applicative
|
||||
import Control.Monad
|
||||
import Data.Aeson as JSON
|
||||
import Data.Aeson.Types as JSON
|
||||
|
@ -270,3 +270,23 @@ optionalFieldWithOmittedDefault' ::
|
||||
output ->
|
||||
ObjectCodec output output
|
||||
optionalFieldWithOmittedDefault' key defaultValue = optionalFieldWithOmittedDefaultWith' key codec defaultValue
|
||||
|
||||
optionalFieldOrNullWithOmittedDefault ::
|
||||
(Eq output, HasCodec output) =>
|
||||
-- | Key
|
||||
Text ->
|
||||
-- | Default value
|
||||
output ->
|
||||
-- | Documentation
|
||||
Text ->
|
||||
ObjectCodec output output
|
||||
optionalFieldOrNullWithOmittedDefault key defaultValue doc = optionalFieldOrNullWithOmittedDefaultWith key codec defaultValue doc
|
||||
|
||||
optionalFieldOrNullWithOmittedDefault' ::
|
||||
(Eq output, HasCodec output) =>
|
||||
-- | Key
|
||||
Text ->
|
||||
-- | Default value
|
||||
output ->
|
||||
ObjectCodec output output
|
||||
optionalFieldOrNullWithOmittedDefault' key defaultValue = optionalFieldOrNullWithOmittedDefaultWith' key codec defaultValue
|
||||
|
@ -569,6 +569,40 @@ optionalFieldWithOmittedDefaultWith' ::
|
||||
ObjectCodec output output
|
||||
optionalFieldWithOmittedDefaultWith' key c defaultValue = OptionalKeyWithOmittedDefaultCodec key c defaultValue Nothing
|
||||
|
||||
optionalFieldOrNullWithOmittedDefaultWith ::
|
||||
Eq output =>
|
||||
-- | Key
|
||||
Text ->
|
||||
-- | Codec for the value
|
||||
JSONCodec output ->
|
||||
-- | Default value
|
||||
output ->
|
||||
-- | Documentation
|
||||
Text ->
|
||||
ObjectCodec output output
|
||||
optionalFieldOrNullWithOmittedDefaultWith key c defaultValue doc = dimapCodec f g $ optionalFieldWithOmittedDefaultWith key (maybeCodec c) (Just defaultValue) doc
|
||||
where
|
||||
f = \case
|
||||
Just v -> v
|
||||
Nothing -> defaultValue
|
||||
g v = if v == defaultValue then Nothing else Just v
|
||||
|
||||
optionalFieldOrNullWithOmittedDefaultWith' ::
|
||||
Eq output =>
|
||||
-- | Key
|
||||
Text ->
|
||||
-- | Codec for the value
|
||||
JSONCodec output ->
|
||||
-- | Default value
|
||||
output ->
|
||||
ObjectCodec output output
|
||||
optionalFieldOrNullWithOmittedDefaultWith' key c defaultValue = dimapCodec f g $ optionalFieldWithOmittedDefaultWith' key (maybeCodec c) (Just defaultValue)
|
||||
where
|
||||
f = \case
|
||||
Just v -> v
|
||||
Nothing -> defaultValue
|
||||
g v = if v == defaultValue then Nothing else Just v
|
||||
|
||||
-- | An optional, or null, field
|
||||
--
|
||||
-- During decoding, the field may be in the object. 'Nothing' will be parsed if it is not.
|
||||
|
Loading…
Reference in New Issue
Block a user