From ab80230c89f24f05039d4ccc77ef151a5a5e459d Mon Sep 17 00:00:00 2001 From: Xiaozhen Liu Date: Mon, 3 Feb 2020 14:19:02 -0800 Subject: [PATCH] feat!: resource generation logic fix (#225) * resource generation logic fix * baseline etst * nox gts fix * naming * comments * comments everywhere for resources map * lint --- typescript/src/schema/api.ts | 18 ++++++++----- typescript/src/schema/proto.ts | 25 +++++++++++------- .../dlp/src/v2/dlp_service_client.ts.baseline | 26 +++++++++++++++++++ 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/typescript/src/schema/api.ts b/typescript/src/schema/api.ts index d2a8dbac8..892f1a095 100644 --- a/typescript/src/schema/api.ts +++ b/typescript/src/schema/api.ts @@ -53,7 +53,7 @@ export class API { this.publishName = options.publishName || this.naming.productName.toKebabCase(); // construct resource map - const [resourceDatabase, resourceDefinitionDatabase] = getResourceDatabase( + const [allResourceDatabase, resourceDatabase] = getResourceDatabase( fileDescriptors ); // parse resource map to Proto constructor @@ -65,8 +65,8 @@ export class API { fd, packageName, options.grpcServiceConfig, - resourceDatabase, - resourceDefinitionDatabase + allResourceDatabase, + resourceDatabase ); return map; }, {} as ProtosMap); @@ -134,13 +134,13 @@ export class API { function getResourceDatabase( fileDescriptors: plugin.google.protobuf.IFileDescriptorProto[] ): ResourceDatabase[] { - const resourceDatabase = new ResourceDatabase(); - const resourceDefinitionDatabase = new ResourceDatabase(); + const resourceDatabase = new ResourceDatabase(); // resources that defined by `google.api.resource` + const allResourceDatabase = new ResourceDatabase(); // all resources defined by `google.api.resource` or `google.api.resource_definition` for (const fd of fileDescriptors.filter(fd => fd)) { // process file-level options for (const resource of fd.options?.['.google.api.resourceDefinition'] ?? []) { - resourceDefinitionDatabase.registerResource( + allResourceDatabase.registerResource( resource as ResourceDescriptor, `file ${fd.name} resource_definition option` ); @@ -159,7 +159,11 @@ function getResourceDatabase( m?.options?.['.google.api.resource'] as ResourceDescriptor | undefined, `file ${fd.name} message ${property}` ); + allResourceDatabase.registerResource( + m?.options?.['.google.api.resource'] as ResourceDescriptor | undefined, + `file ${fd.name} message ${property}` + ); } } - return [resourceDatabase, resourceDefinitionDatabase]; + return [allResourceDatabase, resourceDatabase]; } diff --git a/typescript/src/schema/proto.ts b/typescript/src/schema/proto.ts index fbdae2fab..a62ab9fef 100644 --- a/typescript/src/schema/proto.ts +++ b/typescript/src/schema/proto.ts @@ -508,8 +508,8 @@ function augmentService( service: plugin.google.protobuf.IServiceDescriptorProto, commentsMap: CommentsMap, grpcServiceConfig: plugin.grpc.service_config.ServiceConfig, - resourceDatabase: ResourceDatabase, - resourceDefinitionDatabase: ResourceDatabase + allResourceDatabase: ResourceDatabase, + resourceDatabase: ResourceDatabase ) { const augmentedService = service as ServiceDescriptorProto; augmentedService.packageName = packageName; @@ -562,6 +562,9 @@ function augmentService( } // Build a list of resources referenced by this service + + // allResourceDatabase: resources that defined by `google.api.resource` + // resourceDatabase: all resources defined by `google.api.resource` or `google.api.resource_definition` const uniqueResources: { [name: string]: ResourceDescriptor } = {}; // Copy all resources in resourceDatabase to uniqueResources const allPatterns = resourceDatabase.patterns; @@ -578,7 +581,7 @@ function augmentService( const resourceReference = fieldDescriptor.options?.['.google.api.resourceReference']; // 1. If this resource reference has .child_type, figure out if we have any known parent resources. - const parentResources = resourceDefinitionDatabase.getParentResourcesByChildType( + const parentResources = allResourceDatabase.getParentResourcesByChildType( resourceReference?.childType, errorLocation ); @@ -587,12 +590,12 @@ function augmentService( ); // 2. If this resource reference has .type, we should have a known resource with this type, check two maps. - let resourceByType = resourceDefinitionDatabase.getResourceByType( + let resourceByType = allResourceDatabase.getResourceByType( resourceReference?.type ); resourceByType = resourceByType ?? - resourceDatabase.getResourceByType( + allResourceDatabase.getResourceByType( resourceReference?.type, errorLocation ); @@ -600,7 +603,7 @@ function augmentService( // For multi pattern resources, we look up the type first, and get the [pattern] from resource, // look up pattern map for all resources. for (const pattern of resourceByType!.pattern!) { - const resourceByPattern = resourceDefinitionDatabase.getResourceByPattern( + const resourceByPattern = allResourceDatabase.getResourceByPattern( pattern ); if (!resourceByPattern) continue; @@ -620,12 +623,14 @@ export class Proto { fileToGenerate: boolean; // TODO: need to store metadata? address? + // allResourceDatabase: resources that defined by `google.api.resource` + // resourceDatabase: all resources defined by `google.api.resource` or `google.api.resource_definition` constructor( fd: plugin.google.protobuf.IFileDescriptorProto, packageName: string, grpcServiceConfig: plugin.grpc.service_config.ServiceConfig, - resourceDatabase: ResourceDatabase, - resourceDefinitionDatabase: ResourceDatabase + allResourceDatabase: ResourceDatabase, + resourceDatabase: ResourceDatabase ) { fd.enumType = fd.enumType || []; fd.messageType = fd.messageType || []; @@ -659,8 +664,8 @@ export class Proto { service, commentsMap, grpcServiceConfig, - resourceDatabase, - resourceDefinitionDatabase + allResourceDatabase, + resourceDatabase ) ) .reduce((map, service) => { diff --git a/typescript/test/testdata/dlp/src/v2/dlp_service_client.ts.baseline b/typescript/test/testdata/dlp/src/v2/dlp_service_client.ts.baseline index f16e5bc47..22bc8ddac 100644 --- a/typescript/test/testdata/dlp/src/v2/dlp_service_client.ts.baseline +++ b/typescript/test/testdata/dlp/src/v2/dlp_service_client.ts.baseline @@ -166,6 +166,9 @@ export class DlpServiceClient { projectPathTemplate: new gaxModule.PathTemplate( 'projects/{project}' ), + organizationPathTemplate: new gaxModule.PathTemplate( + 'organizations/{organization}' + ), }; // Some of the methods on this service return "paged" results, @@ -3292,6 +3295,29 @@ export class DlpServiceClient { return this._pathTemplates.projectPathTemplate.match(projectName).project; } + /** + * Return a fully-qualified organization resource name string. + * + * @param {string} organization + * @returns {string} Resource name string. + */ + organizationPath(organization:string) { + return this._pathTemplates.organizationPathTemplate.render({ + organization: organization, + }); + } + + /** + * Parse the organization from Organization resource. + * + * @param {string} organizationName + * A fully-qualified path representing Organization resource. + * @returns {string} A string representing the organization. + */ + matchOrganizationFromOrganizationName(organizationName: string) { + return this._pathTemplates.organizationPathTemplate.match(organizationName).organization; + } + /** * Terminate the GRPC channel and close the client. *