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

Shorten $type tag used for discriminating sealed trait cases #596

Merged
merged 8 commits into from
Jul 11, 2024

Conversation

lihaoyi
Copy link
Member

@lihaoyi lihaoyi commented Jul 4, 2024

Fixes #527

This PR shortens the "$type": "foo.bar.Qux" tag that uPickle uses to distinguish between cases of a sealed trait, down to "$type": "Qux" or "$type": "bar.Qux". Because the trait is sealed, upickle is able to look at all the other cases, and picks the shortest partially-qualified name that is enough to distinguish all the different cases. As mentioned in the original issue, this makes the serialization format more compact, more robust against code changes (e.g. changes in package name), and it homogenizes the serialization format of sealed trait hierarchies and Scala 3 enums (which already use a short partially-qualified name as the $type tag)

Despite being binary compatible at the JVM level, this is a backwards incompatible change that will need to go into uPickle 4.x. To ease the rollout, the implementation is able to read both old-style fully qualified $type tags as well as new-style partially-qualified $type-tags, and whether new-style output or old-style output is generated during serialization is controlled by a objectTypeKeyWriteFullyQualified flag.

Someone migrating to uPickle 4.x from upickle 3.x can preserve the existing read/write behavior by replacing upickle.default with a CustomPickler with objectTypeKeyWriteFullyQualified = true, roll it out across their system so their entire system can read both old-style and new-style $type tags, and then flip individual components to objectTypeKeyWriteFullyQualified = false (or back to upickle.default). The system will work correctly with a mix of old-style and new-style $type tags being generated, until everything is on new-style $type tags.

@lihaoyi lihaoyi requested review from lefou and lolgab July 5, 2024 02:35
@lihaoyi lihaoyi changed the title Short type tag Shorten $type tag used for discriminating sealed trait cases Jul 5, 2024
@lihaoyi lihaoyi merged commit 961c5c7 into main Jul 11, 2024
8 checks passed
@lihaoyi lihaoyi deleted the short-type-tag branch July 11, 2024 02:50
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

Successfully merging this pull request may close these issues.

Shorten $type tag in common scenario where all cases are in same namespace
1 participant