keiro-dsl-0.26.0.0: test/conformance-intake-delegated-noop/Generated/DeliveryOutcomes/Courier/Contract.hs
-- @generated by keiro-dsl 0.26.0.0 (language keiro-dsl 6) from contract courier; do not edit.
module Generated.DeliveryOutcomes.Courier.Contract
( CourierPayload (..)
, DeliveryOutcomeReportedData (..)
, deliveryEventsTopic
, messageTypeOf
, encodeCourierPayload
, parseCourierPayload
) where
import Data.Aeson (Value, object, withObject, withText, (.:), (.=))
import Data.Aeson.Types (Parser, explicitParseField, parseEither)
import Data.Text (Text)
import qualified Data.Text as T
-- topic constants
deliveryEventsTopic :: Text
deliveryEventsTopic = "courier.delivery.events"
-- the closed payload set (discriminated by "messageType")
data DeliveryOutcomeReportedData = DeliveryOutcomeReportedData { deliveryId :: !Text, outcome :: !Text }
deriving stock (Eq, Show)
data CourierPayload = DeliveryOutcomeReported !DeliveryOutcomeReportedData
deriving stock (Eq, Show)
messageTypeOf :: CourierPayload -> Text
messageTypeOf = \case
DeliveryOutcomeReported {} -> "DeliveryOutcomeReported"
encodeCourierPayload :: CourierPayload -> Value
encodeCourierPayload = \case
DeliveryOutcomeReported payload ->
object
[ "messageType" .= ("DeliveryOutcomeReported" :: Text)
, "deliveryId" .= payload.deliveryId
, "outcome" .= payload.outcome
]
parseCourierPayload :: Value -> Either Text CourierPayload
parseCourierPayload = mapLeftText . parseEither (withObject "CourierPayload" go)
where
go o = do
kind <- explicitParseField (withText "messageType" validateMessageType) o "messageType"
case kind of
"DeliveryOutcomeReported" ->
DeliveryOutcomeReported
<$> ( DeliveryOutcomeReportedData
<$> o .: "deliveryId"
<*> o .: "outcome"
)
_ -> fail "validated message type was not handled"
mapLeftText :: Either String b -> Either Text b
mapLeftText = either (Left . T.pack) Right
validateMessageType :: Text -> Parser Text
validateMessageType kind
| kind `elem` ["DeliveryOutcomeReported"] = pure kind
| otherwise = fail ("unknown message type " <> show kind <> "; expected one of: DeliveryOutcomeReported")