keiro-dsl-0.2.0.0: test/conformance-skeletons/SkelEmit/Generated/MyService/MyContract/Contract.hs
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
-- @generated by keiro-dsl; do not edit. Regenerated from the .keiro spec.
module SkelEmit.Generated.MyService.MyContract.Contract (
MyContractPayload (..),
ThingAcceptedData (..),
messageTypeOf,
encodeMyContractPayload,
parseMyContractPayload,
) where
import Data.Aeson (Value, object, withObject, (.:), (.=))
import Data.Aeson.Types (Parser, parseEither)
import Data.Text (Text)
import Data.Text qualified as T
-- topic constants
eventsTopic :: Text
eventsTopic = "my-service.events"
-- the closed payload set (discriminated by "messageType")
data ThingAcceptedData = ThingAcceptedData {thingId :: !Text}
deriving stock (Eq, Show)
data MyContractPayload = ThingAccepted !ThingAcceptedData
deriving stock (Eq, Show)
messageTypeOf :: MyContractPayload -> Text
messageTypeOf = \case
ThingAccepted{} -> "ThingAccepted"
encodeMyContractPayload :: MyContractPayload -> Value
encodeMyContractPayload = \case
ThingAccepted payload ->
object
[ "messageType" .= ("ThingAccepted" :: Text)
, "thingId" .= payload.thingId
]
parseMyContractPayload :: Value -> Either Text MyContractPayload
parseMyContractPayload = mapLeftText . parseEither (withObject "MyContractPayload" go)
where
go o = do
kind <- o .: "messageType" :: Parser Text
case kind of
"ThingAccepted" ->
ThingAccepted <$> (ThingAcceptedData <$> o .: "thingId")
_ -> fail "unknown message type"
mapLeftText :: Either String b -> Either Text b
mapLeftText = either (Left . T.pack) Right