Skip to content

Implement wrapUnions#44

Open
ssonne wants to merge 3 commits intojoewood:masterfrom
ssonne:ssonne/support-wrapUnions
Open

Implement wrapUnions#44
ssonne wants to merge 3 commits intojoewood:masterfrom
ssonne:ssonne/support-wrapUnions

Conversation

@ssonne
Copy link
Copy Markdown

@ssonne ssonne commented Mar 7, 2026

This is an implementation of the wrapEnums option described in this issue: #43

The basic idea is to support generating types compatible with avsc when it requires wrapped enums.

See https://github.com/mtth/avsc/blob/91d653f72906102448a059cb81692177bb678f52/lib/types.js#L3113 for how avsc decides whether a union is ambiguous.

ssonne added 3 commits March 6, 2026 18:21
test

Indentation

No newlines

remove unused import

Support namespaces

single-key union record types

Inline namespace const

Support auto union wrapping

Update tests for auto-wrapping

Wrapping is all-or-nothing

avsc treats bytes and fixed the same for ambiguity

Add comments, and special case for null
const output: string[] = [];
if (isEnumType(schema)) convertEnum(schema, output);
else if (isRecordType(schema)) convertRecord(schema, output, opts);
else if (isRecordType(schema)) convertRecord(schema, output, opts, schema.namespace);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense at this point to introduce a "parse context" kind of object to hold output, opts, and namespace instead of passing three variables around everywhere. I didn't do this to keep this PR focused on the new feature.

export type Schema = RecordType | EnumType;
export interface ConversionOptions {
logicalTypes?: { [type: string]: string };
wrapUnions?: boolean | "always" | "never" | "auto";
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows boolean to match the corresponding flag in avsc, so that code that uses both avro-typescript and avsc can use the same definition of the flag. It could also use a tighter definition without the boolean: wrapUnions?: "always" | "never" | "auto"; or even wrapUnions?: "always" | "never" | "avsc-auto"; to make it clear that auto is targeting avsc's behavior.

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.

1 participant