Skip to content

Commit

Permalink
chore: move string manipulation methods to template (#102)
Browse files Browse the repository at this point in the history
* move toInterface to template

* move toLRinterface to templat, baseline should not break

* pass test
  • Loading branch information
xiaozhenliu-gg5 authored Nov 5, 2019
1 parent 48011c6 commit 2ae6f8a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 77 deletions.
12 changes: 11 additions & 1 deletion templates/typescript_gapic/_util.njk
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
-#}
{%- macro initRequestWithHeaderParam(method) -%}
const request: protosTypes{{ method.inputInterface }} = {};
const request: protosTypes{{ toInterface(method.inputInterface) }} = {};
{%- if method.headerRequestParams.length > 1 %}
{%- set chain = "request" -%}
{%- for field in method.headerRequestParams.slice(0, -1) %}
Expand All @@ -27,6 +27,16 @@ limitations under the License.
{%- endif %}
{%- endmacro -%}

{%- macro toInterface(type) -%}
{%- set index = type.lastIndexOf('.') -%}
{{ type.substring(0, index + 1) + 'I' + type.substring(index + 1) }}
{%- endmacro -%}

{%- macro toLRInterface(type, inputType) -%}
{%- set index = inputType.lastIndexOf('.') -%}
{{ inputType.substring(0, index + 1) + 'I' + type }}
{%- endmacro -%}

{%- macro printCommentsForParams(method) -%}
{%- set commentsMap = method.paramComment -%}
{%- for oneComment in commentsMap -%}
Expand Down
97 changes: 49 additions & 48 deletions templates/typescript_gapic/src/$version/$service_client.ts.njk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
{% import "../../_license.njk" as license -%}
{{license.license()}}
{% import "../../_util.njk" as util -%}

import * as gax from 'google-gax';
import * as path from 'path';

Expand Down Expand Up @@ -345,31 +346,31 @@ export class {{ service.name }}Client {
{{- util.printReturn(method, service) }}
*/
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
options?: gax.CallOptions):
Promise<[
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined, {}|undefined
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined, {}|undefined
]>;
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
options: gax.CallOptions,
callback: Callback<
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined,
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined,
{}|undefined>): void;
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
optionsOrCallback?: gax.CallOptions|Callback<
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined, {}|undefined>,
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined, {}|undefined>,
callback?: Callback<
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined,
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined,
{}|undefined>):
Promise<[
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined, {}|undefined
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined, {}|undefined
]>|void {
request = request || {};
let options: gax.CallOptions;
Expand Down Expand Up @@ -409,7 +410,7 @@ export class {{ service.name }}Client {
}
{%- elif method.serverStreaming %}
{{ method.name.toCamelCase() }}(
request?: protosTypes{{ method.inputInterface }},
request?: protosTypes{{ util.toInterface(method.inputInterface) }},
options?: gax.CallOptions):
gax.CancellableStream{
request = request || {};
Expand All @@ -420,21 +421,21 @@ export class {{ service.name }}Client {
{{ method.name.toCamelCase() }}(
options: gax.CallOptions,
callback: Callback<
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined, {}|undefined>):
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined, {}|undefined>):
gax.CancellableStream;
{{ method.name.toCamelCase() }}(
callback: Callback<
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined, {}|undefined>):
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined, {}|undefined>):
gax.CancellableStream;
{{ method.name.toCamelCase() }}(
optionsOrCallback: gax.CallOptions|Callback<
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined, {}|undefined>,
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined, {}|undefined>,
callback?: Callback<
protosTypes{{ method.outputInterface }},
protosTypes{{ method.inputInterface }}|undefined, {}|undefined>):
protosTypes{{ util.toInterface(method.outputInterface) }},
protosTypes{{ util.toInterface(method.inputInterface) }}|undefined, {}|undefined>):
gax.CancellableStream {
if (optionsOrCallback instanceof Function && callback === undefined) {
callback = optionsOrCallback;
Expand All @@ -453,31 +454,31 @@ export class {{ service.name }}Client {
{{- util.printReturn(method, service) }}
*/
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
options?: gax.CallOptions):
Promise<[
Operation<protosTypes{{ method.longRunningResponseType }}, protosTypes{{ method.longRunningMetadataType }}>,
protosTypes{{ method.outputInterface }}|undefined, {}|undefined
Operation<protosTypes{{ util.toLRInterface(method.longRunningResponseType, method.inputType) }}, protosTypes{{ util.toLRInterface(method.longRunningMetadataType, method.inputType) }}>,
protosTypes{{ util.toInterface(method.outputInterface) }}|undefined, {}|undefined
]>;
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
options: gax.CallOptions,
callback: Callback<
Operation<protosTypes{{ method.longRunningResponseType }}, protosTypes{{ method.longRunningMetadataType }}>,
protosTypes{{ method.outputInterface }}|undefined,
Operation<protosTypes{{ util.toLRInterface(method.longRunningResponseType, method.inputType) }}, protosTypes{{ util.toLRInterface(method.longRunningMetadataType, method.inputType) }}>,
protosTypes{{ util.toInterface(method.outputInterface) }}|undefined,
{}|undefined>): void;
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
optionsOrCallback?: gax.CallOptions|Callback<
Operation<protosTypes{{ method.longRunningResponseType }}, protosTypes{{ method.longRunningMetadataType }}>,
protosTypes{{ method.outputInterface }}|undefined, {}|undefined>,
Operation<protosTypes{{ util.toLRInterface(method.longRunningResponseType, method.inputType)}}, protosTypes{{ util.toLRInterface(method.longRunningMetadataType, method.inputType) }}>,
protosTypes{{ util.toInterface(method.outputInterface) }}|undefined, {}|undefined>,
callback?: Callback<
Operation<protosTypes{{ method.longRunningResponseType }}, protosTypes{{ method.longRunningMetadataType }}>,
protosTypes{{ method.outputInterface }}|undefined,
Operation<protosTypes{{ util.toLRInterface(method.longRunningResponseType, method.inputType) }}, protosTypes{{ util.toLRInterface(method.longRunningMetadataType, method.inputType) }}>,
protosTypes{{ util.toInterface(method.outputInterface) }}|undefined,
{}|undefined>):
Promise<[
Operation<protosTypes{{ method.longRunningResponseType }}, protosTypes{{ method.longRunningMetadataType }}>,
protosTypes{{ method.outputInterface }}|undefined, {}|undefined
Operation<protosTypes{{ util.toLRInterface(method.longRunningResponseType, method.inputType) }}, protosTypes{{ util.toLRInterface(method.longRunningMetadataType, method.inputType) }}>,
protosTypes{{ util.toInterface(method.outputInterface) }}|undefined, {}|undefined
]>|void {
request = request || {};
let options: gax.CallOptions;
Expand Down Expand Up @@ -509,34 +510,34 @@ export class {{ service.name }}Client {
{{- util.printReturn(method, service) }}
*/
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
options?: gax.CallOptions):
Promise<[
protosTypes{{ method.pagingResponseType }}[],
protosTypes{{ method.inputInterface }}|null,
protosTypes{{ method.outputInterface }}
protosTypes{{ util.toInterface(method.inputInterface) }}|null,
protosTypes{{ util.toInterface(method.outputInterface) }}
]>;
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
options: gax.CallOptions,
callback: Callback<
protosTypes{{ method.pagingResponseType }}[],
protosTypes{{ method.inputInterface }}|null,
protosTypes{{ method.outputInterface }}>): void;
protosTypes{{ util.toInterface(method.inputInterface) }}|null,
protosTypes{{ util.toInterface(method.outputInterface) }}>): void;
{{ method.name.toCamelCase() }}(
request: protosTypes{{ method.inputInterface }},
request: protosTypes{{ util.toInterface(method.inputInterface) }},
optionsOrCallback?: gax.CallOptions|Callback<
protosTypes{{ method.pagingResponseType }}[],
protosTypes{{ method.inputInterface }}|null,
protosTypes{{ method.outputInterface }}>,
protosTypes{{ util.toInterface(method.inputInterface) }}|null,
protosTypes{{ util.toInterface(method.outputInterface) }}>,
callback?: Callback<
protosTypes{{ method.pagingResponseType }}[],
protosTypes{{ method.inputInterface }}|null,
protosTypes{{ method.outputInterface }}>):
protosTypes{{ util.toInterface(method.inputInterface) }}|null,
protosTypes{{ util.toInterface(method.outputInterface) }}>):
Promise<[
protosTypes{{ method.pagingResponseType }}[],
protosTypes{{ method.inputInterface }}|null,
protosTypes{{ method.outputInterface }}
protosTypes{{ util.toInterface(method.inputInterface) }}|null,
protosTypes{{ util.toInterface(method.outputInterface) }}
]>|void {
request = request || {};
let options: gax.CallOptions;
Expand Down
39 changes: 11 additions & 28 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ function longrunning(method: MethodDescriptorProto) {
function longRunningResponseType(method: MethodDescriptorProto) {
if (method.options && method.options['.google.longrunning.operationInfo']) {
if (method.options['.google.longrunning.operationInfo'].responseType) {
return toLRInterface(
method.options['.google.longrunning.operationInfo'].responseType,
method.inputType!.toString()
);
return method.options['.google.longrunning.operationInfo'].responseType;
}
}
return undefined;
Expand All @@ -227,15 +224,19 @@ function longRunningResponseType(method: MethodDescriptorProto) {
function longRunningMetadataType(method: MethodDescriptorProto) {
if (method.options && method.options['.google.longrunning.operationInfo']) {
if (method.options['.google.longrunning.operationInfo'].metadataType) {
return toLRInterface(
method.options['.google.longrunning.operationInfo'].metadataType,
method.inputType!.toString()
);
return method.options['.google.longrunning.operationInfo'].metadataType;
}
}
return undefined;
}

// convert from input interface to message name
// eg: .google.showcase.v1beta1.EchoRequest -> EchoRequest
function toMessageName(messageType: string): string {
const arr = messageType.split('.');
return arr[arr.length - 1];
}

function streaming(method: MethodDescriptorProto) {
if (method.serverStreaming && method.clientStreaming) {
return 'BIDI_STREAMING';
Expand Down Expand Up @@ -294,24 +295,6 @@ function pagingResponseType(
return undefined;
}

function toInterface(type: string) {
return type.replace(/\.([^.]+)$/, '.I$1');
}

// convert from input interface to message name
// eg: .google.showcase.v1beta1.EchoRequest -> EchoRequest
function toMessageName(messageType: string): string {
const arr = messageType.split('.');
return arr[arr.length - 1];
}

// Convert long running type to the interface
// eg: WaitResponse -> .google.showcase.v1beta1.IWaitResponse
// eg: WaitMetadata -> .google.showcase.v1beta1.IWaitMetadata
function toLRInterface(type: string, inputType: string) {
return inputType.replace(/\.([^.]+)$/, '.I' + type);
}

export function getHeaderParams(rule: plugin.google.api.IHttpRule): string[] {
const message =
rule.post || rule.delete || rule.get || rule.put || rule.patch;
Expand Down Expand Up @@ -361,8 +344,8 @@ function augmentMethod(
streaming: streaming(method),
pagingFieldName: pagingFieldName(messages, method),
pagingResponseType: pagingResponseType(messages, method),
inputInterface: toInterface(method.inputType!),
outputInterface: toInterface(method.outputType!),
inputInterface: method.inputType!,
outputInterface: method.outputType!,
comments: service.commentsMap.getMethodComments(
service.name!,
method.name!
Expand Down

0 comments on commit 2ae6f8a

Please sign in to comment.