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

Added force_disable_services option to protobuf-ts/plugin for disabling service metadata generation #268

Merged
6 changes: 6 additions & 0 deletions packages/plugin/src/our-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export interface InternalOptions {
readonly esLintDisable: boolean;
readonly transpileTarget: ts.ScriptTarget | undefined,
readonly transpileModule: ts.ModuleKind,
readonly forceDisableServices: boolean;
readonly addPbSuffix: boolean;
}

Expand All @@ -227,6 +228,7 @@ export function makeInternalOptions(
client_none: boolean,
client_grpc1: boolean,
add_pb_suffix: boolean,
force_disable_services: boolean;
output_typescript: boolean,
output_javascript: boolean,
output_javascript_es2015: boolean,
Expand Down Expand Up @@ -265,6 +267,7 @@ export function makeInternalOptions(
esLintDisable: false,
transpileTarget: undefined,
transpileModule: ts.ModuleKind.ES2015,
forceDisableServices: false,
addPbSuffix: false,
},
) as Writeable<InternalOptions>;
Expand Down Expand Up @@ -328,6 +331,9 @@ export function makeInternalOptions(
if (params?.add_pb_suffix) {
o.addPbSuffix = true;
}
if (params?.force_disable_services) {
o.forceDisableServices = true;
}
Comment on lines +334 to +336
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if we should set forcedServerStyle and forcedClientStyle here to be explicit, but can't think of a case the very obvious if-statement below wouldn't catch.

if (params?.output_javascript) {
o.transpileTarget = ts.ScriptTarget.ES2020;
}
Expand Down
63 changes: 36 additions & 27 deletions packages/plugin/src/protobufts-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export class ProtobuftsPlugin extends PluginBase {
"the default behaviour, this option has no effect.",
excludes: ['eslint_disable'],
},
force_disable_services: {
description: "Do not generate anything for service definitions, and \n" +
"ignore options in proto files. This is the same as setting both \n" +
"`force_server_none` and `force_client_none`, but also stops \n" +
"generating service metadata."
excludes: ['client_generic', 'client_grpc1', 'server_generic', 'server_grpc1']
},
add_pb_suffix: {
description: "Adds the suffix `_pb` to the names of all generated files. This will become the \n" +
"default behaviour in the next major release.",
Expand Down Expand Up @@ -152,12 +159,12 @@ export class ProtobuftsPlugin extends PluginBase {
client_generic: {
description: "Only applies to services that do *not* use the option `ts.client`. \n" +
"Since GENERIC_CLIENT is the default, this option has no effect.",
excludes: ['client_none', 'client_grpc1', 'force_client_none'],
excludes: ['client_none', 'client_grpc1', 'force_client_none', 'force_disable_services'],
},
client_grpc1: {
description: "Generate a client using @grpc/grpc-js (major version 1). \n" +
"Only applies to services that do *not* use the option `ts.client`." ,
excludes: ['client_none', 'client_generic', 'force_client_none'],
excludes: ['client_none', 'client_generic', 'force_client_none', 'force_disable_services'],
},
force_client_none: {
description: "Do not generate rpc clients, ignore options in proto files.",
Expand All @@ -184,13 +191,13 @@ export class ProtobuftsPlugin extends PluginBase {
"for example @protobuf-ts/grpc-backend for gRPC. \n" +
"Note that this is an experimental feature and may change with a minor release. \n" +
"Only applies to services that do *not* use the option `ts.server`.",
excludes: ['server_none', 'force_server_none'],
excludes: ['server_none', 'force_server_none', 'force_disable_services'],
},
server_grpc1: {
description: "Generate a server interface and definition for use with @grpc/grpc-js \n" +
"(major version 1). \n" +
"Only applies to services that do *not* use the option `ts.server`.",
excludes: ['server_none', 'force_server_none'],
excludes: ['server_none', 'force_server_none', 'force_disable_services'],
},
force_server_none: {
description: "Do not generate rpc servers, ignore options in proto files.",
Expand Down Expand Up @@ -312,30 +319,32 @@ export class ProtobuftsPlugin extends PluginBase {
if (DescriptorProto.is(descriptor)) {
genMessageType.generateMessageType(outMain, descriptor, optionResolver.getOptimizeMode(fileDescriptor));
}
if (ServiceDescriptorProto.is(descriptor)) {

// service type
genServiceType.generateServiceType(outMain, descriptor)

// clients
const clientStyles = optionResolver.getClientStyles(descriptor);
if (clientStyles.includes(ClientStyle.GENERIC_CLIENT)) {
genClientGeneric.generateInterface(outClientCall, descriptor);
genClientGeneric.generateImplementationClass(outClientCall, descriptor);
}
if (clientStyles.includes(ClientStyle.GRPC1_CLIENT)) {
genClientGrpc.generateInterface(outClientGrpc, descriptor);
genClientGrpc.generateImplementationClass(outClientGrpc, descriptor);
}

// servers
const serverStyles = optionResolver.getServerStyles(descriptor);
if (serverStyles.includes(ServerStyle.GENERIC_SERVER)) {
genServerGeneric.generateInterface(outServerGeneric, descriptor);
}
if (serverStyles.includes(ServerStyle.GRPC1_SERVER)) {
genServerGrpc.generateInterface(outServerGrpc, descriptor);
genServerGrpc.generateDefinition(outServerGrpc, descriptor);
if (!options.forceDisableServices) {
if (ServiceDescriptorProto.is(descriptor)) {
// service type
genServiceType.generateServiceType(outMain, descriptor);

// clients
const clientStyles = optionResolver.getClientStyles(descriptor);
if (clientStyles.includes(ClientStyle.GENERIC_CLIENT)) {
genClientGeneric.generateInterface(outClientCall, descriptor);
genClientGeneric.generateImplementationClass(outClientCall, descriptor);
}
if (clientStyles.includes(ClientStyle.GRPC1_CLIENT)) {
genClientGrpc.generateInterface(outClientGrpc, descriptor);
genClientGrpc.generateImplementationClass(outClientGrpc, descriptor);
}

// servers
const serverStyles = optionResolver.getServerStyles(descriptor);
if (serverStyles.includes(ServerStyle.GENERIC_SERVER)) {
genServerGeneric.generateInterface(outServerGeneric, descriptor);
}
if (serverStyles.includes(ServerStyle.GRPC1_SERVER)) {
genServerGrpc.generateInterface(outServerGrpc, descriptor);
genServerGrpc.generateDefinition(outServerGrpc, descriptor);
}
}
}
});
Expand Down