Skip to content

Commit 2c32bb4

Browse files
authored
Merge pull request #50 from thomashoneyman/trh/format
Add purs-tidy
2 parents 3297cce + d36852b commit 2c32bb4

File tree

18 files changed

+132
-95
lines changed

18 files changed

+132
-95
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: 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: 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)