jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.45k stars 76 forks source link

Add support for having multiple `tag_fields` #603

Open serozhenka opened 11 months ago

serozhenka commented 11 months ago

Description

It would be beneficial to differentiate struct uniqueness by multiple fields. A good use case example is message versioning. You might have multiple message types and multiple versions associated with it.

Usage example:

class MessageType1V1(Struct, tag=("type1", "v1"), tag_field=("type", "version")):
     foo: int
     bar: str

... multiple message type1 versions

class MessageType2V1(Struct, tag=("type2", "v1"), tag_field=("type", "version")):
     some_other_foo: int
     some_other_bar: str

... multiple message type2 versions

Adding to this it would be good to add an option to preserve tag fields after decoding, so we can differentiate messages based on their attributes (type and version in this case) instead of a type.