Skip to content

Commit 3ae9622

Browse files
Introduce purs-tidy formatter (#38)
* Add purs-tidy formatter * Run purs-tidy
1 parent 0282eb2 commit 3ae9622

File tree

8 files changed

+143
-106
lines changed

8 files changed

+143
-106
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515

1616
- name: Set up a PureScript toolchain
1717
uses: purescript-contrib/setup-purescript@main
18+
with:
19+
purs-tidy: "latest"
1820

1921
- name: Cache PureScript dependencies
2022
uses: actions/cache@v2
@@ -32,3 +34,6 @@ jobs:
3234

3335
- name: Run tests
3436
run: spago test --no-install
37+
38+
- name: Check formatting
39+
run: purs-tidy check src test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
!.gitignore
33
!.github
44
!.editorconfig
5+
!.tidyrc.json
56

67
output
78
generated-docs

.tidyrc.json

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

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ New features:
1111
Bugfixes:
1212

1313
Other improvements:
14+
- Added `purs-tidy` formatter (#38 by @thomashoneyman)
1415

1516
## [v7.0.1](https://github.com/purescript-contrib/purescript-argonaut-generic/releases/tag/v7.0.1) - 2021-05-06
1617

src/Data/Argonaut/Decode/Generic.purs

Lines changed: 78 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
module Data.Argonaut.Decode.Generic (
2-
class DecodeRep,
3-
class DecodeRepArgs,
4-
class DecodeLiteral,
5-
decodeRep,
6-
decodeRepWith,
7-
decodeRepArgs,
8-
genericDecodeJson,
9-
genericDecodeJsonWith,
10-
decodeLiteralSum,
11-
decodeLiteralSumWithTransform,
12-
decodeLiteral
13-
) where
1+
module Data.Argonaut.Decode.Generic
2+
( class DecodeRep
3+
, class DecodeRepArgs
4+
, class DecodeLiteral
5+
, decodeRep
6+
, decodeRepWith
7+
, decodeRepArgs
8+
, genericDecodeJson
9+
, genericDecodeJsonWith
10+
, decodeLiteralSum
11+
, decodeLiteralSumWithTransform
12+
, decodeLiteral
13+
) where
1414

1515
import Prelude
1616

@@ -40,90 +40,94 @@ instance decodeRepNoConstructors :: DecodeRep Rep.NoConstructors where
4040
instance decodeRepSum :: (DecodeRep a, DecodeRep b) => DecodeRep (Rep.Sum a b) where
4141
decodeRepWith e j = Rep.Inl <$> decodeRepWith e j <|> Rep.Inr <$> decodeRepWith e j
4242

43-
withTag ::
44-
Encoding ->
45-
Json ->
46-
String ->
47-
Either JsonDecodeError
48-
{ tag :: String
49-
, decodingErr :: JsonDecodeError -> JsonDecodeError
50-
}
43+
withTag
44+
:: Encoding
45+
-> Json
46+
-> String
47+
-> Either JsonDecodeError
48+
{ tag :: String
49+
, decodingErr :: JsonDecodeError -> JsonDecodeError
50+
}
5151
withTag e j name = do
5252
let decodingErr = Named name
5353
jObj <- note (decodingErr $ TypeMismatch "Object") (toObject j)
5454
jTag <- note (decodingErr $ AtKey e.tagKey MissingValue) (FO.lookup e.tagKey jObj)
5555
tag <- note (decodingErr $ AtKey e.tagKey $ TypeMismatch "String") (toString jTag)
56-
when (tag /= name) $
57-
Left $ decodingErr $ AtKey e.tagKey $ UnexpectedValue $ fromString tag
58-
pure {tag, decodingErr}
59-
60-
withTagAndValues ::
61-
Encoding ->
62-
Json ->
63-
String ->
64-
Either JsonDecodeError
65-
{ tag :: String
66-
, values :: Json
67-
, decodingErr :: JsonDecodeError -> JsonDecodeError
68-
}
56+
when (tag /= name)
57+
$ Left
58+
$ decodingErr
59+
$ AtKey e.tagKey
60+
$ UnexpectedValue
61+
$ fromString tag
62+
pure { tag, decodingErr }
63+
64+
withTagAndValues
65+
:: Encoding
66+
-> Json
67+
-> String
68+
-> Either JsonDecodeError
69+
{ tag :: String
70+
, values :: Json
71+
, decodingErr :: JsonDecodeError -> JsonDecodeError
72+
}
6973
withTagAndValues e j name = do
70-
{tag, decodingErr} <- withTag e j name
74+
{ tag, decodingErr } <- withTag e j name
7175
jObj <- note (decodingErr $ TypeMismatch "Object") (toObject j)
7276
values <- note (decodingErr $ AtKey e.valuesKey MissingValue) (FO.lookup e.valuesKey jObj)
73-
pure {tag, values, decodingErr}
74-
75-
construct ::
76-
forall e t s .
77-
DecodeRepArgs t =>
78-
Encoding ->
79-
Array Json ->
80-
(JsonDecodeError -> e) ->
81-
Either e (Rep.Constructor s t)
77+
pure { tag, values, decodingErr }
78+
79+
construct
80+
:: forall e t s
81+
. DecodeRepArgs t
82+
=> Encoding
83+
-> Array Json
84+
-> (JsonDecodeError -> e)
85+
-> Either e (Rep.Constructor s t)
8286
construct e valuesArray decodingErr = do
83-
{init, rest} <- lmap decodingErr $ decodeRepArgs valuesArray
84-
when (rest /= []) $
85-
Left $ decodingErr $ AtKey e.valuesKey $ UnexpectedValue (fromArray rest)
87+
{ init, rest } <- lmap decodingErr $ decodeRepArgs valuesArray
88+
when (rest /= [])
89+
$ Left
90+
$ decodingErr
91+
$ AtKey e.valuesKey
92+
$ UnexpectedValue (fromArray rest)
8693
pure $ Rep.Constructor init
8794

8895
instance decodeRepConstructorNoArgs :: IsSymbol name => DecodeRep (Rep.Constructor name Rep.NoArguments) where
8996
decodeRepWith e j = do
9097
let name = reflectSymbol (Proxy :: Proxy name)
91-
{decodingErr} <- withTag e j name
98+
{ decodingErr } <- withTag e j name
9299
construct e [] decodingErr
93-
else
94-
instance decodeRepConstructorArg :: (IsSymbol name, DecodeJson a) => DecodeRep (Rep.Constructor name (Rep.Argument a)) where
100+
else instance decodeRepConstructorArg :: (IsSymbol name, DecodeJson a) => DecodeRep (Rep.Constructor name (Rep.Argument a)) where
95101
decodeRepWith e j = do
96102
let name = reflectSymbol (Proxy :: Proxy name)
97-
{values, decodingErr} <- withTagAndValues e j name
98-
if e.unwrapSingleArguments
99-
then construct e [values] decodingErr
100-
else do
101-
valuesArray <- note (decodingErr $ AtKey e.valuesKey $ TypeMismatch "Array") (toArray values)
102-
construct e valuesArray decodingErr
103-
else
104-
instance decodeRepConstructor :: (IsSymbol name, DecodeRepArgs a) => DecodeRep (Rep.Constructor name a) where
103+
{ values, decodingErr } <- withTagAndValues e j name
104+
if e.unwrapSingleArguments then construct e [ values ] decodingErr
105+
else do
106+
valuesArray <- note (decodingErr $ AtKey e.valuesKey $ TypeMismatch "Array") (toArray values)
107+
construct e valuesArray decodingErr
108+
else instance decodeRepConstructor :: (IsSymbol name, DecodeRepArgs a) => DecodeRep (Rep.Constructor name a) where
105109
decodeRepWith e j = do
106110
let name = reflectSymbol (Proxy :: Proxy name)
107-
{values, decodingErr} <- withTagAndValues e j name
111+
{ values, decodingErr } <- withTagAndValues e j name
108112
valuesArray <- note (decodingErr $ AtKey e.valuesKey $ TypeMismatch "Array") (toArray values)
109113
construct e valuesArray decodingErr
110114

111115
class DecodeRepArgs r where
112-
decodeRepArgs :: Array Json -> Either JsonDecodeError {init :: r, rest :: Array Json}
116+
decodeRepArgs :: Array Json -> Either JsonDecodeError { init :: r, rest :: Array Json }
113117

114118
instance decodeRepArgsNoArguments :: DecodeRepArgs Rep.NoArguments where
115-
decodeRepArgs js = Right {init: Rep.NoArguments, rest: js}
119+
decodeRepArgs js = Right { init: Rep.NoArguments, rest: js }
116120

117121
instance decodeRepArgsProduct :: (DecodeRepArgs a, DecodeRepArgs b) => DecodeRepArgs (Rep.Product a b) where
118122
decodeRepArgs js = do
119-
{init: a, rest: js'} <- decodeRepArgs js
120-
{init: b, rest: js''} <- decodeRepArgs js'
121-
pure {init: Rep.Product a b, rest: js''}
123+
{ init: a, rest: js' } <- decodeRepArgs js
124+
{ init: b, rest: js'' } <- decodeRepArgs js'
125+
pure { init: Rep.Product a b, rest: js'' }
122126

123127
instance decodeRepArgsArgument :: (DecodeJson a) => DecodeRepArgs (Rep.Argument a) where
124128
decodeRepArgs js = do
125-
{head, tail} <- note (TypeMismatch "NonEmptyArray") (uncons js)
126-
{init: _, rest: tail} <<< Rep.Argument <$> decodeJson head
129+
{ head, tail } <- note (TypeMismatch "NonEmptyArray") (uncons js)
130+
{ init: _, rest: tail } <<< Rep.Argument <$> decodeJson head
127131

128132
-- | Decode `Json` representation of a value which has a `Generic` type.
129133
genericDecodeJson :: forall a r. Rep.Generic a r => DecodeRep r => Json -> Either JsonDecodeError a
@@ -154,15 +158,16 @@ instance decodeLiteralConstructor :: (IsSymbol name) => DecodeLiteral (Rep.Const
154158
let name = reflectSymbol (Proxy :: Proxy name)
155159
let decodingErr = Named name
156160
tag <- note (decodingErr $ TypeMismatch "String") (toString j)
157-
when (tag /= tagNameTransform name) $
158-
Left $ decodingErr $ UnexpectedValue (fromString tag)
161+
when (tag /= tagNameTransform name)
162+
$ Left
163+
$ decodingErr
164+
$ UnexpectedValue (fromString tag)
159165
pure $ Rep.Constructor (Rep.NoArguments)
160166

161-
162167
type FailMessage =
163168
Text "`decodeLiteralSum` can only be used with sum types, where all of the constructors are nullary. This is because a string literal cannot be encoded into a product type."
164169

165-
instance decodeLiteralConstructorCannotTakeProduct
166-
:: Fail FailMessage
167-
=> DecodeLiteral (Rep.Product a b) where
168-
decodeLiteral _ _ = unsafeCrashWith "unreachable DecodeLiteral was reached."
170+
instance decodeLiteralConstructorCannotTakeProduct ::
171+
Fail FailMessage =>
172+
DecodeLiteral (Rep.Product a b) where
173+
decodeLiteral _ _ = unsafeCrashWith "unreachable DecodeLiteral was reached."

src/Data/Argonaut/Encode/Generic.purs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
module Data.Argonaut.Encode.Generic (
2-
class EncodeRep,
3-
class EncodeRepArgs,
4-
class EncodeLiteral,
5-
encodeRep,
6-
encodeRepWith,
7-
encodeRepArgs,
8-
genericEncodeJson,
9-
genericEncodeJsonWith,
10-
encodeLiteralSum,
11-
encodeLiteralSumWithTransform,
12-
encodeLiteral
13-
) where
1+
module Data.Argonaut.Encode.Generic
2+
( class EncodeRep
3+
, class EncodeRepArgs
4+
, class EncodeLiteral
5+
, encodeRep
6+
, encodeRepWith
7+
, encodeRepArgs
8+
, genericEncodeJson
9+
, genericEncodeJsonWith
10+
, encodeLiteralSum
11+
, encodeLiteralSumWithTransform
12+
, encodeLiteral
13+
) where
1414

1515
import Prelude
1616

@@ -44,13 +44,14 @@ instance encodeRepConstructor :: (IsSymbol name, EncodeRepArgs a) => EncodeRep (
4444
$ FO.insert e.valuesKey values
4545
$ FO.empty
4646
where
47-
values =
48-
let vs = encodeRepArgs a in
49-
if e.unwrapSingleArguments
50-
then case vs of
51-
[v] -> v
52-
_ -> fromArray vs
53-
else fromArray vs
47+
values =
48+
let
49+
vs = encodeRepArgs a
50+
in
51+
if e.unwrapSingleArguments then case vs of
52+
[ v ] -> v
53+
_ -> fromArray vs
54+
else fromArray vs
5455

5556
class EncodeRepArgs r where
5657
encodeRepArgs :: r -> Array Json
@@ -62,7 +63,7 @@ instance encodeRepArgsProduct :: (EncodeRepArgs a, EncodeRepArgs b) => EncodeRep
6263
encodeRepArgs (Rep.Product a b) = encodeRepArgs a <> encodeRepArgs b
6364

6465
instance encodeRepArgsArgument :: (EncodeJson a) => EncodeRepArgs (Rep.Argument a) where
65-
encodeRepArgs (Rep.Argument a) = [encodeJson a]
66+
encodeRepArgs (Rep.Argument a) = [ encodeJson a ]
6667

6768
-- | Encode any `Generic` data structure into `Json`.
6869
genericEncodeJson :: forall a r. Rep.Generic a r => EncodeRep r => a -> Json
@@ -95,7 +96,7 @@ instance encodeLiteralConstructor :: (IsSymbol name) => EncodeLiteral (Rep.Const
9596
type FailMessage =
9697
Text """`encodeLiteralSum` can only be used with sum types, where all of the constructors are nullary. This is because a string literal cannot be encoded into a product type."""
9798

98-
instance encodeLiteralConstructorCannotBeProduct
99-
:: Fail FailMessage
100-
=> EncodeLiteral (Rep.Product a b) where
99+
instance encodeLiteralConstructorCannotBeProduct ::
100+
Fail FailMessage =>
101+
EncodeLiteral (Rep.Product a b) where
101102
encodeLiteral _ _ = unsafeCrashWith "unreachable encodeLiteral was reached."

src/Data/Argonaut/Types/Generic.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module Data.Argonaut.Types.Generic (
2-
Encoding(..),
3-
defaultEncoding
4-
) where
1+
module Data.Argonaut.Types.Generic
2+
( Encoding(..)
3+
, defaultEncoding
4+
) where
55

66
-- | Encoding settings:
77
-- | tagKey -- which key to use in the JSON object for sum-type constructors

0 commit comments

Comments
 (0)