Skip to content

Commit a3525d4

Browse files
feat: Add diregapic option in command line (#948)
1 parent bbdb2bc commit a3525d4

File tree

10 files changed

+59
-27
lines changed

10 files changed

+59
-27
lines changed

baselines/pubsub-api-dump/api.json.baseline

+1
Original file line numberDiff line numberDiff line change
@@ -14062,5 +14062,6 @@
1406214062
]
1406314063
}
1406414064
],
14065+
"diregapic": false,
1406514066
"legacyProtoLoad": false
1406614067
}

rules_typescript_gapic/typescript_gapic.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def typescript_gapic_library(
2525
service_yaml = None,
2626
metadata = None,
2727
transport = None,
28+
diregapic = None,
2829
legacy_proto_load = None,
2930
extra_protoc_parameters = [],
3031
extra_protoc_file_parameters = {},
@@ -39,6 +40,8 @@ def typescript_gapic_library(
3940
plugin_args_dict["metadata"] = "true"
4041
if transport:
4142
plugin_args_dict["transport"] = transport
43+
if diregapic:
44+
plugin_args_dict["diregapic"] = "true"
4245
if legacy_proto_load:
4346
plugin_args_dict["legacy-proto-load"] = "true"
4447

typescript/src/gapic-generator-typescript.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ yargs.describe(
8181
yargs.boolean('metadata');
8282
yargs.describe(
8383
'transport',
84-
'Default transport is gRPC. Set transport=rest for an API requires HTTP transport, or Google Discovery API.'
84+
'Default transport is gRPC. Set transport=rest for gRPC or non-gRPC API requires REST transport with http annotation in proto3 files.'
85+
);
86+
yargs.describe(
87+
'diregapic',
88+
'DIREGAPIC represents Discovery Rest GAPICs. Set to true for GCE API or non-gRPC APIs with a Discovery doc description.'
8589
);
8690
yargs.describe(
8791
'legacy_proto_load',
@@ -111,6 +115,7 @@ export interface IArguments {
111115
commonProtoPath?: string;
112116
descriptor?: string;
113117
transport?: string;
118+
diregapic?: boolean;
114119
legacyProtoLoad?: boolean;
115120
_: string[];
116121
$0: string;
@@ -128,6 +133,7 @@ const gapicValidatorOut = argv.gapicValidatorOut as string | undefined;
128133
const validation = (argv.validation as string | undefined) ?? 'true';
129134
const metadata = argv.metadata as boolean | undefined;
130135
const transport = argv.transport as string | undefined;
136+
const diregapic = argv.diregapic as boolean | undefined;
131137
const legacyProtoLoad = argv.legacyProtoLoad as boolean | undefined;
132138
const protoc = (argv.protoc as string | undefined) ?? 'protoc';
133139
const protoDirs: string[] = [];
@@ -178,6 +184,9 @@ if (template) {
178184
if (metadata) {
179185
protocCommand.push('--typescript_gapic_opt="metadata"');
180186
}
187+
if (diregapic) {
188+
protocCommand.push('--typescript_gapic_opt="diregapic"');
189+
}
181190
if (transport && transport === 'rest') {
182191
protocCommand.push('--typescript_gapic_opt="transport=rest"');
183192
}

typescript/src/generator.ts

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class Generator {
7171
templates: string[];
7272
metadata?: boolean;
7373
rest?: boolean;
74+
diregapic?: boolean;
7475
legacyProtoLoad?: boolean;
7576

7677
constructor() {
@@ -189,6 +190,13 @@ export class Generator {
189190
}
190191
}
191192

193+
private readDiregapic() {
194+
if (this.paramMap['diregapic'] === 'true') {
195+
this.diregapic = true;
196+
this.rest = true;
197+
}
198+
}
199+
192200
private readLegacyProtoLoad() {
193201
if (this.paramMap['legacy-proto-load'] === 'true') {
194202
this.legacyProtoLoad = true;
@@ -209,6 +217,7 @@ export class Generator {
209217
this.readMainServiceName();
210218
this.readTemplates();
211219
this.readRest();
220+
this.readDiregapic();
212221
this.readLegacyProtoLoad();
213222
}
214223
}
@@ -248,6 +257,7 @@ export class Generator {
248257
mainServiceName: this.mainServiceName,
249258
serviceYaml: this.serviceYaml,
250259
rest: this.rest,
260+
diregapic: this.diregapic,
251261
legacyProtoLoad: this.legacyProtoLoad,
252262
});
253263
return api;

typescript/src/schema/api.ts

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class API {
3636
uniqKeywords: string[];
3737
packageName: string;
3838
rest?: boolean;
39+
diregapic?: boolean;
3940
legacyProtoLoad: boolean;
4041

4142
static isIgnoredService(
@@ -87,6 +88,7 @@ export class API {
8788
this.publishName =
8889
options.publishName || this.naming.productName.toKebabCase();
8990
this.rest = options.rest;
91+
this.diregapic = options.diregapic ?? false;
9092
this.legacyProtoLoad = options.legacyProtoLoad ?? false;
9193

9294
const [allResourceDatabase, resourceDatabase] = getResourceDatabase(
@@ -209,6 +211,7 @@ export class API {
209211
port: this.port,
210212
services: this.services,
211213
rest: this.rest,
214+
diregapic: this.diregapic,
212215
legacyProtoLoad: this.legacyProtoLoad,
213216
});
214217
}

typescript/src/schema/naming.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Options {
2525
mainServiceName?: string;
2626
serviceYaml?: ServiceYaml;
2727
rest?: boolean;
28+
diregapic?: boolean;
2829
legacyProtoLoad?: boolean;
2930
}
3031

typescript/src/schema/proto.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ function pagingField(
183183
messages: MessagesMap,
184184
method: MethodDescriptorProto,
185185
service?: ServiceDescriptorProto,
186-
rest?: boolean
186+
diregapic?: boolean
187187
) {
188188
// TODO: remove this once the next version of the Talent API is published.
189189
//
@@ -213,7 +213,8 @@ function pagingField(
213213
inputType &&
214214
inputType.field!.some(
215215
field =>
216-
field.name === 'page_size' || (rest && field.name === 'max_results')
216+
field.name === 'page_size' ||
217+
(diregapic && field.name === 'max_results')
217218
);
218219
const hasNextPageToken =
219220
outputType &&
@@ -263,18 +264,18 @@ function pagingFieldName(
263264
messages: MessagesMap,
264265
method: MethodDescriptorProto,
265266
service?: ServiceDescriptorProto,
266-
rest?: boolean
267+
diregapic?: boolean
267268
) {
268-
const field = pagingField(messages, method, service, rest);
269+
const field = pagingField(messages, method, service, diregapic);
269270
return field?.name;
270271
}
271272

272273
function pagingResponseType(
273274
messages: MessagesMap,
274275
method: MethodDescriptorProto,
275-
rest?: boolean
276+
diregapic?: boolean
276277
) {
277-
const field = pagingField(messages, method, undefined, rest);
278+
const field = pagingField(messages, method, undefined, diregapic);
278279
if (!field || !field.type) {
279280
return undefined;
280281
}
@@ -292,11 +293,11 @@ function pagingResponseType(
292293
function pagingMapResponseType(
293294
messages: MessagesMap,
294295
method: MethodDescriptorProto,
295-
rest?: boolean
296+
diregapic?: boolean
296297
) {
297-
const pagingfield = pagingField(messages, method, undefined, rest);
298+
const pagingfield = pagingField(messages, method, undefined, diregapic);
298299
const outputType = messages[method.outputType!];
299-
if (!pagingfield?.type || !rest || !outputType.nestedType) {
300+
if (!pagingfield?.type || !diregapic || !outputType.nestedType) {
300301
return undefined;
301302
}
302303
const mapResponses = outputType.nestedType.filter(desProto => {
@@ -375,7 +376,7 @@ interface AugmentMethodParameters {
375376
allMessages: MessagesMap;
376377
localMessages: MessagesMap;
377378
service: ServiceDescriptorProto;
378-
rest?: boolean;
379+
diregapic?: boolean;
379380
}
380381

381382
function augmentMethod(
@@ -398,17 +399,17 @@ function augmentMethod(
398399
parameters.allMessages,
399400
method,
400401
parameters.service,
401-
parameters.rest
402+
parameters.diregapic
402403
),
403404
pagingResponseType: pagingResponseType(
404405
parameters.allMessages,
405406
method,
406-
parameters.rest
407+
parameters.diregapic
407408
),
408409
pagingMapResponseType: pagingMapResponseType(
409410
parameters.allMessages,
410411
method,
411-
parameters.rest
412+
parameters.diregapic
412413
),
413414
inputInterface: method.inputType!,
414415
outputInterface: method.outputType!,
@@ -603,7 +604,7 @@ function augmentService(parameters: AugmentServiceParameters) {
603604
allMessages: parameters.allMessages,
604605
localMessages: parameters.localMessages,
605606
service: augmentedService,
606-
rest: parameters.options.rest,
607+
diregapic: parameters.options.diregapic,
607608
},
608609
method
609610
)
@@ -735,7 +736,7 @@ export class Proto {
735736
allMessages: MessagesMap = {};
736737
localMessages: MessagesMap = {};
737738
fileToGenerate = true;
738-
rest?: boolean;
739+
diregapic?: boolean;
739740
// TODO: need to store metadata? address?
740741

741742
// allResourceDatabase: resources that defined by `google.api.resource`
@@ -752,7 +753,7 @@ export class Proto {
752753
map[`.${parameters.fd.package!}.${message.name!}`] = message;
753754
return map;
754755
}, {} as MessagesMap);
755-
this.rest = parameters.options.rest;
756+
this.diregapic = parameters.options.diregapic;
756757
const protopackage = parameters.fd.package;
757758
// Allow to generate if a proto has no service and its package name is differ from its service's.
758759
if (

typescript/test/unit/baselines.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,6 @@ describe('Baseline tests', () => {
165165
protoPath: 'google/cloud/compute/v1/*.proto',
166166
useCommonProto: false,
167167
packageName: '@google-cloud/compute',
168-
transport: 'rest',
168+
diregapic: true,
169169
});
170170
});

typescript/test/unit/proto.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ describe('src/schema/proto.ts', () => {
347347
});
348348
});
349349

350-
describe('should add rest for Proto class', () => {
351-
it('should be false when rest is not set', () => {
350+
describe('should add diregapic option for Proto class', () => {
351+
it('should be false when diregapic is not set', () => {
352352
const fd = new protos.google.protobuf.FileDescriptorProto();
353353
const proto = new Proto({
354354
fd,
@@ -361,9 +361,9 @@ describe('src/schema/proto.ts', () => {
361361
},
362362
commentsMap: new CommentsMap([fd]),
363363
});
364-
assert.strictEqual(proto.rest, undefined);
364+
assert.strictEqual(proto.diregapic, undefined);
365365
});
366-
it('should be true when rest is set', () => {
366+
it('should be true when diregapic is set', () => {
367367
const fd = new protos.google.protobuf.FileDescriptorProto();
368368
const proto = new Proto({
369369
fd,
@@ -373,16 +373,16 @@ describe('src/schema/proto.ts', () => {
373373
resourceDatabase: new ResourceDatabase(),
374374
options: {
375375
grpcServiceConfig: new protos.grpc.service_config.ServiceConfig(),
376-
rest: true,
376+
diregapic: true,
377377
},
378378
commentsMap: new CommentsMap([fd]),
379379
});
380-
assert.strictEqual(proto.rest, true);
380+
assert.strictEqual(proto.diregapic, true);
381381
});
382382
});
383383

384-
describe('should support pagination for Discovery-based APIs', () => {
385-
it('should be page field if api require rest transport and use "max_results" as field name', () => {
384+
describe('should support pagination for non-gRPC APIs, diregapic mode', () => {
385+
it('should be page field if diregapic mode and use "max_results" as field name', () => {
386386
const fd = new protos.google.protobuf.FileDescriptorProto();
387387
fd.name = 'google/cloud/showcase/v1beta1/test.proto';
388388
fd.package = 'google.cloud.showcase.v1beta1';
@@ -425,7 +425,7 @@ describe('src/schema/proto.ts', () => {
425425
fd.messageType[1].field[1].name = 'page_token';
426426
const options: Options = {
427427
grpcServiceConfig: new protos.grpc.service_config.ServiceConfig(),
428-
rest: true,
428+
diregapic: true,
429429
};
430430
const allMessages: MessagesMap = {};
431431
fd.messageType

typescript/test/util.ts

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface BaselineOptions {
3838
metadata?: boolean;
3939
legacyProtoLoad?: boolean;
4040
transport?: string;
41+
diregapic?: boolean;
4142
}
4243

4344
const cwd = process.cwd();
@@ -125,6 +126,9 @@ export function runBaselineTest(options: BaselineOptions) {
125126
if (options.transport && options.transport === 'rest') {
126127
commandLine += ' --transport=rest';
127128
}
129+
if (options.diregapic) {
130+
commandLine += ' --diregapic';
131+
}
128132
execSync(commandLine);
129133
assert(equalToBaseline(outputDir, baselineDir));
130134
});

0 commit comments

Comments
 (0)