Skip to content
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

Allow unknown tags, defaulting to tagged base #649

Open
TLouf opened this issue Mar 2, 2024 · 0 comments
Open

Allow unknown tags, defaulting to tagged base #649

TLouf opened this issue Mar 2, 2024 · 0 comments

Comments

@TLouf
Copy link

TLouf commented Mar 2, 2024

Description

I'm dealing with line-delimited JSON files in which one field can be of multiple types, which all have very different structures. So I naturally defined a base Struct to parse all of these, doing something like:

class BaseType(msgspec.Struct, tag_field="tag"):
    pass

class InterestingSubType(BaseType):
    id: int
	...

However, I'm only interested in parsing a few of these types, so I end up having many

class NotInterestingSubType(BaseType):
    pass

and in the very first parent class:

class ObjectToParse(msgpec.Struct):
    ...
	field: InterestingSubType | NotInterestingSubType | ....

To avoid all this, I would have enjoyed being able to do just

class ObjectToParse(msgpec.Struct):
    ...
	field: InterestingSubType | BaseType

to mean that if the interesting tagged type is found, parse it as I described it, otherwise just return an empty BaseType to signal there was one of the NotInterestingSubTypes there. Also, in the same way that you can forbid unknown fields, you could also forbid unknown tags to get the current behavior.

I hope I managed to convey my idea clearly. Also I must say I've used msgspec only a little until now so I'm not 100% sure that's not possible already, but I've looked and tested quite extensively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant