diff --git a/typescript/src/schema/api.ts b/typescript/src/schema/api.ts index 7d8f901fa..601e56d5c 100644 --- a/typescript/src/schema/api.ts +++ b/typescript/src/schema/api.ts @@ -50,7 +50,6 @@ export class API { return ( fd.package === 'google.longrunning' || fd.package === 'google.cloud' || - fd.package === 'google.cloud.location' || fd.package === 'google.protobuf' || fd.package === 'google.type' || fd.package === 'google.rpc' || @@ -63,7 +62,8 @@ export class API { fds: protos.google.protobuf.IFileDescriptorProto[] ) { let filteredProtos = fds.filter(fd => !API.isIgnoredService(fd)); - // Special case: google.iam.v1 can be either a separate service to generate, + // Special cases: google.iam.v1 or google.cloud.location + // can be either a separate service to generate, // or a dependency that should be ignored here const packages = filteredProtos.reduce((set, fd) => { set.add(fd.package!); @@ -74,6 +74,11 @@ export class API { p => p.package !== 'google.iam.v1' ); } + if (packages.size > 1 && packages.has('google.cloud.location')) { + filteredProtos = filteredProtos.filter( + p => p.package !== 'google.cloud.location' + ); + } return filteredProtos; } diff --git a/typescript/src/schema/naming.ts b/typescript/src/schema/naming.ts index fa549ef53..9cf2051c2 100644 --- a/typescript/src/schema/naming.ts +++ b/typescript/src/schema/naming.ts @@ -75,7 +75,8 @@ export class Naming { const versionIndex = segments.findIndex(segment => segment.match(versionPattern) ); - if (versionIndex === -1) { + // Special exception for location, which does not have a version + if (versionIndex === -1 && rootPackage !== 'google.cloud.location') { throw new Error( `ERROR: Cannot parse package name ${rootPackage}: version does not match ${versionPattern}.` ); @@ -88,7 +89,8 @@ export class Naming { `ERROR: Cannot parse package name ${rootPackage}: version ${version} is the first segment in the name.` ); } - const name = segments[versionIndex - 1]; + // If there is no version (in the case of location), just grab the last segment + const name = segments[versionIndex - 1] || segments[segments.length - 1]; // everything before the name is namespace const namespaces = segments.slice(0, versionIndex - 1).join('.'); diff --git a/typescript/test/unit/api.ts b/typescript/test/unit/api.ts index ee99fd574..be6bea86d 100644 --- a/typescript/test/unit/api.ts +++ b/typescript/test/unit/api.ts @@ -91,6 +91,23 @@ describe('src/schema/api.ts', () => { ]); }); + it('should be able to generate google.cloud.location alone', () => { + const fd = {} as protos.google.protobuf.FileDescriptorProto; + fd.name = 'google/cloud/location/location.proto'; + fd.package = 'google.cloud.location'; + fd.service = [{} as protos.google.protobuf.ServiceDescriptorProto]; + fd.service[0].name = 'IAMPolicy'; + fd.service[0].options = { + '.google.api.defaultHost': 'cloud.googleapis.com', + }; + const api = new API([fd], 'google.cloud.location', { + grpcServiceConfig: {} as protos.grpc.service_config.ServiceConfig, + }); + assert.deepStrictEqual(api.filesToGenerate, [ + 'google/cloud/location/location.proto', + ]); + }); + it('should not return common protos in the proto list', () => { const fd1 = {} as protos.google.protobuf.FileDescriptorProto; fd1.name = 'google/cloud/test/v1/test.proto';