Skip to content

Commit

Permalink
Merge pull request #14 from composite-hs/df/default-field
Browse files Browse the repository at this point in the history
Field default behavior
  • Loading branch information
dfithian authored Nov 30, 2023
2 parents a756801 + 0f4427e commit 283cf87
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions composite-aeson/src/Composite/Aeson/Record.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Composite.Aeson.Record
( ToJsonField(..), FromJsonField(..), JsonField(..)
, field, valField, field', fromField, valFromField, fromField', toField, toField'
, defaultField, valDefaultField, defaultField'
, optionalField, valOptionalField, optionalField', fromOptionalField, valFromOptionalField, fromOptionalField', toOptionalField, toOptionalField', defaultValFromOptionalField
, JsonFormatRecord, ToJsonFormatRecord, FromJsonFormatRecord, zipJsonFormatRecord, toJsonFormatRecord, fromJsonFormatRecord
, DefaultJsonFormatRecord, defaultJsonFormatRecord
Expand Down Expand Up @@ -67,6 +68,26 @@ valField = field
field' :: JsonFormat e a -> JsonField e a
field' (JsonFormat (JsonProfunctor o i)) = JsonField (Just . o) (`ABE.key` i)

-- | Given a 'JsonFormat' for some type @a@, produce a 'JsonField' for fields of type @a@ which substitutes a default value if the field is missing
-- and never elides the field.
defaultField :: (Wrapped a', Unwrapped a' ~ a) => a -> JsonFormat e a -> JsonField e a'
defaultField default_ (JsonFormat (JsonProfunctor o i)) =
JsonField
(Just . o . view _Wrapped')
(\k -> view (from _Wrapped') . fromMaybe default_ . join <$> ABE.keyMay k (ABE.perhaps i))

-- | Specialized type for 'defaultField' so we can specify the 'Val' symbol.
valDefaultField :: forall s a e. a -> JsonFormat e a -> JsonField e (s :-> a)
valDefaultField = defaultField

-- | Given a 'JsonFormat' for some type @a@, produce a 'JsonField' for fields of type @a@ which substitutes a default value if the field is missing
-- and never elides the field.
defaultField' :: a -> JsonFormat e a -> JsonField e a
defaultField' default_ (JsonFormat (JsonProfunctor o i)) =
JsonField
(Just . o)
(\k -> fromMaybe default_ . join <$> ABE.keyMay k (ABE.perhaps i))

-- |Given a parser for @'Unwrapped' a@, produce a @'FromField' e a@.
fromField :: Wrapped a => ABE.Parse e (Unwrapped a) -> FromJsonField e a
fromField = FromJsonField . flip ABE.key . fmap (review _Wrapped')
Expand Down

0 comments on commit 283cf87

Please sign in to comment.