-
Notifications
You must be signed in to change notification settings - Fork 127
/
descriptor-info.ts
656 lines (558 loc) · 23.4 KB
/
descriptor-info.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import {
DescriptorProto,
EnumDescriptorProto,
EnumOptions,
EnumValueDescriptorProto,
EnumValueOptions,
FieldDescriptorProto,
FieldDescriptorProto_Label,
FieldDescriptorProto_Type,
FieldOptions,
FileDescriptorProto,
FileOptions,
MessageOptions,
MethodDescriptorProto,
MethodOptions,
OneofDescriptorProto,
OneofOptions,
ServiceDescriptorProto,
ServiceOptions
} from "./google/protobuf/descriptor";
import {ITypeNameLookup} from "./type-names";
import {IDescriptorTree} from "./descriptor-tree";
import {assert, lowerCamelCase} from "@protobuf-ts/runtime";
import {StringFormat} from "./string-format";
/**
* Union of all known descriptor proto.
*/
export type AnyDescriptorProto =
| FileDescriptorProto
| DescriptorProto
| FieldDescriptorProto
| OneofDescriptorProto
| EnumDescriptorProto
| EnumValueDescriptorProto
| ServiceDescriptorProto
| MethodDescriptorProto;
/**
* Messages, enums and services are the only first-class
* types in the protobuf world.
*
* We assume that it should be possible to lookup these
* types by name.
*/
export type AnyTypeDescriptorProto =
| DescriptorProto
| EnumDescriptorProto
| ServiceDescriptorProto;
/**
* Union of all known descriptor options.
*/
export type AnyOptions =
| FileOptions
| MessageOptions
| FieldOptions
| EnumOptions
| OneofOptions
| EnumValueOptions
| ServiceOptions
| MethodOptions
/**
* Is this a first-class type?
*/
export function isAnyTypeDescriptorProto(arg: any): arg is AnyTypeDescriptorProto {
return DescriptorProto.is(arg) || EnumDescriptorProto.is(arg) || ServiceDescriptorProto.is(arg);
}
/**
* All scalar values types (which includes bytes)
* https://developers.google.com/protocol-buffers/docs/proto3#scalar_value_types
*/
export type ScalarValueType = Exclude<FieldDescriptorProto_Type,
| typeof FieldDescriptorProto_Type.UNSPECIFIED$
| typeof FieldDescriptorProto_Type.MESSAGE
| typeof FieldDescriptorProto_Type.ENUM
| typeof FieldDescriptorProto_Type.GROUP>;
/**
* Map field key type.
*
* The key_type can be any integral or string type
* (so, any scalar type except for floating point
* types and bytes)
*/
export type MapFieldKeyType = Exclude<FieldDescriptorProto_Type,
| typeof FieldDescriptorProto_Type.FLOAT
| typeof FieldDescriptorProto_Type.DOUBLE
| typeof FieldDescriptorProto_Type.BYTES
| typeof FieldDescriptorProto_Type.UNSPECIFIED$
| typeof FieldDescriptorProto_Type.MESSAGE
| typeof FieldDescriptorProto_Type.ENUM
| typeof FieldDescriptorProto_Type.GROUP>;
/**
* Map field value type.
*
* Can be any scalar type, enum, or message.
*/
export type MapFieldValueType =
| DescriptorProto
| EnumDescriptorProto
| ScalarValueType;
export interface IDescriptorInfo {
/**
* Is this an extension field?
*/
isExtension(descriptor: FieldDescriptorProto): boolean;
/**
* Finds all extension fields extending the given message descriptor.
*/
extensionsFor(descriptorOrTypeName: DescriptorProto | string): FieldDescriptorProto[];
/**
* Get the name of an extension field, including the namespace
* where it was defined.
*
* For example, an extension field defined in the package "foo":
*
* ```proto
* extend google.protobuf.FieldOptions {
* bool opt = 1001;
* }
* ```
*
* Will have the extension name "foo.opt".
*
*/
getExtensionName(fieldDescriptor: FieldDescriptorProto): string;
/**
* Return the user-specified JSON name.
* Returns `undefined` if no name was specified or name
* equals lowerCamelCaseName.
*/
getFieldCustomJsonName(fieldDescriptor: FieldDescriptorProto): string | undefined;
/**
* Is this a enum field?
*/
isEnumField(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Get the enum descriptor for a enum field.
*/
getEnumFieldEnum(fieldDescriptor: FieldDescriptorProto): EnumDescriptorProto;
/**
* Is this a message field?
*
* Returns false if this is a map field, even though map fields have type MESSAGE.
* Returns true if this is a group field (type GROUP).
*/
isMessageField(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Get the message descriptor for a message field.
*/
getMessageFieldMessage(fieldDescriptor: FieldDescriptorProto): DescriptorProto;
/**
* Is this a scalar field?
*/
isScalarField(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Get the scalar type of a scalar field.
*/
getScalarFieldType(fieldDescriptor: FieldDescriptorProto): ScalarValueType;
/**
* Is this a map field?
*/
isMapField(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Get the key type of a map field.
*/
getMapKeyType(fieldDescriptor: FieldDescriptorProto): MapFieldKeyType;
/**
* Get the value type (can be enum or message) of a map field.
*/
getMapValueType(fieldDescriptor: FieldDescriptorProto): DescriptorProto | EnumDescriptorProto | ScalarValueType;
/**
* Determines whether the user declared the field
* with `optional`.
*
* For proto2, the field descriptor's `label` is
* `LABEL_OPTIONAL`.
*
* For proto3, the field descriptor's `proto3_optional`
* is `true` and the field will be the sole member
* of a "synthetic" oneof.
*/
isUserDeclaredOptional(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Is this field declared as a oneof member by the user?
*
* When a field is declared `optional` in proto3, a
* "synthetic" oneof is generated by the compiler.
*/
isUserDeclaredOneof(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Determines whether the user declared the field
* with `repeated`.
*
* A map<K,V> for example cannot be repeated, but
* is internally represented as a repeated
* entry-message. This function recognizes this
* and returns false.
*/
isUserDeclaredRepeated(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Should this (repeated) field be encoded packed?
*
* Returns true if the user set [packed = true] or if syntax is proto3
* (and user did not explicitly disable default behaviour of proto3).
*
* Returns false if:
* - the field is not declared with `repeated`.
* - the user set [packed = false].
* - syntax is proto2 and there is no [packed = true].
*
* Throws if the field is `repeated` and type is `bytes` or `string`.
* This should have been a parse failure by protoc.
*/
shouldBePackedRepeated(fieldDescriptor: FieldDescriptorProto): boolean;
/**
* Is this element marked deprecated by the user?
*/
isExplicitlyDeclaredDeprecated(descriptor: AnyDescriptorProto): boolean;
/**
* Is the element intentionally created by the user
* or synthesized by the protobuf compiler?
*
* For example, the compiler generates an entry
* message for each map.
*/
isSyntheticElement(descriptor: AnyDescriptorProto): boolean;
/**
* Determine whether all enum value names start with the snake_case
* version of the enums name (enumLocalName or descriptor.name).
* If so, return the shared prefix. Otherwise, return undefined.
*
* For example, the following enum...
*
* ```proto
* enum MyEnum {
* MY_ENUM_FOO = 0;
* MY_ENUM_BAR = 1;
* }
* ```
*
* ... has the shared prefix "MY_ENUM_".
*/
findEnumSharedPrefix(enumDescriptor: EnumDescriptorProto, enumLocalName?: string): string | undefined;
/**
* Is a top-level type declared in the given file used anywhere the given
* "inFiles"?
*
* Returns true if a type from the file is used in a message field or
* method input or output.
*/
isFileUsed(file: FileDescriptorProto, inFiles: FileDescriptorProto[]): boolean;
/**
* Is the given type used anywhere in the given "inFiles"?
*
* Returns true if the type is used in a message field or method input or
* output.
*/
isTypeUsed(type: AnyTypeDescriptorProto, inFiles: FileDescriptorProto[]): boolean;
}
export class DescriptorInfo implements IDescriptorInfo {
constructor(private readonly tree: IDescriptorTree, private readonly nameLookup: ITypeNameLookup) {
}
private allExtensions: FieldDescriptorProto[] | undefined;
private getAllExtensions(): FieldDescriptorProto[] {
if (!this.allExtensions) {
this.allExtensions = [];
for (let file of this.tree.allFiles()) {
this.allExtensions.push(...file.extension);
for (let msg of file.messageType) {
this.allExtensions.push(...msg.extension);
}
}
}
return this.allExtensions;
}
isExtension(fieldDescriptor: FieldDescriptorProto): boolean {
if (fieldDescriptor.extendee === undefined) {
return false;
}
const parent = this.tree.parentOf(fieldDescriptor);
return parent.extension.includes(fieldDescriptor);
}
extensionsFor(descriptorOrTypeName: DescriptorProto | string): FieldDescriptorProto[] {
let extendeeTypeName: string;
if (typeof descriptorOrTypeName === "string") {
extendeeTypeName = this.nameLookup.makeTypeName(
this.nameLookup.resolveTypeName(descriptorOrTypeName)
);
} else {
extendeeTypeName = this.nameLookup.makeTypeName(descriptorOrTypeName);
}
return this.getAllExtensions().filter(ext => this.nameLookup.normalizeTypeName(ext.extendee!) === extendeeTypeName);
}
getExtensionName(fieldDescriptor: FieldDescriptorProto): string {
assert(this.isExtension(fieldDescriptor), `${StringFormat.formatName(fieldDescriptor)} is not an extension. use isExtension() before getExtensionName()`)
assert(fieldDescriptor.name);
let extensionName: string;
let parent = this.tree.parentOf(fieldDescriptor);
if (FileDescriptorProto.is(parent)) {
extensionName = parent.package
? `${parent.package}.${fieldDescriptor.name}`
: `${fieldDescriptor.name}`;
} else {
extensionName = `${this.nameLookup.makeTypeName(parent)}.${fieldDescriptor.name}`
}
return extensionName;
}
getFieldCustomJsonName(fieldDescriptor: FieldDescriptorProto): string | undefined {
const name = lowerCamelCase(fieldDescriptor.name!);
const jsonName = fieldDescriptor.jsonName;
if (jsonName !== undefined && jsonName !== '' && jsonName !== name) {
return jsonName;
}
return undefined;
}
isEnumField(fieldDescriptor: FieldDescriptorProto): boolean {
return fieldDescriptor.type === FieldDescriptorProto_Type.ENUM;
}
getEnumFieldEnum(fieldDescriptor: FieldDescriptorProto): EnumDescriptorProto {
if (fieldDescriptor.type !== FieldDescriptorProto_Type.ENUM) {
throw new Error(`${StringFormat.formatName(fieldDescriptor)} is not a enum field. use isEnumField() before getEnumFieldEnum().`);
}
assert(fieldDescriptor.typeName !== undefined, `Missing enum type name for ${StringFormat.formatName(fieldDescriptor)}`);
let enumType = this.nameLookup.peekTypeName(fieldDescriptor.typeName);
assert(enumType !== undefined, `Missing enum type ${fieldDescriptor.typeName} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(EnumDescriptorProto.is(enumType), `Invalid enum type for ${StringFormat.formatName(fieldDescriptor)}`);
return enumType;
}
isMessageField(fieldDescriptor: FieldDescriptorProto): boolean {
let group = fieldDescriptor.type === FieldDescriptorProto_Type.GROUP;
let msg = fieldDescriptor.type === FieldDescriptorProto_Type.MESSAGE;
if (fieldDescriptor.typeName !== undefined && (group || msg)) {
return !this.isMapField(fieldDescriptor);
}
return false;
}
getMessageFieldMessage(fieldDescriptor: FieldDescriptorProto): DescriptorProto {
if (!this.isMessageField(fieldDescriptor)) {
throw new Error(`${StringFormat.formatName(fieldDescriptor)} is not a message field. use isMessageField() before getMessageFieldMessage().`);
}
assert(fieldDescriptor.typeName !== undefined, `Missing message type name for ${StringFormat.formatName(fieldDescriptor)}`);
let messageType = this.nameLookup.peekTypeName(fieldDescriptor.typeName);
assert(messageType !== undefined, `Missing message type ${fieldDescriptor.typeName} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(DescriptorProto.is(messageType), `Invalid message type for ${StringFormat.formatName(fieldDescriptor)}`);
return messageType;
}
isScalarField(fieldDescriptor: FieldDescriptorProto): boolean {
switch (fieldDescriptor.type) {
case FieldDescriptorProto_Type.ENUM:
case FieldDescriptorProto_Type.MESSAGE:
case FieldDescriptorProto_Type.GROUP:
case FieldDescriptorProto_Type.UNSPECIFIED$:
return false;
}
return true;
}
getScalarFieldType(fieldDescriptor: FieldDescriptorProto): ScalarValueType {
if (!this.isScalarField(fieldDescriptor)) {
throw new Error(`${StringFormat.formatName(fieldDescriptor)} is not a scalar field. use isScalarField() before getScalarFieldType().`);
}
assert(fieldDescriptor.type !== undefined);
assert(fieldDescriptor.type !== FieldDescriptorProto_Type.ENUM);
assert(fieldDescriptor.type !== FieldDescriptorProto_Type.MESSAGE);
assert(fieldDescriptor.type !== FieldDescriptorProto_Type.GROUP);
assert(fieldDescriptor.type !== FieldDescriptorProto_Type.UNSPECIFIED$);
return fieldDescriptor.type;
}
isMapField(fieldDescriptor: FieldDescriptorProto): boolean {
return this.getMapEntryMessage(fieldDescriptor) !== undefined;
}
getMapKeyType(fieldDescriptor: FieldDescriptorProto): MapFieldKeyType {
let entry = this.getMapEntryMessage(fieldDescriptor);
if (!entry) {
throw new Error(`${StringFormat.formatName(fieldDescriptor)} is not a map field. use isMapField() before getMapKeyType().`);
}
let keyField = entry.field.find(fd => fd.number === 1);
assert(keyField !== undefined, `Missing map entry key field 1 for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== undefined, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== FieldDescriptorProto_Type.UNSPECIFIED$, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== FieldDescriptorProto_Type.GROUP, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== FieldDescriptorProto_Type.MESSAGE, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== FieldDescriptorProto_Type.ENUM, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== FieldDescriptorProto_Type.FLOAT, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== FieldDescriptorProto_Type.DOUBLE, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
assert(keyField.type !== FieldDescriptorProto_Type.BYTES, `Unexpected map key type ${keyField?.type} for ${StringFormat.formatName(fieldDescriptor)}`);
return keyField.type;
}
getMapValueType(fieldDescriptor: FieldDescriptorProto): MapFieldValueType {
let entry = this.getMapEntryMessage(fieldDescriptor);
if (!entry) {
throw new Error(`${StringFormat.formatName(fieldDescriptor)} is not a map field. use isMapField() before getMapValueType().`);
}
let valueField = entry.field.find(fd => fd.number === 2);
assert(valueField !== undefined, `Missing map entry value field 2 for ${StringFormat.formatName(fieldDescriptor)}`);
if (this.isScalarField(valueField)) {
return this.getScalarFieldType(valueField);
}
if (this.isEnumField(valueField)) {
return this.getEnumFieldEnum(valueField);
}
return this.getMessageFieldMessage(valueField);
}
private getMapEntryMessage(fieldDescriptor: FieldDescriptorProto): DescriptorProto | undefined {
if (fieldDescriptor.type !== FieldDescriptorProto_Type.MESSAGE) {
return undefined;
}
if (fieldDescriptor.typeName === undefined || fieldDescriptor.typeName === "") {
return undefined;
}
let typeDescriptor = this.nameLookup.resolveTypeName(fieldDescriptor.typeName);
if (!DescriptorProto.is(typeDescriptor)) {
return undefined;
}
if (typeDescriptor.options?.mapEntry !== true) {
return undefined;
}
return typeDescriptor;
}
isExplicitlyDeclaredDeprecated(descriptor: AnyDescriptorProto): boolean {
if (FileDescriptorProto.is(descriptor)) {
return descriptor.options?.deprecated ?? false;
}
if (DescriptorProto.is(descriptor)) {
return descriptor.options?.deprecated ?? false;
}
if (FieldDescriptorProto.is(descriptor)) {
return descriptor.options?.deprecated ?? false;
}
if (EnumDescriptorProto.is(descriptor)) {
return descriptor.options?.deprecated ?? false;
}
if (EnumValueDescriptorProto.is(descriptor)) {
return descriptor.options?.deprecated ?? false;
}
if (ServiceDescriptorProto.is(descriptor)) {
return descriptor.options?.deprecated ?? false;
}
if (MethodDescriptorProto.is(descriptor)) {
return descriptor.options?.deprecated ?? false;
}
if (OneofDescriptorProto.is(descriptor)) {
return false;
}
return false;
}
isSyntheticElement(descriptor: AnyDescriptorProto): boolean {
if (DescriptorProto.is(descriptor)) {
if (descriptor.options?.mapEntry) {
return true;
}
if (descriptor.name && descriptor.name.startsWith("$synthetic.")) {
return true;
}
}
return false;
}
isUserDeclaredOneof(fieldDescriptor: FieldDescriptorProto): boolean {
if (fieldDescriptor.oneofIndex === undefined) {
return false;
}
return fieldDescriptor.proto3Optional !== true;
}
isUserDeclaredOptional(fieldDescriptor: FieldDescriptorProto): boolean {
if (this.isUserDeclaredOneof(fieldDescriptor)) {
return false;
}
if (fieldDescriptor.proto3Optional === true) {
return true;
}
if (fieldDescriptor.proto3Optional === false) {
return false;
}
const file = this.tree.fileOf(fieldDescriptor);
if (file.syntax === 'proto3') {
return false;
}
assert(file.syntax === undefined || file.syntax === 'proto2', `unsupported syntax "${file.syntax}"`);
return fieldDescriptor.label === FieldDescriptorProto_Label.OPTIONAL;
}
isUserDeclaredRepeated(fieldDescriptor: FieldDescriptorProto): boolean {
if (fieldDescriptor.label !== FieldDescriptorProto_Label.REPEATED) {
return false;
}
const name = fieldDescriptor.typeName;
if (name === undefined || name === "") {
return true;
}
const typeDescriptor = this.nameLookup.resolveTypeName(name);
if (DescriptorProto.is(typeDescriptor)) {
return !typeDescriptor.options?.mapEntry;
}
return true;
}
shouldBePackedRepeated(fieldDescriptor: FieldDescriptorProto): boolean {
let file = this.tree.fileOf(fieldDescriptor);
let standard, declared = fieldDescriptor.options?.packed;
if (file.syntax === 'proto3') {
standard = true;
} else {
assert(file.syntax === undefined || file.syntax === 'proto2', `unsupported syntax "${file.syntax}"`);
standard = false;
}
if (fieldDescriptor.type === FieldDescriptorProto_Type.BYTES || fieldDescriptor.type === FieldDescriptorProto_Type.STRING) {
assert(!declared, `repeated bytes | string cannot be packed. protoc should have caught this. probably unsupported protoc version.`);
standard = false;
}
return declared ?? standard;
}
findEnumSharedPrefix(enumDescriptor: EnumDescriptorProto, enumLocalName?: string): string | undefined {
if (enumLocalName === undefined) {
enumLocalName = `${enumDescriptor.name}`;
}
// create possible prefix from local enum name
// for example, "MyEnum" => "MY_ENUM_"
let enumPrefix = enumLocalName;
enumPrefix = enumPrefix.replace(/[A-Z]/g, letter => "_" + letter.toLowerCase());
enumPrefix = (enumPrefix[0] === "_") ? enumPrefix.substring(1) : enumPrefix;
enumPrefix = enumPrefix.toUpperCase();
enumPrefix += '_';
// do all members share the prefix?
let names = enumDescriptor.value.map(enumValue => `${enumValue.name}`);
let allNamesSharePrefix = names.every(name => name.startsWith(enumPrefix));
// are the names with stripped prefix still valid?
// (start with uppercase letter, at least 2 chars long)
let strippedNames = names.map(name => name.substring(enumPrefix.length));
let strippedNamesAreValid = strippedNames.every(name => name.length > 0 && /^[A-Z].+/.test(name));
return (allNamesSharePrefix && strippedNamesAreValid) ? enumPrefix : undefined;
}
isFileUsed(file: FileDescriptorProto, inFiles: FileDescriptorProto[]): boolean {
let used = false;
this.tree.visitTypes(file, typeDescriptor => {
if (used) return;
if (this.isTypeUsed(typeDescriptor, inFiles)) {
used = true;
}
});
return used;
}
isTypeUsed(type: AnyTypeDescriptorProto, inFiles: FileDescriptorProto[]): boolean {
let used = false;
for (let fd of inFiles) {
this.tree.visitTypes(fd, typeDescriptor => {
if (used) return;
if (DescriptorProto.is(typeDescriptor)) {
const usedInField = typeDescriptor.field.includes(type);
if (usedInField) {
used = true;
}
} else if (ServiceDescriptorProto.is(typeDescriptor)) {
const usedInMethodInput = typeDescriptor.method.some(md => this.nameLookup.resolveTypeName(md.inputType!) === type);
const usedInMethodOutput = typeDescriptor.method.some(md => this.nameLookup.resolveTypeName(md.outputType!) === type);
if (usedInMethodInput || usedInMethodOutput) {
return true;
}
}
})
}
return used;
}
}