Module

Data.Codec.Argonaut.Variant

Package
purescript-codec-argonaut
Repository
garyb/purescript-codec-argonaut

#variant Source

variant :: JsonCodec (Variant ())

Allows building codecs for variants in combination with variantCase.

Commonly used to write decoders for sum-types, by providing a mapping from and to a Variant from that type and then using dimap.

codecMaybe ∷ ∀ a. JA.JsonCodec a → JA.JsonCodec (Maybe a)
codecMaybe codecA =
  dimap toVariant fromVariant
    (JAV.variant
      # JAV.variantCase _Just (Right codecA)
      # JAV.variantCase _Nothing (Left unit))
  where
  toVariant = case _ of
    Just a → V.inj _Just a
    Nothing → V.inj _Nothing unit
  fromVariant = V.case_
    # V.on _Just Just
    # V.on _Nothing (const Nothing)
  _Just = SProxy ∷ SProxy "just"
  _Nothing = SProxy ∷ SProxy "nothing"

#variantCase Source

variantCase :: forall r' r a l. IsSymbol l => Cons l a r r' => SProxy l -> Either a (JsonCodec a) -> JsonCodec (Variant r) -> JsonCodec (Variant r')

#VariantCodec Source

class VariantCodec (rl :: RowList) (ri :: Row Type) (ro :: Row Type) | rl -> ri ro where

Members

Instances

#variantMatch Source

variantMatch :: forall ro ri rl. RowToList ri rl => VariantCodec rl ri ro => Record ri -> JsonCodec (Variant ro)