Skip to content

Commit e084d8a

Browse files
committed
adds an example to variant and variantCase
1 parent cdb1112 commit e084d8a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/Data/Codec/Argonaut/Variant.purs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ import Type.Equality as TE
2020
import Type.Row as R
2121
import Unsafe.Coerce (unsafeCoerce)
2222

23+
-- | Allows building codecs for variants in combination with variantCase.
24+
-- |
25+
-- | Commonly used to write decoders for sum-types, by providing a mapping from
26+
-- | and to a Variant from that type and then using `dimap`.
27+
-- |
28+
-- |```purescript
29+
-- | codecMaybe ∷ ∀ a. JA.JsonCodec a → JA.JsonCodec (Maybe a)
30+
-- | codecMaybe codecA =
31+
-- | dimap toVariant fromVariant
32+
-- | (JAV.variant
33+
-- | # JAV.variantCase _Just (Right codecA)
34+
-- | # JAV.variantCase _Nothing (Left unit))
35+
-- | where
36+
-- | toVariant = case _ of
37+
-- | Just a → V.inj _Just a
38+
-- | Nothing → V.inj _Nothing unit
39+
-- | fromVariant = V.case_
40+
-- | # V.on _Just Just
41+
-- | # V.on _Nothing (const Nothing)
42+
-- | _Just = SProxy ∷ SProxy "just"
43+
-- | _Nothing = SProxy ∷ SProxy "nothing"
44+
-- |```
2345
variant JsonCodec (Variant ())
2446
variant = GCodec (ReaderT (Left <<< UnexpectedValue)) (Star case_)
2547

0 commit comments

Comments
 (0)