Skip to content

Commit ebb69dd

Browse files
Sync with latest formatting
2 parents 179409c + 2c32bb4 commit ebb69dd

File tree

18 files changed

+168
-128
lines changed

18 files changed

+168
-128
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
- uses: actions/setup-node@v1
1616
with:
17-
node-version: "12"
17+
node-version: "16"
1818

1919
- name: Install dependencies
2020
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/.*
22
!/.gitignore
33
!/.github
4+
!/.tidyrc.json
45
/bower_components/
56
/node_modules/
67
/output/

.tidyrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"importSort": "ide",
3+
"importWrap": "source",
4+
"indent": 2,
5+
"operatorsFile": null,
6+
"ribbon": 1,
7+
"typeArrowPlacement": "first",
8+
"unicode": "always",
9+
"width": null
10+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"pulp": "^16.0.0",
1010
"purescript": "^0.15.0",
1111
"purescript-psa": "^0.8.2",
12+
"purs-tidy": "^0.9.2",
1213
"rimraf": "^3.0.0"
1314
}
1415
}

src/Data/Codec/Argonaut.purs

Lines changed: 69 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ module Data.Codec.Argonaut
3131
import Prelude
3232

3333
import Control.Monad.Reader (ReaderT(..), runReaderT)
34-
import Control.Monad.Writer (Writer, writer, mapWriter)
34+
import Control.Monad.Writer (Writer, mapWriter, writer)
3535
import Data.Argonaut.Core as J
3636
import Data.Array as A
3737
import Data.Bifunctor as BF
3838
import Data.Codec (BasicCodec, Codec, GCodec(..), basicCodec, bihoistGCodec, decode, encode)
39-
import Data.Codec (decode, encode, (~), (<~<), (>~>)) as Exports
39+
import Data.Codec (decode, encode, (<~<), (>~>), (~)) as Exports
4040
import Data.Either (Either(..), note)
4141
import Data.Generic.Rep (class Generic)
4242
import Data.Int as I
4343
import Data.List ((:))
4444
import Data.List as L
45-
import Data.Maybe (Maybe(..), maybe, fromJust)
45+
import Data.Maybe (Maybe(..), fromJust, maybe)
4646
import Data.Profunctor.Star (Star(..))
4747
import Data.String as S
4848
import Data.String.CodeUnits as SCU
@@ -73,25 +73,25 @@ derive instance genericJsonDecodeError ∷ Generic JsonDecodeError _
7373

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

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

9696
-- | The "identity codec" for `Json` values.
9797
json JsonCodec J.Json
@@ -163,7 +163,8 @@ type JIndexedCodec a =
163163
(Either JsonDecodeError)
164164
(Array J.Json)
165165
(L.List J.Json)
166-
a a
166+
a
167+
a
167168

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

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

@@ -265,29 +268,33 @@ recordProp
265268
recordProp p codecA codecR =
266269
let key = reflectSymbol p in GCodec (dec' key) (enc' key)
267270
where
268-
dec' String ReaderT (FO.Object J.Json) (Either JsonDecodeError) (Record r')
269-
dec' key = ReaderT \obj → do
270-
r ← decode codecR obj
271-
a ← BF.lmap (AtKey key) case FO.lookup key obj of
272-
Just val → decode codecA val
273-
NothingLeft MissingValue
274-
pure $ unsafeSet key a r
275-
enc' String Star (Writer (L.List (Tuple String J.Json))) (Record r') (Record r')
276-
enc' key = Star \val →
277-
writer $ Tuple val
278-
$ Tuple key (encode codecA (unsafeGet key val))
279-
: encode codecR (unsafeForget val)
280-
unsafeForget Record r' Record r
281-
unsafeForget = unsafeCoerce
282-
unsafeSet String a Record r Record r'
283-
unsafeSet key a = unsafeCoerce <<< FO.insert key a <<< unsafeCoerce
284-
unsafeGet String Record r' a
285-
unsafeGet s = unsafePartial fromJust <<< FO.lookup s <<< unsafeCoerce
286-
271+
dec' String ReaderT (FO.Object J.Json) (Either JsonDecodeError) (Record r')
272+
dec' key = ReaderT \obj → do
273+
r ← decode codecR obj
274+
a ← BF.lmap (AtKey key) case FO.lookup key obj of
275+
Just val → decode codecA val
276+
NothingLeft MissingValue
277+
pure $ unsafeSet key a r
278+
279+
enc' String Star (Writer (L.List (Tuple String J.Json))) (Record r') (Record r')
280+
enc' key = Star \val →
281+
writer $ Tuple val
282+
$ Tuple key (encode codecA (unsafeGet key val))
283+
: encode codecR (unsafeForget val)
284+
285+
unsafeForget Record r' Record r
286+
unsafeForget = unsafeCoerce
287+
288+
unsafeSet String a Record r Record r'
289+
unsafeSet key a = unsafeCoerce <<< FO.insert key a <<< unsafeCoerce
290+
291+
unsafeGet String Record r' a
292+
unsafeGet s = unsafePartial fromJust <<< FO.lookup s <<< unsafeCoerce
293+
287294
-- | Used with `record` to define an optional field.
288295
-- |
289296
-- | This will only decode the property as `Nothing` if the field does not exist
290-
-- | in the object - having a values such as `null` assigned will need handling
297+
-- | in the object - having a values such as `null` assigned will need handling
291298
-- | separately.
292299
-- |
293300
-- | The property will be omitted when encoding and the value is `Nothing`.
@@ -302,29 +309,33 @@ recordPropOptional
302309
recordPropOptional p codecA codecR =
303310
let key = reflectSymbol p in GCodec (dec' key) (enc' key)
304311
where
305-
dec' String ReaderT (FO.Object J.Json) (Either JsonDecodeError) (Record r')
306-
dec' key = ReaderT \obj → do
307-
r ← decode codecR obj
308-
a ← BF.lmap (AtKey key) case FO.lookup key obj of
309-
Just val → Just <$> decode codecA val
310-
_ → Right Nothing
311-
pure $ unsafeSet key a r
312-
enc' String Star (Writer (L.List (Tuple String J.Json))) (Record r') (Record r')
313-
enc' key = Star \val → do
314-
let w = encode codecR (unsafeForget val)
315-
writer $ Tuple val case unsafeGet key val of
316-
Just a → Tuple key (encode codecA a) : w
317-
Nothing → w
318-
unsafeForget Record r' Record r
319-
unsafeForget = unsafeCoerce
320-
unsafeSet String Maybe a Record r Record r'
321-
unsafeSet key a = unsafeCoerce <<< FO.insert key a <<< unsafeCoerce
322-
unsafeGet String Record r' Maybe a
323-
unsafeGet s = unsafePartial fromJust <<< FO.lookup s <<< unsafeCoerce
312+
dec' String ReaderT (FO.Object J.Json) (Either JsonDecodeError) (Record r')
313+
dec' key = ReaderT \obj → do
314+
r ← decode codecR obj
315+
a ← BF.lmap (AtKey key) case FO.lookup key obj of
316+
Just val → Just <$> decode codecA val
317+
_ → Right Nothing
318+
pure $ unsafeSet key a r
319+
320+
enc' String Star (Writer (L.List (Tuple String J.Json))) (Record r') (Record r')
321+
enc' key = Star \val → do
322+
let w = encode codecR (unsafeForget val)
323+
writer $ Tuple val case unsafeGet key val of
324+
Just a → Tuple key (encode codecA a) : w
325+
Nothing → w
326+
327+
unsafeForget Record r' Record r
328+
unsafeForget = unsafeCoerce
329+
330+
unsafeSet String Maybe a Record r Record r'
331+
unsafeSet key a = unsafeCoerce <<< FO.insert key a <<< unsafeCoerce
332+
333+
unsafeGet String Record r' Maybe a
334+
unsafeGet s = unsafePartial fromJust <<< FO.lookup s <<< unsafeCoerce
324335

325336
jsonPrimCodec
326337
a
327-
. String
338+
. String
328339
(J.Json Maybe a)
329340
(a J.Json)
330341
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ 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
48+
Left (CA.Named name (CA.UnexpectedValue j))
49+
else
50+
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

0 commit comments

Comments
 (0)