Open tlambert03 opened 6 months ago
Leaving Other
empty seems to work:
class Other(msgspec.Struct, tag=True):
pass
>>> msgspec.json.decode(msg, type=list[Get | Put | Other])
[Put(key='my key1', val='my val'), Get(key='my key2'), Other()]
Yes it works, but it means that you need to know ahead of time the literal string of every type name you're ever going to encounter, even if you don't care about them (and would be happy to either leave them as unstructured dicts or empty structs)
(Assume that there are many additional names, not just Other)
Sorry, I misunderstood!
Question
Hello, and thanks as always for the amazing library. I have a use case where I'm decoding a document with a huge amount of types, only a handful of which I care about. The schema uses tagged unions, and I'm hoping there is a way to to essentially ignore and/or simply leave as
dict
and objects that have an unrecognized type. As a simple example, I'd like to be able to deal with the"type": "Other"
object below:alternatives I have considered
I tried using something like
msgspec.json.Decoder(list[Get | Put | dict])
, but that results in:the only other thing I can think of is to (laboriously) define stub
Structs
for every key I ever encounter but don't care about. i.e. add:and then hope i don't encounter something later that I haven't seen before...
tips?