Repository navigation
feat: add deterministic Haskell engine codecs #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| module EventSorcery.Engine.Codec ( | ||
| decodeStoredEvents, | ||
| encodeCommit, | ||
| encodeLoadStream, | ||
| encodeOpenOptions, | ||
| ) where | ||
|
|
||
| import Codec.CBOR.Decoding ( | ||
| Decoder, | ||
| decodeBytes, | ||
| decodeListLen, | ||
| decodeString, | ||
| decodeWord, | ||
| decodeWord64, | ||
| ) | ||
| import Codec.CBOR.Encoding ( | ||
| Encoding, | ||
| encodeBytes, | ||
| encodeListLen, | ||
| encodeNull, | ||
| encodeString, | ||
| encodeWord, | ||
| encodeWord32, | ||
| encodeWord64, | ||
| ) | ||
| import Codec.CBOR.Read (deserialiseFromBytes) | ||
| import Codec.CBOR.Write (toStrictByteString) | ||
| import Control.Monad (replicateM) | ||
| import Data.ByteString (ByteString) | ||
| import Data.ByteString.Lazy qualified as LazyByteString | ||
| import Data.Foldable (foldMap) | ||
| import Data.Maybe (maybe) | ||
| import Data.Word (Word64) | ||
| import EventSorcery.Engine.Protocol ( | ||
| AggregateId (..), | ||
| AggregateType (..), | ||
| EventType (..), | ||
| EventVersion (..), | ||
| OpenOptions (..), | ||
| ProposedEvent (..), | ||
| StoredEvent (..), | ||
| StreamIdentity (..), | ||
| ) | ||
| import Prelude ( | ||
| Either (..), | ||
| Int, | ||
| Maybe, | ||
| String, | ||
| fail, | ||
| fromIntegral, | ||
| length, | ||
| otherwise, | ||
| pure, | ||
| show, | ||
| ($), | ||
| (<$>), | ||
| (<>), | ||
| (==), | ||
| ) | ||
|
|
||
|
|
||
| encodeOpenOptions :: OpenOptions -> ByteString | ||
| encodeOpenOptions options = | ||
| toStrictByteString $ | ||
| encodeListLen 5 | ||
| <> encodeWord 1 | ||
| <> encodeString options.path | ||
| <> encodeWord64 options.busyTimeoutMilliseconds | ||
| <> encodeWord32 options.poolSize | ||
| <> encodeWord32 options.runtimeThreads | ||
|
|
||
|
|
||
| encodeLoadStream :: StreamIdentity -> Maybe Word64 -> ByteString | ||
| encodeLoadStream stream after = | ||
| toStrictByteString $ | ||
| encodeListLen 4 | ||
| <> encodeWord 1 | ||
| <> encodeAggregateType stream.aggregateType | ||
| <> encodeAggregateId stream.aggregateId | ||
| <> maybe encodeNull encodeWord64 after | ||
|
|
||
|
|
||
| encodeCommit :: StreamIdentity -> Word64 -> [ProposedEvent] -> ByteString | ||
| encodeCommit stream expected events = | ||
| toStrictByteString $ | ||
| encodeListLen 5 | ||
| <> encodeWord 1 | ||
| <> encodeAggregateType stream.aggregateType | ||
| <> encodeAggregateId stream.aggregateId | ||
| <> encodeWord64 expected | ||
| <> encodeListLen (fromIntegral (length events)) | ||
| <> foldMap encodeProposedEvent events | ||
|
|
||
|
|
||
| encodeProposedEvent :: ProposedEvent -> Encoding | ||
| encodeProposedEvent event = | ||
| encodeListLen 3 | ||
| <> encodeEventType event.eventType | ||
| <> encodeEventVersion event.eventVersion | ||
| <> encodeBytes event.payload | ||
|
|
||
|
|
||
| decodeStoredEvents :: ByteString -> Either String [StoredEvent] | ||
| decodeStoredEvents bytes = | ||
| case deserialiseFromBytes decodeStoredEventsWire (LazyByteString.fromStrict bytes) of | ||
| Left failure -> Left (show failure) | ||
| Right (remaining, events) | ||
| | LazyByteString.null remaining -> Right events | ||
| | otherwise -> Left "trailing bytes after stored events" | ||
|
|
||
|
|
||
| decodeStoredEventsWire :: Decoder s [StoredEvent] | ||
| decodeStoredEventsWire = do | ||
| expectListLength 2 | ||
| version <- decodeWord | ||
| if version == 1 | ||
| then do | ||
| count <- decodeListLen | ||
| replicateM count decodeStoredEvent | ||
| else fail "unsupported stored-events format version" | ||
|
|
||
|
|
||
| decodeStoredEvent :: Decoder s StoredEvent | ||
| decodeStoredEvent = do | ||
| expectListLength 4 | ||
| sequence <- decodeWord64 | ||
| eventType <- EventType <$> decodeString | ||
| eventVersion <- EventVersion <$> decodeString | ||
| payload <- decodeBytes | ||
| pure StoredEvent {sequence, eventType, eventVersion, payload} | ||
|
|
||
|
|
||
| encodeAggregateType :: AggregateType -> Encoding | ||
| encodeAggregateType (AggregateType value) = encodeString value | ||
|
|
||
|
|
||
| encodeAggregateId :: AggregateId -> Encoding | ||
| encodeAggregateId (AggregateId value) = encodeString value | ||
|
|
||
|
|
||
| encodeEventType :: EventType -> Encoding | ||
| encodeEventType (EventType value) = encodeString value | ||
|
|
||
|
|
||
| encodeEventVersion :: EventVersion -> Encoding | ||
| encodeEventVersion (EventVersion value) = encodeString value | ||
|
|
||
|
|
||
| expectListLength :: Int -> Decoder s () | ||
| expectListLength expected = do | ||
| actual <- decodeListLen | ||
| if actual == expected | ||
| then pure () | ||
| else fail "unexpected CBOR list length" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| module EventSorcery.Engine.Protocol ( | ||
| AggregateId (..), | ||
| AggregateType (..), | ||
| EventType (..), | ||
| EventVersion (..), | ||
| OpenOptions (..), | ||
| ProposedEvent (..), | ||
| StoredEvent (..), | ||
| StreamIdentity (..), | ||
| ) where | ||
|
|
||
| import Data.ByteString (ByteString) | ||
| import Data.Text (Text) | ||
| import Data.Word (Word32, Word64) | ||
| import Prelude (Eq, Show) | ||
|
|
||
|
|
||
| newtype AggregateType = AggregateType Text | ||
| deriving newtype (Eq, Show) | ||
|
|
||
|
|
||
| newtype AggregateId = AggregateId Text | ||
| deriving newtype (Eq, Show) | ||
|
|
||
|
|
||
| newtype EventType = EventType Text | ||
| deriving newtype (Eq, Show) | ||
|
|
||
|
|
||
| newtype EventVersion = EventVersion Text | ||
| deriving newtype (Eq, Show) | ||
|
|
||
|
|
||
| data OpenOptions = OpenOptions | ||
| { path :: Text | ||
| , busyTimeoutMilliseconds :: Word64 | ||
| , poolSize :: Word32 | ||
| , runtimeThreads :: Word32 | ||
| } | ||
| deriving stock (Eq, Show) | ||
|
|
||
|
|
||
| data StreamIdentity = StreamIdentity | ||
| { aggregateType :: AggregateType | ||
| , aggregateId :: AggregateId | ||
| } | ||
| deriving stock (Eq, Show) | ||
|
|
||
|
|
||
| data ProposedEvent = ProposedEvent | ||
| { eventType :: EventType | ||
| , eventVersion :: EventVersion | ||
| , payload :: ByteString | ||
| } | ||
| deriving stock (Eq, Show) | ||
|
|
||
|
|
||
| data StoredEvent = StoredEvent | ||
| { sequence :: Word64 | ||
| , eventType :: EventType | ||
| , eventVersion :: EventVersion | ||
| , payload :: ByteString | ||
| } | ||
| deriving stock (Eq, Show) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
Consider newtypes for same-typed positional fields to prevent mix-ups.
StreamIdentity(aggregateType/aggregateId), andProposedEvent/StoredEvent(eventType/eventVersion) each pair twoTextfields of different domain meaning. Positional construction likeStreamIdentity "account" "one"(seen inCodecSpec.hs) offers the compiler no protection against swapped arguments. Since this is the first version of the wire contract, introducing lightweightnewtype AggregateType,newtype AggregateId,newtype EventType,newtype EventVersionnow (withderiving newtype (Eq, Show)) would close this footgun before more call sites accumulate.🤖 Prompt for AI Agents
Source: Path instructions