Skip to content

Commit

Permalink
feat!: resource generation logic fix (#225)
Browse files Browse the repository at this point in the history
* resource generation logic fix

* baseline etst

* nox gts fix

* naming

* comments

* comments everywhere for resources map

* lint
  • Loading branch information
xiaozhenliu-gg5 committed Feb 3, 2020
1 parent c74d31e commit ab80230
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
18 changes: 11 additions & 7 deletions typescript/src/schema/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -65,8 +65,8 @@ export class API {
fd,
packageName,
options.grpcServiceConfig,
resourceDatabase,
resourceDefinitionDatabase
allResourceDatabase,
resourceDatabase
);
return map;
}, {} as ProtosMap);
Expand Down Expand Up @@ -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`
);
Expand All @@ -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];
}
25 changes: 15 additions & 10 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
);
Expand All @@ -587,20 +590,20 @@ 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
);
if (!resourceByType || !resourceByType.pattern) continue;
// 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;
Expand All @@ -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 || [];
Expand Down Expand Up @@ -659,8 +664,8 @@ export class Proto {
service,
commentsMap,
grpcServiceConfig,
resourceDatabase,
resourceDefinitionDatabase
allResourceDatabase,
resourceDatabase
)
)
.reduce((map, service) => {
Expand Down
26 changes: 26 additions & 0 deletions typescript/test/testdata/dlp/src/v2/dlp_service_client.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit ab80230

Please sign in to comment.