Skip to content

Commit

Permalink
fix: multi package name in grpc client (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Mar 14, 2021
1 parent 351d5c9 commit 9e08c93
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 5 deletions.
7 changes: 3 additions & 4 deletions packages/grpc/src/comsumer/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@midwayjs/decorator';
import { credentials, loadPackageDefinition } from '@grpc/grpc-js';
import { DefaultConfig, IClientOptions } from '../interface';
import { loadProto } from '../util';
import { finePackageProto, loadProto } from '../util';
import * as camelCase from 'camelcase';
import { ILogger } from '@midwayjs/logger';
import { ClientUnaryRequest } from './type/unary-request';
Expand Down Expand Up @@ -36,9 +36,8 @@ export class GRPCClients extends Map {
loaderOptions: cfg.loaderOptions,
protoPath: cfg.protoPath,
});
const packageProto: any = loadPackageDefinition(packageDefinition)[
cfg.package
];
const allProto = loadPackageDefinition(packageDefinition);
const packageProto: any = finePackageProto(allProto, cfg.package);
for (const definition in packageDefinition) {
if (!packageDefinition[definition]['format']) {
const serviceName = definition.replace(`${cfg.package}.`, '');
Expand Down
2 changes: 1 addition & 1 deletion packages/grpc/src/comsumer/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class AutoConfiguration {
@Logger()
logger: ILogger;

async onReady(container: IMidwayContainer) {
async onReady(container: IMidwayContainer, app) {
setLogger(this.logger);
await container.getAsync('grpc:clients');
}
Expand Down
11 changes: 11 additions & 0 deletions packages/grpc/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ export const createGRPCConsumer = async <T>(
await clients.initService();
return Array.from(clients.values())[0];
};

export const finePackageProto = (allProto: any, packageName: string) => {
const packages = packageName.split('.');
let currentProto = allProto;
for (const pkg of packages) {
if (currentProto[pkg]) {
currentProto = currentProto[pkg];
}
}
return currentProto;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
imports: [
],
})
export class AutoConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IClientOptions, IClientUnaryService } from '../../../../src';

export namespace hello {
export namespace world {
export interface HelloRequest {
name: string;
}

export interface HelloReply {
message: string;
}

// The greeting service definition.
export interface Greeter {
// Sends a greeting
sayHello(data: HelloRequest): Promise<HelloReply>;
}

export interface GreeterClient {
sayHello (options?: IClientOptions): IClientUnaryService<HelloRequest, HelloReply>
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { MSProviderType, Provider, Provide, Inject, GrpcMethod } from '@midwayjs/decorator';
import { hello } from '../interface';
import { ILogger } from '@midwayjs/logger';
import { Context } from '../../../../../src';

/**
* package helloworld
* service Greeter
*/
@Provide()
@Provider(MSProviderType.GRPC, { package: 'hello.world' })
export class Greeter implements hello.world.Greeter {

@Inject()
logger: ILogger;

@Inject()
ctx: Context;

/**
* Implements the SayHello RPC method.
*/
@GrpcMethod()
async sayHello(request: hello.world.HelloRequest) {
return { message: 'Hello ' + request.name };
}
}
38 changes: 38 additions & 0 deletions packages/grpc/test/fixtures/proto/hello_world.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.hello.world";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package hello.world;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}
25 changes: 25 additions & 0 deletions packages/grpc/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../src';

import { Metadata } from '@grpc/grpc-js';
import { hello } from './fixtures/base-app-multiple-package/src/interface';

export namespace hero {
export interface HeroServiceClient {
Expand Down Expand Up @@ -239,4 +240,28 @@ describe('/test/index.test.ts', function () {

await closeApp(app);
});

it('should test multi-package service', async () => {
const app = await createServer('base-app-multiple-package', {
services: [
{
protoPath: join(__dirname, 'fixtures/proto/hello_world.proto'),
package: 'hello.world',
}
],
});

const service = await createGRPCConsumer<hello.world.GreeterClient>({
package: 'hello.world',
protoPath: join(__dirname, 'fixtures/proto/hello_world.proto'),
url: 'localhost:6565'
});

const result = await service.sayHello().sendMessage({
name: 'harry'
});

expect(result).toEqual({ message: 'Hello harry' });
await closeApp(app);
});
});

0 comments on commit 9e08c93

Please sign in to comment.