Skip to content

Commit

Permalink
Added disable_service_types option to protobuf-ts/plugin for disabl…
Browse files Browse the repository at this point in the history
…ing service metadata generation (#268)
  • Loading branch information
ColinLaws authored Jun 16, 2022
1 parent dffaada commit b3f912c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
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;
}
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

0 comments on commit b3f912c

Please sign in to comment.