question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested Structs how to.

See original GitHub issue

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):

[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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
goodboycommented, Jun 7, 2021

Would you want to submit a PR?

Yah more then happy to since I already have a real-world use case: IPC messages which can describe their “value” as a Struct.

1reaction
jcristcommented, Jun 7, 2021

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 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found