Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

proto-loader-gen-types: Support nominal typing with type branding #2281

Merged
merged 7 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/proto-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ Options:
[string] [default: "%s"]
--outputTemplate Template for mapping output or "restricted" type names
[string] [default: "%s__Output"]
--branded Emit property for branded type whose value is fullName
of the Message [boolean] [default: false]
--inputBranded Output property for branded type for "permissive"
types with fullName of the Message as its value
[boolean]
--outputBranded Output property for branded type for "restricted"
types with fullName of the Message as its value
[boolean]
LunaTK marked this conversation as resolved.
Show resolved Hide resolved
```

### Example Usage
Expand Down
13 changes: 7 additions & 6 deletions packages/proto-loader/bin/proto-loader-gen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
includeComments?: boolean;
inputTemplate: string;
outputTemplate: string;
outputBranded: boolean;
inputBranded?: boolean;
outputBranded?: boolean;
}

class TextFormatter {
Expand Down Expand Up @@ -272,9 +273,9 @@ function generatePermissiveMessageInterface(formatter: TextFormatter, messageTyp
}
formatter.writeLine(`'${oneof.name}'?: ${typeString};`);
}
// if (options.inputBranded) {
// formatTypeBrand(formatter, messageType);
// }
if (options.inputBranded) {
formatTypeBrand(formatter, messageType);
}
formatter.unindent();
formatter.writeLine('}');
}
Expand Down Expand Up @@ -830,7 +831,7 @@ async function runScript() {
.string(['includeDirs', 'grpcLib'])
.normalize(['includeDirs', 'outDir'])
.array('includeDirs')
.boolean(['keepCase', 'defaults', 'arrays', 'objects', 'oneofs', 'json', 'verbose', 'includeComments', 'outputBranded'])
.boolean(['keepCase', 'defaults', 'arrays', 'objects', 'oneofs', 'json', 'verbose', 'includeComments', 'inputBranded', 'outputBranded'])
.string(['longs', 'enums', 'bytes', 'inputTemplate', 'outputTemplate'])
.default('keepCase', false)
.default('defaults', false)
Expand All @@ -844,7 +845,6 @@ async function runScript() {
.default('bytes', 'Buffer')
.default('inputTemplate', `${templateStr}`)
.default('outputTemplate', `${templateStr}__Output`)
.default('outputBranded', false)
LunaTK marked this conversation as resolved.
Show resolved Hide resolved
.coerce('longs', value => {
switch (value) {
case 'String': return String;
Expand Down Expand Up @@ -884,6 +884,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',
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',
}).demandOption(['outDir', 'grpcLib'])
.demand(1)
Expand Down