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: support multi-pattern resources #213

Merged
merged 5 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
71 changes: 54 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.map(r => r.toPascalCase()).join('');
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
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,26 @@ export class ResourceDatabase {

return result;
}

private getParams(pattern: string): string[] {
const params = pattern.match(/{[a-zA-Z_]+(?:=.*?)?}/g) || [];
for (let i = 0; i < params.length; i++) {
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
params[i] = params[i].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