Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add option to suppress permissive types #2840

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add option to suppress permissive types
Fixes #2839
  • Loading branch information
jdforsythe committed Oct 10, 2024
commit cdf51b49dbb9c8265d36276ace6618f5899c08e4
45 changes: 29 additions & 16 deletions packages/proto-loader/bin/proto-loader-gen-types.ts
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
outputTemplate: string;
inputBranded: boolean;
outputBranded: boolean;
suppressPermissiveTypes: boolean;
}

class TextFormatter {
@@ -146,7 +147,11 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
}
} else {
if (dependency instanceof Protobuf.Type || dependency instanceof Protobuf.Enum) {
importedTypes = `${inputName(dependency.name)} as ${inputName(typeInterfaceName)}, ${outputName(dependency.name)} as ${outputName(typeInterfaceName)}`;
if (options.suppressPermissiveTypes) {
importedTypes = `${outputName(dependency.name)} as ${outputName(typeInterfaceName)}`;
} else {
importedTypes = `${inputName(dependency.name)} as ${inputName(typeInterfaceName)}, ${outputName(dependency.name)} as ${outputName(typeInterfaceName)}`;
}
} else if (dependency instanceof Protobuf.Service) {
importedTypes = `${dependency.name}Client as ${typeInterfaceName}Client, ${dependency.name}Definition as ${typeInterfaceName}Definition`;
} else {
@@ -457,17 +462,21 @@ function generateMessageInterfaces(formatter: TextFormatter, messageType: Protob
for (const childType of childTypes.sort(compareName)) {
const nameOverride = getTypeInterfaceName(childType);
if (childType instanceof Protobuf.Type) {
generatePermissiveMessageInterface(formatter, childType, options, nameOverride);
formatter.writeLine('');
if (!options.suppressPermissiveTypes) {
generatePermissiveMessageInterface(formatter, childType, options, nameOverride);
formatter.writeLine('');
}
generateRestrictedMessageInterface(formatter, childType, options, nameOverride);
} else {
generateEnumInterface(formatter, childType, options, nameOverride);
}
formatter.writeLine('');
}

generatePermissiveMessageInterface(formatter, messageType, options);
formatter.writeLine('');
if (!options.suppressPermissiveTypes) {
generatePermissiveMessageInterface(formatter, messageType, options);
formatter.writeLine('');
}
generateRestrictedMessageInterface(formatter, messageType, options);
}

@@ -491,20 +500,22 @@ function generateEnumInterface(formatter: TextFormatter, enumType: Protobuf.Enum
formatter.writeLine('} as const;');

// Permissive Type
formatter.writeLine('');
if (options.includeComments) {
formatComment(formatter, enumType.comment, enumType.options);
}
formatter.writeLine(`export type ${inputName(name)} =`)
formatter.indent();
for (const key of Object.keys(enumType.values)) {
if (!options.suppressPermissiveTypes) {
formatter.writeLine('');
if (options.includeComments) {
formatComment(formatter, enumType.comments[key]);
formatComment(formatter, enumType.comment, enumType.options);
}
formatter.writeLine(`| '${key}'`);
formatter.writeLine(`| ${enumType.values[key]}`);
formatter.writeLine(`export type ${inputName(name)} =`)
formatter.indent();
for (const key of Object.keys(enumType.values)) {
if (options.includeComments) {
formatComment(formatter, enumType.comments[key]);
}
formatter.writeLine(`| '${key}'`);
formatter.writeLine(`| ${enumType.values[key]}`);
}
formatter.unindent();
}
formatter.unindent();

// Restrictive Type
formatter.writeLine('');
@@ -877,6 +888,7 @@ async function runScript() {
.option('outputTemplate', { string: true, default: `${templateStr}__Output` })
.option('inputBranded', boolDefaultFalseOption)
.option('outputBranded', boolDefaultFalseOption)
.option('suppressPermissiveTypes', boolDefaultFalseOption)
.coerce('longs', value => {
switch (value) {
case 'String': return String;
@@ -916,6 +928,7 @@ async function runScript() {
outputTemplate: 'Template for mapping output or "restricted" type names',
inputBranded: 'Output property for branded type for "permissive" types with fullName of the Message as its value',
outputBranded: 'Output property for branded type for "restricted" types with fullName of the Message as its value',
suppressPermissiveTypes: `Don't output "permissive" types`,
}).demandOption(['outDir'])
.demand(1)
.usage('$0 [options] filenames...')
Loading