Skip to content

Commit

Permalink
feat: respect resource annotation (#71)
Browse files Browse the repository at this point in the history
* find path template for the service

* permission

* fix

* expose params for path template

* add match function

* update baseline

* fix

* format

* feedback

* format

* template change

* extra line in baseline
  • Loading branch information
xiaozhenliu-gg5 authored and alexander-fenster committed Oct 25, 2019
1 parent 980d2aa commit c6de4c5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"codecov": "c8 --reporter=lcov mocha build/test/unit && c8 report",
"lint": "gts check",
"clean": "gts clean",
"compile-protos": "pbjs -p protos -p node_modules/google-gax/protos -t static-module -o pbjs-genfiles/plugin.js google/protobuf/compiler/plugin.proto google/api/annotations.proto google/api/client.proto google/longrunning/operations.proto service_config.proto && pbts pbjs-genfiles/plugin.js -o pbjs-genfiles/plugin.d.ts",
"compile-protos": "pbjs -p protos -p node_modules/google-gax/protos -t static-module -o pbjs-genfiles/plugin.js google/protobuf/compiler/plugin.proto google/api/annotations.proto google/api/resource.proto google/api/client.proto google/longrunning/operations.proto service_config.proto && pbts pbjs-genfiles/plugin.js -o pbjs-genfiles/plugin.d.ts",
"compile": "tsc -p . && cp -r typescript/test/protos build/test/",
"fix": "gts fix",
"prepare": "npm run compile-protos && npm run compile",
Expand Down
37 changes: 37 additions & 0 deletions templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export class {{ service.name }}Client {
*/
private _descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
private _innerApiCalls: {[name: string]: Function};
{%- if (service.pathTemplate.length > 0) %}
private _pathTemplates: {[name: string]: gax.PathTemplate};
{%- endif %}
auth: gax.GoogleAuth;

/**
Expand Down Expand Up @@ -141,6 +144,16 @@ export class {{ service.name }}Client {
require("../../protos/protos.json") :
nodejsProtoPath
);
{%- if (service.pathTemplate.length > 0) %}
this._pathTemplates = {
{%- for template in service.pathTemplate %}
{{ template.type.toCamelCase() }}PathTemplate: new gaxModule.PathTemplate(
'{{ template.pattern }}'
),
{%- endfor %}
};
{%- endif %}

{%- if (service.paging.length > 0) %}

// Some of the methods on this service return "paged" results,
Expand Down Expand Up @@ -482,4 +495,28 @@ export class {{ service.name }}Client {
return this._innerApiCalls.{{ method.name.toCamelCase() }}(request, options, callback);
}
{%- endfor %}
{%- if (service.pathTemplate.length > 0) %}
// --------------------
// -- Path templates --
// --------------------
{%- for template in service.pathTemplate %}
{{ template.type.toLowerCase() }}Path(
{%- set paramJoiner = joiner() %}
{%- for param in template.params %}
{{-paramJoiner()-}}{{ param }}:string
{%- endfor -%}
){
return this._pathTemplates.{{ template.type.toLowerCase() }}PathTemplate.render({
{%- for param in template.params %}
{{ param }}: {{ param }},
{%- endfor %}
});
}
{%- for param in template.params %}
match{{ param.capitalize() }}From{{ template.type }}Name({{ template.type.toLowerCase() }}Name: string){
return this._pathTemplates.{{ template.type.toLowerCase() }}PathTemplate.match({{ template.type.toLowerCase() }}Name).{{ param }};
}
{%- endfor %}
{%- endfor %}
{%- endif %}
}
35 changes: 35 additions & 0 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,17 @@ interface ServiceDescriptorProto
port: number;
oauthScopes: string[];
comments: string;
pathTemplate: ResourceDescriptor[];
commentsMap: CommentsMap;
retryableCodeMap: RetryableCodeMap;
grpcServiceConfig: plugin.grpc.service_config.ServiceConfig;
}

export interface ResourceDescriptor
extends plugin.google.api.IResourceDescriptor {
params: string[];
}

export interface ServicesMap {
[name: string]: ServiceDescriptorProto;
}
Expand Down Expand Up @@ -436,6 +442,35 @@ function augmentService(
'.google.api.oauthScopes'
].split(',');
}
augmentedService.pathTemplate = [];
for (const property of Object.keys(messages)) {
const m = messages[property];
if (m && m.options) {
const option = m.options;
if (option && option['.google.api.resource']) {
const opt = option['.google.api.resource'];
const onePathTemplate = option[
'.google.api.resource'
] as ResourceDescriptor;
if (opt.type) {
const arr = opt.type.match(/\/([^.]+)$/);
if (arr) {
opt.type = arr[arr.length - 1];
}
}
const pattern = opt.pattern;
//TODO: SUPPORT MULTIPLE PATTERNS
if (pattern && pattern[0]) {
const params = pattern[0].match(/{[a-zA-Z]+}/g) || [];
for (let i = 0; i < params.length; i++) {
params[i] = params[i].replace('{', '').replace('}', '');
}
onePathTemplate.params = params;
}
augmentedService.pathTemplate.push(onePathTemplate);
}
}
}
return augmentedService;
}

Expand Down

0 comments on commit c6de4c5

Please sign in to comment.