Skip to content

Commit 1e14970

Browse files
committed
feat: clean up precedence for LRO operations mixin duplicate methods
1 parent 9b5b11f commit 1e14970

6 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* // doThingsWith(response)
3737
* ```
3838
*/
39+
{%- if service.longRunningOperationsMixinFlags.getOperation %}
3940
getOperation(
4041
request: protos.google.longrunning.GetOperationRequest,
4142
optionsOrCallback?:
@@ -67,6 +68,8 @@
6768
});
6869
return this.operationsClient.getOperation(request, options, callback);
6970
}
71+
{%- endif %}
72+
{%- if service.longRunningOperationsMixinFlags.listOperations %}
7073
/**
7174
* Lists operations that match the specified filter in the request. If the
7275
* server doesn't support this method, it returns `UNIMPLEMENTED`. Returns an iterable object.
@@ -110,6 +113,8 @@
110113
});
111114
return this.operationsClient.listOperationsAsync(request, options);
112115
}
116+
{%- endif %}
117+
{%- if service.longRunningOperationsMixinFlags.cancelOperation %}
113118
/**
114119
* Starts asynchronous cancellation on a long-running operation. The server
115120
* makes a best effort to cancel the operation, but success is not
@@ -172,7 +177,9 @@
172177
});
173178
return this.operationsClient.cancelOperation(request, options, callback);
174179
}
180+
{%- endif %}
175181

182+
{%- if service.longRunningOperationsMixinFlags.deleteOperation %}
176183
/**
177184
* Deletes a long-running operation. This method indicates that the client is
178185
* no longer interested in the operation result. It does not cancel the
@@ -229,4 +236,5 @@
229236
});
230237
return this.operationsClient.deleteOperation(request, options, callback);
231238
}
239+
{%- endif %}
232240
{%- endmacro -%}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
14051405
{%- if (service.LongRunningOperationsMixin) > 0 %}
14061406
{%- set Operationsmethods = ['getOperation', 'cancelOperation', 'deleteOperation'] %}
14071407
{%- for method in Operationsmethods %}
1408+
{%- if service.longRunningOperationsMixinFlags[method] %}
14081409
describe('{{ method }}', () => {
14091410
it('invokes {{ method }} without error', async () => {
14101411
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1495,8 +1496,10 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
14951496
.getCall(0).calledWith(request));
14961497
});
14971498
});
1499+
{%- endif %}
14981500
{%- endfor %}
14991501
{%- set method = 'listOperations' %}
1502+
{%- if service.longRunningOperationsMixinFlags.listOperations %}
15001503
describe('{{ method }}Async', () => {
15011504
it('uses async iteration with {{method}} without error', async () => {
15021505
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client({{- util.initClientOptions(api.rest) -}});
@@ -1561,6 +1564,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
15611564
});
15621565
});
15631566
{%- endif %}
1567+
{%- endif %}
15641568
{%- if (service.pathTemplates.length > 0) %}
15651569

15661570
describe('Path templates', () => {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* // doThingsWith(response)
3737
* ```
3838
*/
39+
{%- if service.longRunningOperationsMixinFlags.getOperation %}
3940
getOperation(
4041
request: protos.google.longrunning.GetOperationRequest,
4142
optionsOrCallback?:
@@ -67,6 +68,8 @@
6768
});
6869
return this.operationsClient.getOperation(request, options, callback);
6970
}
71+
{%- endif %}
72+
{%- if service.longRunningOperationsMixinFlags.listOperations %}
7073
/**
7174
* Lists operations that match the specified filter in the request. If the
7275
* server doesn't support this method, it returns `UNIMPLEMENTED`. Returns an iterable object.
@@ -110,6 +113,8 @@
110113
});
111114
return this.operationsClient.listOperationsAsync(request, options);
112115
}
116+
{%- endif %}
117+
{%- if service.longRunningOperationsMixinFlags.cancelOperation %}
113118
/**
114119
* Starts asynchronous cancellation on a long-running operation. The server
115120
* makes a best effort to cancel the operation, but success is not
@@ -172,7 +177,9 @@
172177
});
173178
return this.operationsClient.cancelOperation(request, options, callback);
174179
}
180+
{%- endif %}
175181

182+
{%- if service.longRunningOperationsMixinFlags.deleteOperation %}
176183
/**
177184
* Deletes a long-running operation. This method indicates that the client is
178185
* no longer interested in the operation result. It does not cancel the
@@ -229,4 +236,5 @@
229236
});
230237
return this.operationsClient.deleteOperation(request, options, callback);
231238
}
239+
{%- endif %}
232240
{%- endmacro -%}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
14111411
{%- if (service.LongRunningOperationsMixin) > 0 %}
14121412
{%- set Operationsmethods = ['getOperation', 'cancelOperation', 'deleteOperation'] %}
14131413
{%- for method in Operationsmethods %}
1414+
{%- if service.longRunningOperationsMixinFlags[method] %}
14141415
describe('{{ method }}', () => {
14151416
it('invokes {{ method }} without error', async () => {
14161417
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client(
@@ -1501,8 +1502,10 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
15011502
.getCall(0).calledWith(request));
15021503
});
15031504
});
1505+
{%- endif %}
15041506
{%- endfor %}
15051507
{%- set method = 'listOperations' %}
1508+
{%- if service.longRunningOperationsMixinFlags.listOperations %}
15061509
describe('{{ method }}Async', () => {
15071510
it('uses async iteration with {{method}} without error', async () => {
15081511
const client = new {{ service.name.toLowerCase() }}Module.{{ api.naming.version }}.{{ service.name }}Client({{- util.initClientOptions(api.rest) -}});
@@ -1567,6 +1570,7 @@ describe('{{ api.naming.version }}.{{ service.name }}Client', () => {
15671570
});
15681571
});
15691572
{%- endif %}
1573+
{%- endif %}
15701574
{%- if (service.pathTemplates.length > 0) %}
15711575

15721576
describe('Path templates', () => {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ export interface LocationMixinConfig extends MixinConfig {
9898
listLocations: boolean;
9999
}
100100

101+
export interface OperationsMixinConfig extends MixinConfig {
102+
cancelOperation: boolean;
103+
deleteOperation: boolean;
104+
getOperation: boolean;
105+
listOperations: boolean;
106+
}
107+
101108
export interface ServiceDescriptorProto
102109
extends protos.google.protobuf.IServiceDescriptorProto {
103110
internalMethods: MethodDescriptorProto[];
@@ -128,6 +135,7 @@ export interface ServiceDescriptorProto
128135
LongRunningOperationsMixin: number;
129136
iamPolicyMixinFlags?: IamPolicyMixinConfig;
130137
locationMixinFlags?: LocationMixinConfig;
138+
longRunningOperationsMixinFlags?: OperationsMixinConfig;
131139
protoFile: string;
132140
diregapicLRO?: MethodDescriptorProto[];
133141
httpRules?: protos.google.api.IHttpRule[];
@@ -1103,6 +1111,14 @@ export function augmentService(parameters: AugmentServiceParameters) {
11031111
augmentedService.LongRunningOperationsMixin = 1;
11041112
}
11051113

1114+
augmentedService.longRunningOperationsMixinFlags = {
1115+
enabled: augmentedService.LongRunningOperationsMixin === 1,
1116+
getOperation: !nativeMethodNames.includes('GetOperation'),
1117+
cancelOperation: !nativeMethodNames.includes('CancelOperation'),
1118+
deleteOperation: !nativeMethodNames.includes('DeleteOperation'),
1119+
listOperations: !nativeMethodNames.includes('ListOperations'),
1120+
};
1121+
11061122
augmentedService.hostname = '';
11071123
augmentedService.port = 0;
11081124
if (augmentedService.options?.['.google.api.defaultHost']) {

core/generator/gapic-generator-typescript/typescript/test/unit/proto.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,13 @@ describe('src/schema/proto.ts', () => {
427427
fd.service[0].method = [
428428
{name: 'GetIamPolicy'},
429429
{name: 'ListLocations'},
430+
{name: 'GetOperation'},
430431
] as protos.google.protobuf.MethodDescriptorProto[];
431432

432433
// Simulating mixins being enabled (non-zero value)
433434
(fd.service[0] as any).IAMPolicyMixin = 1;
434435
(fd.service[0] as any).LocationMixin = 1;
436+
(fd.service[0] as any).LongRunningOperationsMixin = 1;
435437

436438
const options: Options = {
437439
grpcServiceConfig: {} as protos.grpc.service_config.ServiceConfig,
@@ -464,6 +466,11 @@ describe('src/schema/proto.ts', () => {
464466
augmentedService.locationMixinFlags?.listLocations,
465467
false
466468
); // Native ListLocations exists
469+
470+
assert.strictEqual(augmentedService.longRunningOperationsMixinFlags?.getOperation, false); // Native GetOperation exists
471+
assert.strictEqual(augmentedService.longRunningOperationsMixinFlags?.cancelOperation, true);
472+
assert.strictEqual(augmentedService.longRunningOperationsMixinFlags?.deleteOperation, true);
473+
assert.strictEqual(augmentedService.longRunningOperationsMixinFlags?.listOperations, true);
467474
});
468475

469476
it('should return api version if it exists', () => {

0 commit comments

Comments
 (0)