Skip to content

Customizing union generation #1612

Open
@abarreau

Description

@abarreau

Hello,

I'm trying to customize the way unions are generated. More specifically, my unions are annotated with a specific trait that would need to change the whole structure of the generated typescript code.

Here is my contract:

@discriminated("_type")
union MyUnion {
  package: PackageDTO
  simulation: SimulationDTO
}

Currently the UnionGenerator generates this:

export namespace MyUnion {
    export interface PackageMember {
        package: PackageDTO;
        simulation: never;
    } 

    export interface SimulationMember {
        package: never;
        simulation: SimulationDTO
    }

    // UnknownMember and Visitor interfaces + function ommited
}

What I would like to generate:

export type MyUnion = (PackageDTO & { "_type": "package" }) | (SimulationDTO & { "_type": "simulation" });

I tried to look into interceptors to see if any CodeSection would help me intercept this part of the generation, but unfortunaly the UnionGenerator has no call to TypescriptWriter.injectSection.

What would be the best approach here? Is this doable without reimplementing the whole DirectedCodegen? (The TypescriptDirectedCodegen class is protected and final which makes it painful to customize behavior and I didn't want to rewrite the whole codegen, just override the generateUnions method).

I'm willing to open a PR if that's needed to add new CodeSections in UnionGenerator.

Thanks for your help.

Metadata

Metadata

Assignees

Labels

feature-requestA feature should be added or improved.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions