Skip to content

Commit

Permalink
fix: sort path templates, ignore useless templates (#231)
Browse files Browse the repository at this point in the history
Co-authored-by: Summer Ji <[email protected]>
  • Loading branch information
alexander-fenster and summer-ji-eng authored Feb 6, 2020
1 parent 5621dc8 commit 401e3b5
Show file tree
Hide file tree
Showing 5 changed files with 546 additions and 550 deletions.
16 changes: 15 additions & 1 deletion typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,21 @@ function augmentService(
}
}
}
augmentedService.pathTemplates = Object.values(uniqueResources).sort();
augmentedService.pathTemplates = Object.values(uniqueResources).sort(
(resourceA, resourceB) => {
// Path templates names can be cased differently
// (e.g. 'Project' and 'project_deidentify_template'),
// so we use camel case for comparison.
if (resourceA.name.toCamelCase() < resourceB.name.toCamelCase()) {
return -1;
}
if (resourceA.name.toCamelCase() > resourceB.name.toCamelCase()) {
return 1;
}
return 0;
}
);
console.warn(augmentedService.pathTemplates);
return augmentedService;
}

Expand Down
12 changes: 10 additions & 2 deletions typescript/src/schema/resourceDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,22 @@ export class ResourceDatabase {
params,
resource
);
this.patterns[patterns?.[0]] = resourceDescriptor;
this.types[resourceDescriptor.type!] = resourceDescriptor;
// We ignore resources with no parameters (e.g. pattern = '*').
if (params.length > 0) {
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);
// We ignore resources with no parameters (e.g. pattern = '*').
if (params.length === 0) {
continue;
}
const name = params.join('_');
let resourceDescriptor: ResourceDescriptor = {
name,
Expand Down Expand Up @@ -177,6 +184,7 @@ export class ResourceDatabase {
params = params.map(p => p.replace(/{([a-zA-Z_]+).*/, '$1'));
return params;
}

private getResourceDescriptor(
name: string,
params: string[],
Expand Down
Loading

0 comments on commit 401e3b5

Please sign in to comment.