Skip to content

Introduce purs-tidy formatter #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:

- name: Set up a PureScript toolchain
uses: purescript-contrib/setup-purescript@main
with:
purs-tidy: "latest"

- name: Cache PureScript dependencies
uses: actions/cache@v2
Expand All @@ -32,3 +34,6 @@ jobs:

- name: Run tests
run: spago test --no-install

- name: Check formatting
run: purs-tidy check src test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
!.gitignore
!.github
!.editorconfig
!.tidyrc.json

output
generated-docs
Expand Down
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "source",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "never",
"width": null
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
Bugfixes:

Other improvements:
- Added `purs-tidy` formatter (#104 by @thomashoneyman)

## [v8.1.0](https://github.com/purescript-contrib/purescript-argonaut-codecs/releases/tag/v8.1.0) - 2021-04-09

Expand Down
38 changes: 19 additions & 19 deletions src/Data/Argonaut/Decode/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ instance decodeMap :: (Ord a, DecodeJson a, DecodeJson b) => DecodeJson (M.Map a
instance decodeVoid :: DecodeJson Void where
decodeJson = decodeVoid

instance decodeRecord
:: ( GDecodeJson row list
, RL.RowToList row list
)
=> DecodeJson (Record row) where
instance decodeRecord ::
( GDecodeJson row list
, RL.RowToList row list
) =>
DecodeJson (Record row) where
decodeJson json =
case toObject json of
Just object -> gDecodeJson object (Proxy :: Proxy list)
Expand All @@ -110,14 +110,14 @@ class GDecodeJson (row :: Row Type) (list :: RL.RowList Type) | list -> row wher
instance gDecodeJsonNil :: GDecodeJson () RL.Nil where
gDecodeJson _ _ = Right {}

instance gDecodeJsonCons
:: ( DecodeJsonField value
, GDecodeJson rowTail tail
, IsSymbol field
, Row.Cons field value rowTail row
, Row.Lacks field rowTail
)
=> GDecodeJson row (RL.Cons field value tail) where
instance gDecodeJsonCons ::
( DecodeJsonField value
, GDecodeJson rowTail tail
, IsSymbol field
, Row.Cons field value rowTail row
, Row.Lacks field rowTail
) =>
GDecodeJson row (RL.Cons field value tail) where
gDecodeJson object _ = do
let
_field = Proxy :: Proxy field
Expand All @@ -136,13 +136,13 @@ instance gDecodeJsonCons
class DecodeJsonField a where
decodeJsonField :: Maybe Json -> Maybe (Either JsonDecodeError a)

instance decodeFieldMaybe
:: DecodeJson a
=> DecodeJsonField (Maybe a) where
instance decodeFieldMaybe ::
DecodeJson a =>
DecodeJsonField (Maybe a) where
decodeJsonField Nothing = Just $ Right Nothing
decodeJsonField (Just j) = Just $ decodeJson j

else instance decodeFieldId
:: DecodeJson a
=> DecodeJsonField a where
else instance decodeFieldId ::
DecodeJson a =>
DecodeJsonField a where
decodeJsonField j = decodeJson <$> j
18 changes: 9 additions & 9 deletions src/Data/Argonaut/Decode/Decoders.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ decodeTuple decoderA decoderB json = decodeArray Right json >>= f
where
f :: Array Json -> Either JsonDecodeError (Tuple a b)
f = case _ of
[a, b] -> Tuple <$> decoderA a <*> decoderB b
[ a, b ] -> Tuple <$> decoderA a <*> decoderB b
_ -> Left $ TypeMismatch "Tuple"

decodeEither
Expand Down Expand Up @@ -100,8 +100,8 @@ decodeNonEmpty_Array decoder =
lmap (Named "NonEmpty Array")
<<< traverse decoder
<=< map (\x -> x.head :| x.tail)
<<< note (TypeMismatch "NonEmpty Array")
<<< Arr.uncons
<<< note (TypeMismatch "NonEmpty Array")
<<< Arr.uncons
<=< decodeJArray

decodeNonEmptyArray
Expand All @@ -113,8 +113,8 @@ decodeNonEmptyArray decoder =
lmap (Named "NonEmptyArray")
<<< traverse decoder
<=< map (\x -> NEA.cons' x.head x.tail)
<<< note (TypeMismatch "NonEmptyArray")
<<< Arr.uncons
<<< note (TypeMismatch "NonEmptyArray")
<<< Arr.uncons
<=< decodeJArray

decodeNonEmpty_List
Expand All @@ -126,8 +126,8 @@ decodeNonEmpty_List decoder =
lmap (Named "NonEmpty List")
<<< traverse decoder
<=< map (\x -> x.head :| x.tail)
<<< note (TypeMismatch "NonEmpty List")
<<< L.uncons
<<< note (TypeMismatch "NonEmpty List")
<<< L.uncons
<=< map (map fromFoldable) decodeJArray

decodeNonEmptyList
Expand All @@ -139,8 +139,8 @@ decodeNonEmptyList decoder =
lmap (Named "NonEmptyList")
<<< traverse decoder
<=< map (\x -> NEL.cons' x.head x.tail)
<<< note (TypeMismatch "NonEmptyList")
<<< L.uncons
<<< note (TypeMismatch "NonEmptyList")
<<< L.uncons
Comment on lines +142 to +143
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The operator precedence stuff is so nice 😍

<=< map (map fromFoldable) decodeJArray

decodeCodePoint :: Json -> Either JsonDecodeError CodePoint
Expand Down
24 changes: 12 additions & 12 deletions src/Data/Argonaut/Encode/Class.purs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ instance encodeMap :: (Ord a, EncodeJson a, EncodeJson b) => EncodeJson (M.Map a
instance encodeVoid :: EncodeJson Void where
encodeJson = encodeVoid

instance encodeRecord
:: ( GEncodeJson row list
, RL.RowToList row list
)
=> EncodeJson (Record row) where
instance encodeRecord ::
( GEncodeJson row list
, RL.RowToList row list
) =>
EncodeJson (Record row) where
encodeJson rec = fromObject $ gEncodeJson rec (Proxy :: Proxy list)

class GEncodeJson (row :: Row Type) (list :: RL.RowList Type) where
Expand All @@ -108,13 +108,13 @@ class GEncodeJson (row :: Row Type) (list :: RL.RowList Type) where
instance gEncodeJsonNil :: GEncodeJson row RL.Nil where
gEncodeJson _ _ = FO.empty

instance gEncodeJsonCons
:: ( EncodeJson value
, GEncodeJson row tail
, IsSymbol field
, Row.Cons field value tail' row
)
=> GEncodeJson row (RL.Cons field value tail) where
instance gEncodeJsonCons ::
( EncodeJson value
, GEncodeJson row tail
, IsSymbol field
, Row.Cons field value tail' row
) =>
GEncodeJson row (RL.Cons field value tail) where
gEncodeJson row _ = do
let _field = Proxy :: Proxy field
FO.insert
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Argonaut/Encode/Encoders.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ encodeMaybe encoder = case _ of
Just a -> encoder a

encodeTuple :: forall a b. (a -> Json) -> (b -> Json) -> Tuple a b -> Json
encodeTuple encoderA encoderB (Tuple a b) = fromArray [encoderA a, encoderB b]
encodeTuple encoderA encoderB (Tuple a b) = fromArray [ encoderA a, encoderB b ]

encodeEither :: forall a b . (a -> Json) -> (b -> Json) -> Either a b -> Json
encodeEither :: forall a b. (a -> Json) -> (b -> Json) -> Either a b -> Json
encodeEither encoderA encoderB = either (obj encoderA "Left") (obj encoderB "Right")
where
obj :: forall c. (c -> Json) -> String -> c -> Json
Expand Down
25 changes: 13 additions & 12 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ combinatorsCheck = do
where
go :: FO.Object Json -> Boolean
go object =
let keys = FO.keys object
in foldl (\ok key -> ok && isJust (FO.lookup key object)) true keys
let
keys = FO.keys object
in
foldl (\ok key -> ok && isJust (FO.lookup key object)) true keys

eitherCheck :: Test
eitherCheck = do
Expand Down Expand Up @@ -287,7 +289,7 @@ manualRecordDecode = do
testBazCases = do
test "Missing 'baz' key should decode to FooNested" do
case decodeJson fooNestedBarJson of
Right (FooNested { bar: Just [1], baz: false }) -> pure unit
Right (FooNested { bar: Just [ 1 ], baz: false }) -> pure unit
_ -> failure ("Failed to properly decode JSON string: " <> stringify fooNestedBarJson)

test "Null 'baz' key should fail to decode to FooNested" do
Expand All @@ -297,24 +299,24 @@ manualRecordDecode = do

test "Missing 'baz' key should decode to FooNested'" do
case decodeJson fooNestedBarJson of
Right (FooNested' { bar: Just [1], baz: false }) -> pure unit
Right (FooNested' { bar: Just [ 1 ], baz: false }) -> pure unit
_ -> failure ("Failed to properly decode JSON string: " <> stringify fooNestedBarJson)

test "Null 'baz' key should decode to FooNested'" do
case decodeJson fooNestedBarJsonNull of
Right (FooNested' { bar: Just [1], baz: false }) -> pure unit
Right (FooNested' { bar: Just [ 1 ], baz: false }) -> pure unit
_ -> failure ("Failed to properly decode JSON string: " <> stringify fooNestedBarJsonNull)

testFullCases :: Test
testFullCases = do
test "Json should decode to FooNested" do
case decodeJson fooNestedFullJson of
Right (FooNested { bar: Just [1], baz: true }) -> pure unit
Right (FooNested { bar: Just [ 1 ], baz: true }) -> pure unit
_ -> failure ("Failed to properly decode JSON string: " <> stringify fooNestedFullJson)

test "Json should decode to FooNested'" do
case decodeJson fooNestedFullJson of
Right (FooNested { bar: Just [1], baz: true }) -> pure unit
Right (FooNested { bar: Just [ 1 ], baz: true }) -> pure unit
_ -> failure ("Failed to properly decode JSON string: " <> stringify fooNestedFullJson)

test "Test that decoding custom record is pure unitful" do
Expand All @@ -338,17 +340,16 @@ nonEmptyCheck = do
Left err ->
false <?> printJsonDecodeError err


test "Test EncodeJson/DecodeJson on NonEmptyString" do
quickCheck \(x :: NonEmptyString) ->
case decodeJson (encodeJson x) of
Right decoded ->
decoded == x
<?>
( " x = "
<> NonEmptyString.toString x
<> ", decoded = "
<> NonEmptyString.toString decoded
( " x = "
<> NonEmptyString.toString x
<> ", decoded = "
<> NonEmptyString.toString decoded
)
Left err ->
false <?> printJsonDecodeError err
Expand Down