ts-proto 1.102.2 → 1.105.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.105.0](https://github.com/stephenh/ts-proto/compare/v1.104.1...v1.105.0) (2022-02-12)
2
+
3
+
4
+ ### Features
5
+
6
+ * Bump ts-proto-descriptors. ([#489](https://github.com/stephenh/ts-proto/issues/489)) ([d454448](https://github.com/stephenh/ts-proto/commit/d454448b1889b1576c1ebcc6964a55a03af7d921)), closes [#493](https://github.com/stephenh/ts-proto/issues/493)
7
+
8
+ ## [1.104.1](https://github.com/stephenh/ts-proto/compare/v1.104.0...v1.104.1) (2022-02-12)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * make struct types play well with type registry ([#503](https://github.com/stephenh/ts-proto/issues/503)) ([d62f854](https://github.com/stephenh/ts-proto/commit/d62f85478011c7eb3dbca196f79b452895406ece))
14
+
15
+ # [1.104.0](https://github.com/stephenh/ts-proto/compare/v1.103.0...v1.104.0) (2022-01-21)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * Leave mixed case in all words. ([#488](https://github.com/stephenh/ts-proto/issues/488)) ([8a26c9c](https://github.com/stephenh/ts-proto/commit/8a26c9cba4c9897700aafe1a7f59d0b0f537764b))
21
+
22
+
23
+ ### Features
24
+
25
+ * 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))
26
+
27
+ # [1.103.0](https://github.com/stephenh/ts-proto/compare/v1.102.2...v1.103.0) (2022-01-20)
28
+
29
+
30
+ ### Features
31
+
32
+ * add usePrototypeForDefaults option ([#484](https://github.com/stephenh/ts-proto/issues/484)) ([8e8c810](https://github.com/stephenh/ts-proto/commit/8e8c81016968e7d772dfac5ed54800898f039cbe))
33
+
1
34
  ## [1.102.2](https://github.com/stephenh/ts-proto/compare/v1.102.1...v1.102.2) (2022-01-19)
2
35
 
3
36
 
package/README.markdown CHANGED
@@ -376,11 +376,22 @@ 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
- ### Only Types
379
+ - With `--ts_proto_opt=onlyTypes=true`, only types will be emitted, and imports for `long` and `protobufjs/minimal` will be excluded.
380
+
381
+ Note: _This is a combination_ of `outputJsonMethods=false,outputEncodeMethods=false,outputClientImpl=false,nestJs=false`
382
+
383
+ - With `--ts_proto_opt=usePrototypeForDefaults=true`, the generated code will wrap new objects with `Object.create`.
384
+
385
+ 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.
380
386
 
387
+ 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.
388
+
389
+ 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.
390
+
391
+ ### Only Types
381
392
  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.:
382
393
 
383
- `--ts_proto_opt=outputEncodeMethods=false,outputJsonMethods=false,outputClientImpl=false`.
394
+ `--ts_proto_opt=onlyTypes=true`.
384
395
 
385
396
  ### NestJS Support
386
397
 
package/build/case.js CHANGED
@@ -7,13 +7,9 @@ function maybeSnakeToCamel(s, options) {
7
7
  return s
8
8
  .split('_')
9
9
  .map((word, i) => {
10
- if (i === 0) {
11
- // if first symbol is "_" then skip it
12
- return word ? word[0] + (hasLowerCase ? word.substring(1) : word.substring(1).toLowerCase()) : '';
13
- }
14
- else {
15
- return capitalize(word.toLowerCase());
16
- }
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);
17
13
  })
18
14
  .join('');
19
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);
@@ -1341,7 +1349,7 @@ function generateWrap(ctx, fullProtoTypeName) {
1341
1349
  const chunks = [];
1342
1350
  if (types_1.isStructTypeName(fullProtoTypeName)) {
1343
1351
  chunks.push(ts_poet_1.code `wrap(object: {[key: string]: any} | undefined): Struct {
1344
- const struct = Struct.fromPartial({});
1352
+ const struct = createBaseStruct();
1345
1353
  if (object !== undefined) {
1346
1354
  Object.keys(object).forEach(key => {
1347
1355
  struct.fields[key] = object[key];
@@ -1353,50 +1361,58 @@ function generateWrap(ctx, fullProtoTypeName) {
1353
1361
  if (types_1.isAnyValueTypeName(fullProtoTypeName)) {
1354
1362
  if (ctx.options.oneof === options_1.OneofOption.UNIONS) {
1355
1363
  chunks.push(ts_poet_1.code `wrap(value: any): Value {
1356
- if (value === null) {
1357
- return {kind: {$case: 'nullValue', nullValue: NullValue.NULL_VALUE}};
1358
- } else if (typeof value === 'boolean') {
1359
- return {kind: {$case: 'boolValue', boolValue: value}};
1360
- } else if (typeof value === 'number') {
1361
- return {kind: {$case: 'numberValue', numberValue: value}};
1362
- } else if (typeof value === 'string') {
1363
- return {kind: {$case: 'stringValue', stringValue: value}};
1364
- } else if (Array.isArray(value)) {
1365
- return {kind: {$case: 'listValue', listValue: value}};
1366
- } else if (typeof value === 'object') {
1367
- return {kind: {$case: 'structValue', structValue: value}};
1368
- } else if (typeof value === 'undefined') {
1369
- return {} as Value;
1370
- } else {
1371
- throw new Error('Unsupported any value type: ' + typeof value);
1372
- }
1364
+ const result = createBaseValue();
1365
+
1366
+ if (value === null) {
1367
+ result.kind = {$case: 'nullValue', nullValue: NullValue.NULL_VALUE};
1368
+ } else if (typeof value === 'boolean') {
1369
+ result.kind = {$case: 'boolValue', boolValue: value};
1370
+ } else if (typeof value === 'number') {
1371
+ result.kind = {$case: 'numberValue', numberValue: value};
1372
+ } else if (typeof value === 'string') {
1373
+ result.kind = {$case: 'stringValue', stringValue: value};
1374
+ } else if (Array.isArray(value)) {
1375
+ result.kind = {$case: 'listValue', listValue: value};
1376
+ } else if (typeof value === 'object') {
1377
+ result.kind = {$case: 'structValue', structValue: value};
1378
+ } else if (typeof value !== 'undefined') {
1379
+ throw new Error('Unsupported any value type: ' + typeof value);
1380
+ }
1381
+
1382
+ return result;
1373
1383
  }`);
1374
1384
  }
1375
1385
  else {
1376
1386
  chunks.push(ts_poet_1.code `wrap(value: any): Value {
1377
- if (value === null) {
1378
- return {nullValue: NullValue.NULL_VALUE} as Value;
1379
- } else if (typeof value === 'boolean') {
1380
- return {boolValue: value} as Value;
1381
- } else if (typeof value === 'number') {
1382
- return {numberValue: value} as Value;
1383
- } else if (typeof value === 'string') {
1384
- return {stringValue: value} as Value;
1385
- } else if (Array.isArray(value)) {
1386
- return {listValue: value} as Value;
1387
- } else if (typeof value === 'object') {
1388
- return {structValue: value} as Value;
1389
- } else if (typeof value === 'undefined') {
1390
- return {} as Value;
1391
- } else {
1392
- throw new Error('Unsupported any value type: ' + typeof value);
1393
- }
1387
+ const result = createBaseValue();
1388
+
1389
+ if (value === null) {
1390
+ result.nullValue = NullValue.NULL_VALUE;
1391
+ } else if (typeof value === 'boolean') {
1392
+ result.boolValue = value;
1393
+ } else if (typeof value === 'number') {
1394
+ result.numberValue = value;
1395
+ } else if (typeof value === 'string') {
1396
+ result.stringValue = value;
1397
+ } else if (Array.isArray(value)) {
1398
+ result.listValue = value;
1399
+ } else if (typeof value === 'object') {
1400
+ result.structValue = value;
1401
+ } else if (typeof value !== 'undefined') {
1402
+ throw new Error('Unsupported any value type: ' + typeof value);
1403
+ }
1404
+
1405
+ return result;
1394
1406
  }`);
1395
1407
  }
1396
1408
  }
1397
1409
  if (types_1.isListValueTypeName(fullProtoTypeName)) {
1398
1410
  chunks.push(ts_poet_1.code `wrap(value: Array<any> | undefined): ListValue {
1399
- return {values: value ?? []};
1411
+ const result = createBaseListValue();
1412
+
1413
+ result.values = value ?? [];
1414
+
1415
+ return result;
1400
1416
  }`);
1401
1417
  }
1402
1418
  return chunks;
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/build/schema.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateSchema = void 0;
4
+ const ts_proto_descriptors_1 = require("ts-proto-descriptors");
4
5
  const ts_poet_1 = require("ts-poet");
5
6
  const visit_1 = require("./visit");
6
7
  const utils_1 = require("./utils");
@@ -36,7 +37,7 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
36
37
  return ts_poet_1.code `${ts_poet_1.imp(`protoMetadata@./${dep.replace('.proto', '')}`)}`;
37
38
  });
38
39
  // Use toObject so that we get enums as numbers (instead of the default toJSON behavior)
39
- const descriptor = { ...fileDesc };
40
+ const descriptor = ts_proto_descriptors_1.FileDescriptorProto.fromPartial(fileDesc);
40
41
  // Only keep locations that include comments
41
42
  descriptor.sourceCodeInfo = {
42
43
  location: ((_a = descriptor.sourceCodeInfo) === null || _a === void 0 ? void 0 : _a.location.filter((loc) => loc['leadingComments'] || loc['trailingComments'])) || [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.102.2",
3
+ "version": "1.105.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.3.1"
67
+ "ts-proto-descriptors": "1.6.0"
68
68
  }
69
69
  }