Skip to content

Commit 376076a

Browse files
committed
Add object for D.C.A.Record
1 parent 889b91e commit 376076a

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/Data/Codec/Argonaut/Record.purs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
module Data.Codec.Argonaut.Record
2-
( class RowListCodec
3-
, rowListCodec
4-
, record
5-
) where
1+
module Data.Codec.Argonaut.Record where
62

73
import Data.Codec.Argonaut as CA
84
import Data.Symbol (class IsSymbol, SProxy(..))
@@ -13,6 +9,37 @@ import Type.Data.RowList (RLProxy(..))
139
import Type.Equality as TE
1410
import Unsafe.Coerce (unsafeCoerce)
1511

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.
1643
class RowListCodec (rlRL.RowList) (ri ∷ # Type) (ro ∷ # Type) | rl ri ro where
1744
rowListCodec RLProxy rl Record ri CA.JPropCodec (Record ro)
1845

@@ -34,22 +61,3 @@ instance rowListCodecCons ∷
3461

3562
tail CA.JPropCodec (Record ro')
3663
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

Comments
 (0)