-
Notifications
You must be signed in to change notification settings - Fork 482
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
add forward compatibility for byte datagram keys #3045
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 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 |
---|---|---|
|
@@ -144,27 +144,37 @@ def __init__(self, packet_type: int, rpc_id: bytes, node_id: bytes, exception_ty | |
self.response = response.decode() | ||
|
||
|
||
def decode_datagram(datagram: bytes) -> typing.Union[RequestDatagram, ResponseDatagram, ErrorDatagram]: | ||
def _decode_datagram(datagram: bytes): | ||
msg_types = { | ||
REQUEST_TYPE: RequestDatagram, | ||
RESPONSE_TYPE: ResponseDatagram, | ||
ERROR_TYPE: ErrorDatagram | ||
} | ||
|
||
primitive: typing.Dict = bdecode(datagram) | ||
if primitive[0] in [REQUEST_TYPE, ERROR_TYPE, RESPONSE_TYPE]: # pylint: disable=unsubscriptable-object | ||
datagram_type = primitive[0] # pylint: disable=unsubscriptable-object | ||
|
||
converted = { | ||
str(k).encode() if not isinstance(k, bytes) else k: v for k, v in primitive.items() | ||
} | ||
|
||
if converted[b'0'] in [REQUEST_TYPE, ERROR_TYPE, RESPONSE_TYPE]: # pylint: disable=unsubscriptable-object | ||
datagram_type = converted[b'0'] # pylint: disable=unsubscriptable-object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does more recent |
||
else: | ||
raise ValueError("invalid datagram type") | ||
datagram_class = msg_types[datagram_type] | ||
decoded = { | ||
k: primitive[i] # pylint: disable=unsubscriptable-object | ||
k: converted[str(i).encode()] # pylint: disable=unsubscriptable-object | ||
for i, k in enumerate(datagram_class.required_fields) | ||
if i in primitive # pylint: disable=unsupported-membership-test | ||
if str(i).encode() in converted # pylint: disable=unsupported-membership-test | ||
} | ||
for i, _ in enumerate(OPTIONAL_FIELDS): | ||
if i + OPTIONAL_ARG_OFFSET in primitive: | ||
decoded[i + OPTIONAL_ARG_OFFSET] = primitive[i + OPTIONAL_ARG_OFFSET] | ||
if str(i + OPTIONAL_ARG_OFFSET).encode() in converted: | ||
decoded[i + OPTIONAL_ARG_OFFSET] = converted[str(i + OPTIONAL_ARG_OFFSET).encode()] | ||
return decoded, datagram_class | ||
|
||
|
||
def decode_datagram(datagram: bytes) -> typing.Union[RequestDatagram, ResponseDatagram, ErrorDatagram]: | ||
decoded, datagram_class = _decode_datagram(datagram) | ||
return datagram_class(**decoded) | ||
|
||
|
||
|
This file contains 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
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.
you lost return type here
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.
i assume that since @jackrobison made this an internal helper function (with underscore) it didn't benefit from having the type info? i think either way is fine: having it doesn't hurt anything, not having it is probably okay in this special case
personally, my preference would be to avoid having such large type
Union
in the first place, if all of the types have the same parent class, perhaps just say it returns the parent class type? unions should be used as last resort (or when the values returned are truely different, like a function returning an int and a str, in this case it seems it returnsDatagram
s)