Skip to content

Generate a _CHOICE_DISPATCH table on choice classes, so the runtime can find the option for a choice's @type #333

Description

@dschwartznyc

Feature Request

Description of Problem:

When Python reads a choice in Java's flattened format, it needs to find which option of the choice holds the value. @type names the leaf type ("example.H"), not the option (ChoiceEFGH), so for a nested choice the option can't be read from the JSON directly.

Today the runtime works this out on every read. For each option it inspects the field's annotation to find the option's class, and for a nested choice it searches every leaf beneath it, at every level. The generator already knows the full structure of each choice, but it doesn't pass that knowledge on, so the runtime has to rebuild it from annotations on every read.

When reading a choice, the runtime should be able to find the option for @type from what the generator states about the model, so that:

  • reading doesn't depend on annotations being resolved;
  • each level costs one lookup;
  • a model in which two options lead to the same @type is reported when code is generated, not when data is read.

Potential Solutions:

Generate a _CHOICE_DISPATCH: ClassVar[dict[str, str]] on every choice class, mapping each @type the choice can be read with to the Python attribute name of the option that holds it. The runtime looks up @type in the table, one level at a time.

Keys.

What the option holds Key Value
A data type The type's Rune FQN, as _fqrtn() returns it: "example.A" The option's attribute name
A basic type, an enum, or a type alias of either The Rune option name, as Java writes it: "string", "CurrencyEnum" The option's attribute name
A type alias of a data type The aliased data type's FQN The option's attribute name
A nested choice Every leaf beneath it, at any depth, by the rules above, plus the FQN of every choice beneath it, including itself. Python writes a nested choice option with its own @type when the option has metadata or is a reference The option's attribute name
  • nothing options are skipped.
  • Subtypes of an option's type aren't keys: Java requires @type to match an option exactly.
  • Keys are Rune names, without the generator's -x namespace prefix. Values are the attribute names as declared, after mangling.
  • Keys are written in sorted order, so the output is stable.
  • If two options lead to the same key, generation fails with an exception naming the choice, the key and both options. This is a DSL warning today (rune-dsl#1422) and is expected to become a parse error.

Example.

namespace example

type A:
    value string (1..1)
// ... B, C, D, E1, F, G and H, each with `value string (1..1)`

choice ChoiceAB:
    A
    B
choice ChoiceCD:
    C
    D
choice ChoiceEF:
    E1
    F
choice ChoiceGH:
    G
    H
choice ChoiceABCD:
    ChoiceAB
    ChoiceCD
choice ChoiceEFGH:
    ChoiceEF
    ChoiceGH
choice OuterChoice:
    ChoiceABCD
    ChoiceEFGH
class OuterChoice(BaseDataClass):
    _RUNE_CHOICE: ClassVar[bool] = True
    _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {
        "example.A": "ChoiceABCD",
        "example.B": "ChoiceABCD",
        "example.C": "ChoiceABCD",
        "example.ChoiceAB": "ChoiceABCD",
        "example.ChoiceABCD": "ChoiceABCD",
        "example.ChoiceCD": "ChoiceABCD",
        "example.ChoiceEF": "ChoiceEFGH",
        "example.ChoiceEFGH": "ChoiceEFGH",
        "example.ChoiceGH": "ChoiceEFGH",
        "example.D": "ChoiceABCD",
        "example.E1": "ChoiceEFGH",
        "example.F": "ChoiceEFGH",
        "example.G": "ChoiceEFGH",
        "example.H": "ChoiceEFGH",
    }

class ChoiceABCD(BaseDataClass):
    _RUNE_CHOICE: ClassVar[bool] = True
    _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {
        "example.A": "ChoiceAB",
        "example.B": "ChoiceAB",
        "example.C": "ChoiceCD",
        "example.ChoiceAB": "ChoiceAB",
        "example.ChoiceCD": "ChoiceCD",
        "example.D": "ChoiceCD",
    }

class ChoiceAB(BaseDataClass):
    _RUNE_CHOICE: ClassVar[bool] = True
    _CHOICE_DISPATCH: ClassVar[dict[str, str]] = {
        "example.A": "A",
        "example.B": "B",
    }

A choice with basic-type and enum options, choice Pick: A, CurrencyEnum, string, gets {"CurrencyEnum": "CurrencyEnum", "example.A": "A", "string": "string"}.

Runtime. finos/rune-python-runtime#75 (finos/rune-python-runtime#55) reads through the table when a choice class has one, and keeps its current search as a fallback for code generated without one. Writing is unchanged. The generator's minimum rune.runtime version moves to the release that includes #75.

Drawbacks.

  • Every imported choice class carries its table, with one entry per leaf and nested choice beneath it, so size grows with nesting depth and width. To be measured on CDM.
  • The table and the field annotations describe the same structure. A defect on either side could make them disagree.
  • Until code generated without the table is no longer supported, the runtime has two read paths.

Alternative considered. The runtime could build the same table from the annotations on the first read of each choice class, and cache it. That needs no generator change and no release coordination, and only choices that are read build a table. The generated table was preferred because:

  • the generator is the authority on the model's structure;
  • collisions are reported at generation;
  • the table can be checked in the generated code and the generator's tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions