Skip to content

Commit

Permalink
add branded option for proto-loader-gen-types
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTK committed Nov 30, 2022
1 parent 271c848 commit 6f755fe
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/proto-loader/bin/proto-loader-gen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
includeComments?: boolean;
inputTemplate: string;
outputTemplate: string;
branded: boolean;
}

class TextFormatter {
Expand Down Expand Up @@ -263,6 +264,9 @@ function generatePermissiveMessageInterface(formatter: TextFormatter, messageTyp
}
formatter.writeLine(`'${oneof.name}'?: ${typeString};`);
}
if (options.branded) {
formatter.writeLine(`__type: '${messageType.fullName}'`)
}
formatter.unindent();
formatter.writeLine('}');
}
Expand Down Expand Up @@ -383,6 +387,9 @@ function generateRestrictedMessageInterface(formatter: TextFormatter, messageTyp
formatter.writeLine(`'${oneof.name}': ${typeString};`);
}
}
if (options.branded) {
formatter.writeLine(`__type: '${messageType.fullName}'`)
}
formatter.unindent();
formatter.writeLine('}');
}
Expand Down Expand Up @@ -815,7 +822,7 @@ async function runScript() {
.string(['includeDirs', 'grpcLib'])
.normalize(['includeDirs', 'outDir'])
.array('includeDirs')
.boolean(['keepCase', 'defaults', 'arrays', 'objects', 'oneofs', 'json', 'verbose', 'includeComments'])
.boolean(['keepCase', 'defaults', 'arrays', 'objects', 'oneofs', 'json', 'verbose', 'includeComments', 'branded'])
.string(['longs', 'enums', 'bytes', 'inputTemplate', 'outputTemplate'])
.default('keepCase', false)
.default('defaults', false)
Expand All @@ -829,6 +836,7 @@ async function runScript() {
.default('bytes', 'Buffer')
.default('inputTemplate', `${templateStr}`)
.default('outputTemplate', `${templateStr}__Output`)
.default('branded', false)
.coerce('longs', value => {
switch (value) {
case 'String': return String;
Expand Down Expand Up @@ -868,6 +876,7 @@ async function runScript() {
grpcLib: 'The gRPC implementation library that these types will be used with',
inputTemplate: 'Template for mapping input or "permissive" type names',
outputTemplate: 'Template for mapping output or "restricted" type names',
branded: 'Emit property for branded type whose value is fullName of the Message',
}).demandOption(['outDir', 'grpcLib'])
.demand(1)
.usage('$0 [options] filenames...')
Expand Down

0 comments on commit 6f755fe

Please sign in to comment.