packages feed

keiro-dsl-0.18.0.0: test/conformance-checked-mapping-replay/Generated/CheckedMappingReplay/ReplayJobs/Queue.hs

-- @generated by keiro-dsl 0.18.0.0 (language keiro-dsl 6) from workqueue replay_jobs; do not edit.
module Generated.CheckedMappingReplay.ReplayJobs.Queue
  ( ReplayJob (..)
  , encodeReplayJob
  , parseReplayJob
  , encodeContentHashMapped
  , decodeContentHashMapped
  , encodeImportantDaysMapped
  , decodeImportantDaysMapped
  , encodeMaybeContentHashMapped
  , decodeMaybeContentHashMapped
  , encodeMaybeLabelMapped
  , decodeMaybeLabelMapped
  , encodeReplayEnvelopeMapped
  , decodeReplayEnvelopeMapped
  , encodeTextLabelsMapped
  , decodeTextLabelsMapped
  , queuePhysical, queueDlq, queueTable

  ) where

import Control.Monad (unless)
import Data.Aeson (Value (..), object, parseJSON, toJSON, withObject, (.=))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Aeson.Types (Parser, JSONPathElement (..), (<?>), explicitParseField, parseEither)
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import qualified Data.Text as T
import Keiro.Codec.Structural (bindingFromShape, bindingToShape)
import Keiro.Codec.CalendarDay (encodeCalendarDay, parseCalendarDay)
import Keiro.Codec.TextSet (encodeTextSet, parseTextSet)
import Keiro.Codec.Base16Bytes (encodeBase16Bytes, parseBase16Bytes)
import Generated.CheckedMappingReplay.Structural.NominalLeaves (encodeRetainedIdLeaf, parseRetainedIdLeaf)
import Generated.CheckedMappingReplay.Structural.NominalLeaves (parseRetainedIdLeafKey, renderRetainedIdLeafKey)
import Conformance.CheckedMappingReplay.Bindings qualified as Bindings
import Conformance.CheckedMappingReplay.Domain (ContentHash, ImportantDays, MaybeContentHash, MaybeLabel, ReplayEnvelope, TextLabels)
import Generated.CheckedMappingReplay.Nominals (RetainedId)
import Generated.CheckedMappingReplay.Structural.Shape.ContentHash qualified as ShapeContentHash
import Generated.CheckedMappingReplay.Structural.Shape.ImportantDays qualified as ShapeImportantDays
import Generated.CheckedMappingReplay.Structural.Shape.MaybeContentHash qualified as ShapeMaybeContentHash
import Generated.CheckedMappingReplay.Structural.Shape.MaybeLabel qualified as ShapeMaybeLabel
import Generated.CheckedMappingReplay.Structural.Shape.ReplayEnvelope qualified as ShapeReplayEnvelope
import Generated.CheckedMappingReplay.Structural.Shape.TextLabels qualified as ShapeTextLabels

queuePhysical, queueDlq, queueTable :: Text
queuePhysical = "checked_mapping_replay"
queueDlq = "checked_mapping_replay_dlq"
queueTable = "pgmq.q_checked_mapping_replay"

data ReplayJob = ReplayJob
  { retainedId :: !RetainedId
  , envelope :: !ReplayEnvelope
  }
  deriving stock (Eq, Show)

encodeContentHashMapped :: ContentHash -> Value
encodeContentHashMapped = encodeContentHashShape . bindingToShape Bindings.contentHashBinding

parseContentHashMapped :: Value -> Parser ContentHash
parseContentHashMapped value = bindingFromShape Bindings.contentHashBinding <$> parseContentHashShape value

decodeContentHashMapped :: Value -> Either Text ContentHash
decodeContentHashMapped = mapLeftText . parseEither parseContentHashMapped

encodeContentHashShape :: ShapeContentHash.ContentHashShape -> Value
encodeContentHashShape = encodeBase16Bytes

parseContentHashShape :: Value -> Parser ShapeContentHash.ContentHashShape
parseContentHashShape = parseBase16Bytes

encodeImportantDaysMapped :: ImportantDays -> Value
encodeImportantDaysMapped = encodeImportantDaysShape . bindingToShape Bindings.importantDaysBinding

parseImportantDaysMapped :: Value -> Parser ImportantDays
parseImportantDaysMapped value = bindingFromShape Bindings.importantDaysBinding <$> parseImportantDaysShape value

decodeImportantDaysMapped :: Value -> Either Text ImportantDays
decodeImportantDaysMapped = mapLeftText . parseEither parseImportantDaysMapped

encodeImportantDaysShape :: ShapeImportantDays.ImportantDaysShape -> Value
encodeImportantDaysShape value = toJSON (map (\item0 -> encodeCalendarDay (item0)) (value))

parseImportantDaysShape :: Value -> Parser ShapeImportantDays.ImportantDaysShape
parseImportantDaysShape = \value0 -> do items0 <- (parseJSON value0 :: Parser [Value]); traverse (\(index0, item0) -> (parseCalendarDay) item0 <?> Index index0) (zip [0..] items0)

encodeMaybeContentHashMapped :: MaybeContentHash -> Value
encodeMaybeContentHashMapped = encodeMaybeContentHashShape . bindingToShape Bindings.maybeContentHashBinding

parseMaybeContentHashMapped :: Value -> Parser MaybeContentHash
parseMaybeContentHashMapped value = bindingFromShape Bindings.maybeContentHashBinding <$> parseMaybeContentHashShape value

decodeMaybeContentHashMapped :: Value -> Either Text MaybeContentHash
decodeMaybeContentHashMapped = mapLeftText . parseEither parseMaybeContentHashMapped

encodeMaybeContentHashShape :: ShapeMaybeContentHash.MaybeContentHashShape -> Value
encodeMaybeContentHashShape value = maybe Null (\item0 -> encodeContentHashShape (item0)) (value)

parseMaybeContentHashShape :: Value -> Parser ShapeMaybeContentHash.MaybeContentHashShape
parseMaybeContentHashShape = \value0 -> case value0 of Null -> pure Nothing; other0 -> Just <$> (parseContentHashShape) other0

encodeMaybeLabelMapped :: MaybeLabel -> Value
encodeMaybeLabelMapped = encodeMaybeLabelShape . bindingToShape Bindings.maybeLabelBinding

parseMaybeLabelMapped :: Value -> Parser MaybeLabel
parseMaybeLabelMapped value = bindingFromShape Bindings.maybeLabelBinding <$> parseMaybeLabelShape value

decodeMaybeLabelMapped :: Value -> Either Text MaybeLabel
decodeMaybeLabelMapped = mapLeftText . parseEither parseMaybeLabelMapped

encodeMaybeLabelShape :: ShapeMaybeLabel.MaybeLabelShape -> Value
encodeMaybeLabelShape value = maybe Null (\item0 -> toJSON (item0)) (value)

parseMaybeLabelShape :: Value -> Parser ShapeMaybeLabel.MaybeLabelShape
parseMaybeLabelShape = \value0 -> case value0 of Null -> pure Nothing; other0 -> Just <$> (parseJSON) other0

encodeReplayEnvelopeMapped :: ReplayEnvelope -> Value
encodeReplayEnvelopeMapped = encodeReplayEnvelopeShape . bindingToShape Bindings.replayEnvelopeBinding

parseReplayEnvelopeMapped :: Value -> Parser ReplayEnvelope
parseReplayEnvelopeMapped value = bindingFromShape Bindings.replayEnvelopeBinding <$> parseReplayEnvelopeShape value

decodeReplayEnvelopeMapped :: Value -> Either Text ReplayEnvelope
decodeReplayEnvelopeMapped = mapLeftText . parseEither parseReplayEnvelopeMapped

encodeReplayEnvelopeShape :: ShapeReplayEnvelope.ReplayEnvelopeShape -> Value
encodeReplayEnvelopeShape shape =
  object
      [ "label" .= encodeMaybeLabelShape (shape.label)
      , "days" .= encodeImportantDaysShape (shape.days)
      , "labels" .= encodeTextLabelsShape (shape.labels)
      , "contentHash" .= encodeMaybeContentHashShape (shape.contentHash)
      , "primary" .= encodeRetainedIdLeaf shape.primary
      , "identities" .= Object (KeyMap.fromList [(Key.fromText (renderRetainedIdLeafKey key0), toJSON (item0)) | (key0, item0) <- Map.toList (shape.identities)])
      , "optionalLabels" .= maybe Null (\item0 -> encodeTextSet (item0)) (shape.optionalLabels)
      ]

parseReplayEnvelopeShape :: Value -> Parser ShapeReplayEnvelope.ReplayEnvelopeShape
parseReplayEnvelopeShape = withObject "ReplayEnvelopeShape" $ \objectValue -> do
  rejectUnknownFields "ReplayEnvelope" ["label", "days", "labels", "contentHash", "primary", "identities", "optionalLabels"] objectValue
  ShapeReplayEnvelope.ReplayEnvelope
    <$> explicitParseField (parseMaybeLabelShape) objectValue "label"
    <*> explicitParseField (parseImportantDaysShape) objectValue "days"
    <*> explicitParseField (parseTextLabelsShape) objectValue "labels"
    <*> explicitParseField (parseMaybeContentHashShape) objectValue "contentHash"
    <*> explicitParseField (parseRetainedIdLeaf) objectValue "primary"
    <*> explicitParseField (\value0 -> withObject "Map[RetainedId]" (\object0 -> Map.fromList <$> traverse (\(rawKey0, item0) -> do key0 <- parseRetainedIdLeafKey (Key.toText rawKey0) <?> Key rawKey0; parsedItem0 <- (parseJSON) item0 <?> Key rawKey0; pure (key0, parsedItem0)) (KeyMap.toList object0)) value0) objectValue "identities"
    <*> parseOptionalField (pure Nothing) (\value0 -> case value0 of Null -> pure Nothing; other0 -> Just <$> (parseTextSet) other0) objectValue "optionalLabels"

encodeTextLabelsMapped :: TextLabels -> Value
encodeTextLabelsMapped = encodeTextLabelsShape . bindingToShape Bindings.textLabelsBinding

parseTextLabelsMapped :: Value -> Parser TextLabels
parseTextLabelsMapped value = bindingFromShape Bindings.textLabelsBinding <$> parseTextLabelsShape value

decodeTextLabelsMapped :: Value -> Either Text TextLabels
decodeTextLabelsMapped = mapLeftText . parseEither parseTextLabelsMapped

encodeTextLabelsShape :: ShapeTextLabels.TextLabelsShape -> Value
encodeTextLabelsShape value = encodeTextSet (value)

parseTextLabelsShape :: Value -> Parser ShapeTextLabels.TextLabelsShape
parseTextLabelsShape = parseTextSet

encodeReplayJob :: ReplayJob -> Value
encodeReplayJob payload =
  object
    [ "retained_id" .= encodeRetainedIdLeaf payload.retainedId
    , "envelope" .= encodeReplayEnvelopeMapped payload.envelope
    ]

parseReplayJob :: Value -> Either Text ReplayJob
parseReplayJob = mapLeftText . parseEither (withObject "ReplayJob" go)
  where
    go objectValue = ReplayJob <$> explicitParseField (parseRetainedIdLeaf) objectValue "retained_id" <*> explicitParseField (parseReplayEnvelopeMapped) objectValue "envelope"

mapLeftText :: Either String b -> Either Text b
mapLeftText = either (Left . T.pack) Right

parseOptionalField :: Parser fieldValue -> (Value -> Parser fieldValue) -> KeyMap.KeyMap Value -> Key.Key -> Parser fieldValue
parseOptionalField onMissing parseItem objectValue key =
  case KeyMap.lookup key objectValue of
    Nothing -> onMissing
    Just _ -> explicitParseField parseItem objectValue key

rejectUnknownFields :: String -> [Text] -> KeyMap.KeyMap Value -> Parser ()
rejectUnknownFields label allowed objectValue =
  unless (null extras) (fail (label <> " contains unknown fields: " <> show extras))
  where
    extras = filter (`notElem` allowed) (map Key.toText (KeyMap.keys objectValue))