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: respect resource annotation #71

Merged
merged 16 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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
34 changes: 33 additions & 1 deletion 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,23 @@ 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({{ template.params.addStringType() }}){
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
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 %}
}
4 changes: 4 additions & 0 deletions typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ interface String {
toKebabCase(): string;
toSnakeCase(): string;
}

interface Array<T>{
addStringType(): string[];
}
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[
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
'.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
7 changes: 7 additions & 0 deletions typescript/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ String.prototype.toSnakeCase = function(this: string): string {
}
return words.join('_');
};

Array.prototype.addStringType = function(this: string[]): string[] {
if (this.length === 0) {
return this;
}
return this.map(p => p + ': string');
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
};
Original file line number Diff line number Diff line change
Expand Up @@ -1231,4 +1231,4 @@ export class KeyManagementServiceClient {
options = options || {};
return this._innerApiCalls.listImportJobs(request, options, callback);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,4 @@ export class EchoClient {
options = options || {};
return this._innerApiCalls.pagedExpand(request, options, callback);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,4 @@ export class TextToSpeechClient {
return this._innerApiCalls.synthesizeSpeech(request, options, callback);
}

}
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved Hide resolved
}