Skip to content

Commit

Permalink
Clean duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusbfs committed Feb 10, 2025
1 parent f8fea7c commit adba5b1
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 52 deletions.
4 changes: 1 addition & 3 deletions src/Internal/QueryM.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Prelude

import Aeson (class DecodeAeson, decodeAeson, parseJsonStringToAeson)
import Affjax (Error, Response) as Affjax
import Affjax.StatusCode as Affjax.StatusCode
import Cardano.Provider.Error
( ClientError(ClientHttpError, ClientHttpResponseError, ClientDecodeJsonError)
, ServiceError(ServiceOtherError)
Expand All @@ -33,8 +32,7 @@ import Ctl.Internal.Helpers (logWithLevel)
import Ctl.Internal.QueryM.HttpUtils (handleAffjaxResponseGeneric)
import Ctl.Internal.QueryM.Ogmios.QueryEnv (QueryRuntime)
import Ctl.Internal.ServerConfig (ServerConfig)
import Data.Bifunctor (lmap)
import Data.Either (Either(Left, Right))
import Data.Either (Either)
import Data.Log.Level (LogLevel)
import Data.Log.Message (Message)
import Data.Maybe (Maybe, fromMaybe)
Expand Down
4 changes: 1 addition & 3 deletions src/Internal/QueryM/Ogmios.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import Affjax.RequestBody as Affjax.RequestBody
import Affjax.RequestHeader as Affjax.RequestHeader
import Affjax.ResponseFormat (string) as Affjax.ResponseFormat
import Affjax.StatusCode (StatusCode(StatusCode))
import Affjax.StatusCode as Affjax.StatusCode
import Cardano.Provider.Error
( ClientError(ClientHttpError, ClientHttpResponseError, ClientDecodeJsonError)
, ServiceError(ServiceOtherError)
Expand Down Expand Up @@ -52,9 +51,8 @@ import Ctl.Internal.QueryM.Ogmios.Types
, pprintOgmiosDecodeError
)
import Ctl.Internal.ServerConfig (ServerConfig, mkHttpUrl)
import Data.Bifunctor (lmap)
import Data.ByteArray (byteArrayToHex)
import Data.Either (Either(Right, Left), either)
import Data.Either (Either(Left), either)
import Data.HTTP.Method (Method(POST))
import Data.Lens (_Right, to, (^?))
import Data.Maybe (Maybe(Just))
Expand Down
4 changes: 1 addition & 3 deletions src/Internal/QueryM/Ogmios/Mempool.purs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ import Ctl.Internal.QueryM.Ogmios.JsonRpc2 as JsonRpc2
import Ctl.Internal.QueryM.Ogmios.Types
( class DecodeOgmios
, OgmiosDecodeError
, aesonNull
, aesonObject
, aesonString
, decodeOgmios
, decodeResult
, ogmiosDecodeErrorToError
, submitSuccessPartialResp
)
import Ctl.Internal.Service.Helpers (aesonNull, aesonObject, aesonString)
import Data.Argonaut.Encode.Encoders as Argonaut
import Data.Bifunctor (lmap)
import Data.Either (Either(Left, Right), either, isRight)
Expand Down
38 changes: 1 addition & 37 deletions src/Internal/QueryM/Ogmios/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ module Ctl.Internal.QueryM.Ogmios.Types
, SubmitTxR(SubmitTxSuccess, SubmitFail)
, StakePoolsQueryArgument(StakePoolsQueryArgument)
, OgmiosTxEvaluationR(OgmiosTxEvaluationR)
, aesonObject
, submitSuccessPartialResp
, parseIpv6String
, rationalToSubcoin
, aesonNull
, aesonString
) where

import Prelude
Expand All @@ -46,9 +43,6 @@ import Aeson
, class EncodeAeson
, Aeson
, JsonDecodeError(TypeMismatch, MissingValue, AtKey)
, caseAesonArray
, caseAesonNull
, caseAesonObject
, caseAesonString
, decodeAeson
, encodeAeson
Expand Down Expand Up @@ -141,6 +135,7 @@ import Cardano.Types.Value (Value, getMultiAsset, valueToCoin)
import Control.Alt ((<|>))
import Control.Alternative (guard)
import Ctl.Internal.Helpers (encodeMap, showWithParens)
import Ctl.Internal.Service.Helpers (aesonArray, aesonObject, aesonString)
import Ctl.Internal.Types.ProtocolParameters
( ProtocolParameters(ProtocolParameters)
)
Expand Down Expand Up @@ -990,37 +985,6 @@ instance EncodeAeson AdditionalUtxoSet where
(\m' (k /\ v) -> Map.insert (f k) v m')
Map.empty

-- helper for assuming we get an object
aesonObject
:: forall (a :: Type)
. (Object Aeson -> Either JsonDecodeError a)
-> Aeson
-> Either JsonDecodeError a
aesonObject = caseAesonObject (Left (TypeMismatch "Expected Object"))

-- helper for assuming we get an array
aesonArray
:: forall (a :: Type)
. (Array Aeson -> Either JsonDecodeError a)
-> Aeson
-> Either JsonDecodeError a
aesonArray = caseAesonArray (Left (TypeMismatch "Expected Array"))

-- Helper that decodes a string
aesonString
:: forall (a :: Type)
. (String -> Either JsonDecodeError a)
-> Aeson
-> Either JsonDecodeError a
aesonString = caseAesonString (Left (TypeMismatch "Expected String"))

-- Helper that decodes a null
aesonNull
:: forall (a :: Type)
. Aeson
-> Either JsonDecodeError Unit
aesonNull = caseAesonNull (Left (TypeMismatch "Expected Null")) pure

-- Decode utilities

newtype OgmiosError = OgmiosError
Expand Down
8 changes: 8 additions & 0 deletions src/Internal/Service/Helpers.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Ctl.Internal.Service.Helpers
( aesonArray
, aesonString
, aesonObject
, aesonNull
, decodeAssetClass
) where

Expand All @@ -11,6 +12,7 @@ import Aeson
( Aeson
, JsonDecodeError(TypeMismatch)
, caseAesonArray
, caseAesonNull
, caseAesonObject
, caseAesonString
)
Expand Down Expand Up @@ -46,6 +48,12 @@ aesonString
-> Either JsonDecodeError a
aesonString = caseAesonString (Left (TypeMismatch "String"))

aesonNull
:: forall (a :: Type)
. Aeson
-> Either JsonDecodeError Unit
aesonNull = caseAesonNull (Left (TypeMismatch "Null")) pure

decodeAssetClass
:: String
-> String
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Types/Interval.purs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import Ctl.Internal.Helpers
, showWithParens
, unsafeFromJust
)
import Ctl.Internal.QueryM.Ogmios.Types (aesonObject)
import Ctl.Internal.Service.Helpers (aesonObject)
import Ctl.Internal.Types.SystemStart (SystemStart, sysStartUnixTime)
import Data.Argonaut.Encode.Encoders (encodeString)
import Data.Array (find, head, index, length)
Expand Down
7 changes: 2 additions & 5 deletions test/Ogmios/Aeson.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ import Aeson as Aeson
import Control.Monad.Error.Class (liftEither)
import Control.Monad.Trans.Class (lift)
import Control.Parallel (parTraverse)
import Ctl.Internal.QueryM.Ogmios.Mempool
( HasTxR
, MempoolSizeAndCapacity
) as Mempool
import Ctl.Internal.QueryM.Ogmios.Mempool (HasTxR, MempoolSizeAndCapacity) as Mempool
import Ctl.Internal.QueryM.Ogmios.Types
( class DecodeOgmios
, OgmiosDecodeError(ErrorResponse)
, OgmiosTxEvaluationR
, SubmitTxR
, aesonObject
, decodeOgmios
)
import Ctl.Internal.QueryM.Ogmios.Types as O
import Ctl.Internal.Service.Helpers (aesonObject)
import Data.Array (catMaybes, groupAllBy, nubBy)
import Data.Array.NonEmpty (NonEmptyArray, head, length, tail)
import Data.Bifunctor (lmap)
Expand Down

0 comments on commit adba5b1

Please sign in to comment.