Skip to content

Commit

Permalink
Merge pull request #1745 from murgatroid99/proto-loader_generate_serv…
Browse files Browse the repository at this point in the history
…ice_definition

proto-loader: generator: add specific service definition interfaces
  • Loading branch information
murgatroid99 committed Apr 14, 2021
2 parents aefc9d1 + e7dccd6 commit bf2e5cb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
24 changes: 21 additions & 3 deletions packages/proto-loader/bin/proto-loader-gen-types.ts
Expand Up @@ -126,7 +126,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
} else if (dependency instanceof Protobuf.Enum) {
importedTypes = `${typeInterfaceName}`;
} else if (dependency instanceof Protobuf.Service) {
importedTypes = `${typeInterfaceName}Client`;
importedTypes = `${typeInterfaceName}Client, ${typeInterfaceName}Definition`;
} else {
throw new Error('Invalid object passed to getImportLine');
}
Expand All @@ -136,7 +136,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
} else if (dependency instanceof Protobuf.Enum) {
importedTypes = `${dependency.name} as ${typeInterfaceName}`;
} else if (dependency instanceof Protobuf.Service) {
importedTypes = `${dependency.name}Client as ${typeInterfaceName}Client`;
importedTypes = `${dependency.name}Client as ${typeInterfaceName}Client, ${dependency.name}Definition as ${typeInterfaceName}Definition`;
} else {
throw new Error('Invalid object passed to getImportLine');
}
Expand Down Expand Up @@ -541,11 +541,25 @@ function generateServiceHandlerInterface(formatter: TextFormatter, serviceType:
formatter.writeLine('}');
}

function generateServiceDefinitionInterface(formatter: TextFormatter, serviceType: Protobuf.Service) {
formatter.writeLine(`export interface ${serviceType.name}Definition {`);
formatter.indent();
for (const methodName of Object.keys(serviceType.methods).sort()) {
const method = serviceType.methods[methodName];
const requestType = getTypeInterfaceName(method.resolvedRequestType!);
const responseType = getTypeInterfaceName(method.resolvedResponseType!);
formatter.writeLine(`${methodName}: MethodDefinition<${requestType}, ${responseType}, ${requestType}__Output, ${responseType}__Output>`);
}
formatter.unindent();
formatter.writeLine('}')
}

function generateServiceInterfaces(formatter: TextFormatter, serviceType: Protobuf.Service, options: GeneratorOptions) {
formatter.writeLine(`// Original file: ${serviceType.filename}`);
formatter.writeLine('');
const grpcImportPath = options.grpcLib.startsWith('.') ? getPathToRoot(serviceType) + options.grpcLib : options.grpcLib;
formatter.writeLine(`import type * as grpc from '${grpcImportPath}'`);
formatter.writeLine(`import type { MethodDefinition } from '@grpc/proto-loader'`)
const dependencies: Set<Protobuf.Type> = new Set<Protobuf.Type>();
for (const method of serviceType.methodsArray) {
dependencies.add(method.resolvedRequestType!);
Expand All @@ -560,6 +574,9 @@ function generateServiceInterfaces(formatter: TextFormatter, serviceType: Protob
formatter.writeLine('');

generateServiceHandlerInterface(formatter, serviceType, options);
formatter.writeLine('');

generateServiceDefinitionInterface(formatter, serviceType);
}

function generateServiceImports(formatter: TextFormatter, namespace: Protobuf.NamespaceBase, options: GeneratorOptions) {
Expand All @@ -577,7 +594,8 @@ function generateSingleLoadedDefinitionType(formatter: TextFormatter, nested: Pr
if (options.includeComments) {
formatComment(formatter, nested.comment);
}
formatter.writeLine(`${nested.name}: SubtypeConstructor<typeof grpc.Client, ${getTypeInterfaceName(nested)}Client> & { service: ServiceDefinition }`)
const typeInterfaceName = getTypeInterfaceName(nested);
formatter.writeLine(`${nested.name}: SubtypeConstructor<typeof grpc.Client, ${typeInterfaceName}Client> & { service: ${typeInterfaceName}Definition }`);
} else if (nested instanceof Protobuf.Enum) {
formatter.writeLine(`${nested.name}: EnumTypeDefinition`);
} else if (nested instanceof Protobuf.Type) {
Expand Down
8 changes: 4 additions & 4 deletions packages/proto-loader/golden-generated/echo.ts
@@ -1,8 +1,8 @@
import type * as grpc from '@grpc/grpc-js';
import type { ServiceDefinition, EnumTypeDefinition, MessageTypeDefinition } from '@grpc/proto-loader';

import type { OperationsClient as _google_longrunning_OperationsClient } from './google/longrunning/Operations';
import type { EchoClient as _google_showcase_v1beta1_EchoClient } from './google/showcase/v1beta1/Echo';
import type { OperationsClient as _google_longrunning_OperationsClient, OperationsDefinition as _google_longrunning_OperationsDefinition } from './google/longrunning/Operations';
import type { EchoClient as _google_showcase_v1beta1_EchoClient, EchoDefinition as _google_showcase_v1beta1_EchoDefinition } from './google/showcase/v1beta1/Echo';

type SubtypeConstructor<Constructor extends new (...args: any) => any, Subtype> = {
new(...args: ConstructorParameters<Constructor>): Subtype;
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface ProtoGrpcType {
* returns long-running operations should implement the `Operations` interface
* so developers can have a consistent client experience.
*/
Operations: SubtypeConstructor<typeof grpc.Client, _google_longrunning_OperationsClient> & { service: ServiceDefinition }
Operations: SubtypeConstructor<typeof grpc.Client, _google_longrunning_OperationsClient> & { service: _google_longrunning_OperationsDefinition }
WaitOperationRequest: MessageTypeDefinition
}
protobuf: {
Expand Down Expand Up @@ -78,7 +78,7 @@ export interface ProtoGrpcType {
* paginated calls. Set the 'showcase-trailer' metadata key on any method
* to have the values echoed in the response trailers.
*/
Echo: SubtypeConstructor<typeof grpc.Client, _google_showcase_v1beta1_EchoClient> & { service: ServiceDefinition }
Echo: SubtypeConstructor<typeof grpc.Client, _google_showcase_v1beta1_EchoClient> & { service: _google_showcase_v1beta1_EchoDefinition }
EchoRequest: MessageTypeDefinition
EchoResponse: MessageTypeDefinition
ExpandRequest: MessageTypeDefinition
Expand Down
@@ -1,6 +1,7 @@
// Original file: deps/googleapis/google/longrunning/operations.proto

import type * as grpc from '@grpc/grpc-js'
import type { MethodDefinition } from '@grpc/proto-loader'
import type { CancelOperationRequest as _google_longrunning_CancelOperationRequest, CancelOperationRequest__Output as _google_longrunning_CancelOperationRequest__Output } from '../../google/longrunning/CancelOperationRequest';
import type { DeleteOperationRequest as _google_longrunning_DeleteOperationRequest, DeleteOperationRequest__Output as _google_longrunning_DeleteOperationRequest__Output } from '../../google/longrunning/DeleteOperationRequest';
import type { Empty as _google_protobuf_Empty, Empty__Output as _google_protobuf_Empty__Output } from '../../google/protobuf/Empty';
Expand Down Expand Up @@ -230,3 +231,11 @@ export interface OperationsHandlers extends grpc.UntypedServiceImplementation {
WaitOperation: grpc.handleUnaryCall<_google_longrunning_WaitOperationRequest__Output, _google_longrunning_Operation>;

}

export interface OperationsDefinition {
CancelOperation: MethodDefinition<_google_longrunning_CancelOperationRequest, _google_protobuf_Empty, _google_longrunning_CancelOperationRequest__Output, _google_protobuf_Empty__Output>
DeleteOperation: MethodDefinition<_google_longrunning_DeleteOperationRequest, _google_protobuf_Empty, _google_longrunning_DeleteOperationRequest__Output, _google_protobuf_Empty__Output>
GetOperation: MethodDefinition<_google_longrunning_GetOperationRequest, _google_longrunning_Operation, _google_longrunning_GetOperationRequest__Output, _google_longrunning_Operation__Output>
ListOperations: MethodDefinition<_google_longrunning_ListOperationsRequest, _google_longrunning_ListOperationsResponse, _google_longrunning_ListOperationsRequest__Output, _google_longrunning_ListOperationsResponse__Output>
WaitOperation: MethodDefinition<_google_longrunning_WaitOperationRequest, _google_longrunning_Operation, _google_longrunning_WaitOperationRequest__Output, _google_longrunning_Operation__Output>
}
@@ -1,6 +1,7 @@
// Original file: deps/gapic-showcase/schema/google/showcase/v1beta1/echo.proto

import type * as grpc from '@grpc/grpc-js'
import type { MethodDefinition } from '@grpc/proto-loader'
import type { BlockRequest as _google_showcase_v1beta1_BlockRequest, BlockRequest__Output as _google_showcase_v1beta1_BlockRequest__Output } from '../../../google/showcase/v1beta1/BlockRequest';
import type { BlockResponse as _google_showcase_v1beta1_BlockResponse, BlockResponse__Output as _google_showcase_v1beta1_BlockResponse__Output } from '../../../google/showcase/v1beta1/BlockResponse';
import type { EchoRequest as _google_showcase_v1beta1_EchoRequest, EchoRequest__Output as _google_showcase_v1beta1_EchoRequest__Output } from '../../../google/showcase/v1beta1/EchoRequest';
Expand Down Expand Up @@ -189,3 +190,13 @@ export interface EchoHandlers extends grpc.UntypedServiceImplementation {
Wait: grpc.handleUnaryCall<_google_showcase_v1beta1_WaitRequest__Output, _google_longrunning_Operation>;

}

export interface EchoDefinition {
Block: MethodDefinition<_google_showcase_v1beta1_BlockRequest, _google_showcase_v1beta1_BlockResponse, _google_showcase_v1beta1_BlockRequest__Output, _google_showcase_v1beta1_BlockResponse__Output>
Chat: MethodDefinition<_google_showcase_v1beta1_EchoRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_EchoRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
Collect: MethodDefinition<_google_showcase_v1beta1_EchoRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_EchoRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
Echo: MethodDefinition<_google_showcase_v1beta1_EchoRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_EchoRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
Expand: MethodDefinition<_google_showcase_v1beta1_ExpandRequest, _google_showcase_v1beta1_EchoResponse, _google_showcase_v1beta1_ExpandRequest__Output, _google_showcase_v1beta1_EchoResponse__Output>
PagedExpand: MethodDefinition<_google_showcase_v1beta1_PagedExpandRequest, _google_showcase_v1beta1_PagedExpandResponse, _google_showcase_v1beta1_PagedExpandRequest__Output, _google_showcase_v1beta1_PagedExpandResponse__Output>
Wait: MethodDefinition<_google_showcase_v1beta1_WaitRequest, _google_longrunning_Operation, _google_showcase_v1beta1_WaitRequest__Output, _google_longrunning_Operation__Output>
}
2 changes: 1 addition & 1 deletion packages/proto-loader/package.json
@@ -1,6 +1,6 @@
{
"name": "@grpc/proto-loader",
"version": "0.6.0",
"version": "0.6.1",
"author": "Google Inc.",
"contributors": [
{
Expand Down
6 changes: 3 additions & 3 deletions packages/proto-loader/src/index.ts
Expand Up @@ -115,14 +115,14 @@ export interface EnumTypeDefinition extends ProtobufTypeDefinition {
format: 'Protocol Buffer 3 EnumDescriptorProto';
}

export interface MethodDefinition<RequestType, ResponseType> {
export interface MethodDefinition<RequestType, ResponseType, OutputRequestType=RequestType, OutputResponseType=ResponseType> {
path: string;
requestStream: boolean;
responseStream: boolean;
requestSerialize: Serialize<RequestType>;
responseSerialize: Serialize<ResponseType>;
requestDeserialize: Deserialize<RequestType>;
responseDeserialize: Deserialize<ResponseType>;
requestDeserialize: Deserialize<OutputRequestType>;
responseDeserialize: Deserialize<OutputResponseType>;
originalName?: string;
requestType: MessageTypeDefinition;
responseType: MessageTypeDefinition;
Expand Down

0 comments on commit bf2e5cb

Please sign in to comment.