Skip to content

Commit adba5b1

Browse files
committed
Clean duplicated code
1 parent f8fea7c commit adba5b1

File tree

7 files changed

+15
-52
lines changed

7 files changed

+15
-52
lines changed

src/Internal/QueryM.purs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Prelude
1414

1515
import Aeson (class DecodeAeson, decodeAeson, parseJsonStringToAeson)
1616
import Affjax (Error, Response) as Affjax
17-
import Affjax.StatusCode as Affjax.StatusCode
1817
import Cardano.Provider.Error
1918
( ClientError(ClientHttpError, ClientHttpResponseError, ClientDecodeJsonError)
2019
, ServiceError(ServiceOtherError)
@@ -33,8 +32,7 @@ import Ctl.Internal.Helpers (logWithLevel)
3332
import Ctl.Internal.QueryM.HttpUtils (handleAffjaxResponseGeneric)
3433
import Ctl.Internal.QueryM.Ogmios.QueryEnv (QueryRuntime)
3534
import Ctl.Internal.ServerConfig (ServerConfig)
36-
import Data.Bifunctor (lmap)
37-
import Data.Either (Either(Left, Right))
35+
import Data.Either (Either)
3836
import Data.Log.Level (LogLevel)
3937
import Data.Log.Message (Message)
4038
import Data.Maybe (Maybe, fromMaybe)

src/Internal/QueryM/Ogmios.purs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import Affjax.RequestBody as Affjax.RequestBody
1919
import Affjax.RequestHeader as Affjax.RequestHeader
2020
import Affjax.ResponseFormat (string) as Affjax.ResponseFormat
2121
import Affjax.StatusCode (StatusCode(StatusCode))
22-
import Affjax.StatusCode as Affjax.StatusCode
2322
import Cardano.Provider.Error
2423
( ClientError(ClientHttpError, ClientHttpResponseError, ClientDecodeJsonError)
2524
, ServiceError(ServiceOtherError)
@@ -52,9 +51,8 @@ import Ctl.Internal.QueryM.Ogmios.Types
5251
, pprintOgmiosDecodeError
5352
)
5453
import Ctl.Internal.ServerConfig (ServerConfig, mkHttpUrl)
55-
import Data.Bifunctor (lmap)
5654
import Data.ByteArray (byteArrayToHex)
57-
import Data.Either (Either(Right, Left), either)
55+
import Data.Either (Either(Left), either)
5856
import Data.HTTP.Method (Method(POST))
5957
import Data.Lens (_Right, to, (^?))
6058
import Data.Maybe (Maybe(Just))

src/Internal/QueryM/Ogmios/Mempool.purs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ import Ctl.Internal.QueryM.Ogmios.JsonRpc2 as JsonRpc2
7676
import Ctl.Internal.QueryM.Ogmios.Types
7777
( class DecodeOgmios
7878
, OgmiosDecodeError
79-
, aesonNull
80-
, aesonObject
81-
, aesonString
8279
, decodeOgmios
8380
, decodeResult
8481
, ogmiosDecodeErrorToError
8582
, submitSuccessPartialResp
8683
)
84+
import Ctl.Internal.Service.Helpers (aesonNull, aesonObject, aesonString)
8785
import Data.Argonaut.Encode.Encoders as Argonaut
8886
import Data.Bifunctor (lmap)
8987
import Data.Either (Either(Left, Right), either, isRight)

src/Internal/QueryM/Ogmios/Types.purs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ module Ctl.Internal.QueryM.Ogmios.Types
3131
, SubmitTxR(SubmitTxSuccess, SubmitFail)
3232
, StakePoolsQueryArgument(StakePoolsQueryArgument)
3333
, OgmiosTxEvaluationR(OgmiosTxEvaluationR)
34-
, aesonObject
3534
, submitSuccessPartialResp
3635
, parseIpv6String
3736
, rationalToSubcoin
38-
, aesonNull
39-
, aesonString
4037
) where
4138

4239
import Prelude
@@ -46,9 +43,6 @@ import Aeson
4643
, class EncodeAeson
4744
, Aeson
4845
, JsonDecodeError(TypeMismatch, MissingValue, AtKey)
49-
, caseAesonArray
50-
, caseAesonNull
51-
, caseAesonObject
5246
, caseAesonString
5347
, decodeAeson
5448
, encodeAeson
@@ -141,6 +135,7 @@ import Cardano.Types.Value (Value, getMultiAsset, valueToCoin)
141135
import Control.Alt ((<|>))
142136
import Control.Alternative (guard)
143137
import Ctl.Internal.Helpers (encodeMap, showWithParens)
138+
import Ctl.Internal.Service.Helpers (aesonArray, aesonObject, aesonString)
144139
import Ctl.Internal.Types.ProtocolParameters
145140
( ProtocolParameters(ProtocolParameters)
146141
)
@@ -990,37 +985,6 @@ instance EncodeAeson AdditionalUtxoSet where
990985
(\m' (k /\ v) -> Map.insert (f k) v m')
991986
Map.empty
992987

993-
-- helper for assuming we get an object
994-
aesonObject
995-
:: forall (a :: Type)
996-
. (Object Aeson -> Either JsonDecodeError a)
997-
-> Aeson
998-
-> Either JsonDecodeError a
999-
aesonObject = caseAesonObject (Left (TypeMismatch "Expected Object"))
1000-
1001-
-- helper for assuming we get an array
1002-
aesonArray
1003-
:: forall (a :: Type)
1004-
. (Array Aeson -> Either JsonDecodeError a)
1005-
-> Aeson
1006-
-> Either JsonDecodeError a
1007-
aesonArray = caseAesonArray (Left (TypeMismatch "Expected Array"))
1008-
1009-
-- Helper that decodes a string
1010-
aesonString
1011-
:: forall (a :: Type)
1012-
. (String -> Either JsonDecodeError a)
1013-
-> Aeson
1014-
-> Either JsonDecodeError a
1015-
aesonString = caseAesonString (Left (TypeMismatch "Expected String"))
1016-
1017-
-- Helper that decodes a null
1018-
aesonNull
1019-
:: forall (a :: Type)
1020-
. Aeson
1021-
-> Either JsonDecodeError Unit
1022-
aesonNull = caseAesonNull (Left (TypeMismatch "Expected Null")) pure
1023-
1024988
-- Decode utilities
1025989

1026990
newtype OgmiosError = OgmiosError

src/Internal/Service/Helpers.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Ctl.Internal.Service.Helpers
22
( aesonArray
33
, aesonString
44
, aesonObject
5+
, aesonNull
56
, decodeAssetClass
67
) where
78

@@ -11,6 +12,7 @@ import Aeson
1112
( Aeson
1213
, JsonDecodeError(TypeMismatch)
1314
, caseAesonArray
15+
, caseAesonNull
1416
, caseAesonObject
1517
, caseAesonString
1618
)
@@ -46,6 +48,12 @@ aesonString
4648
-> Either JsonDecodeError a
4749
aesonString = caseAesonString (Left (TypeMismatch "String"))
4850

51+
aesonNull
52+
:: forall (a :: Type)
53+
. Aeson
54+
-> Either JsonDecodeError Unit
55+
aesonNull = caseAesonNull (Left (TypeMismatch "Null")) pure
56+
4957
decodeAssetClass
5058
:: String
5159
-> String

src/Internal/Types/Interval.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ import Ctl.Internal.Helpers
104104
, showWithParens
105105
, unsafeFromJust
106106
)
107-
import Ctl.Internal.QueryM.Ogmios.Types (aesonObject)
107+
import Ctl.Internal.Service.Helpers (aesonObject)
108108
import Ctl.Internal.Types.SystemStart (SystemStart, sysStartUnixTime)
109109
import Data.Argonaut.Encode.Encoders (encodeString)
110110
import Data.Array (find, head, index, length)

test/Ogmios/Aeson.purs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ import Aeson as Aeson
1010
import Control.Monad.Error.Class (liftEither)
1111
import Control.Monad.Trans.Class (lift)
1212
import Control.Parallel (parTraverse)
13-
import Ctl.Internal.QueryM.Ogmios.Mempool
14-
( HasTxR
15-
, MempoolSizeAndCapacity
16-
) as Mempool
13+
import Ctl.Internal.QueryM.Ogmios.Mempool (HasTxR, MempoolSizeAndCapacity) as Mempool
1714
import Ctl.Internal.QueryM.Ogmios.Types
1815
( class DecodeOgmios
1916
, OgmiosDecodeError(ErrorResponse)
2017
, OgmiosTxEvaluationR
2118
, SubmitTxR
22-
, aesonObject
2319
, decodeOgmios
2420
)
2521
import Ctl.Internal.QueryM.Ogmios.Types as O
22+
import Ctl.Internal.Service.Helpers (aesonObject)
2623
import Data.Array (catMaybes, groupAllBy, nubBy)
2724
import Data.Array.NonEmpty (NonEmptyArray, head, length, tail)
2825
import Data.Bifunctor (lmap)

0 commit comments

Comments
 (0)