1
- module Data.Codec.Argonaut.Record where
1
+ module Data.Codec.Argonaut.Record
2
+ ( OptionalWith
3
+ , class RowListCodec
4
+ , object
5
+ , optional
6
+ , optionalWith
7
+ , record
8
+ , rowListCodec
9
+ )
10
+ where
2
11
3
12
import Data.Codec.Argonaut as CA
13
+ import Data.Function (identity )
4
14
import Data.Maybe (Maybe )
5
15
import Data.Symbol (class IsSymbol )
6
16
import Prim.Row as R
@@ -39,30 +49,27 @@ record
39
49
→ CA.JPropCodec (Record ro )
40
50
record = rowListCodec (Proxy ∷ Proxy rl )
41
51
52
+
53
+ newtype OptionalWith a b = OptionalWith
54
+ { normalize ∷ Maybe a → b
55
+ , denormalize ∷ b → Maybe a
56
+ , codec ∷ CA.JsonCodec a
57
+ }
58
+
42
59
-- | Used to wrap codec values provided in `record` to indicate the field is optional.
43
60
-- |
44
61
-- | This will only decode the property as `Nothing` if the field does not exist
45
62
-- | in the object - having a values such as `null` assigned will need handling
46
63
-- | separately.
47
64
-- |
48
65
-- | The property will be omitted when encoding and the value is `Nothing`.
49
- newtype Optional a = Optional (CA.JsonCodec a )
66
+ optional ∷ ∀ a . CA.JsonCodec a → OptionalWith a (Maybe a )
67
+ optional = optionalWith identity identity
50
68
51
- -- | Like `Optional`, but allows you to provide a function to transform the
69
+ -- | Like `Optional`, but more general. It allows you to provide a function to transform the
52
70
-- | `Maybe a` value into a different type `b`. This is useful when you want to
53
71
-- | provide a default value or perform some other transformation when the
54
72
-- | property is not present in the JSON object.
55
- newtype OptionalWith a b = OptionalWith
56
- { normalize ∷ Maybe a → b
57
- , denormalize ∷ b → Maybe a
58
- , codec ∷ CA.JsonCodec a
59
- }
60
-
61
- -- | A lowercase alias for `Optional`, provided for stylistic reasons only.
62
- optional ∷ ∀ a . CA.JsonCodec a → Optional a
63
- optional = Optional
64
-
65
- -- | A lowercase alias for `OptionalWith`, provided for stylistic reasons only.
66
73
optionalWith ∷ ∀ a b . (Maybe a → b ) → (b → Maybe a ) → CA.JsonCodec a → OptionalWith a b
67
74
optionalWith normalize denormalize codec = OptionalWith { normalize, denormalize, codec }
68
75
@@ -74,23 +81,7 @@ class RowListCodec (rl ∷ RL.RowList Type) (ri ∷ Row Type) (ro ∷ Row Type)
74
81
instance rowListCodecNil ∷ RowListCodec RL.Nil () () where
75
82
rowListCodec _ _ = CA .record
76
83
77
- instance rowListCodecConsOptional ∷
78
- ( RowListCodec rs ri' ro'
79
- , R.Cons sym (Optional a ) ri' ri
80
- , R.Cons sym (Maybe a ) ro' ro
81
- , IsSymbol sym
82
- ) ⇒
83
- RowListCodec (RL.Cons sym (Optional a ) rs ) ri ro where
84
- rowListCodec _ codecs =
85
- CA .recordPropOptional (Proxy ∷ Proxy sym ) codec tail
86
- where
87
- codec ∷ CA.JsonCodec a
88
- codec = coerce (Rec .get (Proxy ∷ Proxy sym ) codecs ∷ Optional a )
89
-
90
- tail ∷ CA.JPropCodec (Record ro' )
91
- tail = rowListCodec (Proxy ∷ Proxy rs ) ((unsafeCoerce ∷ Record ri → Record ri' ) codecs)
92
-
93
- else instance rowListCodecConsOptionalWith ∷
84
+ instance rowListCodecConsOptionalWith ∷
94
85
( RowListCodec rs ri' ro'
95
86
, R.Cons sym (OptionalWith a b ) ri' ri
96
87
, R.Cons sym b ro' ro
@@ -100,7 +91,7 @@ else instance rowListCodecConsOptionalWith ∷
100
91
) ⇒
101
92
RowListCodec (RL.Cons sym (OptionalWith a b ) rs ) ri ro where
102
93
rowListCodec _ codecs =
103
- CA .recordPropOptionalWith (Proxy ∷ Proxy sym ) ret.normalize ret.denormalize ret.codec tail
94
+ CA .recordPropOptionalWith ret.normalize ret.denormalize (Proxy ∷ Proxy sym ) ret.codec tail
104
95
105
96
where
106
97
ret ∷ { normalize ∷ Maybe a → b , denormalize ∷ b → Maybe a , codec ∷ CA.JsonCodec a }
0 commit comments