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

feat: support proto packages in different namespace #219

Merged
merged 7 commits into from
Jan 31, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class API {
this.naming = new Naming(
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
fileDescriptors.filter(
fd => fd.package && fd.package.startsWith(packageName)
)
),
options
);
// users specify the actual package name, if not, set it to product name.
this.publishName =
Expand Down
34 changes: 30 additions & 4 deletions typescript/src/schema/naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
import * as plugin from '../../../pbjs-genfiles/plugin';
import { commonPrefix } from '../util';

export interface Options {
grpcServiceConfig: plugin.grpc.service_config.ServiceConfig;
publishName?: string;
mainServiceName?: string;
}
export class Naming {
name: string;
namespace: string[];
version: string;
productName: string;
protoPackage: string;

constructor(fileDescriptors: plugin.google.protobuf.IFileDescriptorProto[]) {
constructor(
fileDescriptors: plugin.google.protobuf.IFileDescriptorProto[],
options?: Options
) {
let rootPackage = '';
const mainServiceName = options ? options.mainServiceName : '';
const protoPackages = fileDescriptors
.filter(fd => fd.service && fd.service.length > 0)
// LRO is an exception: it's a service but we don't generate any code for it
Expand All @@ -32,9 +42,13 @@ export class Naming {
// common prefix must either end with `.`, or be equal to at least one of
// the packages' prefix
if (!prefix.endsWith('.') && !protoPackages.some(pkg => pkg === prefix)) {
throw new Error('Protos provided have different proto packages.');
}
const rootPackage = prefix.replace(/\.$/, '');
if (mainServiceName) {
rootPackage = this.checkServiceInPackage(
protoPackages,
mainServiceName
);
} else throw new Error('Protos provided have different proto packages.');
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
} else rootPackage = prefix.replace(/\.$/, '');
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
const segments = rootPackage.split('.');
if (!segments || segments.length < 2) {
throw new Error(`Cannot parse package name ${rootPackage}.`);
Expand All @@ -61,4 +75,16 @@ export class Naming {
);
}
}

private checkServiceInPackage(
protoPackages: string[],
mainServiceName: string
) {
for (const packageName of protoPackages) {
if (packageName.indexOf(mainServiceName.toLowerCase()) !== -1) {
return packageName;
}
}
return '';
}
}
26 changes: 24 additions & 2 deletions typescript/test/unit/naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import * as assert from 'assert';
import { describe, it } from 'mocha';
import * as plugin from '../../../pbjs-genfiles/plugin';
import { Naming } from '../../src/schema/naming';
import { Naming, Options } from '../../src/schema/naming';

describe('schema/naming.ts', () => {
it('parses name correctly', () => {
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('schema/naming.ts', () => {
});
});

it('fails if no common package', () => {
it('fails if no common package, no service-name', () => {
const descriptor1 = new plugin.google.protobuf.FileDescriptorProto();
const descriptor2 = new plugin.google.protobuf.FileDescriptorProto();
descriptor1.package = 'namespace1.service.v1beta1';
Expand All @@ -125,6 +125,28 @@ describe('schema/naming.ts', () => {
});
});

it('parse name correctly if no common package, but service-name specified', () => {
const descriptor1 = new plugin.google.protobuf.FileDescriptorProto();
const descriptor2 = new plugin.google.protobuf.FileDescriptorProto();
descriptor1.package = 'namespace1.service1.v1beta1';
descriptor1.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
descriptor2.package = 'namespace2.service2.v1beta1';
descriptor2.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
const serviceConfig = new plugin.grpc.service_config.ServiceConfig();
const options: Options = {
grpcServiceConfig: serviceConfig,
mainServiceName: 'service1',
};
assert.throws(() => {
const naming = new Naming([descriptor1, descriptor2], options);
assert.strictEqual(naming.name, 'service1');
assert.strictEqual(naming.productName, 'service1');
assert.strictEqual(naming.version, 'v1beta1');
assert.strictEqual(naming.namespace, 'namespace1');
assert.strictEqual(naming.protoPackage, 'namespace1.service1.v1beta1');
});
});

it('fails if different versions', () => {
const descriptor1 = new plugin.google.protobuf.FileDescriptorProto();
const descriptor2 = new plugin.google.protobuf.FileDescriptorProto();
Expand Down