ts-proto 1.122.0 → 1.124.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.markdown +4 -1
- package/build/case.js +4 -4
- package/build/encode.js +21 -21
- package/build/enums.js +11 -11
- package/build/generate-generic-service-definition.js +2 -2
- package/build/generate-grpc-js.js +20 -20
- package/build/generate-grpc-web.js +11 -11
- package/build/generate-nestjs.js +15 -15
- package/build/generate-nice-grpc.js +5 -5
- package/build/generate-services.js +27 -27
- package/build/generate-type-registry.js +4 -4
- package/build/main.js +103 -103
- package/build/options.js +16 -12
- package/build/plugin.js +6 -6
- package/build/schema.js +37 -37
- package/build/sourceInfo.js +3 -3
- package/build/types.js +71 -71
- package/build/utils.js +14 -14
- package/build/visit.js +5 -5
- package/package.json +6 -6
package/build/main.js
CHANGED
|
@@ -23,11 +23,11 @@ function generateFile(ctx, fileDesc) {
|
|
|
23
23
|
const { options, utils } = ctx;
|
|
24
24
|
if (options.useOptionals === false) {
|
|
25
25
|
console.warn("ts-proto: Passing useOptionals as a boolean option is deprecated and will be removed in a future version. Please pass the string 'none' instead of false.");
|
|
26
|
-
options.useOptionals =
|
|
26
|
+
options.useOptionals = "none";
|
|
27
27
|
}
|
|
28
28
|
else if (options.useOptionals === true) {
|
|
29
29
|
console.warn("ts-proto: Passing useOptionals as a boolean option is deprecated and will be removed in a future version. Please pass the string 'messages' instead of true.");
|
|
30
|
-
options.useOptionals =
|
|
30
|
+
options.useOptionals = "messages";
|
|
31
31
|
}
|
|
32
32
|
// Google's protofiles are organized like Java, where package == the folder the file
|
|
33
33
|
// is in, and file == a specific service within the package. I.e. you can have multiple
|
|
@@ -41,7 +41,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
41
41
|
// We'll also assume that the fileDesc.name is already the `company/foo.proto` path, with
|
|
42
42
|
// the package already implicitly in it, so we won't re-append/strip/etc. it out/back in.
|
|
43
43
|
const suffix = `${options.fileSuffix}.ts`;
|
|
44
|
-
const moduleName = fileDesc.name.replace(
|
|
44
|
+
const moduleName = fileDesc.name.replace(".proto", suffix);
|
|
45
45
|
const chunks = [];
|
|
46
46
|
// Indicate this file's source protobuf package for reflective use with google.protobuf.Any
|
|
47
47
|
if (options.exportCommonSymbols) {
|
|
@@ -65,7 +65,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
65
65
|
});
|
|
66
66
|
// If nestJs=true export [package]_PACKAGE_NAME and [service]_SERVICE_NAME const
|
|
67
67
|
if (options.nestJs) {
|
|
68
|
-
const prefix = (0, case_1.camelToSnake)(fileDesc.package.replace(/\./g,
|
|
68
|
+
const prefix = (0, case_1.camelToSnake)(fileDesc.package.replace(/\./g, "_"));
|
|
69
69
|
chunks.push((0, ts_poet_1.code) `export const ${prefix}_PACKAGE_NAME = '${fileDesc.package}';`);
|
|
70
70
|
}
|
|
71
71
|
if (options.outputEncodeMethods || options.outputJsonMethods || options.outputTypeRegistry) {
|
|
@@ -93,22 +93,22 @@ function generateFile(ctx, fileDesc) {
|
|
|
93
93
|
staticMembers.push(generateFromPartial(ctx, fullName, message));
|
|
94
94
|
}
|
|
95
95
|
const structFieldNames = {
|
|
96
|
-
nullValue: (0, case_1.maybeSnakeToCamel)(
|
|
97
|
-
numberValue: (0, case_1.maybeSnakeToCamel)(
|
|
98
|
-
stringValue: (0, case_1.maybeSnakeToCamel)(
|
|
99
|
-
boolValue: (0, case_1.maybeSnakeToCamel)(
|
|
100
|
-
structValue: (0, case_1.maybeSnakeToCamel)(
|
|
101
|
-
listValue: (0, case_1.maybeSnakeToCamel)(
|
|
96
|
+
nullValue: (0, case_1.maybeSnakeToCamel)("null_value", ctx.options),
|
|
97
|
+
numberValue: (0, case_1.maybeSnakeToCamel)("number_value", ctx.options),
|
|
98
|
+
stringValue: (0, case_1.maybeSnakeToCamel)("string_value", ctx.options),
|
|
99
|
+
boolValue: (0, case_1.maybeSnakeToCamel)("bool_value", ctx.options),
|
|
100
|
+
structValue: (0, case_1.maybeSnakeToCamel)("struct_value", ctx.options),
|
|
101
|
+
listValue: (0, case_1.maybeSnakeToCamel)("list_value", ctx.options),
|
|
102
102
|
};
|
|
103
103
|
staticMembers.push(...generateWrap(ctx, fullTypeName, structFieldNames));
|
|
104
104
|
staticMembers.push(...generateUnwrap(ctx, fullTypeName, structFieldNames));
|
|
105
105
|
chunks.push((0, ts_poet_1.code) `
|
|
106
106
|
export const ${(0, ts_poet_1.def)(fullName)} = {
|
|
107
|
-
${(0, ts_poet_1.joinCode)(staticMembers, { on:
|
|
107
|
+
${(0, ts_poet_1.joinCode)(staticMembers, { on: ",\n\n" })}
|
|
108
108
|
};
|
|
109
109
|
`);
|
|
110
110
|
if (options.outputTypeRegistry) {
|
|
111
|
-
const messageTypeRegistry = (0, utils_1.impFile)(options,
|
|
111
|
+
const messageTypeRegistry = (0, utils_1.impFile)(options, "messageTypeRegistry@./typeRegistry");
|
|
112
112
|
chunks.push((0, ts_poet_1.code) `
|
|
113
113
|
${messageTypeRegistry}.set(${fullName}.$type, ${fullName});
|
|
114
114
|
`);
|
|
@@ -127,7 +127,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
127
127
|
// generate nestjs grpc service controller decorator
|
|
128
128
|
chunks.push((0, generate_nestjs_1.generateNestjsGrpcServiceMethodsDecorator)(ctx, serviceDesc));
|
|
129
129
|
let serviceConstName = `${(0, case_1.camelToSnake)(serviceDesc.name)}_NAME`;
|
|
130
|
-
if (!serviceDesc.name.toLowerCase().endsWith(
|
|
130
|
+
if (!serviceDesc.name.toLowerCase().endsWith("service")) {
|
|
131
131
|
serviceConstName = `${(0, case_1.camelToSnake)(serviceDesc.name)}_SERVICE_NAME`;
|
|
132
132
|
}
|
|
133
133
|
chunks.push((0, ts_poet_1.code) `export const ${serviceConstName} = "${serviceDesc.name}";`);
|
|
@@ -151,7 +151,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
151
151
|
if (options.outputClientImpl === true) {
|
|
152
152
|
chunks.push((0, generate_services_1.generateServiceClientImpl)(ctx, fileDesc, serviceDesc));
|
|
153
153
|
}
|
|
154
|
-
else if (options.outputClientImpl ===
|
|
154
|
+
else if (options.outputClientImpl === "grpc-web") {
|
|
155
155
|
chunks.push((0, generate_grpc_web_1.generateGrpcClientImpl)(ctx, fileDesc, serviceDesc));
|
|
156
156
|
chunks.push((0, generate_grpc_web_1.generateGrpcServiceDesc)(fileDesc, serviceDesc));
|
|
157
157
|
serviceDesc.method.forEach((method) => {
|
|
@@ -178,7 +178,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
178
178
|
if (options.outputClientImpl === true) {
|
|
179
179
|
chunks.push((0, generate_services_1.generateRpcType)(ctx, hasStreamingMethods));
|
|
180
180
|
}
|
|
181
|
-
else if (options.outputClientImpl ===
|
|
181
|
+
else if (options.outputClientImpl === "grpc-web") {
|
|
182
182
|
chunks.push((0, generate_grpc_web_1.addGrpcWebMisc)(ctx, hasServerStreamingMethods));
|
|
183
183
|
}
|
|
184
184
|
}
|
|
@@ -206,7 +206,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
206
206
|
svc.method[i] = methodInfo.getSource();
|
|
207
207
|
}
|
|
208
208
|
}
|
|
209
|
-
return [moduleName, (0, ts_poet_1.joinCode)(chunks, { on:
|
|
209
|
+
return [moduleName, (0, ts_poet_1.joinCode)(chunks, { on: "\n\n" })];
|
|
210
210
|
}
|
|
211
211
|
exports.generateFile = generateFile;
|
|
212
212
|
/** These are runtime utility methods used by the generated code. */
|
|
@@ -228,8 +228,8 @@ exports.makeUtils = makeUtils;
|
|
|
228
228
|
function makeLongUtils(options, bytes) {
|
|
229
229
|
// Regardless of which `forceLong` config option we're using, we always use
|
|
230
230
|
// the `long` library to either represent or at least sanity-check 64-bit values
|
|
231
|
-
const util = (0, utils_1.impFile)(options,
|
|
232
|
-
const configure = (0, utils_1.impFile)(options,
|
|
231
|
+
const util = (0, utils_1.impFile)(options, "util@protobufjs/minimal");
|
|
232
|
+
const configure = (0, utils_1.impFile)(options, "configure@protobufjs/minimal");
|
|
233
233
|
// Before esModuleInterop, we had to use 'import * as Long from long` b/c long is
|
|
234
234
|
// an `export =` module and exports only the Long constructor (which is callable).
|
|
235
235
|
// See https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require.
|
|
@@ -240,16 +240,16 @@ function makeLongUtils(options, bytes) {
|
|
|
240
240
|
//
|
|
241
241
|
// I.e there is not an import for long that "just works" in both esModuleInterop and
|
|
242
242
|
// not esModuleInterop.
|
|
243
|
-
const LongImp = options.esModuleInterop ? (0, ts_poet_1.imp)(
|
|
243
|
+
const LongImp = options.esModuleInterop ? (0, ts_poet_1.imp)("Long=long") : (0, ts_poet_1.imp)("Long*long");
|
|
244
244
|
const disclaimer = options.esModuleInterop
|
|
245
|
-
?
|
|
245
|
+
? ""
|
|
246
246
|
: `
|
|
247
247
|
// If you get a compile-error about 'Constructor<Long> and ... have no overlap',
|
|
248
248
|
// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'.`;
|
|
249
249
|
// Instead of exposing `LongImp` directly, let callers think that they are getting the
|
|
250
250
|
// `imp(Long)` but really it is that + our long initialization snippet. This means the
|
|
251
251
|
// initialization code will only be emitted in files that actually use the Long import.
|
|
252
|
-
const Long = (0, ts_poet_1.conditionalOutput)(
|
|
252
|
+
const Long = (0, ts_poet_1.conditionalOutput)("Long", (0, ts_poet_1.code) `
|
|
253
253
|
${disclaimer}
|
|
254
254
|
if (${util}.Long !== ${LongImp}) {
|
|
255
255
|
${util}.Long = ${LongImp} as any;
|
|
@@ -257,17 +257,17 @@ function makeLongUtils(options, bytes) {
|
|
|
257
257
|
}
|
|
258
258
|
`);
|
|
259
259
|
// TODO This is unused?
|
|
260
|
-
const numberToLong = (0, ts_poet_1.conditionalOutput)(
|
|
260
|
+
const numberToLong = (0, ts_poet_1.conditionalOutput)("numberToLong", (0, ts_poet_1.code) `
|
|
261
261
|
function numberToLong(number: number) {
|
|
262
262
|
return ${Long}.fromNumber(number);
|
|
263
263
|
}
|
|
264
264
|
`);
|
|
265
|
-
const longToString = (0, ts_poet_1.conditionalOutput)(
|
|
265
|
+
const longToString = (0, ts_poet_1.conditionalOutput)("longToString", (0, ts_poet_1.code) `
|
|
266
266
|
function longToString(long: ${Long}) {
|
|
267
267
|
return long.toString();
|
|
268
268
|
}
|
|
269
269
|
`);
|
|
270
|
-
const longToNumber = (0, ts_poet_1.conditionalOutput)(
|
|
270
|
+
const longToNumber = (0, ts_poet_1.conditionalOutput)("longToNumber", (0, ts_poet_1.code) `
|
|
271
271
|
function longToNumber(long: ${Long}): number {
|
|
272
272
|
if (long.gt(Number.MAX_SAFE_INTEGER)) {
|
|
273
273
|
throw new ${bytes.globalThis}.Error("Value is larger than Number.MAX_SAFE_INTEGER")
|
|
@@ -278,7 +278,7 @@ function makeLongUtils(options, bytes) {
|
|
|
278
278
|
return { numberToLong, longToNumber, longToString, Long };
|
|
279
279
|
}
|
|
280
280
|
function makeByteUtils() {
|
|
281
|
-
const globalThis = (0, ts_poet_1.conditionalOutput)(
|
|
281
|
+
const globalThis = (0, ts_poet_1.conditionalOutput)("globalThis", (0, ts_poet_1.code) `
|
|
282
282
|
declare var self: any | undefined;
|
|
283
283
|
declare var window: any | undefined;
|
|
284
284
|
declare var global: any | undefined;
|
|
@@ -290,7 +290,7 @@ function makeByteUtils() {
|
|
|
290
290
|
throw "Unable to locate global object";
|
|
291
291
|
})();
|
|
292
292
|
`);
|
|
293
|
-
const bytesFromBase64 = (0, ts_poet_1.conditionalOutput)(
|
|
293
|
+
const bytesFromBase64 = (0, ts_poet_1.conditionalOutput)("bytesFromBase64", (0, ts_poet_1.code) `
|
|
294
294
|
function bytesFromBase64(b64: string): Uint8Array {
|
|
295
295
|
if (${globalThis}.Buffer) {
|
|
296
296
|
return Uint8Array.from(${globalThis}.Buffer.from(b64, 'base64'));
|
|
@@ -304,7 +304,7 @@ function makeByteUtils() {
|
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
306
|
`);
|
|
307
|
-
const base64FromBytes = (0, ts_poet_1.conditionalOutput)(
|
|
307
|
+
const base64FromBytes = (0, ts_poet_1.conditionalOutput)("base64FromBytes", (0, ts_poet_1.code) `
|
|
308
308
|
function base64FromBytes(arr: Uint8Array): string {
|
|
309
309
|
if (${globalThis}.Buffer) {
|
|
310
310
|
return ${globalThis}.Buffer.from(arr).toString('base64')
|
|
@@ -320,20 +320,20 @@ function makeByteUtils() {
|
|
|
320
320
|
return { globalThis, bytesFromBase64, base64FromBytes };
|
|
321
321
|
}
|
|
322
322
|
function makeDeepPartial(options, longs) {
|
|
323
|
-
let oneofCase =
|
|
323
|
+
let oneofCase = "";
|
|
324
324
|
if (options.oneof === options_1.OneofOption.UNIONS) {
|
|
325
325
|
oneofCase = `
|
|
326
326
|
: T extends { $case: string }
|
|
327
327
|
? { [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]> } & { $case: T['$case'] }
|
|
328
328
|
`;
|
|
329
329
|
}
|
|
330
|
-
const maybeExport = options.exportCommonSymbols ?
|
|
330
|
+
const maybeExport = options.exportCommonSymbols ? "export" : "";
|
|
331
331
|
// Allow passing longs as numbers or strings, nad we'll convert them
|
|
332
|
-
const maybeLong = options.forceLong === options_1.LongOption.LONG ? (0, ts_poet_1.code) ` : T extends ${longs.Long} ? string | number | Long ` :
|
|
333
|
-
const Builtin = (0, ts_poet_1.conditionalOutput)(
|
|
332
|
+
const maybeLong = options.forceLong === options_1.LongOption.LONG ? (0, ts_poet_1.code) ` : T extends ${longs.Long} ? string | number | Long ` : "";
|
|
333
|
+
const Builtin = (0, ts_poet_1.conditionalOutput)("Builtin", (0, ts_poet_1.code) `type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;`);
|
|
334
334
|
// Based on https://github.com/sindresorhus/type-fest/pull/259
|
|
335
|
-
const maybeExcludeType = options.outputTypeRegistry ? `| '$type'` :
|
|
336
|
-
const Exact = (0, ts_poet_1.conditionalOutput)(
|
|
335
|
+
const maybeExcludeType = options.outputTypeRegistry ? `| '$type'` : "";
|
|
336
|
+
const Exact = (0, ts_poet_1.conditionalOutput)("Exact", (0, ts_poet_1.code) `
|
|
337
337
|
type KeysOfUnion<T> = T extends T ? keyof T : never;
|
|
338
338
|
${maybeExport} type Exact<P, I extends P> = P extends ${Builtin}
|
|
339
339
|
? P
|
|
@@ -342,7 +342,7 @@ function makeDeepPartial(options, longs) {
|
|
|
342
342
|
`);
|
|
343
343
|
// Based on the type from ts-essentials
|
|
344
344
|
const keys = options.outputTypeRegistry ? (0, ts_poet_1.code) `Exclude<keyof T, '$type'>` : (0, ts_poet_1.code) `keyof T`;
|
|
345
|
-
const DeepPartial = (0, ts_poet_1.conditionalOutput)(
|
|
345
|
+
const DeepPartial = (0, ts_poet_1.conditionalOutput)("DeepPartial", (0, ts_poet_1.code) `
|
|
346
346
|
${maybeExport} type DeepPartial<T> = T extends ${Builtin}
|
|
347
347
|
? T
|
|
348
348
|
${maybeLong}
|
|
@@ -357,13 +357,13 @@ function makeDeepPartial(options, longs) {
|
|
|
357
357
|
return { Builtin, DeepPartial, Exact };
|
|
358
358
|
}
|
|
359
359
|
function makeObjectIdMethods(options) {
|
|
360
|
-
const mongodb = (0, ts_poet_1.imp)(
|
|
361
|
-
const fromProtoObjectId = (0, ts_poet_1.conditionalOutput)(
|
|
360
|
+
const mongodb = (0, ts_poet_1.imp)("mongodb*mongodb");
|
|
361
|
+
const fromProtoObjectId = (0, ts_poet_1.conditionalOutput)("fromProtoObjectId", (0, ts_poet_1.code) `
|
|
362
362
|
function fromProtoObjectId(oid: ObjectId): ${mongodb}.ObjectId {
|
|
363
363
|
return new ${mongodb}.ObjectId(oid.value);
|
|
364
364
|
}
|
|
365
365
|
`);
|
|
366
|
-
const fromJsonObjectId = (0, ts_poet_1.conditionalOutput)(
|
|
366
|
+
const fromJsonObjectId = (0, ts_poet_1.conditionalOutput)("fromJsonObjectId", (0, ts_poet_1.code) `
|
|
367
367
|
function fromJsonObjectId(o: any): ${mongodb}.ObjectId {
|
|
368
368
|
if (o instanceof ${mongodb}.ObjectId) {
|
|
369
369
|
return o;
|
|
@@ -374,7 +374,7 @@ function makeObjectIdMethods(options) {
|
|
|
374
374
|
}
|
|
375
375
|
}
|
|
376
376
|
`);
|
|
377
|
-
const toProtoObjectId = (0, ts_poet_1.conditionalOutput)(
|
|
377
|
+
const toProtoObjectId = (0, ts_poet_1.conditionalOutput)("toProtoObjectId", (0, ts_poet_1.code) `
|
|
378
378
|
function toProtoObjectId(oid: ${mongodb}.ObjectId): ObjectId {
|
|
379
379
|
const value = oid.toString();
|
|
380
380
|
return { value };
|
|
@@ -383,22 +383,22 @@ function makeObjectIdMethods(options) {
|
|
|
383
383
|
return { fromJsonObjectId, fromProtoObjectId, toProtoObjectId };
|
|
384
384
|
}
|
|
385
385
|
function makeTimestampMethods(options, longs) {
|
|
386
|
-
const Timestamp = (0, utils_1.impProto)(options,
|
|
387
|
-
let seconds =
|
|
388
|
-
let toNumberCode =
|
|
386
|
+
const Timestamp = (0, utils_1.impProto)(options, "google/protobuf/timestamp", "Timestamp");
|
|
387
|
+
let seconds = "date.getTime() / 1_000";
|
|
388
|
+
let toNumberCode = "t.seconds";
|
|
389
389
|
if (options.forceLong === options_1.LongOption.LONG) {
|
|
390
|
-
toNumberCode =
|
|
390
|
+
toNumberCode = "t.seconds.toNumber()";
|
|
391
391
|
seconds = (0, ts_poet_1.code) `${longs.numberToLong}(date.getTime() / 1_000)`;
|
|
392
392
|
}
|
|
393
393
|
else if (options.forceLong === options_1.LongOption.STRING) {
|
|
394
|
-
toNumberCode =
|
|
394
|
+
toNumberCode = "Number(t.seconds)";
|
|
395
395
|
// Must discard the fractional piece here
|
|
396
396
|
// Otherwise the fraction ends up on the seconds when parsed as a Long
|
|
397
397
|
// (note this only occurs when the string is > 8 characters)
|
|
398
|
-
seconds =
|
|
398
|
+
seconds = "Math.trunc(date.getTime() / 1_000).toString()";
|
|
399
399
|
}
|
|
400
|
-
const maybeTypeField = options.outputTypeRegistry ? `$type: 'google.protobuf.Timestamp',` :
|
|
401
|
-
const toTimestamp = (0, ts_poet_1.conditionalOutput)(
|
|
400
|
+
const maybeTypeField = options.outputTypeRegistry ? `$type: 'google.protobuf.Timestamp',` : "";
|
|
401
|
+
const toTimestamp = (0, ts_poet_1.conditionalOutput)("toTimestamp", options.useDate === options_1.DateOption.STRING
|
|
402
402
|
? (0, ts_poet_1.code) `
|
|
403
403
|
function toTimestamp(dateStr: string): ${Timestamp} {
|
|
404
404
|
const date = new Date(dateStr);
|
|
@@ -414,7 +414,7 @@ function makeTimestampMethods(options, longs) {
|
|
|
414
414
|
return { ${maybeTypeField} seconds, nanos };
|
|
415
415
|
}
|
|
416
416
|
`);
|
|
417
|
-
const fromTimestamp = (0, ts_poet_1.conditionalOutput)(
|
|
417
|
+
const fromTimestamp = (0, ts_poet_1.conditionalOutput)("fromTimestamp", options.useDate === options_1.DateOption.STRING
|
|
418
418
|
? (0, ts_poet_1.code) `
|
|
419
419
|
function fromTimestamp(t: ${Timestamp}): string {
|
|
420
420
|
let millis = ${toNumberCode} * 1_000;
|
|
@@ -429,7 +429,7 @@ function makeTimestampMethods(options, longs) {
|
|
|
429
429
|
return new Date(millis);
|
|
430
430
|
}
|
|
431
431
|
`);
|
|
432
|
-
const fromJsonTimestamp = (0, ts_poet_1.conditionalOutput)(
|
|
432
|
+
const fromJsonTimestamp = (0, ts_poet_1.conditionalOutput)("fromJsonTimestamp", options.useDate === options_1.DateOption.DATE
|
|
433
433
|
? (0, ts_poet_1.code) `
|
|
434
434
|
function fromJsonTimestamp(o: any): Date {
|
|
435
435
|
if (o instanceof Date) {
|
|
@@ -455,18 +455,18 @@ function makeTimestampMethods(options, longs) {
|
|
|
455
455
|
return { toTimestamp, fromTimestamp, fromJsonTimestamp };
|
|
456
456
|
}
|
|
457
457
|
function makeComparisonUtils() {
|
|
458
|
-
const isObject = (0, ts_poet_1.conditionalOutput)(
|
|
458
|
+
const isObject = (0, ts_poet_1.conditionalOutput)("isObject", (0, ts_poet_1.code) `
|
|
459
459
|
function isObject(value: any): boolean {
|
|
460
460
|
return typeof value === 'object' && value !== null;
|
|
461
461
|
}`);
|
|
462
|
-
const isSet = (0, ts_poet_1.conditionalOutput)(
|
|
462
|
+
const isSet = (0, ts_poet_1.conditionalOutput)("isSet", (0, ts_poet_1.code) `
|
|
463
463
|
function isSet(value: any): boolean {
|
|
464
464
|
return value !== null && value !== undefined;
|
|
465
465
|
}`);
|
|
466
466
|
return { isObject, isSet };
|
|
467
467
|
}
|
|
468
468
|
function makeNiceGrpcServerStreamingMethodResult() {
|
|
469
|
-
const NiceGrpcServerStreamingMethodResult = (0, ts_poet_1.conditionalOutput)(
|
|
469
|
+
const NiceGrpcServerStreamingMethodResult = (0, ts_poet_1.conditionalOutput)("ServerStreamingMethodResult", (0, ts_poet_1.code) `
|
|
470
470
|
export type ServerStreamingMethodResult<Response> = {
|
|
471
471
|
[Symbol.asyncIterator](): AsyncIterator<Response, void>;
|
|
472
472
|
};
|
|
@@ -474,7 +474,7 @@ function makeNiceGrpcServerStreamingMethodResult() {
|
|
|
474
474
|
return { NiceGrpcServerStreamingMethodResult };
|
|
475
475
|
}
|
|
476
476
|
function makeGrpcWebErrorClass() {
|
|
477
|
-
const GrpcWebError = (0, ts_poet_1.conditionalOutput)(
|
|
477
|
+
const GrpcWebError = (0, ts_poet_1.conditionalOutput)("GrpcWebError", (0, ts_poet_1.code) `
|
|
478
478
|
export class GrpcWebError extends Error {
|
|
479
479
|
constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) {
|
|
480
480
|
super(message);
|
|
@@ -510,11 +510,11 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
|
|
|
510
510
|
(0, utils_1.maybeAddComment)(info, chunks, (_a = fieldDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
|
|
511
511
|
const name = (0, case_1.maybeSnakeToCamel)(fieldDesc.name, options);
|
|
512
512
|
const type = (0, types_1.toTypeName)(ctx, messageDesc, fieldDesc);
|
|
513
|
-
const q = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options) ?
|
|
513
|
+
const q = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options) ? "?" : "";
|
|
514
514
|
chunks.push((0, ts_poet_1.code) `${name}${q}: ${type}, `);
|
|
515
515
|
});
|
|
516
516
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
517
|
-
return (0, ts_poet_1.joinCode)(chunks, { on:
|
|
517
|
+
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
518
518
|
}
|
|
519
519
|
function generateOneofProperty(ctx, messageDesc, oneofIndex, sourceInfo) {
|
|
520
520
|
const { options } = ctx;
|
|
@@ -523,7 +523,7 @@ function generateOneofProperty(ctx, messageDesc, oneofIndex, sourceInfo) {
|
|
|
523
523
|
let fieldName = (0, case_1.maybeSnakeToCamel)(f.name, options);
|
|
524
524
|
let typeName = (0, types_1.toTypeName)(ctx, messageDesc, f);
|
|
525
525
|
return (0, ts_poet_1.code) `{ $case: '${fieldName}', ${fieldName}: ${typeName} }`;
|
|
526
|
-
}), { on:
|
|
526
|
+
}), { on: " | " });
|
|
527
527
|
const name = (0, case_1.maybeSnakeToCamel)(messageDesc.oneofDecl[oneofIndex].name, options);
|
|
528
528
|
return (0, ts_poet_1.code) `${name}?: ${unionType},`;
|
|
529
529
|
/*
|
|
@@ -564,11 +564,11 @@ function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
|
|
|
564
564
|
}
|
|
565
565
|
const name = (0, case_1.maybeSnakeToCamel)(field.name, ctx.options);
|
|
566
566
|
const val = (0, types_1.isWithinOneOf)(field)
|
|
567
|
-
?
|
|
567
|
+
? "undefined"
|
|
568
568
|
: (0, types_1.isMapType)(ctx, messageDesc, field)
|
|
569
|
-
?
|
|
569
|
+
? "{}"
|
|
570
570
|
: (0, types_1.isRepeated)(field)
|
|
571
|
-
?
|
|
571
|
+
? "[]"
|
|
572
572
|
: (0, types_1.defaultValue)(ctx, field);
|
|
573
573
|
fields.push((0, ts_poet_1.code) `${name}: ${val}`);
|
|
574
574
|
}
|
|
@@ -577,7 +577,7 @@ function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
|
|
|
577
577
|
}
|
|
578
578
|
return (0, ts_poet_1.code) `
|
|
579
579
|
function createBase${fullName}(): ${fullName} {
|
|
580
|
-
return { ${(0, ts_poet_1.joinCode)(fields, { on:
|
|
580
|
+
return { ${(0, ts_poet_1.joinCode)(fields, { on: "," })} };
|
|
581
581
|
}
|
|
582
582
|
`;
|
|
583
583
|
}
|
|
@@ -589,7 +589,7 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
589
589
|
if (options.usePrototypeForDefaults) {
|
|
590
590
|
createBase = (0, ts_poet_1.code) `Object.create(${createBase}) as ${fullName}`;
|
|
591
591
|
}
|
|
592
|
-
const Reader = (0, utils_1.impFile)(ctx.options,
|
|
592
|
+
const Reader = (0, utils_1.impFile)(ctx.options, "Reader@protobufjs/minimal");
|
|
593
593
|
// create the basic function declaration
|
|
594
594
|
chunks.push((0, ts_poet_1.code) `
|
|
595
595
|
decode(
|
|
@@ -635,7 +635,7 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
635
635
|
}
|
|
636
636
|
else if ((0, types_1.isEnum)(field)) {
|
|
637
637
|
if (options.stringEnums) {
|
|
638
|
-
const fromJson = (0, types_1.getEnumMethod)(ctx, field.typeName,
|
|
638
|
+
const fromJson = (0, types_1.getEnumMethod)(ctx, field.typeName, "FromJSON");
|
|
639
639
|
readSnippet = (0, ts_poet_1.code) `${fromJson}(${readSnippet})`;
|
|
640
640
|
}
|
|
641
641
|
else {
|
|
@@ -671,7 +671,7 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
671
671
|
}
|
|
672
672
|
// and then use the snippet to handle repeated fields if necessary
|
|
673
673
|
if ((0, types_1.isRepeated)(field)) {
|
|
674
|
-
const maybeNonNullAssertion = ctx.options.useOptionals ===
|
|
674
|
+
const maybeNonNullAssertion = ctx.options.useOptionals === "all" ? "!" : "";
|
|
675
675
|
if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
|
|
676
676
|
// We need a unique const within the `cast` statement
|
|
677
677
|
const varName = `entry${field.number}`;
|
|
@@ -728,17 +728,17 @@ function generateDecode(ctx, fullName, messageDesc) {
|
|
|
728
728
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
729
729
|
chunks.push((0, ts_poet_1.code) `return message;`);
|
|
730
730
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
731
|
-
return (0, ts_poet_1.joinCode)(chunks, { on:
|
|
731
|
+
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
732
732
|
}
|
|
733
733
|
/** Creates a function to encode a message by loop overing the tags. */
|
|
734
734
|
function generateEncode(ctx, fullName, messageDesc) {
|
|
735
735
|
const { options, utils, typeMap } = ctx;
|
|
736
736
|
const chunks = [];
|
|
737
|
-
const Writer = (0, utils_1.impFile)(ctx.options,
|
|
737
|
+
const Writer = (0, utils_1.impFile)(ctx.options, "Writer@protobufjs/minimal");
|
|
738
738
|
// create the basic function declaration
|
|
739
739
|
chunks.push((0, ts_poet_1.code) `
|
|
740
740
|
encode(
|
|
741
|
-
${messageDesc.field.length > 0 || options.unknownFields ?
|
|
741
|
+
${messageDesc.field.length > 0 || options.unknownFields ? "message" : "_"}: ${fullName},
|
|
742
742
|
writer: ${Writer} = ${Writer}.create(),
|
|
743
743
|
): ${Writer} {
|
|
744
744
|
`);
|
|
@@ -749,7 +749,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
749
749
|
let writeSnippet;
|
|
750
750
|
if ((0, types_1.isEnum)(field) && options.stringEnums) {
|
|
751
751
|
const tag = ((field.number << 3) | (0, types_1.basicWireType)(field.type)) >>> 0;
|
|
752
|
-
const toNumber = (0, types_1.getEnumMethod)(ctx, field.typeName,
|
|
752
|
+
const toNumber = (0, types_1.getEnumMethod)(ctx, field.typeName, "ToNumber");
|
|
753
753
|
writeSnippet = (place) => (0, ts_poet_1.code) `writer.uint32(${tag}).${(0, types_1.toReaderCall)(field)}(${toNumber}(${place}))`;
|
|
754
754
|
}
|
|
755
755
|
else if ((0, types_1.isScalar)(field) || (0, types_1.isEnum)(field)) {
|
|
@@ -767,7 +767,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
767
767
|
writeSnippet = (place) => (0, ts_poet_1.code) `${type}.encode(${utils.toTimestamp}(${place}), writer.uint32(${tag}).fork()).ldelim()`;
|
|
768
768
|
}
|
|
769
769
|
else if ((0, types_1.isValueType)(ctx, field)) {
|
|
770
|
-
const maybeTypeField = options.outputTypeRegistry ? `$type: '${field.typeName.slice(1)}',` :
|
|
770
|
+
const maybeTypeField = options.outputTypeRegistry ? `$type: '${field.typeName.slice(1)}',` : "";
|
|
771
771
|
const type = (0, types_1.basicTypeName)(ctx, field, { keepValueType: true });
|
|
772
772
|
const wrappedValue = (place) => {
|
|
773
773
|
if ((0, types_1.isAnyValueType)(field) || (0, types_1.isListValueType)(field) || (0, types_1.isStructType)(field) || (0, types_1.isFieldMaskType)(field)) {
|
|
@@ -790,7 +790,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
790
790
|
if ((0, types_1.isRepeated)(field)) {
|
|
791
791
|
if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
|
|
792
792
|
const valueType = typeMap.get(field.typeName)[2].field[1];
|
|
793
|
-
const maybeTypeField = options.outputTypeRegistry ? `$type: '${field.typeName.slice(1)}',` :
|
|
793
|
+
const maybeTypeField = options.outputTypeRegistry ? `$type: '${field.typeName.slice(1)}',` : "";
|
|
794
794
|
const entryWriteSnippet = (0, types_1.isValueType)(ctx, valueType)
|
|
795
795
|
? (0, ts_poet_1.code) `
|
|
796
796
|
if (value !== undefined) {
|
|
@@ -798,7 +798,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
798
798
|
}
|
|
799
799
|
`
|
|
800
800
|
: writeSnippet(`{ ${maybeTypeField} key: key as any, value }`);
|
|
801
|
-
const optionalAlternative = isOptional ?
|
|
801
|
+
const optionalAlternative = isOptional ? " || {}" : "";
|
|
802
802
|
chunks.push((0, ts_poet_1.code) `
|
|
803
803
|
Object.entries(message.${fieldName}${optionalAlternative}).forEach(([key, value]) => {
|
|
804
804
|
${entryWriteSnippet}
|
|
@@ -808,7 +808,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
808
808
|
else if ((0, types_1.packedType)(field.type) === undefined) {
|
|
809
809
|
const listWriteSnippet = (0, ts_poet_1.code) `
|
|
810
810
|
for (const v of message.${fieldName}) {
|
|
811
|
-
${writeSnippet(
|
|
811
|
+
${writeSnippet("v!")};
|
|
812
812
|
}
|
|
813
813
|
`;
|
|
814
814
|
if (isOptional) {
|
|
@@ -828,7 +828,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
828
828
|
// embedded inside of it, and we want to drop that so that we can encode it packed
|
|
829
829
|
// (i.e. just one tag and multiple values).
|
|
830
830
|
const tag = ((field.number << 3) | 2) >>> 0;
|
|
831
|
-
const toNumber = (0, types_1.getEnumMethod)(ctx, field.typeName,
|
|
831
|
+
const toNumber = (0, types_1.getEnumMethod)(ctx, field.typeName, "ToNumber");
|
|
832
832
|
const listWriteSnippet = (0, ts_poet_1.code) `
|
|
833
833
|
writer.uint32(${tag}).fork();
|
|
834
834
|
for (const v of message.${fieldName}) {
|
|
@@ -921,7 +921,7 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
921
921
|
}
|
|
922
922
|
chunks.push((0, ts_poet_1.code) `return writer;`);
|
|
923
923
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
924
|
-
return (0, ts_poet_1.joinCode)(chunks, { on:
|
|
924
|
+
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
925
925
|
}
|
|
926
926
|
/**
|
|
927
927
|
* Creates a function to decode a message from JSON.
|
|
@@ -934,7 +934,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
934
934
|
const chunks = [];
|
|
935
935
|
// create the basic function declaration
|
|
936
936
|
chunks.push((0, ts_poet_1.code) `
|
|
937
|
-
fromJSON(${messageDesc.field.length > 0 ?
|
|
937
|
+
fromJSON(${messageDesc.field.length > 0 ? "object" : "_"}: any): ${fullName} {
|
|
938
938
|
return {
|
|
939
939
|
`);
|
|
940
940
|
if (ctx.options.outputTypeRegistry) {
|
|
@@ -942,7 +942,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
942
942
|
}
|
|
943
943
|
const oneofFieldsCases = messageDesc.oneofDecl.map((oneof, oneofIndex) => messageDesc.field.filter(types_1.isWithinOneOf).filter((field) => field.oneofIndex === oneofIndex));
|
|
944
944
|
const canonicalFromJson = {
|
|
945
|
-
[
|
|
945
|
+
["google.protobuf.FieldMask"]: {
|
|
946
946
|
paths: (from) => (0, ts_poet_1.code) `typeof(${from}) === 'string'
|
|
947
947
|
? ${from}.split(",").filter(Boolean)
|
|
948
948
|
: Array.isArray(${from}?.paths)
|
|
@@ -955,12 +955,12 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
955
955
|
var _a;
|
|
956
956
|
const fieldName = (0, case_1.maybeSnakeToCamel)(field.name, options);
|
|
957
957
|
const jsonName = (0, utils_1.getFieldJsonName)(field, options);
|
|
958
|
-
const jsonProperty = (0, utils_1.getPropertyAccessor)(
|
|
959
|
-
const jsonPropertyOptional = (0, utils_1.getPropertyAccessor)(
|
|
958
|
+
const jsonProperty = (0, utils_1.getPropertyAccessor)("object", jsonName);
|
|
959
|
+
const jsonPropertyOptional = (0, utils_1.getPropertyAccessor)("object", jsonName, true);
|
|
960
960
|
// get code that extracts value from incoming object
|
|
961
961
|
const readSnippet = (from) => {
|
|
962
962
|
if ((0, types_1.isEnum)(field)) {
|
|
963
|
-
const fromJson = (0, types_1.getEnumMethod)(ctx, field.typeName,
|
|
963
|
+
const fromJson = (0, types_1.getEnumMethod)(ctx, field.typeName, "FromJSON");
|
|
964
964
|
return (0, ts_poet_1.code) `${fromJson}(${from})`;
|
|
965
965
|
}
|
|
966
966
|
else if ((0, types_1.isPrimitive)(field)) {
|
|
@@ -1031,7 +1031,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1031
1031
|
return (0, ts_poet_1.code) `Long.fromValue(${from} as Long | string)`;
|
|
1032
1032
|
}
|
|
1033
1033
|
else if ((0, types_1.isEnum)(valueField)) {
|
|
1034
|
-
const fromJson = (0, types_1.getEnumMethod)(ctx, valueField.typeName,
|
|
1034
|
+
const fromJson = (0, types_1.getEnumMethod)(ctx, valueField.typeName, "FromJSON");
|
|
1035
1035
|
return (0, ts_poet_1.code) `${fromJson}(${from})`;
|
|
1036
1036
|
}
|
|
1037
1037
|
else {
|
|
@@ -1070,23 +1070,23 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1070
1070
|
};
|
|
1071
1071
|
// and then use the snippet to handle repeated fields if necessary
|
|
1072
1072
|
if ((_a = canonicalFromJson[fullTypeName]) === null || _a === void 0 ? void 0 : _a[fieldName]) {
|
|
1073
|
-
chunks.push((0, ts_poet_1.code) `${fieldName}: ${canonicalFromJson[fullTypeName][fieldName](
|
|
1073
|
+
chunks.push((0, ts_poet_1.code) `${fieldName}: ${canonicalFromJson[fullTypeName][fieldName]("object")},`);
|
|
1074
1074
|
}
|
|
1075
1075
|
else if ((0, types_1.isRepeated)(field)) {
|
|
1076
1076
|
if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
|
|
1077
1077
|
const fieldType = (0, types_1.toTypeName)(ctx, messageDesc, field);
|
|
1078
|
-
const i = maybeCastToNumber(ctx, messageDesc, field,
|
|
1078
|
+
const i = maybeCastToNumber(ctx, messageDesc, field, "key");
|
|
1079
1079
|
chunks.push((0, ts_poet_1.code) `
|
|
1080
1080
|
${fieldName}: ${ctx.utils.isObject}(${jsonProperty})
|
|
1081
1081
|
? Object.entries(${jsonProperty}).reduce<${fieldType}>((acc, [key, value]) => {
|
|
1082
|
-
acc[${i}] = ${readSnippet(
|
|
1082
|
+
acc[${i}] = ${readSnippet("value")};
|
|
1083
1083
|
return acc;
|
|
1084
1084
|
}, {})
|
|
1085
1085
|
: {},
|
|
1086
1086
|
`);
|
|
1087
1087
|
}
|
|
1088
1088
|
else {
|
|
1089
|
-
const readValueSnippet = readSnippet(
|
|
1089
|
+
const readValueSnippet = readSnippet("e");
|
|
1090
1090
|
if (readValueSnippet.toString() === (0, ts_poet_1.code) `e`.toString()) {
|
|
1091
1091
|
chunks.push((0, ts_poet_1.code) `${fieldName}: Array.isArray(${jsonPropertyOptional}) ? [...${jsonProperty}] : [],`);
|
|
1092
1092
|
}
|
|
@@ -1132,7 +1132,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1132
1132
|
`);
|
|
1133
1133
|
}
|
|
1134
1134
|
else {
|
|
1135
|
-
const fallback = (0, types_1.isWithinOneOf)(field) ?
|
|
1135
|
+
const fallback = (0, types_1.isWithinOneOf)(field) ? "undefined" : (0, types_1.defaultValue)(ctx, field);
|
|
1136
1136
|
chunks.push((0, ts_poet_1.code) `
|
|
1137
1137
|
${fieldName}: ${ctx.utils.isSet}(${jsonProperty})
|
|
1138
1138
|
? ${readSnippet(`${jsonProperty}`)}
|
|
@@ -1143,7 +1143,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1143
1143
|
// and then wrap up the switch/while/return
|
|
1144
1144
|
chunks.push((0, ts_poet_1.code) `};`);
|
|
1145
1145
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
1146
|
-
return (0, ts_poet_1.joinCode)(chunks, { on:
|
|
1146
|
+
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
1147
1147
|
}
|
|
1148
1148
|
function generateCanonicalToJson(fullName, fullProtobufTypeName) {
|
|
1149
1149
|
if ((0, types_1.isFieldMaskTypeName)(fullProtobufTypeName)) {
|
|
@@ -1161,21 +1161,21 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1161
1161
|
const canonicalToJson = generateCanonicalToJson(fullName, fullProtobufTypeName);
|
|
1162
1162
|
if (canonicalToJson) {
|
|
1163
1163
|
chunks.push(canonicalToJson);
|
|
1164
|
-
return (0, ts_poet_1.joinCode)(chunks, { on:
|
|
1164
|
+
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
1165
1165
|
}
|
|
1166
1166
|
// create the basic function declaration
|
|
1167
1167
|
chunks.push((0, ts_poet_1.code) `
|
|
1168
|
-
toJSON(${messageDesc.field.length > 0 ?
|
|
1168
|
+
toJSON(${messageDesc.field.length > 0 ? "message" : "_"}: ${fullName}): unknown {
|
|
1169
1169
|
const obj: any = {};
|
|
1170
1170
|
`);
|
|
1171
1171
|
// then add a case for each field
|
|
1172
1172
|
messageDesc.field.forEach((field) => {
|
|
1173
1173
|
const fieldName = (0, case_1.maybeSnakeToCamel)(field.name, options);
|
|
1174
1174
|
const jsonName = (0, utils_1.getFieldJsonName)(field, options);
|
|
1175
|
-
const jsonProperty = (0, utils_1.getPropertyAccessor)(
|
|
1175
|
+
const jsonProperty = (0, utils_1.getPropertyAccessor)("obj", jsonName);
|
|
1176
1176
|
const readSnippet = (from) => {
|
|
1177
1177
|
if ((0, types_1.isEnum)(field)) {
|
|
1178
|
-
const toJson = (0, types_1.getEnumMethod)(ctx, field.typeName,
|
|
1178
|
+
const toJson = (0, types_1.getEnumMethod)(ctx, field.typeName, "ToJSON");
|
|
1179
1179
|
return (0, types_1.isWithinOneOf)(field)
|
|
1180
1180
|
? (0, ts_poet_1.code) `${from} !== undefined ? ${toJson}(${from}) : undefined`
|
|
1181
1181
|
: (0, ts_poet_1.code) `${toJson}(${from})`;
|
|
@@ -1196,7 +1196,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1196
1196
|
// For map types, drill-in and then admittedly re-hard-code our per-value-type logic
|
|
1197
1197
|
const valueType = typeMap.get(field.typeName)[2].field[1];
|
|
1198
1198
|
if ((0, types_1.isEnum)(valueType)) {
|
|
1199
|
-
const toJson = (0, types_1.getEnumMethod)(ctx, valueType.typeName,
|
|
1199
|
+
const toJson = (0, types_1.getEnumMethod)(ctx, valueType.typeName, "ToJSON");
|
|
1200
1200
|
return (0, ts_poet_1.code) `${toJson}(${from})`;
|
|
1201
1201
|
}
|
|
1202
1202
|
else if ((0, types_1.isBytes)(valueType)) {
|
|
@@ -1251,7 +1251,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1251
1251
|
}
|
|
1252
1252
|
}
|
|
1253
1253
|
else if ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.LONG) {
|
|
1254
|
-
const v = (0, types_1.isWithinOneOf)(field) ?
|
|
1254
|
+
const v = (0, types_1.isWithinOneOf)(field) ? "undefined" : (0, types_1.defaultValue)(ctx, field);
|
|
1255
1255
|
return (0, ts_poet_1.code) `(${from} || ${v}).toString()`;
|
|
1256
1256
|
}
|
|
1257
1257
|
else if ((0, types_1.isWholeNumber)(field) && !((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.STRING)) {
|
|
@@ -1267,7 +1267,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1267
1267
|
${jsonProperty} = {};
|
|
1268
1268
|
if (message.${fieldName}) {
|
|
1269
1269
|
Object.entries(message.${fieldName}).forEach(([k, v]) => {
|
|
1270
|
-
${jsonProperty}[k] = ${readSnippet(
|
|
1270
|
+
${jsonProperty}[k] = ${readSnippet("v")};
|
|
1271
1271
|
});
|
|
1272
1272
|
}
|
|
1273
1273
|
`);
|
|
@@ -1276,7 +1276,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1276
1276
|
// Arrays might need their elements transformed
|
|
1277
1277
|
chunks.push((0, ts_poet_1.code) `
|
|
1278
1278
|
if (message.${fieldName}) {
|
|
1279
|
-
${jsonProperty} = message.${fieldName}.map(e => ${readSnippet(
|
|
1279
|
+
${jsonProperty} = message.${fieldName}.map(e => ${readSnippet("e")});
|
|
1280
1280
|
} else {
|
|
1281
1281
|
${jsonProperty} = [];
|
|
1282
1282
|
}
|
|
@@ -1295,13 +1295,13 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1295
1295
|
});
|
|
1296
1296
|
chunks.push((0, ts_poet_1.code) `return obj;`);
|
|
1297
1297
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
1298
|
-
return (0, ts_poet_1.joinCode)(chunks, { on:
|
|
1298
|
+
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
1299
1299
|
}
|
|
1300
1300
|
function generateFromPartial(ctx, fullName, messageDesc) {
|
|
1301
1301
|
const { options, utils, typeMap } = ctx;
|
|
1302
1302
|
const chunks = [];
|
|
1303
1303
|
// create the basic function declaration
|
|
1304
|
-
const paramName = messageDesc.field.length > 0 ?
|
|
1304
|
+
const paramName = messageDesc.field.length > 0 ? "object" : "_";
|
|
1305
1305
|
if (ctx.options.useExactTypes) {
|
|
1306
1306
|
chunks.push((0, ts_poet_1.code) `
|
|
1307
1307
|
fromPartial<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(${paramName}: I): ${fullName} {
|
|
@@ -1384,11 +1384,11 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1384
1384
|
if ((0, types_1.isRepeated)(field)) {
|
|
1385
1385
|
if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
|
|
1386
1386
|
const fieldType = (0, types_1.toTypeName)(ctx, messageDesc, field);
|
|
1387
|
-
const i = maybeCastToNumber(ctx, messageDesc, field,
|
|
1387
|
+
const i = maybeCastToNumber(ctx, messageDesc, field, "key");
|
|
1388
1388
|
chunks.push((0, ts_poet_1.code) `
|
|
1389
1389
|
message.${fieldName} = Object.entries(object.${fieldName} ?? {}).reduce<${fieldType}>((acc, [key, value]) => {
|
|
1390
1390
|
if (value !== undefined) {
|
|
1391
|
-
acc[${i}] = ${readSnippet(
|
|
1391
|
+
acc[${i}] = ${readSnippet("value")};
|
|
1392
1392
|
}
|
|
1393
1393
|
return acc;
|
|
1394
1394
|
}, {});
|
|
@@ -1396,7 +1396,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1396
1396
|
}
|
|
1397
1397
|
else {
|
|
1398
1398
|
chunks.push((0, ts_poet_1.code) `
|
|
1399
|
-
message.${fieldName} = object.${fieldName}?.map((e) => ${readSnippet(
|
|
1399
|
+
message.${fieldName} = object.${fieldName}?.map((e) => ${readSnippet("e")}) || [];
|
|
1400
1400
|
`);
|
|
1401
1401
|
}
|
|
1402
1402
|
}
|
|
@@ -1413,13 +1413,13 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1413
1413
|
}
|
|
1414
1414
|
`);
|
|
1415
1415
|
}
|
|
1416
|
-
else if (readSnippet(`x`).toCodeString() ==
|
|
1416
|
+
else if (readSnippet(`x`).toCodeString() == "x") {
|
|
1417
1417
|
// An optimized case of the else below that works when `readSnippet` returns the plain input
|
|
1418
|
-
const fallback = (0, types_1.isWithinOneOf)(field) ?
|
|
1418
|
+
const fallback = (0, types_1.isWithinOneOf)(field) ? "undefined" : (0, types_1.defaultValue)(ctx, field);
|
|
1419
1419
|
chunks.push((0, ts_poet_1.code) `message.${fieldName} = object.${fieldName} ?? ${fallback};`);
|
|
1420
1420
|
}
|
|
1421
1421
|
else {
|
|
1422
|
-
const fallback = (0, types_1.isWithinOneOf)(field) ?
|
|
1422
|
+
const fallback = (0, types_1.isWithinOneOf)(field) ? "undefined" : (0, types_1.defaultValue)(ctx, field);
|
|
1423
1423
|
chunks.push((0, ts_poet_1.code) `
|
|
1424
1424
|
message.${fieldName} = (object.${fieldName} !== undefined && object.${fieldName} !== null)
|
|
1425
1425
|
? ${readSnippet(`object.${fieldName}`)}
|
|
@@ -1430,7 +1430,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1430
1430
|
// and then wrap up the switch/while/return
|
|
1431
1431
|
chunks.push((0, ts_poet_1.code) `return message;`);
|
|
1432
1432
|
chunks.push((0, ts_poet_1.code) `}`);
|
|
1433
|
-
return (0, ts_poet_1.joinCode)(chunks, { on:
|
|
1433
|
+
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
1434
1434
|
}
|
|
1435
1435
|
function generateWrap(ctx, fullProtoTypeName, fieldNames) {
|
|
1436
1436
|
const chunks = [];
|
|
@@ -1575,10 +1575,10 @@ function generateUnwrap(ctx, fullProtoTypeName, fieldNames) {
|
|
|
1575
1575
|
}
|
|
1576
1576
|
return chunks;
|
|
1577
1577
|
}
|
|
1578
|
-
exports.contextTypeVar =
|
|
1578
|
+
exports.contextTypeVar = "Context extends DataLoaders";
|
|
1579
1579
|
function maybeCastToNumber(ctx, messageDesc, field, variableName) {
|
|
1580
1580
|
const { keyType } = (0, types_1.detectMapType)(ctx, messageDesc, field);
|
|
1581
|
-
if (keyType.toCodeString() ===
|
|
1581
|
+
if (keyType.toCodeString() === "string") {
|
|
1582
1582
|
return variableName;
|
|
1583
1583
|
}
|
|
1584
1584
|
else {
|