Skip to content

Commit e5c9e10

Browse files
committed
Add doc comment for variantMatch
Also move it up in the file so it's seen first on the docs page
1 parent 376076a commit e5c9e10

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

src/Data/Codec/Argonaut/Variant.purs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,52 @@ import Type.Data.RowList (RLProxy(..))
2222
import Type.Equality as TE
2323
import Unsafe.Coerce (unsafeCoerce)
2424

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.
2627
-- |
2728
-- | Commonly used to write decoders for sum-types, by providing a mapping from
2829
-- | and to a Variant from that type and then using `dimap`.
2930
-- |
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+
-- |
3071
-- |```purescript
3172
-- | codecMaybe ∷ ∀ a. JA.JsonCodec a → JA.JsonCodec (Maybe a)
3273
-- | codecMaybe codecA =
@@ -85,6 +126,8 @@ variantCase proxy eacodec (GCodec dec enc) = GCodec dec' enc'
85126
coerceR Variant r Variant r'
86127
coerceR = unsafeCoerce
87128

129+
-- | The class used to enable the building of `Variant` codecs from a record of
130+
-- | codecs.
88131
class VariantCodec (rlRL.RowList) (ri ∷ # Type) (ro ∷ # Type) | rl ri ro where
89132
variantCodec RLProxy rl Record ri JsonCodec (Variant ro)
90133

@@ -106,11 +149,3 @@ instance variantCodecCons ∷
106149

107150
tail JsonCodec (Variant ro')
108151
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)

test/Test/Variant.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ main = do
3131
propCodec
3232
(GenC.genMaybe genAsciiString)
3333
(codecMaybe JA.string)
34+
log "Checking Maybe-variantMatch codec"
35+
quickCheck $
36+
propCodec
37+
(GenC.genMaybe genAsciiString)
38+
(codecMaybeMatch JA.string)
3439

3540
log "Checking Either-variant codec"
3641
quickCheck $
@@ -58,6 +63,22 @@ codecMaybe codecA =
5863
_Just = SProxy SProxy "just"
5964
_Nothing = SProxy SProxy "nothing"
6065

66+
codecMaybeMatch a. JA.JsonCodec a JA.JsonCodec (Maybe a)
67+
codecMaybeMatch codecA =
68+
dimap toVariant fromVariant
69+
(JAV.variantMatch
70+
{ just: Right codecA
71+
, nothing: Left unit
72+
})
73+
where
74+
toVariant = case _ of
75+
Just a → V.inj (SProxy _ "just") a
76+
NothingV.inj (SProxy _ "nothing") unit
77+
fromVariant = V.match
78+
{ just: Just
79+
, nothing: \_ → Nothing
80+
}
81+
6182
codecEither a b. JA.JsonCodec a JA.JsonCodec b JA.JsonCodec (Either a b)
6283
codecEither codecA codecB =
6384
dimap toVariant fromVariant

0 commit comments

Comments
 (0)