Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: resource generation logic fix #225

Merged
merged 10 commits into from
Feb 3, 2020
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];
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 10 additions & 10 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,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 @@ -542,7 +542,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 @@ -551,20 +551,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 @@ -588,8 +588,8 @@ export class Proto {
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 @@ -623,8 +623,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