I'm trying to write a Katai definition for the Postgres Wire Protocol V3:
The issue I've run into is that every message except for the StartupMessage
follows the same format. The StartupMessage
is shaped differently.
So I need to somehow say "The object can be one of these two types", but I'm unsure how to do it.
The layout for most messages is:
|-----------------------------------------------|
| Type | Length | (Rest of payload)
|-----------------------------------------------|
| Char | Int32 | Bytes
|-----------------------------------------------|
But for the startup message, there's no Type
char at the beginning identifying it:
|-----------------------------------------------|
| Length | Protocol Version | (Rest of payload)
|-----------------------------------------------|
| Int32 | Int32 | Bytes
|-----------------------------------------------|
So far, I've tried something like this:
meta:
id: postgres_wire_protocol_frontend_v3
file-extension: postgres_wire_protocol_frontend_v3
endian: be
seq:
- id: type
type: str
encoding: ASCII
size: 1
- id: length
type: u4
- id: body
size: length
type:
switch-on: type
cases:
'"B"': bind_message
'"E"': execute_message
'"Q"': query_message
_: startup_message
But this doesn't seem to work unfortunately =/
Is there some way to encode this in Kaitai?