Skip to content

Commit

Permalink
feat: support multi-pattern resources (#213)
Browse files Browse the repository at this point in the history
* support multi pattern resources, unit test, end-to-end test

* lint

* disable doc test for dlp, taking so much time

* feedback

* clean
  • Loading branch information
xiaozhenliu-gg5 authored and alexander-fenster committed Jan 28, 2020
1 parent 53520fa commit c70d1d4
Show file tree
Hide file tree
Showing 24 changed files with 9,980 additions and 26 deletions.
21 changes: 21 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
cp -r typescript/test/protos .test-out-keymanager
cp -r typescript/test/protos .test-out-translate
cp -r typescript/test/protos .test-out-texttospeech
cp -r typescript/test/protos .test-out-dlp
- persist_to_workspace:
root: /tmp
paths:
Expand Down Expand Up @@ -121,6 +122,23 @@ jobs:
npm run system-test
npm run docs
npm run docs-test
dlpLibTest:
docker:
- image: circleci/node:10-browsers
steps:
- checkout
- attach_workspace:
at: workspace
- run:
name: Run unit tests, system tests, jsdoc generation, and gts fix of the generated dlp library
command: |
cd workspace/workspace/gapic-generator-typescript/.test-out-dlp
npm install
npm test
npm run fix
npm run compile
npm run system-test
npm run docs
ttsLibTest:
docker:
- image: circleci/node:10-browsers
Expand Down Expand Up @@ -156,6 +174,9 @@ workflows:
- kmsLibTest:
requires:
- testGenerator
- dlpLibTest:
requires:
- testGenerator
- translateLibTest:
requires:
- testGenerator
Expand Down
12 changes: 6 additions & 6 deletions templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export class {{ service.name }}Client {
{%- for template in service.pathTemplates %}

/**
* Return a fully-qualified {{ template.name.toLowerCase() }} resource name string.
* Return a fully-qualified {{ template.name.toCamelCase() }} resource name string.
*
{%- for param in template.params %}
* @param {string} {{ param }}
Expand All @@ -585,7 +585,7 @@ export class {{ service.name }}Client {
{{-paramJoiner()-}}{{ param.toCamelCase() }}:string
{%- endfor -%}
) {
return this._pathTemplates.{{ template.name.toLowerCase() }}PathTemplate.render({
return this._pathTemplates.{{ template.name.toCamelCase() }}PathTemplate.render({
{%- for param in template.params %}
{{ param }}: {{ param.toCamelCase() }},
{%- endfor %}
Expand All @@ -594,14 +594,14 @@ export class {{ service.name }}Client {
{%- for param in template.params %}

/**
* Parse the {{ param }} from {{ template.name }} resource.
* Parse the {{ param }} from {{ template.name.toPascalCase() }} resource.
*
* @param {string} {{ template.name.toLowerCase() }}Name
* @param {string} {{ template.name.toCamelCase() }}Name
* A fully-qualified path representing {{ template.name }} resource.
* @returns {string} A string representing the {{ param }}.
*/
match{{ param.capitalize() }}From{{ template.name }}Name({{ template.name.toLowerCase() }}Name: string) {
return this._pathTemplates.{{ template.name.toLowerCase() }}PathTemplate.match({{ template.name.toLowerCase() }}Name).{{ param }};
match{{ param.toPascalCase() }}From{{ template.name.toPascalCase() }}Name({{ template.name.toCamelCase() }}Name: string) {
return this._pathTemplates.{{ template.name.toCamelCase() }}PathTemplate.match({{ template.name.toCamelCase() }}Name).{{ param }};
}
{%- endfor %}
{%- endfor %}
Expand Down
13 changes: 10 additions & 3 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,19 @@ function augmentService(
);

// 2. If this resource reference has .type, we should have a known resource with this type.
const resource = resourceDatabase.getResourceByType(
const resourceByType = resourceDatabase.getResourceByType(
resourceReference?.type,
errorLocation
);
if (resource) {
uniqueResources[resource.name] = resource;
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 = resourceDatabase.getResourceByPattern(
pattern
);
if (!resourceByPattern) continue;
uniqueResources[resourceByPattern.name] = resourceByPattern;
}
}
}
Expand Down
69 changes: 52 additions & 17 deletions typescript/src/schema/resourceDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,47 @@ export class ResourceDatabase {
}
return;
}
const name = arr[1];

const pattern = resource.pattern;
if (!pattern?.[0]) {
const patterns = resource.pattern;
if (!patterns?.[0]) {
if (errorLocation) {
console.warn(
`Warning: ${errorLocation} refers to a resource which does not have a proper pattern: ${resource}`
);
}
return;
}
const params = pattern[0].match(/{[a-zA-Z_]+(?:=.*?)?}/g) || [];
for (let i = 0; i < params.length; i++) {
params[i] = params[i].replace(/{([a-zA-Z_]+).*/, '$1');
}

const resourceDescriptor: ResourceDescriptor = Object.assign(
{
const multiPattern = patterns!.length > 1;
// only one pattern exists for the resource.
if (!multiPattern) {
const name = arr![1];
const params = this.getParams(patterns![0]);
const resourceDescriptor = this.getResourceDescriptor(
name,
params,
},
resource
);

this.patterns[pattern?.[0]] = resourceDescriptor;
this.types[resourceDescriptor.type!] = resourceDescriptor;
resource
);
this.patterns[patterns?.[0]] = resourceDescriptor;
this.types[resourceDescriptor.type!] = resourceDescriptor;
}
// resource: {name, type, pattern: [p1, p2]}
// register resource does: in type map {type: { name, type, pattern: [p1, p2]} }
// in pattern map {p1: { name1, type, p1} , p2: { name2, type, p2}}
else {
for (const pattern of patterns!) {
const params = this.getParams(pattern);
const name = params.join('_');
let resourceDescriptor: ResourceDescriptor = {
name,
params,
pattern: [pattern],
type: resource.type,
};
this.patterns[pattern] = resourceDescriptor;
resourceDescriptor = this.getResourceDescriptor(name, params, resource);
if (this.types[resource.type]) continue;
this.types[resource.type] = resourceDescriptor;
}
}
}

getResourceByType(
Expand Down Expand Up @@ -155,4 +170,24 @@ export class ResourceDatabase {

return result;
}

private getParams(pattern: string): string[] {
let params = pattern.match(/{[a-zA-Z_]+(?:=.*?)?}/g) || [];
params = params.map(p => p.replace(/{([a-zA-Z_]+).*/, '$1'));
return params;
}
private getResourceDescriptor(
name: string,
params: string[],
resource: plugin.google.api.IResourceDescriptor
): ResourceDescriptor {
const resourceDescriptor = Object.assign(
{
name,
params,
},
resource
);
return resourceDescriptor;
}
}
Loading

0 comments on commit c70d1d4

Please sign in to comment.