Skip to content

Commit

Permalink
Add option to suppress permissive types
Browse files Browse the repository at this point in the history
Fixes grpc#2839
  • Loading branch information
jdforsythe committed Oct 10, 2024
1 parent 6732205 commit cdf51b4
Showing 1 changed file with 29 additions and 16 deletions.
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
Expand Up @@ -47,6 +47,7 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
outputTemplate: string;
inputBranded: boolean;
outputBranded: boolean;
suppressPermissiveTypes: boolean;
}

class TextFormatter {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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('');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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...')
Expand Down

0 comments on commit cdf51b4

Please sign in to comment.