ts-proto 2.4.2 → 2.5.1

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 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 && options.outputEncodeMethods !== "encode-no-creation") ||
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
- staticMembers.push(generateEncode(ctx, fullName, message));
114
- if (options.outputExtensions && options.unknownFields && message.extensionRange.length) {
115
- staticMembers.push(generateSetExtension(ctx, fullName));
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
- staticMembers.push(generateDecode(ctx, fullName, message));
120
- if (options.outputExtensions && options.unknownFields && message.extensionRange.length) {
121
- staticMembers.push(generateGetExtension(ctx, fullName));
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
  }
@@ -737,7 +753,7 @@ function makeTimestampMethods(options, longs, bytes) {
737
753
  } else if (typeof o === "string") {
738
754
  return ${toTimestamp}(new ${bytes.globalThis}.Date(o));
739
755
  } else {
740
- return Timestamp.fromJSON(o);
756
+ return ${options.typePrefix}Timestamp${options.typeSuffix}.fromJSON(o);
741
757
  }
742
758
  }
743
759
  `);
@@ -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;
@@ -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";
@@ -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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.4.2",
3
+ "version": "2.5.1",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",