@@ -22,11 +22,52 @@ import Type.Data.RowList (RLProxy(..))
22
22
import Type.Equality as TE
23
23
import Unsafe.Coerce (unsafeCoerce )
24
24
25
- -- | Allows building codecs for variants in combination with variantCase.
25
+ -- | Builds a codec for a variant from a record, similar to the way
26
+ -- | `Variant.match` works to pattern match on a variant.
26
27
-- |
27
28
-- | Commonly used to write decoders for sum-types, by providing a mapping from
28
29
-- | and to a Variant from that type and then using `dimap`.
29
30
-- |
31
+ -- | Each field in the record accepts an `Either`, where `Right` is used to
32
+ -- | specify a codec used for the constructor, and `Left` is used to specify a
33
+ -- | static value (generally as `Left unit` for nullary constructors).
34
+ -- |
35
+ -- | The variant will be encoded as a JSON object of the form
36
+ -- | `{ "tag": <name>, "value": <value> }`, where `<name>` is the name of the
37
+ -- | variant case, and `<value>` is the associated value (omitted in the case
38
+ -- | of static `Left`-defined values).
39
+ -- |
40
+ -- |```purescript
41
+ -- | codecMaybeMatch ∷ ∀ a. JA.JsonCodec a → JA.JsonCodec (Maybe a)
42
+ -- | codecMaybeMatch codecA =
43
+ -- | dimap toVariant fromVariant
44
+ -- | (JAV.variantMatch
45
+ -- | { just: Right codecA
46
+ -- | , nothing: Left unit
47
+ -- | })
48
+ -- | where
49
+ -- | toVariant = case _ of
50
+ -- | Just a → V.inj (SProxy ∷ _ "just") a
51
+ -- | Nothing → V.inj (SProxy ∷ _ "nothing") unit
52
+ -- | fromVariant = V.match
53
+ -- | { just: Just
54
+ -- | , nothing: \_ → Nothing
55
+ -- | }
56
+ -- |```
57
+ variantMatch
58
+ ∷ ∀ rl ri ro
59
+ . RL.RowToList ri rl
60
+ ⇒ VariantCodec rl ri ro
61
+ ⇒ Record ri
62
+ → JsonCodec (Variant ro )
63
+ variantMatch = variantCodec (RLProxy ∷ RLProxy rl )
64
+
65
+ -- | Builds codecs for variants in combination with `variantCase`.
66
+ -- |
67
+ -- | Provides an alternative means of building variant codecs to that of
68
+ -- | `variantMatch`, often for cases where the codec is being constructed
69
+ -- | with a fold or some other similar technique.
70
+ -- |
30
71
-- |```purescript
31
72
-- | codecMaybe ∷ ∀ a. JA.JsonCodec a → JA.JsonCodec (Maybe a)
32
73
-- | codecMaybe codecA =
@@ -85,6 +126,8 @@ variantCase proxy eacodec (GCodec dec enc) = GCodec dec' enc'
85
126
coerceR ∷ Variant r → Variant r'
86
127
coerceR = unsafeCoerce
87
128
129
+ -- | The class used to enable the building of `Variant` codecs from a record of
130
+ -- | codecs.
88
131
class VariantCodec (rl ∷ RL.RowList ) (ri ∷ # Type ) (ro ∷ # Type ) | rl → ri ro where
89
132
variantCodec ∷ RLProxy rl → Record ri → JsonCodec (Variant ro )
90
133
@@ -106,11 +149,3 @@ instance variantCodecCons ∷
106
149
107
150
tail ∷ JsonCodec (Variant ro' )
108
151
tail = variantCodec (RLProxy ∷ RLProxy rs ) ((unsafeCoerce ∷ Record ri → Record ri' ) codecs)
109
-
110
- variantMatch
111
- ∷ ∀ rl ri ro
112
- . RL.RowToList ri rl
113
- ⇒ VariantCodec rl ri ro
114
- ⇒ Record ri
115
- → JsonCodec (Variant ro )
116
- variantMatch = variantCodec (RLProxy ∷ RLProxy rl )
0 commit comments