ts-proto 2.4.1 → 2.5.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/README.markdown +8 -0
- package/build/src/main.js +71 -15
- package/build/src/options.d.ts +2 -0
- package/build/src/options.js +2 -0
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -408,6 +408,14 @@ Generated code will be placed in the Gradle build directory.
|
|
|
408
408
|
|
|
409
409
|
This is useful if you want "only types".
|
|
410
410
|
|
|
411
|
+
- With `--ts_proto_opt=outputEncodeIncludeTypes=regex`, the `Message.encode` method will only be output for types whose name matches the provided regular expression.
|
|
412
|
+
|
|
413
|
+
This is useful if you want to limit `encode` methods to only select types.
|
|
414
|
+
|
|
415
|
+
- With `--ts_proto_opt=outputDecodeIncludeTypes=regex`, the `Message.decode` method will only be output for types whose name matches the provided regular expression.
|
|
416
|
+
|
|
417
|
+
This is useful if you want to limit `decode` methods to only select types.
|
|
418
|
+
|
|
411
419
|
- With `--ts_proto_opt=outputJsonMethods=false`, the `Message.fromJSON` and `Message.toJSON` methods for working with JSON-coded data will not be output.
|
|
412
420
|
|
|
413
421
|
This is also useful if you want "only types".
|
package/build/src/main.js
CHANGED
|
@@ -90,7 +90,11 @@ function generateFile(ctx, fileDesc) {
|
|
|
90
90
|
const fullTypeName = (0, utils_1.maybePrefixPackage)(fileDesc, fullProtoTypeName);
|
|
91
91
|
const outputWrapAndUnwrap = (0, generate_struct_wrappers_1.isWrapperType)(fullTypeName);
|
|
92
92
|
// Only decode, fromPartial, and wrap use the createBase method
|
|
93
|
-
if ((options.outputEncodeMethods &&
|
|
93
|
+
if ((options.outputEncodeMethods &&
|
|
94
|
+
options.outputEncodeMethods !== "encode-no-creation" &&
|
|
95
|
+
options.outputEncodeMethods !== "encode-only" &&
|
|
96
|
+
(options.outputDecodeIncludeTypes === "" ||
|
|
97
|
+
new RegExp(options.outputDecodeIncludeTypes).test(fullTypeName))) ||
|
|
94
98
|
options.outputPartialMethods ||
|
|
95
99
|
outputWrapAndUnwrap) {
|
|
96
100
|
chunks.push(generateBaseInstanceFactory(ctx, fullName, message, fullTypeName));
|
|
@@ -110,15 +114,27 @@ function generateFile(ctx, fileDesc) {
|
|
|
110
114
|
if (options.outputEncodeMethods === true ||
|
|
111
115
|
options.outputEncodeMethods === "encode-only" ||
|
|
112
116
|
options.outputEncodeMethods === "encode-no-creation") {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
staticMembers.push(
|
|
117
|
+
if (options.outputEncodeIncludeTypes === "" ||
|
|
118
|
+
new RegExp(options.outputEncodeIncludeTypes).test(fullTypeName)) {
|
|
119
|
+
staticMembers.push(generateEncode(ctx, fullName, message));
|
|
120
|
+
if (options.outputExtensions && options.unknownFields && message.extensionRange.length) {
|
|
121
|
+
staticMembers.push(generateSetExtension(ctx, fullName));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else if (options.outputEncodeMethods === true) {
|
|
125
|
+
staticMembers.push(generateEmptyEncode(fullName));
|
|
116
126
|
}
|
|
117
127
|
}
|
|
118
128
|
if (options.outputEncodeMethods === true || options.outputEncodeMethods === "decode-only") {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
staticMembers.push(
|
|
129
|
+
if (options.outputDecodeIncludeTypes === "" ||
|
|
130
|
+
new RegExp(options.outputDecodeIncludeTypes).test(fullTypeName)) {
|
|
131
|
+
staticMembers.push(generateDecode(ctx, fullName, message));
|
|
132
|
+
if (options.outputExtensions && options.unknownFields && message.extensionRange.length) {
|
|
133
|
+
staticMembers.push(generateGetExtension(ctx, fullName));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (options.outputEncodeMethods === true) {
|
|
137
|
+
staticMembers.push(generateEmptyDecode(fullName));
|
|
122
138
|
}
|
|
123
139
|
}
|
|
124
140
|
}
|
|
@@ -992,6 +1008,17 @@ function getDecodeReadSnippet(ctx, field) {
|
|
|
992
1008
|
}
|
|
993
1009
|
return readSnippet;
|
|
994
1010
|
}
|
|
1011
|
+
function generateEmptyDecode(fullName) {
|
|
1012
|
+
const BinaryReader = (0, ts_poet_1.imp)("BinaryReader@@bufbuild/protobuf/wire");
|
|
1013
|
+
return (0, ts_poet_1.code) `
|
|
1014
|
+
decode(
|
|
1015
|
+
_: ${BinaryReader} | Uint8Array,
|
|
1016
|
+
length?: number,
|
|
1017
|
+
): ${fullName} {
|
|
1018
|
+
throw new Error('decode not generated for ${fullName}');
|
|
1019
|
+
}
|
|
1020
|
+
`;
|
|
1021
|
+
}
|
|
995
1022
|
/** Creates a function to decode a message by loop overing the tags. */
|
|
996
1023
|
function generateDecode(ctx, fullName, messageDesc) {
|
|
997
1024
|
const { options, currentFile } = ctx;
|
|
@@ -1251,6 +1278,17 @@ function getEncodeWriteSnippet(ctx, field) {
|
|
|
1251
1278
|
throw new Error(`Unhandled field ${field}`);
|
|
1252
1279
|
}
|
|
1253
1280
|
}
|
|
1281
|
+
function generateEmptyEncode(fullName) {
|
|
1282
|
+
const BinaryWriter = (0, ts_poet_1.imp)("BinaryWriter@@bufbuild/protobuf/wire");
|
|
1283
|
+
return (0, ts_poet_1.code) `
|
|
1284
|
+
encode(
|
|
1285
|
+
_: ${fullName},
|
|
1286
|
+
writer: ${BinaryWriter} = new ${BinaryWriter}(),
|
|
1287
|
+
): ${BinaryWriter} {
|
|
1288
|
+
throw new Error('encode not generated for ${fullName}');
|
|
1289
|
+
}
|
|
1290
|
+
`;
|
|
1291
|
+
}
|
|
1254
1292
|
/** Creates a function to encode a message by loop overing the tags. */
|
|
1255
1293
|
function generateEncode(ctx, fullName, messageDesc) {
|
|
1256
1294
|
const { options, utils, typeMap, currentFile } = ctx;
|
|
@@ -2178,12 +2216,17 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
2178
2216
|
createBase = (0, ts_poet_1.code) `Object.create(${createBase}) as ${fullName}`;
|
|
2179
2217
|
}
|
|
2180
2218
|
chunks.push((0, ts_poet_1.code) `const message = ${createBase}${maybeAsAny(options)};`);
|
|
2181
|
-
let
|
|
2219
|
+
let currentSwitchTarget;
|
|
2182
2220
|
// add a check for each incoming field
|
|
2183
2221
|
messageDesc.field.forEach((field) => {
|
|
2184
2222
|
const fieldName = (0, utils_1.getFieldName)(field, options);
|
|
2185
2223
|
const messageProperty = (0, utils_1.getPropertyAccessor)("message", fieldName);
|
|
2186
2224
|
const objectProperty = (0, utils_1.getPropertyAccessor)("object", fieldName);
|
|
2225
|
+
if (currentSwitchTarget && !(0, types_1.isWithinOneOfThatShouldBeUnion)(options, field)) {
|
|
2226
|
+
// We are exiting a switch, we need to close it
|
|
2227
|
+
chunks.push((0, ts_poet_1.code) `}`);
|
|
2228
|
+
currentSwitchTarget = undefined;
|
|
2229
|
+
}
|
|
2187
2230
|
const readSnippet = (from) => {
|
|
2188
2231
|
if (((0, types_1.isLong)(field) || (0, types_1.isLongValueType)(field)) &&
|
|
2189
2232
|
options.forceLong === options_1.LongOption.LONG &&
|
|
@@ -2300,16 +2343,24 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
2300
2343
|
const oneofNameWithObject = (0, utils_1.getPropertyAccessor)("object", oneofName);
|
|
2301
2344
|
const valueName = (0, utils_1.oneofValueName)(fieldName, options);
|
|
2302
2345
|
const v = readSnippet(`${oneofNameWithObject}.${valueName}`);
|
|
2346
|
+
// If we are entering a new switch, close the previous one
|
|
2347
|
+
if (currentSwitchTarget !== undefined && currentSwitchTarget !== oneofNameWithObject) {
|
|
2348
|
+
chunks.push((0, ts_poet_1.code) `}`);
|
|
2349
|
+
currentSwitchTarget = undefined;
|
|
2350
|
+
}
|
|
2351
|
+
if (currentSwitchTarget === undefined) {
|
|
2352
|
+
chunks.push((0, ts_poet_1.code) `switch (${oneofNameWithObject}?.$case) {`);
|
|
2353
|
+
}
|
|
2303
2354
|
chunks.push((0, ts_poet_1.code) `
|
|
2304
|
-
${
|
|
2305
|
-
${oneofNameWithObject}?.$
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2355
|
+
case '${fieldName}': {
|
|
2356
|
+
if (${oneofNameWithObject}?.${valueName} !== undefined
|
|
2357
|
+
&& ${oneofNameWithObject}?.${valueName} !== null) {
|
|
2358
|
+
${oneofNameWithMessage} = { $case: '${fieldName}', ${valueName}: ${v} };
|
|
2359
|
+
}
|
|
2360
|
+
break;
|
|
2310
2361
|
}
|
|
2311
2362
|
`);
|
|
2312
|
-
|
|
2363
|
+
currentSwitchTarget = oneofNameWithObject;
|
|
2313
2364
|
}
|
|
2314
2365
|
else if (readSnippet(`x`).toCodeString([]) == "x") {
|
|
2315
2366
|
// An optimized case of the else below that works when `readSnippet` returns the plain input
|
|
@@ -2325,6 +2376,11 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
2325
2376
|
`);
|
|
2326
2377
|
}
|
|
2327
2378
|
});
|
|
2379
|
+
if (currentSwitchTarget) {
|
|
2380
|
+
// We are exiting a switch, we need to close it
|
|
2381
|
+
chunks.push((0, ts_poet_1.code) `}`);
|
|
2382
|
+
currentSwitchTarget = "";
|
|
2383
|
+
}
|
|
2328
2384
|
// and then wrap up the switch/while/return
|
|
2329
2385
|
chunks.push((0, ts_poet_1.code) `return message;`);
|
|
2330
2386
|
chunks.push((0, ts_poet_1.code) `}`);
|
package/build/src/options.d.ts
CHANGED
|
@@ -53,6 +53,8 @@ export type Options = {
|
|
|
53
53
|
fileSuffix: string;
|
|
54
54
|
importSuffix: string;
|
|
55
55
|
outputEncodeMethods: true | false | "encode-only" | "decode-only" | "encode-no-creation";
|
|
56
|
+
outputEncodeIncludeTypes: string;
|
|
57
|
+
outputDecodeIncludeTypes: string;
|
|
56
58
|
outputJsonMethods: true | false | "to-only" | "from-only";
|
|
57
59
|
outputPartialMethods: boolean;
|
|
58
60
|
outputTypeAnnotations: boolean | "static-only" | "optional";
|
package/build/src/options.js
CHANGED
|
@@ -68,6 +68,8 @@ function defaultOptions() {
|
|
|
68
68
|
importSuffix: "",
|
|
69
69
|
lowerCaseServiceMethods: false,
|
|
70
70
|
outputEncodeMethods: true,
|
|
71
|
+
outputEncodeIncludeTypes: "",
|
|
72
|
+
outputDecodeIncludeTypes: "",
|
|
71
73
|
outputJsonMethods: true,
|
|
72
74
|
outputPartialMethods: true,
|
|
73
75
|
outputTypeAnnotations: false,
|