-
Notifications
You must be signed in to change notification settings - Fork 81
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
Nested Structs how to. #25
Comments
Also just as a follow up, what would be super ideal looking forward to python 3.10's new structural pattern matching is supporting stuff like: from importlib import import_module
import msgspec
class Target(msgspec.Struct):
module: str
type: str
func: str
kwargs: dict
class Msg(msgpspec.Struct):
cmd: str
ipc_id: str
payload: msgspec.Struct
async for msg in recv_stream:
match:
case Msg(cmd='cmd', payload=Target(type='async_func', module=m, func=f)):
mod = import_module(m)
await _invoke(getattr(mod, f), msg.payload.kwargs)
case Msg(cmd='cmd', payload=Target(type='sync_func', module=m, func=f)):
mod = import_module(m)
_invoke_sync(getattr(mod, f), msg.payload.kwargs)
case Msg(cmd='yield'):
lookup_mem_chan(cmd.ipc_id).put_nowait(msg.payload)
case Msg(cmd='error'):
await cancel_task(msg.ipc_id) Support for |
Apologies for missing this. Nested structs are fully supported, but the types must be fully known on decode. We don't support deserializing as subclasses of a type (or into a from msgspec import Struct
class Item(Struct):
name: str
count: int
class Cart(Struct):
items: list[Item]
# both serialization and deserialization work fine
data = msgspec.encode(Cart([Item("banana", 2)]))
msg = msgspec.Decoder(Cart).decode(data) But this doesn't: from msgspec import Struct
class Item(Struct):
pass
class Apple(Item):
count: int
class Banana(Item):
count: int
class Cart(Struct):
items: list[Item]
# serialization would work fine
data = msgspec.encode(Cart([Banana(2)]))
# this would error, since there's no way to know from the serialized msg what `Item` subtype to use
msgspec.Decoder(Cart).decode(data) |
From reading the pep I believe pattern matching will work out-of-the-box for keyword arguments in structs, but not positional arguments (a small patch would fix this, I'll open an issue). And serializing enums already works fine. |
@jcrist sweet thanks for the explanation!
This was the part I was missing, of your if you're going to decode a struct you need to define how to decode it through a definition 🤦🏼. For my purposes it does seem to work well 🏄🏼 : [nav] In [14]: class Data(Struct):
...: key1: int
...: key2: float
...: key3: str
...:
[nav] In [15]: class Msg(Struct):
...: msg_type: str
...: value: Data
...: cid: str
[nav] In [16]: msg = Msg('cmd', Data(10, 10.0, '10'), 'blah')
[nav] In [17]: data = msgspec.encode(msg)
[ins] In [18]: data
Out[18]: b'\x83\xa8msg_type\xa3cmd\xa5value\x83\xa4key1\n\xa4key2\xcb@$\x00\x00\x00\x00\x00\x00\xa4key3\xa210\xa3cid\xa4blah'
[ins] In [19]: msgspec.Decoder(Msg).decode(data)
Out[19]: Msg(msg_type='cmd', value=Data(key1=10, key2=10.0, key3='10'), cid='blah')
so slickk! I think I can close this now since it was just my misunderstanding of the decoding requirements. PS: for those interested in the 3.10 pattern matching support it's #28 |
Sure thing. Would you want to submit a PR? Otherwise I'll take care of it. |
Yah more then happy to since I already have a real-world use case: IPC messages which can describe their "value" as a |
I've updated the docs with more examples, I believe this can be closed. |
😿 it was on my list for forever but I never got to it.. Great to see the docs updated and thanks again for the great support on this project 🏄🏼 |
Sorry if I missed this in the docs (or tests which I tried to go through as well) but is it possible to create and decode a
Struct
with child structs that can be recursively decoded?It seems the encoding works just fine but decoding will only work by decoding to a
dict
(the default):If there is no plan to support nested structs natively, might there be a recommended recipe for this kind of thing?
For my purposes it would be lovely to have an IPC message format where payloads in certain types of messages could also be structs (who's schema could potentially be introspected from responding function annotations).
Cheers and thanks for the super sweet lib!
The text was updated successfully, but these errors were encountered: