1
- module Data.Codec.Argonaut.Record
2
- ( class RowListCodec
3
- , rowListCodec
4
- , record
5
- ) where
1
+ module Data.Codec.Argonaut.Record where
6
2
7
3
import Data.Codec.Argonaut as CA
8
4
import Data.Symbol (class IsSymbol , SProxy (..))
@@ -13,6 +9,37 @@ import Type.Data.RowList (RLProxy(..))
13
9
import Type.Equality as TE
14
10
import Unsafe.Coerce (unsafeCoerce )
15
11
12
+ -- | Constructs a `JsonCodec` for a `Record` from a name and a record of codecs.
13
+ -- | The name is used in the error message produced when decoding fails.
14
+ -- |
15
+ -- | ```purescript
16
+ -- | type Person = { name ∷ String, age ∷ Int }
17
+ -- |
18
+ -- | personCodec ∷ CA.JsonCodec Person
19
+ -- | personCodec = CAR.object "Person" { name: CA.string, age: CA.int }
20
+ -- | ```
21
+ object
22
+ ∷ ∀ ri ro rl
23
+ . RL.RowToList ri rl
24
+ ⇒ RowListCodec rl ri ro
25
+ ⇒ String
26
+ → Record ri
27
+ → CA.JsonCodec (Record ro )
28
+ object name rec = CA .object name (record rec)
29
+
30
+ -- | Constructs a `JPropCodec` for a `Record` from a record of codecs. Commonly
31
+ -- | the `object` function in this module will be the preferred choice, as that
32
+ -- | produces a `JsonCodec` instead.
33
+ record
34
+ ∷ ∀ ri ro rl
35
+ . RL.RowToList ri rl
36
+ ⇒ RowListCodec rl ri ro
37
+ ⇒ Record ri
38
+ → CA.JPropCodec (Record ro )
39
+ record = rowListCodec (RLProxy ∷ RLProxy rl )
40
+
41
+ -- | The class used to enable the building of `Record` codecs by providing a
42
+ -- | record of codecs.
16
43
class RowListCodec (rl ∷ RL.RowList ) (ri ∷ # Type ) (ro ∷ # Type ) | rl → ri ro where
17
44
rowListCodec ∷ RLProxy rl → Record ri → CA.JPropCodec (Record ro )
18
45
@@ -34,22 +61,3 @@ instance rowListCodecCons ∷
34
61
35
62
tail ∷ CA.JPropCodec (Record ro' )
36
63
tail = rowListCodec (RLProxy ∷ RLProxy rs ) ((unsafeCoerce ∷ Record ri → Record ri' ) codecs)
37
-
38
- -- | Constructs a record codec from a record of codecs.
39
- -- |
40
- -- | ```purescript
41
- -- | type Person = { name ∷ String, age ∷ Int }
42
- -- |
43
- -- | personCodec ∷ CA.JsonCodec Person
44
- -- | personCodec = CA.object "Person" (record { name: CA.string, age: CA.int })
45
- -- |
46
- -- | decode personCodec "{ name: \"Carl\", age:\"25\" }" == Right { name: "Carl", age: 25 }
47
- -- | ```
48
-
49
- record
50
- ∷ ∀ ri ro rl
51
- . RL.RowToList ri rl
52
- ⇒ RowListCodec rl ri ro
53
- ⇒ Record ri
54
- → CA.JPropCodec (Record ro )
55
- record = rowListCodec (RLProxy ∷ RLProxy rl )
0 commit comments