Skip to content

Commit 67078c3

Browse files
committed
Simplified parsing RPC responses
1 parent 3f4fb73 commit 67078c3

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

tests/TestTypes.hs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ module TestTypes ( TestRequest (..)
88
, versionKey) where
99

1010
import Data.Aeson
11-
import Data.Aeson.Types (Parser)
1211
import Data.Maybe (catMaybes)
1312
import Data.Text (Text)
1413
import Data.Attoparsec.Number (Number)
1514
import Data.HashMap.Strict (size)
16-
import Control.Applicative ((<$>), (<*>), (<|>), (*>), pure, empty)
17-
import Control.Monad (when)
15+
import Control.Applicative ((<$>), (<*>), (<|>), pure, empty)
16+
import Control.Monad (when, guard)
1817

1918
data TestRpcError = TestRpcError { errCode :: Int
2019
, errMsg :: Text
@@ -42,16 +41,13 @@ data TestResponse = TestResponse { rspId :: TestId
4241
deriving (Eq, Show)
4342

4443
instance FromJSON TestResponse where
45-
parseJSON (Object r) = failIf (size r /= 3) *> checkVersion *>
46-
(TestResponse <$>
47-
r .: "id" <*>
48-
((Left <$> r .: "error") <|> (Right <$> r .: "result")))
49-
where checkVersion = r .: versionKey >>= failIf . (/= jsonRpcVersion)
44+
parseJSON (Object obj) = do
45+
guard (size obj == 3)
46+
guard . (jsonRpcVersion ==) =<< obj .: versionKey
47+
TestResponse <$> obj .: "id" <*>
48+
((Left <$> obj .: "error") <|> (Right <$> obj .: "result"))
5049
parseJSON _ = empty
5150

52-
failIf :: Bool -> Parser ()
53-
failIf b = if b then empty else pure ()
54-
5551
data TestId = IdString Text | IdNumber Number | IdNull
5652
deriving (Eq, Show)
5753

0 commit comments

Comments
 (0)