Skip to content

Commit

Permalink
fix: remove common protos in generated proto list (#455)
Browse files Browse the repository at this point in the history
* fix proto list logic

* add unit test for apis in different dirs

* update asset baseline

Co-authored-by: Alexander Fenster <[email protected]>
  • Loading branch information
xiaozhenliu-gg5 and alexander-fenster authored May 5, 2020
1 parent 5305fcd commit 1c4bd49
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
16 changes: 1 addition & 15 deletions baselines/asset/src/v1/asset_service_proto_list.json.baseline
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
[
"../../protos/google/api/annotations.proto",
"../../protos/google/api/client.proto",
"../../protos/google/api/field_behavior.proto",
"../../protos/google/api/http.proto",
"../../protos/google/api/resource.proto",
"../../protos/google/cloud/asset/v1/asset_service.proto",
"../../protos/google/cloud/asset/v1/assets.proto",
"../../protos/google/cloud/orgpolicy/v1/orgpolicy.proto",
"../../protos/google/identity/accesscontextmanager/type/device_resources.proto",
"../../protos/google/identity/accesscontextmanager/v1/access_level.proto",
"../../protos/google/identity/accesscontextmanager/v1/access_policy.proto",
"../../protos/google/identity/accesscontextmanager/v1/service_perimeter.proto",
"../../protos/google/protobuf/any.proto",
"../../protos/google/protobuf/descriptor.proto",
"../../protos/google/protobuf/duration.proto",
"../../protos/google/protobuf/empty.proto",
"../../protos/google/protobuf/field_mask.proto",
"../../protos/google/protobuf/struct.proto",
"../../protos/google/protobuf/timestamp.proto",
"../../protos/google/rpc/status.proto",
"../../protos/google/type/expr.proto"
"../../protos/google/identity/accesscontextmanager/v1/service_perimeter.proto"
]
23 changes: 19 additions & 4 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ import {
import {BundleConfig} from 'src/bundle';
import {Options} from './naming';

const COMMON_PROTO_LIST = [
'google.api',
'google.rpc',
'google.protobuf',
'google.type',
];

interface MethodDescriptorProto
extends plugin.google.protobuf.IMethodDescriptorProto {
longRunning?: plugin.google.longrunning.IOperationInfo;
Expand Down Expand Up @@ -617,7 +624,7 @@ export class Proto {
services: ServicesMap = {};
allMessages: MessagesMap = {};
localMessages: MessagesMap = {};
fileToGenerate: boolean;
fileToGenerate = true;
// TODO: need to store metadata? address?

// allResourceDatabase: resources that defined by `google.api.resource`
Expand All @@ -634,9 +641,17 @@ export class Proto {
map[`.${parameters.fd.package!}.${message.name!}`] = message;
return map;
}, {} as MessagesMap);
this.fileToGenerate = parameters.fd.package
? parameters.fd.package.startsWith(parameters.packageName)
: false;
const protopackage = parameters.fd.package;
if (!protopackage || !protopackage.startsWith(parameters.packageName)) {
this.fileToGenerate = false;
}
if (this.fileToGenerate) {
for (const commonProto of COMMON_PROTO_LIST) {
if (protopackage?.startsWith(commonProto)) {
this.fileToGenerate = false;
}
}
}
this.services = parameters.fd.service
.filter(service => service.name)
.map(service =>
Expand Down
29 changes: 28 additions & 1 deletion typescript/test/unit/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('src/schema/api.ts', () => {
const fd3 = new plugin.google.protobuf.FileDescriptorProto();
fd3.name = 'google/iam/v1/iam_policy.proto';
fd3.package = 'google.iam.v1';
fd2.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
fd3.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
const api = new API([fd1, fd2, fd3], 'google.cloud.test.v1', {
grpcServiceConfig: new plugin.grpc.service_config.ServiceConfig(),
});
Expand All @@ -75,6 +75,33 @@ describe('src/schema/api.ts', () => {
]);
});

it('should not return common protos in the proto list', () => {
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()];
fd1.service[0].name = 'ZService';
fd1.service[0].options = {
'.google.api.defaultHost': 'hostname.example.com:443',
};
const fd2 = new plugin.google.protobuf.FileDescriptorProto();
fd2.name = 'google/api/annotations.proto';
fd2.package = 'google.api';
const fd3 = new plugin.google.protobuf.FileDescriptorProto();
fd3.name = 'google/orgpolicy/v1/orgpolicy.proto';
fd3.package = 'google.orgpolicy.v1';
const fd4 = new plugin.google.protobuf.FileDescriptorProto();
fd4.name = 'google/cloud/common_resources.proto';
fd4.package = 'google.cloud';
const api = new API([fd1, fd2, fd3, fd4], 'google', {
grpcServiceConfig: new plugin.grpc.service_config.ServiceConfig(),
});
assert.deepStrictEqual(api.filesToGenerate, [
'google/cloud/test/v1/test.proto',
'google/orgpolicy/v1/orgpolicy.proto',
]);
});

it('should return lexicographically first service name as mainServiceName', () => {
const fd1 = new plugin.google.protobuf.FileDescriptorProto();
fd1.name = 'google/cloud/test/v1/test.proto';
Expand Down

0 comments on commit 1c4bd49

Please sign in to comment.