Skip to content

Commit e5d9ada

Browse files
committed
Update logic to be in the TypeScript layer
1 parent 3027667 commit e5d9ada

File tree

9 files changed

+77
-14
lines changed

9 files changed

+77
-14
lines changed

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/_iam.njk

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{%- macro iamServiceMethods() -%}
1+
{%- macro iamServiceMethods(service) -%}
22
{#-
33
This part will be added into src/version/client.ts.
44
If the --service-yaml contains "google.iam.v1.IAMPolicy", it means the client requires IAM methods [setIamPolicy, getIamPolicy, testIamPermission].
@@ -30,7 +30,8 @@
3030
* The first element of the array is an object representing {@link google.iam.v1.Policy | Policy}.
3131
* The promise has a method named "cancel" which cancels the ongoing API call.
3232
*/
33-
getIamPolicy(
33+
{%- if service.iamPolicyMixinFlags.getIamPolicy == true %}
34+
/**
3435
request: IamProtos.google.iam.v1.GetIamPolicyRequest,
3536
options?:
3637
| gax.CallOptions
@@ -47,6 +48,7 @@
4748
):Promise<[IamProtos.google.iam.v1.Policy]> {
4849
return this.iamClient.getIamPolicy(request, options, callback);
4950
}
51+
{%- endif %}
5052

5153
/**
5254
* Returns permissions that a caller has on the specified resource. If the
@@ -77,7 +79,8 @@
7779
* The first element of the array is an object representing {@link google.iam.v1.TestIamPermissionsResponse | TestIamPermissionsResponse}.
7880
* The promise has a method named "cancel" which cancels the ongoing API call.
7981
*/
80-
setIamPolicy(
82+
{%- if service.iamPolicyMixinFlags.setIamPolicy %}
83+
/**
8184
request: IamProtos.google.iam.v1.SetIamPolicyRequest,
8285
options?:
8386
| gax.CallOptions
@@ -94,6 +97,7 @@
9497
):Promise<[IamProtos.google.iam.v1.Policy]> {
9598
return this.iamClient.setIamPolicy(request, options, callback);
9699
}
100+
{%- endif %}
97101

98102
/**
99103
* Returns permissions that a caller has on the specified resource. If the
@@ -125,7 +129,8 @@
125129
* The promise has a method named "cancel" which cancels the ongoing API call.
126130
*
127131
*/
128-
testIamPermissions(
132+
{%- if service.iamPolicyMixinFlags.testIamPermissions %}
133+
/**
129134
request: IamProtos.google.iam.v1.TestIamPermissionsRequest,
130135
options?:
131136
| gax.CallOptions
@@ -142,4 +147,5 @@
142147
):Promise<[IamProtos.google.iam.v1.TestIamPermissionsResponse]> {
143148
return this.iamClient.testIamPermissions(request, options, callback);
144149
}
150+
{%- endif %}
145151
{%- endmacro -%}

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/_locations.njk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
{%- macro locationServiceMethods() -%}
1+
{%- macro locationServiceMethods(service) -%}
22
{#-
33
This part will be added into src/version/client.ts.
44
If the --service-yaml contains "google.cloud.location.Locations", it means the client requires Locations methods [getLocation, listLocations].
55
LocationsClient is created for the client in the constructor.
66
[getLocation, listLocations] methods are created which is calling the corresponding methods from LocationsClient in google-gax. Note that since the listLocations method is a paginated
77
method, we default to using listLocationsAsync for ease of use.
88
-#}
9+
{%- if service.locationMixinFlags.getLocation %}
910
/**
1011
* Gets information about a location.
1112
*
@@ -45,7 +46,9 @@
4546
): Promise<LocationProtos.google.cloud.location.ILocation> {
4647
return this.locationsClient.getLocation(request, options, callback);
4748
}
49+
{%- endif %}
4850

51+
{%- if service.locationMixinFlags.listLocations %}
4952
/**
5053
* Lists information about the supported locations for this service. Returns an iterable object.
5154
*
@@ -83,4 +86,5 @@
8386
): AsyncIterable<LocationProtos.google.cloud.location.ILocation> {
8487
return this.locationsClient.listLocationsAsync(request, options);
8588
}
89+
{%- endif %}
8690
{%- endmacro -%}

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/src/$version/$service_client.ts.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,10 +1103,10 @@ export class {{ service.name }}Client {
11031103
{%- endif %}
11041104
{%- endfor %}
11051105
{%- if service.IAMPolicyMixin > 0 %}
1106-
{{ iam.iamServiceMethods()}}
1106+
{{ iam.iamServiceMethods(service)}}
11071107
{% endif -%}
11081108
{%- if service.LocationMixin > 0 %}
1109-
{{ location.locationServiceMethods()}}
1109+
{{ location.locationServiceMethods(service)}}
11101110
{% endif -%}
11111111
{%- if service.LongRunningOperationsMixin > 0 %}
11121112
{{ operations.operationsServiceMethods()}}

core/generator/gapic-generator-typescript/templates/cjs/typescript_gapic/test/gapic_$service_$version.ts.njk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
10891089
{%- if (service.IAMPolicyMixin) > 0 %}
10901090
{%- set IAMmethods = ['getIamPolicy', 'setIamPolicy', 'testIamPermissions'] %}
10911091
{%- for method in IAMmethods %}
1092+
{%- if service.iamPolicyMixinFlags[method] %}
10921093
describe('{{ method }}', () => {
10931094
it('invokes {{ method }} without error', async () => {
10941095
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1204,10 +1205,12 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
12041205
.getCall(0).calledWith(request, expectedOptions, undefined));
12051206
});
12061207
});
1208+
{%- endif %}
12071209
{%- endfor %}
12081210
{%- endif %}
12091211
{%- if service.LocationMixin > 0 %}
12101212
{%- set method = 'getLocation' %}
1213+
{%- if service.locationMixinFlags.getLocation %}
12111214
describe('{{ method }}', () => {
12121215
it('invokes {{ method }} without error', async () => {
12131216
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1314,7 +1317,9 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
13141317
.getCall(0).calledWith(request, expectedOptions, undefined));
13151318
});
13161319
});
1320+
{%- endif %}
13171321
{%- set method = 'listLocations' %}
1322+
{%- if service.locationMixinFlags.listLocations %}
13181323
describe('{{ method }}Async', () => {
13191324
it('uses async iteration with {{method}} without error', async () => {
13201325
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client({{- util.initClientOptions(api.rest) -}});
@@ -1396,6 +1401,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
13961401
});
13971402
});
13981403
{%- endif %}
1404+
{%- endif %}
13991405
{%- if (service.LongRunningOperationsMixin) > 0 %}
14001406
{%- set Operationsmethods = ['getOperation', 'cancelOperation', 'deleteOperation'] %}
14011407
{%- for method in Operationsmethods %}

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/_iam.njk

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{%- macro iamServiceMethods() -%}
1+
{%- macro iamServiceMethods(service) -%}
22
{#-
33
This part will be added into src/version/client.ts.
44
If the --service-yaml contains "google.iam.v1.IAMPolicy", it means the client requires IAM methods [setIamPolicy, getIamPolicy, testIamPermission].
@@ -30,7 +30,8 @@
3030
* The first element of the array is an object representing {@link google.iam.v1.Policy | Policy}.
3131
* The promise has a method named "cancel" which cancels the ongoing API call.
3232
*/
33-
getIamPolicy(
33+
{%- if service.iamPolicyMixinFlags.getIamPolicy %}
34+
/**
3435
request: IamProtos.google.iam.v1.GetIamPolicyRequest,
3536
options?:
3637
| gax.CallOptions
@@ -47,6 +48,7 @@
4748
):Promise<[IamProtos.google.iam.v1.Policy]> {
4849
return this.iamClient.getIamPolicy(request, options, callback);
4950
}
51+
{%- endif %}
5052

5153
/**
5254
* Returns permissions that a caller has on the specified resource. If the
@@ -77,7 +79,8 @@
7779
* The first element of the array is an object representing {@link google.iam.v1.TestIamPermissionsResponse | TestIamPermissionsResponse}.
7880
* The promise has a method named "cancel" which cancels the ongoing API call.
7981
*/
80-
setIamPolicy(
82+
{%- if service.iamPolicyMixinFlags.setIamPolicy %}
83+
/**
8184
request: IamProtos.google.iam.v1.SetIamPolicyRequest,
8285
options?:
8386
| gax.CallOptions
@@ -94,6 +97,7 @@
9497
):Promise<[IamProtos.google.iam.v1.Policy]> {
9598
return this.iamClient.setIamPolicy(request, options, callback);
9699
}
100+
{%- endif %}
97101

98102
/**
99103
* Returns permissions that a caller has on the specified resource. If the
@@ -125,7 +129,8 @@
125129
* The promise has a method named "cancel" which cancels the ongoing API call.
126130
*
127131
*/
128-
testIamPermissions(
132+
{%- if service.iamPolicyMixinFlags.testIamPermissions %}
133+
/**
129134
request: IamProtos.google.iam.v1.TestIamPermissionsRequest,
130135
options?:
131136
| gax.CallOptions
@@ -142,4 +147,5 @@
142147
):Promise<[IamProtos.google.iam.v1.TestIamPermissionsResponse]> {
143148
return this.iamClient.testIamPermissions(request, options, callback);
144149
}
150+
{%- endif %}
145151
{%- endmacro -%}

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/_locations.njk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
{%- macro locationServiceMethods() -%}
1+
{%- macro locationServiceMethods(service) -%}
22
{#-
33
This part will be added into src/version/client.ts.
44
If the --service-yaml contains "google.cloud.location.Locations", it means the client requires Locations methods [getLocation, listLocations].
55
LocationsClient is created for the client in the constructor.
66
[getLocation, listLocations] methods are created which is calling the corresponding methods from LocationsClient in google-gax. Note that since the listLocations method is a paginated
77
method, we default to using listLocationsAsync for ease of use.
88
-#}
9+
{%- if service.locationMixinFlags.getLocation %}
910
/**
1011
* Gets information about a location.
1112
*
@@ -45,6 +46,7 @@
4546
): Promise<LocationProtos.google.cloud.location.ILocation> {
4647
return this.locationsClient.getLocation(request, options, callback);
4748
}
49+
{%- endif %}
4850

4951
/**
5052
* Lists information about the supported locations for this service. Returns an iterable object.
@@ -83,4 +85,5 @@
8385
): AsyncIterable<LocationProtos.google.cloud.location.ILocation> {
8486
return this.locationsClient.listLocationsAsync(request, options);
8587
}
88+
{%- endif %}
8689
{%- endmacro -%}

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/esm/src/$version/$service_client.ts.njk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,10 +1110,10 @@ export class {{ service.name }}Client {
11101110
{%- endif %}
11111111
{%- endfor %}
11121112
{%- if service.IAMPolicyMixin > 0 %}
1113-
{{ iam.iamServiceMethods()}}
1113+
{{ iam.iamServiceMethods(service)}}
11141114
{% endif -%}
11151115
{%- if service.LocationMixin > 0 %}
1116-
{{ location.locationServiceMethods()}}
1116+
{{ location.locationServiceMethods(service)}}
11171117
{% endif -%}
11181118
{%- if service.LongRunningOperationsMixin > 0 %}
11191119
{{ operations.operationsServiceMethods()}}

core/generator/gapic-generator-typescript/templates/esm/typescript_gapic/esm/test/gapic_$service_$version.ts.njk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
10951095
{%- if (service.IAMPolicyMixin) > 0 %}
10961096
{%- set IAMmethods = ['getIamPolicy', 'setIamPolicy', 'testIamPermissions'] %}
10971097
{%- for method in IAMmethods %}
1098+
{%- if service.iamPolicyMixinFlags[method] %}
10981099
describe('{{ method }}', () => {
10991100
it('invokes {{ method }} without error', async () => {
11001101
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1210,10 +1211,12 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
12101211
.getCall(0).calledWith(request, expectedOptions, undefined));
12111212
});
12121213
});
1214+
{%- endif %}
12131215
{%- endfor %}
12141216
{%- endif %}
12151217
{%- if service.LocationMixin > 0 %}
12161218
{%- set method = 'getLocation' %}
1219+
{%- if service.locationMixinFlags.getLocation %}
12171220
describe('{{ method }}', () => {
12181221
it('invokes {{ method }} without error', async () => {
12191222
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1320,7 +1323,9 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
13201323
.getCall(0).calledWith(request, expectedOptions, undefined));
13211324
});
13221325
});
1326+
{%- endif %}
13231327
{%- set method = 'listLocations' %}
1328+
{%- if service.locationMixinFlags.listLocations %}
13241329
describe('{{ method }}Async', () => {
13251330
it('uses async iteration with {{method}} without error', async () => {
13261331
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client({{- util.initClientOptions(api.rest) -}});
@@ -1402,6 +1407,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
14021407
});
14031408
});
14041409
{%- endif %}
1410+
{%- endif %}
14051411
{%- if (service.LongRunningOperationsMixin) > 0 %}
14061412
{%- set Operationsmethods = ['getOperation', 'cancelOperation', 'deleteOperation'] %}
14071413
{%- for method in Operationsmethods %}

core/generator/gapic-generator-typescript/typescript/src/schema/proto.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ export interface MethodDescriptorProto
8383
maxResultsParameter?: boolean;
8484
}
8585

86+
export interface MixinConfig {
87+
enabled: boolean;
88+
}
89+
90+
export interface IamPolicyMixinConfig extends MixinConfig {
91+
getIamPolicy: boolean;
92+
setIamPolicy: boolean;
93+
testIamPermissions: boolean;
94+
}
95+
96+
export interface LocationMixinConfig extends MixinConfig {
97+
getLocation: boolean;
98+
listLocations: boolean;
99+
}
100+
86101
export interface ServiceDescriptorProto
87102
extends protos.google.protobuf.IServiceDescriptorProto {
88103
internalMethods: MethodDescriptorProto[];
@@ -111,6 +126,8 @@ export interface ServiceDescriptorProto
111126
IAMPolicyMixin: number;
112127
LocationMixin: number;
113128
LongRunningOperationsMixin: number;
129+
iamPolicyMixinFlags?: IamPolicyMixinConfig;
130+
locationMixinFlags?: LocationMixinConfig;
114131
protoFile: string;
115132
diregapicLRO?: MethodDescriptorProto[];
116133
httpRules?: protos.google.api.IHttpRule[];
@@ -1031,6 +1048,21 @@ export function augmentService(parameters: AugmentServiceParameters) {
10311048
SelectiveGapicType.INTERNAL,
10321049
);
10331050

1051+
const nativeMethods = augmentedService.method || [];
1052+
const nativeMethodNames = nativeMethods.map(m => m.name);
1053+
augmentedService.iamPolicyMixinFlags = {
1054+
enabled: augmentedService.IAMPolicyMixin === 1,
1055+
getIamPolicy: !nativeMethodNames.includes('GetIamPolicy'),
1056+
setIamPolicy: !nativeMethodNames.includes('SetIamPolicy'),
1057+
testIamPermissions: !nativeMethodNames.includes('TestIamPermissions'),
1058+
};
1059+
1060+
augmentedService.locationMixinFlags = {
1061+
enabled: augmentedService.LocationMixin === 1,
1062+
getLocation: !nativeMethodNames.includes('GetLocation'),
1063+
listLocations: !nativeMethodNames.includes('ListLocations'),
1064+
};
1065+
10341066
augmentedService.bundleConfigsMethods = augmentedService.method.filter(
10351067
method => method.bundleConfig,
10361068
);

0 commit comments

Comments
 (0)