Skip to content

Commit

Permalink
fix: do not try to generate code for common protos (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster committed Oct 29, 2019
1 parent ef6036c commit 105ecd1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as plugin from '../../../pbjs-genfiles/plugin';
import * as fs from 'fs';
import * as path from 'path';

import { Naming } from './naming';
import { Proto, MessagesMap, ResourceDescriptor, ResourceMap } from './proto';
import { fstat } from 'fs-extra';

const googleGaxLocation = path.dirname(require.resolve('google-gax'));
const gaxProtosLocation = path.join(googleGaxLocation, '..', '..', 'protos');

export interface ProtosMap {
[filename: string]: Proto;
Expand Down Expand Up @@ -31,6 +35,7 @@ export class API {
// parse resource map to Proto constructor
this.protos = fileDescriptors
.filter(fd => fd.name)
.filter(fd => !fs.existsSync(path.join(gaxProtosLocation, fd.name!)))
.reduce(
(map, fd) => {
map[fd.name!] = new Proto(
Expand Down
39 changes: 39 additions & 0 deletions typescript/test/unit/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { API } from '../../src/schema/api';
import * as plugin from '../../../pbjs-genfiles/plugin';
import * as assert from 'assert';

describe('schema/api.ts', () => {
it('should construct an API object and return list of protos', () => {
const fd = new plugin.google.protobuf.FileDescriptorProto();
fd.name = 'google/cloud/test/v1/test.proto';
fd.package = 'google.cloud.test.v1';
fd.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
const api = new API(
[fd],
'google.cloud.test.v1',
new plugin.grpc.service_config.ServiceConfig()
);
assert.deepStrictEqual(api.filesToGenerate, [
'google/cloud/test/v1/test.proto',
]);
});

it('should not return common protos in the list of protos', () => {
const fd1 = new plugin.google.protobuf.FileDescriptorProto();
fd1.name = 'google/cloud/test/v1/test.proto';
fd1.package = 'google.cloud.test.v1';
fd1.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
const fd2 = new plugin.google.protobuf.FileDescriptorProto();
fd2.name = 'google/longrunning/operation.proto';
fd2.package = 'google.longrunning';
fd2.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
const api = new API(
[fd1, fd2],
'google.cloud.test.v1',
new plugin.grpc.service_config.ServiceConfig()
);
assert.deepStrictEqual(api.filesToGenerate, [
'google/cloud/test/v1/test.proto',
]);
});
});

0 comments on commit 105ecd1

Please sign in to comment.