Repository navigation
[BAC-2109] Discriminated entity unions with a typed tag - #28
Closed
HolyWalley wants to merge 1 commit into
Closed
HolyWalley wants to merge 1 commit into
HolyWalley wants to merge 1 commit into
Conversation
An :entity attribute, an :array of entities and a JSON column of entities each take exactly one class. A base entity can now declare a tag attribute and a closed mapping of tag => subclass: discriminated_by :type, admin: "ThreadContext::Admin", whatsapp: "ThreadContext::Whatsapp" new, from_json and represent on the base dispatch on the tag, so every existing site typed with the base works unchanged. A subclass carries its tag as the attribute default and validates it. Resolution picks from the mapping and never constantizes anything else; a missing or unknown tag raises. The base's JSON schema is a oneOf of the subclasses with an OpenAPI discriminator, each subclass marking its tag as a one-value enum, so clients get a tagged union. Version 0.17.0. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Contributor
Author
|
Superseded: karen keeps the thread context in an envelope entity (one slot per type) instead of a discriminated union, so the gem stays as it is. See 44-pixels/backends#2345. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BAC-2109. Needed first by the Brain thread context (BAC-2091, backends), which is Admin | Whatsapp | Messenger | Instagram, and by other unions we keep working around.
What was missing. An
:entityattribute, an:arrayof entities and karen'sActiveRecord::Type::Entitycolumn all take exactly one class. The only polymorphism in the gem is the ActiveJob serializer's class-name tag, which constantizes from the open world. The usual workaround is a flat superset entity with nullable per-type fields.What this adds. STI-style unions declared once on a base entity:
ArgumentErrornaming the base and the allowed tags.new,from_jsonandrepresenton the base dispatch on the tag (string or symbol key, camelized infrom_json). Calling on a subclass builds that subclass as before.Type::Entity,Type::Arrayand the column type need no change:class_name: 'ThreadContext'andof: 'ThreadContext'just work, and a subclass instance assigned to a base-typed attribute is kept.represent, and validates it; nested validation runs the real subclass.oneOfof the subclass refs plus an OpenAPIdiscriminatorwithpropertyNameandmapping; a subclass's schema has the tag as a required string with a one-valueenum. Inline form inlines the branches and drops the mapping. Client generators get a tagged union.Not included. Attribute-level sugar like
class_name: A | B. The base-class form covers it, and a set used in one place only can get an anonymous base later.Known.
spec/active_model/entity/inspect_spec.rb:45fails on Ruby 3.4 because of theHash#inspectspacing change; CI runs 3.3 and it passes there. Untouched here.🤖 Generated with Claude Code