packages feed

keiro-dsl-0.18.0.0: test/conformance-refined-base16/Generated/RefinedBase16/HashJobs/Queue.hs

-- @generated by keiro-dsl 0.18.0.0 (language keiro-dsl 6) from workqueue hash_jobs; do not edit.
module Generated.RefinedBase16.HashJobs.Queue
  ( HashJob (..)
  , encodeHashJob
  , parseHashJob
  , encodeContentHashMapped
  , decodeContentHashMapped
  , encodeHashEnvelopeMapped
  , decodeHashEnvelopeMapped
  , encodeMaybeContentHashMapped
  , decodeMaybeContentHashMapped
  , 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 (Map)
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.Base16Bytes (encodeBase16Bytes, parseBase16Bytes)
import Conformance.RefinedBase16.Bindings qualified as Bindings
import Conformance.RefinedBase16.Domain (ContentHash, HashEnvelope, MaybeContentHash)
import Generated.RefinedBase16.Structural.Shape.ContentHash qualified as ShapeContentHash
import Generated.RefinedBase16.Structural.Shape.HashEnvelope qualified as ShapeHashEnvelope
import Generated.RefinedBase16.Structural.Shape.MaybeContentHash qualified as ShapeMaybeContentHash

queuePhysical, queueDlq, queueTable :: Text
queuePhysical = "hash_jobs"
queueDlq = "hash_jobs_dlq"
queueTable = "pgmq.q_hash_jobs"

data HashJob = HashJob
  { hash :: !ContentHash
  , optionalHash :: !MaybeContentHash
  , envelope :: !HashEnvelope
  }
  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

encodeHashEnvelopeMapped :: HashEnvelope -> Value
encodeHashEnvelopeMapped = encodeHashEnvelopeShape . bindingToShape Bindings.hashEnvelopeBinding

parseHashEnvelopeMapped :: Value -> Parser HashEnvelope
parseHashEnvelopeMapped value = bindingFromShape Bindings.hashEnvelopeBinding <$> parseHashEnvelopeShape value

decodeHashEnvelopeMapped :: Value -> Either Text HashEnvelope
decodeHashEnvelopeMapped = mapLeftText . parseEither parseHashEnvelopeMapped

encodeHashEnvelopeShape :: ShapeHashEnvelope.HashEnvelopeShape -> Value
encodeHashEnvelopeShape shape =
  object
      [ "primary" .= encodeContentHashShape (shape.primary)
      , "optionalHash" .= maybe Null (\item0 -> encodeContentHashShape (item0)) (shape.optionalHash)
      , "namedOptional" .= encodeMaybeContentHashShape (shape.namedOptional)
      , "sequence" .= toJSON (map (\item0 -> encodeContentHashShape (item0)) (shape.sequence))
      , "labelled" .= toJSON (Map.map (\item0 -> encodeContentHashShape (item0)) (shape.labelled))
      ]

parseHashEnvelopeShape :: Value -> Parser ShapeHashEnvelope.HashEnvelopeShape
parseHashEnvelopeShape = withObject "HashEnvelopeShape" $ \objectValue -> do
  rejectUnknownFields "HashEnvelope" ["primary", "optionalHash", "namedOptional", "sequence", "labelled"] objectValue
  ShapeHashEnvelope.HashEnvelope
    <$> explicitParseField (parseContentHashShape) objectValue "primary"
    <*> parseOptionalField (pure Nothing) (\value0 -> case value0 of Null -> pure Nothing; other0 -> Just <$> (parseContentHashShape) other0) objectValue "optionalHash"
    <*> explicitParseField (parseMaybeContentHashShape) objectValue "namedOptional"
    <*> explicitParseField (\value0 -> do items0 <- (parseJSON value0 :: Parser [Value]); traverse (\(index0, item0) -> (parseContentHashShape) item0 <?> Index index0) (zip [0..] items0)) objectValue "sequence"
    <*> explicitParseField (\value0 -> do items0 <- (parseJSON value0 :: Parser (Map Text Value)); Map.traverseWithKey (\key0 item0 -> (parseContentHashShape) item0 <?> Key (Key.fromText key0)) items0) objectValue "labelled"

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

encodeHashJob :: HashJob -> Value
encodeHashJob payload =
  object
    [ "hash" .= encodeContentHashMapped payload.hash
    , "optional_hash" .= encodeMaybeContentHashMapped payload.optionalHash
    , "envelope" .= encodeHashEnvelopeMapped payload.envelope
    ]

parseHashJob :: Value -> Either Text HashJob
parseHashJob = mapLeftText . parseEither (withObject "HashJob" go)
  where
    go objectValue = HashJob <$> explicitParseField (parseContentHashMapped) objectValue "hash" <*> explicitParseField (parseMaybeContentHashMapped) objectValue "optional_hash" <*> explicitParseField (parseHashEnvelopeMapped) 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))