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 5 commits
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
6 changes: 6 additions & 0 deletions packages/proto-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ proto-loader-gen-types.js [options] filenames...
Options:
--help Show help [boolean]
--version Show version number [boolean]
--inputBranded Output property for branded type for "permissive"
types with fullName of the Message as its value
[boolean] [default: false]
--outputBranded Output property for branded type for "restricted"
types with fullName of the Message as its value
[boolean] [default: false]
--keepCase Preserve the case of field names
[boolean] [default: false]
--longs The type that should be used to output 64 bit integer
Expand Down
26 changes: 26 additions & 0 deletions packages/proto-loader/bin/proto-loader-gen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const useNameFmter = ({outputTemplate, inputTemplate}: GeneratorOptions) => {
};
}

const typeBrandHint = `This field is a type brand and is not populated at runtime. Instances of this type should be created using type assertions.
https://github.com/grpc/grpc-node/pull/2281`;

type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
includeDirs?: string[];
grpcLib: string;
Expand All @@ -45,6 +48,8 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
includeComments?: boolean;
inputTemplate: string;
outputTemplate: string;
inputBranded: boolean;
outputBranded: boolean;
}

class TextFormatter {
Expand Down Expand Up @@ -178,6 +183,11 @@ function formatComment(formatter: TextFormatter, comment?: string | null) {
formatter.writeLine(' */');
}

function formatTypeBrand(formatter: TextFormatter, messageType: Protobuf.Type) {
formatComment(formatter, typeBrandHint);
formatter.writeLine(`__type: '${messageType.fullName}'`);
}

// GENERATOR FUNCTIONS

function getTypeNamePermissive(fieldType: string, resolvedType: Protobuf.Type | Protobuf.Enum | null, repeated: boolean, map: boolean, options: GeneratorOptions): string {
Expand Down Expand Up @@ -263,6 +273,9 @@ function generatePermissiveMessageInterface(formatter: TextFormatter, messageTyp
}
formatter.writeLine(`'${oneof.name}'?: ${typeString};`);
}
if (options.inputBranded) {
formatTypeBrand(formatter, messageType);
}
formatter.unindent();
formatter.writeLine('}');
}
Expand Down Expand Up @@ -383,6 +396,9 @@ function generateRestrictedMessageInterface(formatter: TextFormatter, messageTyp
formatter.writeLine(`'${oneof.name}': ${typeString};`);
}
}
if (options.outputBranded) {
formatTypeBrand(formatter, messageType);
}
formatter.unindent();
formatter.writeLine('}');
}
Expand Down Expand Up @@ -848,6 +864,14 @@ async function runScript() {
default: return undefined;
}
})
.option('inputBranded', {
boolean: true,
default: false,
})
.option('outputBranded', {
boolean: true,
default: false,
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is another option we can take, apart from just leaving them optional.

By using option method, we can avoid excessive type instantiation error, still providing the same parsing behavior.

Unfortunately, this changes the order of help descriptions, lifting inputBranded and outputBranded to the top.

Since these options are not significantly important options, I guess they should remain at the bottom.

This can be fixed if all the other options are declared using option as well, but I am not sure if this breaks the current style convention.

Copy link
Member

Choose a reason for hiding this comment

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

Changing everything else to use option seems like a good way to handle this.

Copy link
Contributor Author

@LunaTK LunaTK Dec 2, 2022

Choose a reason for hiding this comment

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

Fixed it.

Since the printed order has not been exactly aligned with declaration order in the code and I don't know how exactly they were decided,
I adjusted declaration order a bit to preserve the original printed order.

.alias({
includeDirs: 'I',
outDir: 'O',
Expand All @@ -868,6 +892,8 @@ 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)
.usage('$0 [options] filenames...')
Expand Down