ts-proto 1.102.0 → 1.104.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,36 @@
1
+ # [1.104.0](https://github.com/stephenh/ts-proto/compare/v1.103.0...v1.104.0) (2022-01-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Leave mixed case in all words. ([#488](https://github.com/stephenh/ts-proto/issues/488)) ([8a26c9c](https://github.com/stephenh/ts-proto/commit/8a26c9cba4c9897700aafe1a7f59d0b0f537764b))
7
+
8
+
9
+ ### Features
10
+
11
+ * enable prototype for defaults for ts-proto-descriptors ([#487](https://github.com/stephenh/ts-proto/issues/487)) ([2b5640f](https://github.com/stephenh/ts-proto/commit/2b5640f582e6adb4e81797a9cec217896061aadb))
12
+
13
+ # [1.103.0](https://github.com/stephenh/ts-proto/compare/v1.102.2...v1.103.0) (2022-01-20)
14
+
15
+
16
+ ### Features
17
+
18
+ * add usePrototypeForDefaults option ([#484](https://github.com/stephenh/ts-proto/issues/484)) ([8e8c810](https://github.com/stephenh/ts-proto/commit/8e8c81016968e7d772dfac5ed54800898f039cbe))
19
+
20
+ ## [1.102.2](https://github.com/stephenh/ts-proto/compare/v1.102.1...v1.102.2) (2022-01-19)
21
+
22
+
23
+ ### Bug Fixes
24
+
25
+ * Have snakeToCamel leave existing mixed case. ([#482](https://github.com/stephenh/ts-proto/issues/482)) ([c0bf0fc](https://github.com/stephenh/ts-proto/commit/c0bf0fc13da70e2bde923cd1746119d2e7ac4b2f)), closes [#478](https://github.com/stephenh/ts-proto/issues/478)
26
+
27
+ ## [1.102.1](https://github.com/stephenh/ts-proto/compare/v1.102.0...v1.102.1) (2022-01-19)
28
+
29
+
30
+ ### Bug Fixes
31
+
32
+ * Pin ts-proto-descriptors to 1.3.1. ([#481](https://github.com/stephenh/ts-proto/issues/481)) ([6f362bf](https://github.com/stephenh/ts-proto/commit/6f362bfd3517a6bcb440d65e7ac63cd2b0bcc293)), closes [#480](https://github.com/stephenh/ts-proto/issues/480)
33
+
1
34
  # [1.102.0](https://github.com/stephenh/ts-proto/compare/v1.101.0...v1.102.0) (2022-01-18)
2
35
 
3
36
 
package/README.markdown CHANGED
@@ -376,6 +376,14 @@ Generated code will be placed in the Gradle build directory.
376
376
 
377
377
  - With `--ts_proto_opt=unknownFields=true`, all unknown fields will be parsed and output as arrays of buffers.
378
378
 
379
+ - With `--ts_proto_opt=usePrototypeForDefaults=true`, the generated code will wrap new objects with `Object.create`.
380
+
381
+ This allows code to do hazzer checks to detect when default values have been applied, which due to proto3's behavior of not putting default values on the wire, is typically only useful for interacting with proto2 messages.
382
+
383
+ When enabled, default values are inherited from a prototype, and so code can use Object.keys().includes("someField") to detect if someField was actually decoded or not.
384
+
385
+ Note that, as indicated, this means Object.keys will not include set-by-default fields, so if you have code that iterates over messages keys in a generic fashion, it will have to also iterate over keys inherited from the prototype.
386
+
379
387
  ### Only Types
380
388
 
381
389
  If you're looking for `ts-proto` to generate only types for your Protobuf types then passing all three of `outputEncodeMethods`, `outputJsonMethods`, and `outputClientImpl` as `false` is probably what you want, i.e.:
package/build/case.js CHANGED
@@ -3,16 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.camelCase = exports.capitalize = exports.camelToSnake = exports.maybeSnakeToCamel = void 0;
4
4
  function maybeSnakeToCamel(s, options) {
5
5
  if (options.snakeToCamel.includes('keys') && s.includes('_')) {
6
+ const hasLowerCase = !!s.match(/[a-z]/);
6
7
  return s
7
8
  .split('_')
8
9
  .map((word, i) => {
9
- if (i === 0) {
10
- // if first symbol is "_" then skip it
11
- return word ? word[0] + word.substring(1).toLowerCase() : '';
12
- }
13
- else {
14
- return capitalize(word.toLowerCase());
15
- }
10
+ // If the word is already mixed case, leave the exist case as-is
11
+ word = hasLowerCase ? word : word.toLowerCase();
12
+ return i === 0 ? word : capitalize(word);
16
13
  })
17
14
  .join('');
18
15
  }
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.0",
3
+ "version": "1.104.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",
@@ -64,6 +64,6 @@
64
64
  "object-hash": "^1.3.1",
65
65
  "protobufjs": "^6.8.8",
66
66
  "ts-poet": "^4.5.0",
67
- "ts-proto-descriptors": "^1.2.1"
67
+ "ts-proto-descriptors": "1.3.1"
68
68
  }
69
69
  }