Nested Structs how to.
See original GitHub issueSorry 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):
[ins] In [2]: import msgspec
[nav] In [3]: class c(msgspec.Struct):
...: s: msgspec.Struct
[nav] In [4]: class b(msgspec.Struct):
...: val: int
[ins] In [5]: msgspec.encode(c(b(10)))
Out[5]: b'\x81\xa1s\x81\xa3val\n'
[ins] In [6]: msg = msgspec.encode(c(b(10)))
[ins] In [7]: msgspec.decode(msg)
Out[7]: {'s': {'val': 10}}
[ins] In [8]: msgspec.decode(msg, type=c)
Out[8]: c(s=Struct())
[ins] In [10]: msgspec.decode(msg, type=msgspec.Struct)
Out[10]: Struct()
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!
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Nested Structure in C with Examples - GeeksforGeeks
A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure ......
Read more >struct - Nested structure in c - Stack Overflow
I have to build a nested structure to store some basic information about some person (name, age, address). So I created a structure...
Read more >Nested Structure in C - Scaler Topics
The structure is a user-defined data type used to store a group of items of different data types as a single data type....
Read more >Nested structure in C - Javatpoint
Nested Structure in C. C provides us the feature of nesting one structure within another structure by using which, complex data types are...
Read more >What are nested structures in C language? - Tutorialspoint
A structure inside another structure is called nested structure. Consider the following example, struct emp{ int eno; char ename[30]; float sal; ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yah more then happy to since I already have a real-world use case: IPC messages which can describe their “value” as a
Struct
.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.