Skip to content

Commit

Permalink
change how deserializer determines legacy format
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-quix committed Oct 27, 2023
1 parent e8a432a commit 2295dcd
Showing 1 changed file with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,16 @@ def __call__(self, value: bytes, ctx: SerializationContext) -> Iterable[dict]:

# Warning: headers can have multiple values for the same key, but in context
# of this function it doesn't matter because we're looking for specific keys.
if ctx.headers is None:
as_legacy = True
if as_legacy := self._is_legacy_format(value):
headers_dict, value = self._parse_legacy_format(value)
else:
as_legacy = False
headers_dict = {key: value.decode() for key, value in ctx.headers}
# Fail if the message is a part of a split
if Q_SPLITMESSAGEID_NAME in headers_dict:
raise SerializationError(
f'Detected "{Q_SPLITMESSAGEID_NAME}" header but message splitting '
f"is not supported"
)
headers_dict = {key: value.decode() for key, value in ctx.headers or {}}
# Fail if the message is a part of a split
if Q_SPLITMESSAGEID_NAME in headers_dict:
raise SerializationError(
f'Detected "{Q_SPLITMESSAGEID_NAME}" header but message splitting '
f"is not supported"
)

codec_id = headers_dict.get(QCodecId.HEADER_NAME)
if not codec_id:
Expand Down Expand Up @@ -232,15 +230,20 @@ def __call__(self, value: bytes, ctx: SerializationContext) -> Iterable[dict]:
# Pass deserialized value to the `deserialize` function
yield from self._deserializers[model_key](value=deserialized)

def _is_legacy_format(self, value: bytes):
try:
if value.startswith(b"<"):
raise SerializationError(
"Message splitting was detected in a legacy-formatted message; "
"it is not supported in the new client."
)
return value.startswith(b'{"C":') and b'"K":' in value
except AttributeError:
return False

def _parse_legacy_format(
self, value: bytes
) -> Tuple[Union[Mapping, List[Mapping]], Mapping]:
if value.startswith(b"<"):
# Instead, you could add the Q_SPLITMESSAGEID_NAME to the headers dict
raise SerializationError(
"Message splitting was detected in a legacy-formatted message; "
"it is not supported in the new client."
)
value = self._loads(value, **self._loads_kwargs)
headers = {
QCodecId.HEADER_NAME: value.pop(LegacyHeaders.Q_CODEC_ID),
Expand Down

0 comments on commit 2295dcd

Please sign in to comment.