ts-proto 1.124.0 → 1.126.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 CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.126.0](https://github.com/stephenh/ts-proto/compare/v1.125.0...v1.126.0) (2022-09-21)
2
+
3
+
4
+ ### Features
5
+
6
+ * **options:** adds protoc-gen-go-like M option ([#672](https://github.com/stephenh/ts-proto/issues/672)) ([9304e5d](https://github.com/stephenh/ts-proto/commit/9304e5db7172db53530fb08fe0486f56b2a17181)), closes [#596](https://github.com/stephenh/ts-proto/issues/596)
7
+
8
+ # [1.125.0](https://github.com/stephenh/ts-proto/compare/v1.124.0...v1.125.0) (2022-09-03)
9
+
10
+
11
+ ### Features
12
+
13
+ * omit optional fields in base instance ([#669](https://github.com/stephenh/ts-proto/issues/669)) ([47b60aa](https://github.com/stephenh/ts-proto/commit/47b60aab95533542cf762f152138f7ab4234de88))
14
+
1
15
  # [1.124.0](https://github.com/stephenh/ts-proto/compare/v1.123.1...v1.124.0) (2022-09-03)
2
16
 
3
17
 
package/README.markdown CHANGED
@@ -448,6 +448,16 @@ Generated code will be placed in the Gradle build directory.
448
448
 
449
449
  - With `--ts_proto_opt=useNumericEnumForJson=true`, the JSON converter (`toJSON`) will encode enum values as int, rather than a string literal.
450
450
 
451
+ - With `--ts_proto_opt=initializeFieldsAsUndefined=false`, all optional field initializers will be omited from the generated base instances.
452
+
453
+ - With `--ts_proto_opt=Mgoogle/protobuf/empty.proto=./google3/protobuf/empty`, ('M' means 'importMapping', similar to [protoc-gen-go](https://developers.google.com/protocol-buffers/docs/reference/go-generated#package)), the generated code import path for `./google/protobuf/empty.ts` will reflect the overridden value:
454
+
455
+ - `Mfoo/bar.proto=@myorg/some-lib` will map `foo/bar.proto` imports into `import ... from '@myorg/some-lib'`.
456
+ - `Mfoo/bar.proto=./some/local/lib` will map `foo/bar.proto` imports into `import ... from './some/local/lib'`.
457
+ - `Mfoo/bar.proto=some-modules/some-lib` will map `foo/bar.proto` imports into `import ... from 'some-module/some-lib'`.
458
+ - **Note**: Uses are accummulated, so multiple values are expected in the form of `--ts_proto_opt=M... --ts_proto_opt=M...` (one `ts_proto_opt` per mapping).
459
+ - **Note**: Proto files that match mapped imports **will not be generated**.
460
+
451
461
  ### NestJS Support
452
462
 
453
463
  We have a great way of working together with [nestjs](https://docs.nestjs.com/microservices/grpc). `ts-proto` generates `interfaces` and `decorators` for you controller, client. For more information see the [nestjs readme](NESTJS.markdown).
package/build/main.js CHANGED
@@ -549,6 +549,7 @@ function generateOneofProperty(ctx, messageDesc, oneofIndex, sourceInfo) {
549
549
  }
550
550
  // Create a function that constructs 'base' instance with default values for decode to use as a prototype
551
551
  function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
552
+ const { options } = ctx;
552
553
  const fields = [];
553
554
  // When oneof=unions, we generate a single property with an ADT per `oneof` clause.
554
555
  const processedOneofs = new Set();
@@ -562,6 +563,9 @@ function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
562
563
  }
563
564
  continue;
564
565
  }
566
+ if (!options.initializeFieldsAsUndefined && (0, types_1.isOptionalProperty)(field, messageDesc.options, options)) {
567
+ continue;
568
+ }
565
569
  const name = (0, case_1.maybeSnakeToCamel)(field.name, ctx.options);
566
570
  const val = (0, types_1.isWithinOneOf)(field)
567
571
  ? "undefined"
package/build/options.js CHANGED
@@ -71,6 +71,8 @@ function defaultOptions() {
71
71
  usePrototypeForDefaults: false,
72
72
  useJsonWireFormat: false,
73
73
  useNumericEnumForJson: false,
74
+ initializeFieldsAsUndefined: true,
75
+ M: {},
74
76
  };
75
77
  }
76
78
  exports.defaultOptions = defaultOptions;
@@ -147,16 +149,36 @@ function optionsFromParameter(parameter) {
147
149
  options.useDate = DateOption.STRING;
148
150
  }
149
151
  }
152
+ if (options.nestJs) {
153
+ options.initializeFieldsAsUndefined = false;
154
+ }
150
155
  return options;
151
156
  }
152
157
  exports.optionsFromParameter = optionsFromParameter;
153
158
  // A very naive parse function, eventually could/should use iots/runtypes
154
159
  function parseParameter(parameter) {
155
160
  const options = {};
156
- const pairs = parameter.split(",").map((s) => s.split("="));
157
- pairs.forEach(([key, _value]) => {
158
- const value = _value === "true" ? true : _value === "false" ? false : _value;
159
- if (options[key]) {
161
+ parameter.split(",").forEach((param) => {
162
+ // same as protoc-gen-go https://github.com/protocolbuffers/protobuf-go/blob/bf9455640daabb98c93b5b5e71628f3f813d57bb/compiler/protogen/protogen.go#L168-L171
163
+ const optionSeparatorPos = param.indexOf("=");
164
+ const key = param.substring(0, optionSeparatorPos);
165
+ const value = parseParamValue(param.substring(optionSeparatorPos + 1));
166
+ if (key.charAt(0) === "M") {
167
+ if (typeof value !== "string") {
168
+ console.warn(`ignoring invalid M option: '${param}'`);
169
+ }
170
+ else {
171
+ const mKey = key.substring(1);
172
+ if (options.M[mKey]) {
173
+ console.warn(`received conflicting M options: '${param}' will override 'M${mKey}=${options.M[mKey]}'`);
174
+ }
175
+ if (param.endsWith(".ts")) {
176
+ console.warn(`received M option '${param}' ending in '.ts' this is usually a mistake`);
177
+ }
178
+ options.M[mKey] = value;
179
+ }
180
+ }
181
+ else if (options[key]) {
160
182
  options[key] = [options[key], value];
161
183
  }
162
184
  else {
@@ -165,6 +187,9 @@ function parseParameter(parameter) {
165
187
  });
166
188
  return options;
167
189
  }
190
+ function parseParamValue(value) {
191
+ return value === "true" ? true : value === "false" ? false : value;
192
+ }
168
193
  function getTsPoetOpts(_options) {
169
194
  const imports = ["protobufjs/minimal" + _options.importSuffix];
170
195
  return {
package/build/plugin.js CHANGED
@@ -18,7 +18,9 @@ async function main() {
18
18
  const utils = (0, main_1.makeUtils)(options);
19
19
  const ctx = { typeMap, options, utils };
20
20
  const filesToGenerate = options.emitImportedFiles ? request.protoFile : (0, utils_1.protoFilesToGenerate)(request);
21
- const files = await Promise.all(filesToGenerate.map(async (file) => {
21
+ const files = await Promise.all(filesToGenerate
22
+ .filter((file) => !options.M[file.name])
23
+ .map(async (file) => {
22
24
  const [path, code] = (0, main_1.generateFile)(ctx, file);
23
25
  const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
24
26
  return { name: path, content };
package/build/utils.js CHANGED
@@ -178,12 +178,11 @@ function impFile(options, spec) {
178
178
  }
179
179
  exports.impFile = impFile;
180
180
  function impProto(options, module, type) {
181
- const importString = `${type}@./${module}${options.fileSuffix}${options.importSuffix}`;
182
- if (options.onlyTypes) {
183
- return (0, ts_poet_1.imp)("t:" + importString);
184
- }
185
- else {
186
- return (0, ts_poet_1.imp)(importString);
181
+ const prefix = options.onlyTypes ? "t:" : "";
182
+ const protoFile = `${module}.proto`;
183
+ if (options.M[protoFile]) {
184
+ return (0, ts_poet_1.imp)(`${prefix}${type}@${options.M[protoFile]}`);
187
185
  }
186
+ return (0, ts_poet_1.imp)(`${prefix}${type}@./${module}${options.fileSuffix}${options.importSuffix}`);
188
187
  }
189
188
  exports.impProto = impProto;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.124.0",
3
+ "version": "1.126.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",