Skip to content

Commit df8934e

Browse files
Format source
1 parent 76d542b commit df8934e

File tree

8 files changed

+84
-68
lines changed

8 files changed

+84
-68
lines changed

src/Data/Codec/Argonaut.purs

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ module Data.Codec.Argonaut
3030
import Prelude
3131

3232
import Control.Monad.Reader (ReaderT(..), runReaderT)
33-
import Control.Monad.Writer (Writer, writer, mapWriter)
33+
import Control.Monad.Writer (Writer, mapWriter, writer)
3434
import Data.Argonaut.Core as J
3535
import Data.Array as A
3636
import Data.Bifunctor as BF
3737
import Data.Codec (BasicCodec, Codec, GCodec(..), basicCodec, bihoistGCodec, decode, encode)
38-
import Data.Codec (decode, encode, (~), (<~<), (>~>)) as Exports
38+
import Data.Codec (decode, encode, (<~<), (>~>), (~)) as Exports
3939
import Data.Either (Either(..), note)
4040
import Data.Generic.Rep (class Generic)
4141
import Data.Int as I
4242
import Data.List ((:))
4343
import Data.List as L
44-
import Data.Maybe (Maybe(..), maybe, fromJust)
44+
import Data.Maybe (Maybe(..), fromJust, maybe)
4545
import Data.Profunctor.Star (Star(..))
4646
import Data.String as S
4747
import Data.String.CodeUnits as SCU
@@ -72,25 +72,25 @@ derive instance genericJsonDecodeError ∷ Generic JsonDecodeError _
7272

7373
instance showJsonDecodeErrorShow JsonDecodeError where
7474
show = case _ of
75-
TypeMismatch s -> "(TypeMismatch " <> show s <> ")"
76-
UnexpectedValue j -> "(UnexpectedValue " <> J.stringify j <> ")"
77-
AtIndex i e -> "(AtIndex " <> show i <> " " <> show e <> ")"
78-
AtKey k e -> "(AtKey " <> show k <> " " <> show e <> ")"
79-
Named s e -> "(Named " <> show s <> " " <> show e <> ")"
80-
MissingValue -> "MissingValue"
75+
TypeMismatch s "(TypeMismatch " <> show s <> ")"
76+
UnexpectedValue j "(UnexpectedValue " <> J.stringify j <> ")"
77+
AtIndex i e "(AtIndex " <> show i <> " " <> show e <> ")"
78+
AtKey k e "(AtKey " <> show k <> " " <> show e <> ")"
79+
Named s e "(Named " <> show s <> " " <> show e <> ")"
80+
MissingValue "MissingValue"
8181

8282
-- | Prints a `JsonDecodeError` as a somewhat readable error message.
8383
printJsonDecodeError JsonDecodeError String
8484
printJsonDecodeError err =
8585
"An error occurred while decoding a JSON value:\n" <> go err
8686
where
87-
go = case _ of
88-
TypeMismatch ty → " Expected value of type '" <> ty <> "'."
89-
UnexpectedValue val → " Unexpected value " <> J.stringify val <> "."
90-
AtIndex ix inner → " At array index " <> show ix <> ":\n" <> go inner
91-
AtKey key inner → " At object key " <> key <> ":\n" <> go inner
92-
Named name inner → " Under '" <> name <> "':\n" <> go inner
93-
MissingValue" No value was found."
87+
go = case _ of
88+
TypeMismatch ty → " Expected value of type '" <> ty <> "'."
89+
UnexpectedValue val → " Unexpected value " <> J.stringify val <> "."
90+
AtIndex ix inner → " At array index " <> show ix <> ":\n" <> go inner
91+
AtKey key inner → " At object key " <> key <> ":\n" <> go inner
92+
Named name inner → " Under '" <> name <> "':\n" <> go inner
93+
MissingValue" No value was found."
9494

9595
-- | The "identity codec" for `Json` values.
9696
json JsonCodec J.Json
@@ -162,7 +162,8 @@ type JIndexedCodec a =
162162
(Either JsonDecodeError)
163163
(Array J.Json)
164164
(L.List J.Json)
165-
a a
165+
a
166+
a
166167

167168
-- | A codec for types that are encoded as an array with a specific layout.
168169
-- |
@@ -203,7 +204,8 @@ type JPropCodec a =
203204
(Either JsonDecodeError)
204205
(FO.Object J.Json)
205206
(L.List (Tuple String J.Json))
206-
a a
207+
a
208+
a
207209

208210
-- | A codec for objects that are encoded with specific properties.
209211
-- |
@@ -224,6 +226,7 @@ prop key codec = GCodec dec enc
224226
BF.lmap (AtKey key) case FO.lookup key obj of
225227
Just val → decode codec val
226228
NothingLeft MissingValue
229+
227230
enc Star (Writer (L.List (Tuple String J.Json))) a a
228231
enc = Star \val → writer $ Tuple val (pure (Tuple key (encode codec val)))
229232

@@ -264,28 +267,32 @@ recordProp
264267
recordProp p codecA codecR =
265268
let key = reflectSymbol p in GCodec (dec' key) (enc' key)
266269
where
267-
dec' String ReaderT (FO.Object J.Json) (Either JsonDecodeError) (Record r')
268-
dec' key = ReaderT \obj → do
269-
r ← decode codecR obj
270-
a ← BF.lmap (AtKey key) case FO.lookup key obj of
271-
Just val → decode codecA val
272-
NothingLeft MissingValue
273-
pure $ unsafeSet key a r
274-
enc' String Star (Writer (L.List (Tuple String J.Json))) (Record r') (Record r')
275-
enc' key = Star \val →
276-
writer $ Tuple val
277-
$ Tuple key (encode codecA (unsafeGet key val))
278-
: encode codecR (unsafeForget val)
279-
unsafeForget Record r' Record r
280-
unsafeForget = unsafeCoerce
281-
unsafeSet String a Record r Record r'
282-
unsafeSet key a = unsafeCoerce <<< FO.insert key a <<< unsafeCoerce
283-
unsafeGet String Record r' a
284-
unsafeGet s = unsafePartial fromJust <<< FO.lookup s <<< unsafeCoerce
270+
dec' String ReaderT (FO.Object J.Json) (Either JsonDecodeError) (Record r')
271+
dec' key = ReaderT \obj → do
272+
r ← decode codecR obj
273+
a ← BF.lmap (AtKey key) case FO.lookup key obj of
274+
Just val → decode codecA val
275+
NothingLeft MissingValue
276+
pure $ unsafeSet key a r
277+
278+
enc' String Star (Writer (L.List (Tuple String J.Json))) (Record r') (Record r')
279+
enc' key = Star \val →
280+
writer $ Tuple val
281+
$ Tuple key (encode codecA (unsafeGet key val))
282+
: encode codecR (unsafeForget val)
283+
284+
unsafeForget Record r' Record r
285+
unsafeForget = unsafeCoerce
286+
287+
unsafeSet String a Record r Record r'
288+
unsafeSet key a = unsafeCoerce <<< FO.insert key a <<< unsafeCoerce
289+
290+
unsafeGet String Record r' a
291+
unsafeGet s = unsafePartial fromJust <<< FO.lookup s <<< unsafeCoerce
285292

286293
jsonPrimCodec
287294
a
288-
. String
295+
. String
289296
(J.Json Maybe a)
290297
(a J.Json)
291298
JsonCodec a

src/Data/Codec/Argonaut/Common.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import Data.List as L
1414
import Data.Map as M
1515
import Data.Maybe (Maybe(..))
1616
import Data.Profunctor (dimap)
17-
import Foreign.Object as FO
1817
import Data.Tuple (Tuple(..), fst, snd)
18+
import Foreign.Object as FO
1919

2020
-- | A codec for `Maybe` values.
2121
-- |

src/Data/Codec/Argonaut/Compat.purs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import Data.Codec.Argonaut.Common (either, list, map, tuple) as Common
1515
import Data.Either (Either)
1616
import Data.Functor as F
1717
import Data.Maybe (Maybe(..))
18-
import Foreign.Object as FO
1918
import Data.Traversable (traverse)
2019
import Data.Tuple (Tuple(..))
20+
import Foreign.Object as FO
2121

2222
-- | A codec for `Maybe` values.
2323
-- |
@@ -32,6 +32,7 @@ maybe codec = basicCodec dec enc
3232
dec j
3333
| J.isNull j = pure Nothing
3434
| otherwise = BF.bimap (Named "Maybe") Just ((decode codec j))
35+
3536
enc Maybe a J.Json
3637
enc = case _ of
3738
NothingJ.jsonNull
@@ -53,6 +54,7 @@ foreignObject codec =
5354
where
5455
fromArray v. Array (Tuple String v) FO.Object v
5556
fromArray = FO.fromFoldable
57+
5658
decodeItem Tuple String J.Json Either JsonDecodeError (Tuple String a)
5759
decodeItem (Tuple key value) =
5860
BF.bimap (AtKey key) (Tuple key) (decode codec value)

src/Data/Codec/Argonaut/Generic.purs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ instance nullarySumCodecSum ∷ (NullarySumCodec a, NullarySumCodec b) ⇒ Nulla
3636
nullarySumEncode = case _ of
3737
Inl a → nullarySumEncode a
3838
Inr b → nullarySumEncode b
39-
nullarySumDecode name j
40-
= Inl <$> nullarySumDecode name j
39+
nullarySumDecode name j = Inl <$> nullarySumDecode name j
4140
<|> Inr <$> nullarySumDecode name j
4241

4342
instance nullarySumCodecCtorIsSymbol name NullarySumCodec (Constructor name NoArguments) where
4443
nullarySumEncode _ =
4544
J.fromString $ reflectSymbol (Proxy Proxy name)
4645
nullarySumDecode name j = do
4746
tag ← note (CA.Named name (CA.TypeMismatch "String")) (J.toString j)
48-
if tag /= reflectSymbol (Proxy Proxy name)
49-
then Left (CA.Named name (CA.UnexpectedValue j))
50-
else Right (Constructor NoArguments)
47+
if tag /= reflectSymbol (Proxy Proxy name) then Left (CA.Named name (CA.UnexpectedValue j))
48+
else Right (Constructor NoArguments)

src/Data/Codec/Argonaut/Migration.purs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ import Prelude
5959
import Data.Argonaut.Core as J
6060
import Data.Codec (basicCodec)
6161
import Data.Codec.Argonaut (JsonCodec)
62-
import Data.Maybe (Maybe(..), maybe, fromMaybe)
62+
import Data.Maybe (Maybe(..), fromMaybe, maybe)
63+
import Data.Tuple (Tuple(..), uncurry)
6364
import Foreign.Object as FO
6465
import Foreign.Object.ST as FOST
65-
import Data.Tuple (Tuple(..), uncurry)
6666

6767
-- | When dealing with a JSON object that may be missing a field, this codec
6868
-- | can be used to alter the JSON before parsing to ensure a default value is
@@ -89,6 +89,7 @@ renameField oldName newName = basicCodec (pure <<< dec) identity
8989
where
9090
dec J.Json J.Json
9191
dec j = J.caseJsonObject j (J.fromObject <<< rename) j
92+
9293
rename FO.Object J.Json FO.Object J.Json
9394
rename obj = maybe obj (uncurry (FO.insert newName)) (FO.pop oldName obj)
9495

@@ -117,6 +118,7 @@ nestForTagged = basicCodec (pure <<< dec) identity
117118
where
118119
dec J.Json J.Json
119120
dec j = J.caseJsonObject j (J.fromObject <<< rewrite) j
121+
120122
rewrite FO.Object J.Json FO.Object J.Json
121123
rewrite obj =
122124
case FO.pop "tag" obj of
@@ -127,6 +129,7 @@ nestForTagged = basicCodec (pure <<< dec) identity
127129
result ← FOST.new
128130
_ ← FOST.poke "tag" tagValue result
129131
FOST.poke "value" (mkValue obj') result
132+
130133
mkValue FO.Object J.Json J.Json
131134
mkValue obj = case FO.pop "value" obj of
132135
Just (Tuple valueValue obj') | FO.isEmpty obj' → valueValue
@@ -137,5 +140,6 @@ alterField field f = basicCodec (pure <<< dec) identity
137140
where
138141
dec J.Json J.Json
139142
dec j = J.caseJsonObject j (J.fromObject <<< setDefault) j
143+
140144
setDefault FO.Object J.Json FO.Object J.Json
141145
setDefault = FO.alter f field

src/Data/Codec/Argonaut/Record.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ record = rowListCodec (Proxy ∷ Proxy rl)
4141
-- | The class used to enable the building of `Record` codecs by providing a
4242
-- | record of codecs.
4343
class RowListCodec (rlRL.RowList Type) (riRow Type) (roRow Type) | rl ri ro where
44-
rowListCodec forall proxy. proxy rl Record ri CA.JPropCodec (Record ro)
44+
rowListCodec proxy. proxy rl Record ri CA.JPropCodec (Record ro)
4545

4646
instance rowListCodecNilRowListCodec RL.Nil () () where
4747
rowListCodec _ _ = CA.record
@@ -52,7 +52,8 @@ instance rowListCodecCons ∷
5252
, R.Cons sym a ro' ro
5353
, IsSymbol sym
5454
, TE.TypeEquals co (CA.JsonCodec a)
55-
) RowListCodec (RL.Cons sym co rs) ri ro where
55+
)
56+
RowListCodec (RL.Cons sym co rs) ri ro where
5657
rowListCodec _ codecs =
5758
CA.recordProp (Proxy Proxy sym) codec tail
5859
where

src/Data/Codec/Argonaut/Sum.purs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import Data.Codec.Argonaut (JsonCodec, JsonDecodeError(..), jobject, json, prop,
1414
import Data.Either (Either(..))
1515
import Data.Maybe (Maybe(..), maybe)
1616
import Data.Profunctor.Star (Star(..))
17+
import Data.Tuple (Tuple(..))
1718
import Foreign.Object as FO
1819
import Foreign.Object.ST as FOST
19-
import Data.Tuple (Tuple(..))
2020

2121
-- | A helper for defining JSON codecs for "enum" sum types, where every
2222
-- | constructor is nullary, and the type will be encoded as a string.
@@ -33,6 +33,7 @@ enumSum printTag parseTag = GCodec dec enc
3333
case parseTag value of
3434
Just a → Right a
3535
NothingLeft (UnexpectedValue j)
36+
3637
enc Star (Writer J.Json) a a
3738
enc = Star \a → writer $ Tuple a (encode string (printTag a))
3839

@@ -70,11 +71,12 @@ taggedSum name printTag parseTag f g = GCodec decodeCase encodeCase
7071
Right decoder → do
7172
value ← decode (prop "value" json) obj
7273
lmap (AtKey "value") (decoder value)
74+
7375
encodeCase Star (Writer J.Json) a a
7476
encodeCase = Star case _ of
7577
a | Tuple tag value ← g a →
7678
writer $ Tuple a $ encode jobject $
7779
FO.runST do
7880
obj ← FOST.new
7981
_ ← FOST.poke "tag" (encode string (printTag tag)) obj
80-
maybe (pure obj) (\v -> FOST.poke "value" v obj) value
82+
maybe (pure obj) (\v FOST.poke "value" v obj) value

src/Data/Codec/Argonaut/Variant.purs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import Prim.Row as R
1919
import Prim.RowList as RL
2020
import Record as Rec
2121
import Type.Equality as TE
22-
import Unsafe.Coerce (unsafeCoerce)
2322
import Type.Proxy (Proxy(..))
23+
import Unsafe.Coerce (unsafeCoerce)
2424

2525
-- | Builds a codec for a variant from a record, similar to the way
2626
-- | `Variant.match` works to pattern match on a variant.
@@ -103,33 +103,34 @@ variantCase proxy eacodec (GCodec dec enc) = GCodec dec' enc'
103103
dec' = ReaderT \j → do
104104
obj ← decode jobject j
105105
tag ← decode (prop "tag" string) obj
106-
if tag == reflectSymbol proxy
107-
then case eacodec of
108-
Left a → pure (inj proxy a)
109-
Right codec → do
110-
value ← decode (prop "value" json) obj
111-
inj proxy <$> decode codec value
112-
else coerceR <$> runReaderT dec j
106+
if tag == reflectSymbol proxy then case eacodec of
107+
Left a → pure (inj proxy a)
108+
Right codec → do
109+
value ← decode (prop "value" json) obj
110+
inj proxy <$> decode codec value
111+
else coerceR <$> runReaderT dec j
113112

114113
enc' Star (Writer J.Json) (Variant r') (Variant r')
115114
enc' = Star \v →
116115
on proxy
117-
(\v' → writer $ Tuple v $ encode jobject $
118-
FO.runST do
119-
obj ← FOST.new
120-
_ ← FOST.poke "tag" (encode string (reflectSymbol proxy)) obj
121-
case eacodec of
122-
Left _ → pure obj
123-
Right codec → FOST.poke "value" (encode codec v') obj)
124-
(\v' → un Star enc v' $> v) v
116+
( \v' → writer $ Tuple v $ encode jobject $
117+
FO.runST do
118+
obj ← FOST.new
119+
_ ← FOST.poke "tag" (encode string (reflectSymbol proxy)) obj
120+
case eacodec of
121+
Left _ → pure obj
122+
Right codec → FOST.poke "value" (encode codec v') obj
123+
)
124+
(\v' → un Star enc v' $> v)
125+
v
125126

126127
coerceR Variant r Variant r'
127128
coerceR = unsafeCoerce
128129

129130
-- | The class used to enable the building of `Variant` codecs from a record of
130131
-- | codecs.
131132
class VariantCodec (rlRL.RowList Type) (riRow Type) (roRow Type) | rl ri ro where
132-
variantCodec forall proxy. proxy rl Record ri JsonCodec (Variant ro)
133+
variantCodec proxy. proxy rl Record ri JsonCodec (Variant ro)
133134

134135
instance variantCodecNilVariantCodec RL.Nil () () where
135136
variantCodec _ _ = variant
@@ -140,7 +141,8 @@ instance variantCodecCons ∷
140141
, R.Cons sym a ro' ro
141142
, IsSymbol sym
142143
, TE.TypeEquals co (Either a (JsonCodec a))
143-
) VariantCodec (RL.Cons sym co rs) ri ro where
144+
)
145+
VariantCodec (RL.Cons sym co rs) ri ro where
144146
variantCodec _ codecs =
145147
variantCase (Proxy Proxy sym) codec tail
146148
where

0 commit comments

Comments
 (0)