-
Notifications
You must be signed in to change notification settings - Fork 1
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
How can I use substructures? #2
Comments
I assume you want the message_length: int = built("H", lambda ctx: ctx.variable_name.sizeof()) It will count the entire subfield's length in bytes. Since it has to "virtually" pack the subfield, it could have slightly worse performance, but it shouldn't be noticeable at all. To use variable_name: WString = subfield() I guess that |
Thanks a lot. That works. About MessageType enum, yes. I'm trying to implement this protocol https://c3.ulsu.tech/protocol/latest and messages shape quite different depending on type. But I don't want to combine them in one model, to keep models simple to read. But any way, thanks for suggestion. |
You can (must) also keep models separate when using Something like: @dataclass
class Message(DataStruct):
tag_id: int = field('H', default= 1)
message_length: int = built("H", lambda ctx: ctx.body.sizeof())
message_type: MessageType = field("B")
body: Union[WString, MessageTypeA, MessageTypeB] = switch(lambda ctx: ctx.message_type)(
CommandReadVariable=(WString, subfield()),
CommandSomeType=(MessageTypeA, subfield()),
CommandSomeOtherType=(MessageTypeB, subfield()),
) "Documentation" here: |
Hello,
The PacketType is enumerator, PacketMajak and PacketMajakRsp are 2 structures representing body of the packets.
Cls is typing.Union, and issubclass will throw exception. When i remove the Union, it will correctly parse the selected packet. (either one, not both at the same time) I have found projects where you use this module. And it inspired my to try the following construct: If i rewrite the body to be list of 1 element:
It works correctly. Am i missing something obvious? I am using Python 3.12.4. |
Hi, @dataclass
@datastruct(endianness=NETWORK, padding_pattern=b"\x00")
class TlsHandshake(DataStruct):
class Type(IntEnum):
CLIENT_HELLO = 1
SERVER_HELLO = 2
CERTIFICATE = 11
type: Type = field("B")
_1: ... = padding(1)
length: int = field("H")
data: bytes | TlsHandshakeHello | TlsHandshakeCertificate = switch(
lambda ctx: ctx.type
)(
CLIENT_HELLO=(TlsHandshakeHello, subfield()),
SERVER_HELLO=(TlsHandshakeHello, subfield()),
CERTIFICATE=(TlsHandshakeCertificate, subfield()),
default=(bytes, field(lambda ctx: ctx.length)),
) On the same branch I've also published some other improvements:
Let me know if this resolves your issue 🙂 P.S.: instead of |
Thanks for the prompt response.
print(value, field_type) is "PacketMajakRsp(loadState=2, latitude=825307441, longitude=842150450) typing.Union" |
This might be hint on how to fix it. |
For now, I think you can use |
That works :) thanks! I will keep an eye open for any updates! |
Hi @robots There is also a new test suite added, which verifies most of the library's functionality, based on the real-life structures used in my other projects. It should now make using union types possible in your structure. Note that there are two (possibly) breaking changes, which are related to how |
How can I use WString as substructure for CommandReadVariable? And is it possible to get length of WString inside CommandReadVariable?
The text was updated successfully, but these errors were encountered: