ts-proto 1.102.2 → 1.103.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,10 @@
1
+ # [1.103.0](https://github.com/stephenh/ts-proto/compare/v1.102.2...v1.103.0) (2022-01-20)
2
+
3
+
4
+ ### Features
5
+
6
+ * add usePrototypeForDefaults option ([#484](https://github.com/stephenh/ts-proto/issues/484)) ([8e8c810](https://github.com/stephenh/ts-proto/commit/8e8c81016968e7d772dfac5ed54800898f039cbe))
7
+
1
8
  ## [1.102.2](https://github.com/stephenh/ts-proto/compare/v1.102.1...v1.102.2) (2022-01-19)
2
9
 
3
10
 
package/build/main.js CHANGED
@@ -537,6 +537,10 @@ function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
537
537
  function generateDecode(ctx, fullName, messageDesc) {
538
538
  const { options, utils, typeMap } = ctx;
539
539
  const chunks = [];
540
+ let createBase = ts_poet_1.code `createBase${fullName}()`;
541
+ if (options.usePrototypeForDefaults) {
542
+ createBase = ts_poet_1.code `Object.create(${createBase}) as ${fullName}`;
543
+ }
540
544
  // create the basic function declaration
541
545
  chunks.push(ts_poet_1.code `
542
546
  decode(
@@ -545,7 +549,7 @@ function generateDecode(ctx, fullName, messageDesc) {
545
549
  ): ${fullName} {
546
550
  const reader = input instanceof ${Reader} ? input : new ${Reader}(input);
547
551
  let end = length === undefined ? reader.len : reader.pos + length;
548
- const message = createBase${fullName}();
552
+ const message = ${createBase};
549
553
  `);
550
554
  if (options.unknownFields) {
551
555
  chunks.push(ts_poet_1.code `(message as any)._unknownFields = {}`);
@@ -1221,7 +1225,11 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1221
1225
  fromPartial(${paramName}: ${utils.DeepPartial}<${fullName}>): ${fullName} {
1222
1226
  `);
1223
1227
  }
1224
- chunks.push(ts_poet_1.code `const message = createBase${fullName}();`);
1228
+ let createBase = ts_poet_1.code `createBase${fullName}()`;
1229
+ if (options.usePrototypeForDefaults) {
1230
+ createBase = ts_poet_1.code `Object.create(${createBase}) as ${fullName}`;
1231
+ }
1232
+ chunks.push(ts_poet_1.code `const message = ${createBase};`);
1225
1233
  // add a check for each incoming field
1226
1234
  messageDesc.field.forEach((field) => {
1227
1235
  const fieldName = case_1.maybeSnakeToCamel(field.name, options);
package/build/options.js CHANGED
@@ -64,6 +64,7 @@ function defaultOptions() {
64
64
  emitImportedFiles: true,
65
65
  useExactTypes: true,
66
66
  unknownFields: false,
67
+ usePrototypeForDefaults: false,
67
68
  };
68
69
  }
69
70
  exports.defaultOptions = defaultOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.102.2",
3
+ "version": "1.103.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",