File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,28 @@ import Type.Equality as TE
20
20
import Type.Row as R
21
21
import Unsafe.Coerce (unsafeCoerce )
22
22
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
+ -- |```
23
45
variant ∷ JsonCodec (Variant ())
24
46
variant = GCodec (ReaderT (Left <<< UnexpectedValue )) (Star case_)
25
47
You can’t perform that action at this time.
0 commit comments