forked from stephenh/ts-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
1147 lines (1053 loc) · 41.5 KB
/
main.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { code, Code, conditionalOutput, def, imp, joinCode } from 'ts-poet';
import {
DescriptorProto,
FieldDescriptorProto,
FileDescriptorProto,
FieldDescriptorProto_Type,
} from 'ts-proto-descriptors/google/protobuf/descriptor';
import {
basicLongWireType,
basicTypeName,
basicWireType,
notDefaultCheck,
defaultValue,
detectMapType,
getEnumMethod,
isBytes,
isBytesValueType,
isEnum,
isLong,
isLongValueType,
isMapType,
isMessage,
isPrimitive,
isRepeated,
isScalar,
isTimestamp,
isValueType,
isWithinOneOf,
isWithinOneOfThatShouldBeUnion,
packedType,
toReaderCall,
toTypeName,
valueTypeName,
} from './types';
import SourceInfo, { Fields } from './sourceInfo';
import { maybeAddComment } from './utils';
import { camelToSnake, capitalize, maybeSnakeToCamel } from './case';
import {
generateNestjsGrpcServiceMethodsDecorator,
generateNestjsServiceClient,
generateNestjsServiceController,
} from './generate-nestjs';
import {
generateDataLoaderOptionsType,
generateDataLoadersType,
generateRpcType,
generateService,
generateServiceClientImpl,
} from './generate-services';
import {
addGrpcWebMisc,
generateGrpcClientImpl,
generateGrpcMethodDesc,
generateGrpcServiceDesc,
} from './generate-grpc-web';
import { generateEnum } from './enums';
import { visit, visitServices } from './visit';
import { EnvOption, LongOption, OneofOption, Options, DateOption } from './options';
import { Context } from './context';
import { generateSchema } from './schema';
export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [string, Code] {
const { options, utils: u } = ctx;
// Google's protofiles are organized like Java, where package == the folder the file
// is in, and file == a specific service within the package. I.e. you can have multiple
// company/foo.proto and company/bar.proto files, where package would be 'company'.
//
// We'll match that structure by setting up the module path as:
//
// company/foo.proto --> company/foo.ts
// company/bar.proto --> company/bar.ts
//
// We'll also assume that the fileDesc.name is already the `company/foo.proto` path, with
// the package already implicitly in it, so we won't re-append/strip/etc. it out/back in.
const moduleName = fileDesc.name.replace('.proto', '.ts');
const chunks: Code[] = [];
// Indicate this file's source protobuf package for reflective use with google.protobuf.Any
if (options.exportCommonSymbols) {
chunks.push(code`export const protobufPackage = '${fileDesc.package}';`);
}
// Syntax, unlike most fields, is not repeated and thus does not use an index
const sourceInfo = SourceInfo.fromDescriptor(fileDesc);
const headerComment = sourceInfo.lookup(Fields.file.syntax, undefined);
maybeAddComment(headerComment, chunks, fileDesc.options?.deprecated);
// first make all the type declarations
visit(
fileDesc,
sourceInfo,
(fullName, message, sInfo) => {
chunks.push(generateInterfaceDeclaration(ctx, fullName, message, sInfo));
},
options,
(fullName, enumDesc, sInfo) => {
chunks.push(generateEnum(ctx, fullName, enumDesc, sInfo));
}
);
// If nestJs=true export [package]_PACKAGE_NAME and [service]_SERVICE_NAME const
if (options.nestJs) {
const prefix = camelToSnake(fileDesc.package.replace(/\./g, '_'));
chunks.push(code`export const ${prefix}_PACKAGE_NAME = '${fileDesc.package}';`);
}
if (options.outputEncodeMethods || options.outputJsonMethods) {
// then add the encoder/decoder/base instance
visit(
fileDesc,
sourceInfo,
(fullName, message) => {
chunks.push(generateBaseInstance(ctx, fullName, message));
const staticMethods: Code[] = [];
if (options.outputEncodeMethods) {
staticMethods.push(generateEncode(ctx, fullName, message));
staticMethods.push(generateDecode(ctx, fullName, message));
}
if (options.outputJsonMethods) {
staticMethods.push(generateFromJson(ctx, fullName, message));
staticMethods.push(generateToJson(ctx, fullName, message));
}
if (options.outputPartialMethods) {
staticMethods.push(generateFromPartial(ctx, fullName, message));
}
chunks.push(code`
export const ${def(fullName)} = {
${joinCode(staticMethods, { on: ',\n\n' })}
};
`);
},
options
);
}
let hasStreamingMethods = false;
visitServices(fileDesc, sourceInfo, (serviceDesc, sInfo) => {
if (options.nestJs) {
// NestJS is sufficiently different that we special case all of the client/server interfaces
// generate nestjs grpc client interface
chunks.push(generateNestjsServiceClient(ctx, fileDesc, sInfo, serviceDesc));
// and the service controller interface
chunks.push(generateNestjsServiceController(ctx, fileDesc, sInfo, serviceDesc));
// generate nestjs grpc service controller decorator
chunks.push(generateNestjsGrpcServiceMethodsDecorator(ctx, serviceDesc));
let serviceConstName = `${camelToSnake(serviceDesc.name)}_NAME`;
if (!serviceDesc.name.toLowerCase().endsWith('service')) {
serviceConstName = `${camelToSnake(serviceDesc.name)}_SERVICE_NAME`;
}
chunks.push(code`export const ${serviceConstName} = "${serviceDesc.name}";`);
} else {
// This service could be Twirp or grpc-web or JSON (maybe). So far all of their
// interfaces are fairly similar so we share the same service interface.
chunks.push(generateService(ctx, fileDesc, sInfo, serviceDesc));
if (options.outputClientImpl === true) {
chunks.push(generateServiceClientImpl(ctx, fileDesc, serviceDesc));
} else if (options.outputClientImpl === 'grpc-web') {
chunks.push(generateGrpcClientImpl(ctx, fileDesc, serviceDesc));
chunks.push(generateGrpcServiceDesc(fileDesc, serviceDesc));
serviceDesc.method.forEach((method) => {
chunks.push(generateGrpcMethodDesc(ctx, serviceDesc, method));
if (method.serverStreaming) {
hasStreamingMethods = true;
}
});
}
}
});
if (options.outputClientImpl && fileDesc.service.length > 0) {
if (options.outputClientImpl === true) {
chunks.push(generateRpcType(ctx));
} else if (options.outputClientImpl === 'grpc-web') {
chunks.push(addGrpcWebMisc(ctx, hasStreamingMethods));
}
}
if (options.context) {
chunks.push(generateDataLoaderOptionsType());
chunks.push(generateDataLoadersType());
}
if (options.outputSchema) {
chunks.push(...generateSchema(ctx, fileDesc, sourceInfo));
}
chunks.push(
...Object.values(u).map((v) => {
if ('ifUsed' in v) {
return code`${v.ifUsed}`;
} else {
return code``;
}
})
);
return [moduleName, joinCode(chunks, { on: '\n\n' })];
}
export type Utils = ReturnType<typeof makeDeepPartial> &
ReturnType<typeof makeTimestampMethods> &
ReturnType<typeof makeByteUtils> &
ReturnType<typeof makeLongUtils>;
/** These are runtime utility methods used by the generated code. */
export function makeUtils(options: Options): Utils {
const bytes = makeByteUtils();
const longs = makeLongUtils(options, bytes);
return {
...bytes,
...makeDeepPartial(options, longs),
...makeTimestampMethods(options, longs),
...longs,
};
}
function makeLongUtils(options: Options, bytes: ReturnType<typeof makeByteUtils>) {
// Regardless of which `forceLong` config option we're using, we always use
// the `long` library to either represent or at least sanity-check 64-bit values
const util = imp('util@protobufjs/minimal');
const configure = imp('configure@protobufjs/minimal');
// Before esModuleInterop, we had to use 'import * as Long from long` b/c long is
// an `export =` module and exports only the Long constructor (which is callable).
// See https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require.
//
// With esModuleInterop on, `* as Long` is no longer the constructor, it's the module,
// so we want to go back to `import { Long } from long`, which is specifically forbidden
// due to `export =` w/o esModuleInterop.
//
// I.e there is not an import for long that "just works" in both esModuleInterop and
// not esModuleInterop.
const Long = options.esModuleInterop ? imp('Long=long') : imp('Long*long');
const init = conditionalOutput(
'',
code`
if (${util}.Long !== ${Long}) {
${util}.Long = ${Long} as any;
${configure}();
}
`
);
// TODO This is unused?
const numberToLong = conditionalOutput(
'numberToLong',
code`
${init}
function numberToLong(number: number) {
return ${Long}.fromNumber(number);
}
`
);
const longToString = conditionalOutput(
'longToString',
code`
${init}
function longToString(long: ${Long}) {
return long.toString();
}
`
);
const longToNumber = conditionalOutput(
'longToNumber',
code`
${init}
function longToNumber(long: ${Long}): number {
if (long.gt(Number.MAX_SAFE_INTEGER)) {
throw new ${bytes.globalThis}.Error("Value is larger than Number.MAX_SAFE_INTEGER")
}
return long.toNumber();
}
`
);
return { numberToLong, longToNumber, longToString, longInit: init, Long };
}
function makeByteUtils() {
const globalThis = conditionalOutput(
'globalThis',
code`
declare var self: any | undefined;
declare var window: any | undefined;
var globalThis: any = (() => {
if (typeof globalThis !== "undefined") return globalThis;
if (typeof self !== "undefined") return self;
if (typeof window !== "undefined") return window;
if (typeof global !== "undefined") return global;
throw "Unable to locate global object";
})();
`
);
const bytesFromBase64 = conditionalOutput(
'bytesFromBase64',
code`
const atob: (b64: string) => string = ${globalThis}.atob || ((b64) => ${globalThis}.Buffer.from(b64, 'base64').toString('binary'));
function bytesFromBase64(b64: string): Uint8Array {
const bin = atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
}
return arr;
}
`
);
const base64FromBytes = conditionalOutput(
'base64FromBytes',
code`
const btoa : (bin: string) => string = ${globalThis}.btoa || ((bin) => ${globalThis}.Buffer.from(bin, 'binary').toString('base64'));
function base64FromBytes(arr: Uint8Array): string {
const bin: string[] = [];
for (let i = 0; i < arr.byteLength; ++i) {
bin.push(String.fromCharCode(arr[i]));
}
return btoa(bin.join(''));
}
`
);
return { globalThis, bytesFromBase64, base64FromBytes };
}
function makeDeepPartial(options: Options, longs: ReturnType<typeof makeLongUtils>) {
let oneofCase = '';
if (options.oneof === OneofOption.UNIONS) {
oneofCase = `
: T extends { $case: string }
? { [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]> } & { $case: T['$case'] }
`;
}
const maybeExport = options.exportCommonSymbols ? 'export' : '';
const maybeLong = options.forceLong === LongOption.LONG ? code` | ${longs.Long}` : '';
// Based on the type from ts-essentials
const DeepPartial = conditionalOutput(
'DeepPartial',
code`
type Builtin = Date | Function | Uint8Array | string | number | undefined${maybeLong};
${maybeExport} type DeepPartial<T> = T extends Builtin
? T
: T extends Array<infer U>
? Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>${oneofCase}
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;
`
);
return { DeepPartial };
}
function makeTimestampMethods(options: Options, longs: ReturnType<typeof makeLongUtils>) {
const Timestamp = imp('Timestamp@./google/protobuf/timestamp');
let seconds: string | Code = 'date.getTime() / 1_000';
let toNumberCode = 't.seconds';
if (options.forceLong === LongOption.LONG) {
toNumberCode = 't.seconds.toNumber()';
seconds = code`${longs.numberToLong}(date.getTime() / 1_000)`;
} else if (options.forceLong === LongOption.STRING) {
toNumberCode = 'Number(t.seconds)';
seconds = '(date.getTime() / 1_000).toString()';
}
const toTimestamp = conditionalOutput(
'toTimestamp',
options.useDate === DateOption.STRING
? code`
function toTimestamp(dateStr: string): ${Timestamp} {
const date = new Date(dateStr);
const seconds = ${seconds};
const nanos = (date.getTime() % 1_000) * 1_000_000;
return { seconds, nanos };
}
`
: code`
function toTimestamp(date: Date): ${Timestamp} {
const seconds = ${seconds};
const nanos = (date.getTime() % 1_000) * 1_000_000;
return { seconds, nanos };
}
`
);
const fromTimestamp = conditionalOutput(
'fromTimestamp',
options.useDate === DateOption.STRING
? code`
function fromTimestamp(t: ${Timestamp}): string {
let millis = ${toNumberCode} * 1_000;
millis += t.nanos / 1_000_000;
return new Date(millis).toISOString();
}
`
: code`
function fromTimestamp(t: ${Timestamp}): Date {
let millis = ${toNumberCode} * 1_000;
millis += t.nanos / 1_000_000;
return new Date(millis);
}
`
);
const fromJsonTimestamp = conditionalOutput(
'fromJsonTimestamp',
options.useDate === DateOption.DATE
? code`
function fromJsonTimestamp(o: any): Date {
if (o instanceof Date) {
return o;
} else if (typeof o === "string") {
return new Date(o);
} else {
return ${fromTimestamp}(Timestamp.fromJSON(o));
}
}
`
: code`
function fromJsonTimestamp(o: any): Timestamp {
if (o instanceof Date) {
return ${toTimestamp}(o);
} else if (typeof o === "string") {
return ${toTimestamp}(new Date(o));
} else {
return Timestamp.fromJSON(o);
}
}
`
);
return { toTimestamp, fromTimestamp, fromJsonTimestamp };
}
// When useOptionals=true, non-scalar fields are translated into optional properties.
function isOptionalProperty(field: FieldDescriptorProto, options: Options): boolean {
return (options.useOptionals && isMessage(field)) || field.proto3Optional;
}
// Create the interface with properties
function generateInterfaceDeclaration(
ctx: Context,
fullName: string,
messageDesc: DescriptorProto,
sourceInfo: SourceInfo
): Code {
const { options } = ctx;
const chunks: Code[] = [];
maybeAddComment(sourceInfo, chunks, messageDesc.options?.deprecated);
chunks.push(code`export interface ${fullName} {`);
// When oneof=unions, we generate a single property with an ADT per `oneof` clause.
const processedOneofs = new Set<number>();
messageDesc.field.forEach((fieldDesc, index) => {
if (isWithinOneOfThatShouldBeUnion(options, fieldDesc)) {
const { oneofIndex } = fieldDesc;
if (!processedOneofs.has(oneofIndex)) {
processedOneofs.add(oneofIndex);
chunks.push(generateOneofProperty(ctx, messageDesc, oneofIndex, sourceInfo));
}
return;
}
const info = sourceInfo.lookup(Fields.message.field, index);
maybeAddComment(info, chunks, fieldDesc.options?.deprecated);
const name = maybeSnakeToCamel(fieldDesc.name, options);
const type = toTypeName(ctx, messageDesc, fieldDesc);
const q = isOptionalProperty(fieldDesc, options) ? '?' : '';
chunks.push(code`${name}${q}: ${type}, `);
});
chunks.push(code`}`);
return joinCode(chunks, { on: '\n' });
}
function generateOneofProperty(
ctx: Context,
messageDesc: DescriptorProto,
oneofIndex: number,
sourceInfo: SourceInfo
): Code {
const { options } = ctx;
const fields = messageDesc.field.filter((field) => isWithinOneOf(field) && field.oneofIndex === oneofIndex);
const unionType = joinCode(
fields.map((f) => {
let fieldName = maybeSnakeToCamel(f.name, options);
let typeName = toTypeName(ctx, messageDesc, f);
return code`{ $case: '${fieldName}', ${fieldName}: ${typeName} }`;
}),
{ on: ' | ' }
);
const name = maybeSnakeToCamel(messageDesc.oneofDecl[oneofIndex].name, options);
return code`${name}?: ${unionType},`;
/*
// Ideally we'd put the comments for each oneof field next to the anonymous
// type we've created in the type union above, but ts-poet currently lacks
// that ability. For now just concatenate all comments into one big one.
let comments: Array<string> = [];
const info = sourceInfo.lookup(Fields.message.oneof_decl, oneofIndex);
maybeAddComment(info, (text) => comments.push(text));
messageDesc.field.forEach((field, index) => {
if (!isWithinOneOf(field) || field.oneofIndex !== oneofIndex) {
return;
}
const info = sourceInfo.lookup(Fields.message.field, index);
const name = maybeSnakeToCamel(field.name, options);
maybeAddComment(info, (text) => comments.push(name + '\n' + text));
});
if (comments.length) {
prop = prop.addJavadoc(comments.join('\n'));
}
return prop;
*/
}
// Create a 'base' instance with default values for decode to use as a prototype
function generateBaseInstance(ctx: Context, fullName: string, messageDesc: DescriptorProto): Code {
const fields = messageDesc.field
.filter((field) => !isWithinOneOf(field))
.map((field) => [field, defaultValue(ctx, field)])
.filter(([field, val]) => val !== 'undefined' && !isBytes(field))
.map(([field, val]) => {
const name = maybeSnakeToCamel(field.name, ctx.options);
return code`${name}: ${val}`;
});
return code`const base${fullName}: object = { ${joinCode(fields, { on: ',' })} };`;
}
/** Creates a function to decode a message by loop overing the tags. */
function generateDecode(ctx: Context, fullName: string, messageDesc: DescriptorProto): Code {
const { options, utils } = ctx;
const chunks: Code[] = [];
// create the basic function declaration
chunks.push(code`
decode(
input: ${Reader} | Uint8Array,
length?: number,
): ${fullName} {
const reader = input instanceof Uint8Array ? new ${Reader}(input) : input;
let end = length === undefined ? reader.len : reader.pos + length;
const message = { ...base${fullName} } as ${fullName};
`);
// initialize all lists
messageDesc.field.filter(isRepeated).forEach((field) => {
const name = maybeSnakeToCamel(field.name, options);
const value = isMapType(ctx, messageDesc, field) ? '{}' : '[]';
chunks.push(code`message.${name} = ${value};`);
});
// start the tag loop
chunks.push(code`
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
`);
// add a case for each incoming field
messageDesc.field.forEach((field) => {
const fieldName = maybeSnakeToCamel(field.name, options);
chunks.push(code`case ${field.number}:`);
// get a generic 'reader.doSomething' bit that is specific to the basic type
let readSnippet: Code;
if (isPrimitive(field)) {
readSnippet = code`reader.${toReaderCall(field)}()`;
if (isBytes(field)) {
if (options.env === EnvOption.NODE) {
readSnippet = code`${readSnippet} as Buffer`;
}
} else if (basicLongWireType(field.type) !== undefined) {
if (options.forceLong === LongOption.LONG) {
readSnippet = code`${readSnippet} as Long`;
} else if (options.forceLong === LongOption.STRING) {
readSnippet = code`${utils.longToString}(${readSnippet} as Long)`;
} else {
readSnippet = code`${utils.longToNumber}(${readSnippet} as Long)`;
}
} else if (isEnum(field)) {
readSnippet = code`${readSnippet} as any`;
}
} else if (isValueType(ctx, field)) {
const type = basicTypeName(ctx, field, { keepValueType: true });
readSnippet = code`${type}.decode(reader, reader.uint32()).value`;
} else if (isTimestamp(field) && (options.useDate === DateOption.DATE || options.useDate === DateOption.STRING)) {
const type = basicTypeName(ctx, field, { keepValueType: true });
readSnippet = code`${utils.fromTimestamp}(${type}.decode(reader, reader.uint32()))`;
} else if (isMessage(field)) {
const type = basicTypeName(ctx, field);
readSnippet = code`${type}.decode(reader, reader.uint32())`;
} else {
throw new Error(`Unhandled field ${field}`);
}
// and then use the snippet to handle repeated fields if necessary
if (isRepeated(field)) {
if (isMapType(ctx, messageDesc, field)) {
// We need a unique const within the `cast` statement
const varName = `entry${field.number}`;
chunks.push(code`
const ${varName} = ${readSnippet};
if (${varName}.value !== undefined) {
message.${fieldName}[${varName}.key] = ${varName}.value;
}
`);
} else if (packedType(field.type) === undefined) {
chunks.push(code`message.${fieldName}.push(${readSnippet});`);
} else {
chunks.push(code`
if ((tag & 7) === 2) {
const end2 = reader.uint32() + reader.pos;
while (reader.pos < end2) {
message.${fieldName}.push(${readSnippet});
}
} else {
message.${fieldName}.push(${readSnippet});
}
`);
}
} else if (isWithinOneOfThatShouldBeUnion(options, field)) {
let oneofName = maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
chunks.push(code`message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${readSnippet} };`);
} else {
chunks.push(code`message.${fieldName} = ${readSnippet};`);
}
chunks.push(code`break;`);
});
chunks.push(code`
default:
reader.skipType(tag & 7);
break;
`);
// and then wrap up the switch/while/return
chunks.push(code`}`);
chunks.push(code`}`);
chunks.push(code`return message;`);
chunks.push(code`}`);
return joinCode(chunks, { on: '\n' });
}
const Writer = imp('Writer@protobufjs/minimal');
const Reader = imp('Reader@protobufjs/minimal');
/** Creates a function to encode a message by loop overing the tags. */
function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorProto): Code {
const { options, utils } = ctx;
const chunks: Code[] = [];
// create the basic function declaration
chunks.push(code`
encode(
${messageDesc.field.length > 0 ? 'message' : '_'}: ${fullName},
writer: ${Writer} = ${Writer}.create(),
): ${Writer} {
`);
// then add a case for each field
messageDesc.field.forEach((field) => {
const fieldName = maybeSnakeToCamel(field.name, options);
// get a generic writer.doSomething based on the basic type
let writeSnippet: (place: string) => Code;
if (isScalar(field) || isEnum(field)) {
const tag = ((field.number << 3) | basicWireType(field.type)) >>> 0;
writeSnippet = (place) => code`writer.uint32(${tag}).${toReaderCall(field)}(${place})`;
} else if (isTimestamp(field) && (options.useDate === DateOption.DATE || options.useDate === DateOption.STRING)) {
const tag = ((field.number << 3) | 2) >>> 0;
const type = basicTypeName(ctx, field, { keepValueType: true });
writeSnippet = (place) =>
code`${type}.encode(${utils.toTimestamp}(${place}), writer.uint32(${tag}).fork()).ldelim()`;
} else if (isValueType(ctx, field)) {
const tag = ((field.number << 3) | 2) >>> 0;
const type = basicTypeName(ctx, field, { keepValueType: true });
writeSnippet = (place) => code`${type}.encode({ value: ${place}! }, writer.uint32(${tag}).fork()).ldelim()`;
} else if (isMessage(field)) {
const tag = ((field.number << 3) | 2) >>> 0;
const type = basicTypeName(ctx, field);
writeSnippet = (place) => code`${type}.encode(${place}, writer.uint32(${tag}).fork()).ldelim()`;
} else {
throw new Error(`Unhandled field ${field}`);
}
if (isRepeated(field)) {
if (options.useOptionals) {
chunks.push(code`
if (message.${fieldName} !== undefined) {
`);
}
if (isMapType(ctx, messageDesc, field)) {
chunks.push(code`
Object.entries(message.${fieldName}).forEach(([key, value]) => {
${writeSnippet('{ key: key as any, value }')};
});
`);
} else if (packedType(field.type) === undefined) {
chunks.push(code`
for (const v of message.${fieldName}) {
${writeSnippet('v!')};
}
`);
} else {
const tag = ((field.number << 3) | 2) >>> 0;
chunks.push(code`
writer.uint32(${tag}).fork();
for (const v of message.${fieldName}) {
writer.${toReaderCall(field)}(v);
}
writer.ldelim();
`);
}
if (options.useOptionals) {
chunks.push(code`
}
`);
}
} else if (isWithinOneOfThatShouldBeUnion(options, field)) {
let oneofName = maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
chunks.push(code`
if (message.${oneofName}?.$case === '${fieldName}') {
${writeSnippet(`message.${oneofName}.${fieldName}`)};
}
`);
} else if (isWithinOneOf(field)) {
// Oneofs don't have a default value check b/c they need to denote which-oneof presence
chunks.push(code`
if (message.${fieldName} !== undefined) {
${writeSnippet(`message.${fieldName}`)};
}
`);
} else if (isMessage(field)) {
chunks.push(code`
if (message.${fieldName} !== undefined) {
${writeSnippet(`message.${fieldName}`)};
}
`);
} else if (isScalar(field) || isEnum(field)) {
chunks.push(code`
if (${notDefaultCheck(ctx, field, `message.${fieldName}`)}) {
${writeSnippet(`message.${fieldName}`)};
}
`);
} else {
chunks.push(code`${writeSnippet(`message.${fieldName}`)};`);
}
});
chunks.push(code`return writer;`);
chunks.push(code`}`);
return joinCode(chunks, { on: '\n' });
}
/**
* Creates a function to decode a message from JSON.
*
* This is very similar to decode, we loop through looking for properties, with
* a few special cases for https://developers.google.com/protocol-buffers/docs/proto3#json.
* */
function generateFromJson(ctx: Context, fullName: string, messageDesc: DescriptorProto): Code {
const { options, utils, typeMap } = ctx;
const chunks: Code[] = [];
// create the basic function declaration
chunks.push(code`
fromJSON(${messageDesc.field.length > 0 ? 'object' : '_'}: any): ${fullName} {
const message = { ...base${fullName} } as ${fullName};
`);
// initialize all lists
messageDesc.field.filter(isRepeated).forEach((field) => {
const value = isMapType(ctx, messageDesc, field) ? '{}' : '[]';
const name = maybeSnakeToCamel(field.name, options);
chunks.push(code`message.${name} = ${value};`);
});
// add a check for each incoming field
messageDesc.field.forEach((field) => {
const fieldName = maybeSnakeToCamel(field.name, options);
// get a generic 'reader.doSomething' bit that is specific to the basic type
const readSnippet = (from: string): Code => {
if (isEnum(field)) {
const fromJson = getEnumMethod(typeMap, field.typeName, 'FromJSON');
return code`${fromJson}(${from})`;
} else if (isPrimitive(field)) {
// Convert primitives using the String(value)/Number(value)/bytesFromBase64(value)
if (isBytes(field)) {
if (options.env === EnvOption.NODE) {
return code`Buffer.from(${utils.bytesFromBase64}(${from}))`;
} else {
return code`${utils.bytesFromBase64}(${from})`;
}
} else if (isLong(field) && options.forceLong === LongOption.LONG) {
const cstr = capitalize(basicTypeName(ctx, field, { keepValueType: true }).toCodeString());
return code`${cstr}.fromString(${from})`;
} else {
const cstr = capitalize(basicTypeName(ctx, field, { keepValueType: true }).toCodeString());
return code`${cstr}(${from})`;
}
} else if (isTimestamp(field) && options.useDate === DateOption.STRING) {
return code`String(${from})`;
} else if (
isTimestamp(field) &&
(options.useDate === DateOption.DATE || options.useDate === DateOption.TIMESTAMP)
) {
return code`${utils.fromJsonTimestamp}(${from})`;
} else if (isValueType(ctx, field)) {
const valueType = valueTypeName(ctx, field.typeName)!;
if (isLongValueType(field) && options.forceLong === LongOption.LONG) {
return code`${capitalize(valueType.toCodeString())}.fromValue(${from})`;
} else if (isBytesValueType(field)) {
return code`new ${capitalize(valueType.toCodeString())}(${from})`;
} else {
return code`${capitalize(valueType.toCodeString())}(${from})`;
}
} else if (isMessage(field)) {
if (isRepeated(field) && isMapType(ctx, messageDesc, field)) {
const valueType = (typeMap.get(field.typeName)![2] as DescriptorProto).field[1];
if (isPrimitive(valueType)) {
// TODO Can we not copy/paste this from ^?
if (isBytes(valueType)) {
if (options.env === EnvOption.NODE) {
return code`Buffer.from(${utils.bytesFromBase64}(${from} as string))`;
} else {
return code`${utils.bytesFromBase64}(${from} as string)`;
}
} else if (isEnum(valueType)) {
return code`${from} as number`;
} else {
const cstr = capitalize(basicTypeName(ctx, valueType).toCodeString());
return code`${cstr}(${from})`;
}
} else if (isTimestamp(valueType) && options.useDate === DateOption.STRING) {
return code`String(${from})`;
} else if (
isTimestamp(valueType) &&
(options.useDate === DateOption.DATE || options.useDate === DateOption.TIMESTAMP)
) {
return code`${utils.fromJsonTimestamp}(${from})`;
} else {
const type = basicTypeName(ctx, valueType);
return code`${type}.fromJSON(${from})`;
}
} else {
const type = basicTypeName(ctx, field);
return code`${type}.fromJSON(${from})`;
}
} else {
throw new Error(`Unhandled field ${field}`);
}
};
// and then use the snippet to handle repeated fields if necessary
chunks.push(code`if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
if (isRepeated(field)) {
if (isMapType(ctx, messageDesc, field)) {
const i = maybeCastToNumber(ctx, messageDesc, field, 'key');
chunks.push(code`
Object.entries(object.${fieldName}).forEach(([key, value]) => {
message.${fieldName}${options.useOptionals ? '!' : ''}[${i}] = ${readSnippet('value')};
});
`);
} else {
chunks.push(code`
for (const e of object.${fieldName}) {
message.${fieldName}${options.useOptionals ? '!' : ''}.push(${readSnippet('e')});
}
`);
}
} else if (isWithinOneOfThatShouldBeUnion(options, field)) {
const oneofName = maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
chunks.push(code`
message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${readSnippet(`object.${fieldName}`)} }
`);
} else {
chunks.push(code`message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
}
// set the default value (TODO Support bytes)
if (
!isRepeated(field) &&
field.type !== FieldDescriptorProto_Type.TYPE_BYTES &&
options.oneof !== OneofOption.UNIONS
) {
const v = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field);
chunks.push(code`} else {`);
chunks.push(code`message.${fieldName} = ${v};`);
}
chunks.push(code`}`);
});
// and then wrap up the switch/while/return
chunks.push(code`return message`);
chunks.push(code`}`);
return joinCode(chunks, { on: '\n' });
}
function generateToJson(ctx: Context, fullName: string, messageDesc: DescriptorProto): Code {
const { options, utils, typeMap } = ctx;
const chunks: Code[] = [];
// create the basic function declaration
chunks.push(code`
toJSON(${messageDesc.field.length > 0 ? 'message' : '_'}: ${fullName}): unknown {
const obj: any = {};
`);
// then add a case for each field
messageDesc.field.forEach((field) => {
const fieldName = maybeSnakeToCamel(field.name, options);
const readSnippet = (from: string): Code => {
if (isEnum(field)) {
const toJson = getEnumMethod(typeMap, field.typeName, 'ToJSON');
return isWithinOneOf(field)
? code`${from} !== undefined ? ${toJson}(${from}) : undefined`
: code`${toJson}(${from})`;
} else if (isTimestamp(field) && options.useDate === DateOption.DATE) {
return code`${from} !== undefined ? ${from}.toISOString() : null`;
} else if (isTimestamp(field) && options.useDate === DateOption.STRING) {
return code`${from}`;
} else if (isTimestamp(field) && options.useDate === DateOption.TIMESTAMP) {
return code`${from} !== undefined ? ${utils.fromTimestamp}(${from}).toISOString() : null`;
} else if (isMapType(ctx, messageDesc, field)) {
// For map types, drill-in and then admittedly re-hard-code our per-value-type logic
const valueType = (typeMap.get(field.typeName)![2] as DescriptorProto).field[1];
if (isEnum(valueType)) {
const toJson = getEnumMethod(typeMap, valueType.typeName, 'ToJSON');
return code`${toJson}(${from})`;
} else if (isBytes(valueType)) {
return code`${utils.base64FromBytes}(${from})`;
} else if (isTimestamp(valueType) && options.useDate === DateOption.DATE) {
return code`${from}.toISOString()`;
} else if (isTimestamp(valueType) && options.useDate === DateOption.STRING) {
return code`${from}`;
} else if (isTimestamp(valueType) && options.useDate === DateOption.TIMESTAMP) {
return code`${utils.fromTimestamp}(${from}).toISOString()`;
} else if (isScalar(valueType)) {
return code`${from}`;
} else {
const type = basicTypeName(ctx, valueType);
return code`${type}.toJSON(${from})`;
}
} else if (isMessage(field) && !isValueType(ctx, field) && !isMapType(ctx, messageDesc, field)) {
const type = basicTypeName(ctx, field, { keepValueType: true });
return code`${from} ? ${type}.toJSON(${from}) : ${defaultValue(ctx, field)}`;
} else if (isBytes(field)) {
if (isWithinOneOf(field)) {
return code`${from} !== undefined ? ${utils.base64FromBytes}(${from}) : undefined`;
} else {
return code`${utils.base64FromBytes}(${from} !== undefined ? ${from} : ${defaultValue(ctx, field)})`;
}
} else if (isLong(field) && options.forceLong === LongOption.LONG) {
const v = isWithinOneOf(field) ? 'undefined' : defaultValue(ctx, field);
return code`(${from} || ${v}).toString()`;
} else {
return code`${from}`;
}
};
if (isMapType(ctx, messageDesc, field)) {
// Maps might need their values transformed, i.e. bytes --> base64
chunks.push(code`
obj.${fieldName} = {};
if (message.${fieldName}) {
Object.entries(message.${fieldName}).forEach(([k, v]) => {
obj.${fieldName}[k] = ${readSnippet('v')};
});
}
`);
} else if (isRepeated(field)) {
// Arrays might need their elements transformed
chunks.push(code`
if (message.${fieldName}) {
obj.${fieldName} = message.${fieldName}.map(e => ${readSnippet('e')});
} else {
obj.${fieldName} = [];
}