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
211
312import Data.Codec.Argonaut as CA
13+ import Data.Function (identity )
414import Data.Maybe (Maybe )
515import Data.Symbol (class IsSymbol )
616import Prim.Row as R
@@ -39,30 +49,27 @@ record
3949 → CA.JPropCodec (Record ro )
4050record = rowListCodec (Proxy ∷ Proxy rl )
4151
52+
53+ newtype OptionalWith a b = OptionalWith
54+ { normalize ∷ Maybe a → b
55+ , denormalize ∷ b → Maybe a
56+ , codec ∷ CA.JsonCodec a
57+ }
58+
4259-- | Used to wrap codec values provided in `record` to indicate the field is optional.
4360-- |
4461-- | This will only decode the property as `Nothing` if the field does not exist
4562-- | in the object - having a values such as `null` assigned will need handling
4663-- | separately.
4764-- |
4865-- | 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
5068
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
5270-- | `Maybe a` value into a different type `b`. This is useful when you want to
5371-- | provide a default value or perform some other transformation when the
5472-- | 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.
6673optionalWith ∷ ∀ a b . (Maybe a → b ) → (b → Maybe a ) → CA.JsonCodec a → OptionalWith a b
6774optionalWith normalize denormalize codec = OptionalWith { normalize, denormalize, codec }
6875
@@ -74,23 +81,7 @@ class RowListCodec (rl ∷ RL.RowList Type) (ri ∷ Row Type) (ro ∷ Row Type)
7481instance rowListCodecNil ∷ RowListCodec RL.Nil () () where
7582 rowListCodec _ _ = CA .record
7683
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 ∷
9485 ( RowListCodec rs ri' ro'
9586 , R.Cons sym (OptionalWith a b ) ri' ri
9687 , R.Cons sym b ro' ro
@@ -100,7 +91,7 @@ else instance rowListCodecConsOptionalWith ∷
10091 ) ⇒
10192 RowListCodec (RL.Cons sym (OptionalWith a b ) rs ) ri ro where
10293 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
10495
10596 where
10697 ret ∷ { normalize ∷ Maybe a → b , denormalize ∷ b → Maybe a , codec ∷ CA.JsonCodec a }
0 commit comments