ts-proto 1.93.0 → 1.94.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,31 @@
1
+ # [1.94.0](https://github.com/stephenh/ts-proto/compare/v1.93.3...v1.94.0) (2021-12-14)
2
+
3
+
4
+ ### Features
5
+
6
+ * Round numbers in toJSON. ([#444](https://github.com/stephenh/ts-proto/issues/444)) ([bd2df7b](https://github.com/stephenh/ts-proto/commit/bd2df7b7176e961955ed1dcacb3602384e13ee45))
7
+
8
+ ## [1.93.3](https://github.com/stephenh/ts-proto/compare/v1.93.2...v1.93.3) (2021-12-13)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * support mutliple options in snakeToCamel flag ([#429](https://github.com/stephenh/ts-proto/issues/429)) ([cff6674](https://github.com/stephenh/ts-proto/commit/cff667406cba21676546fd91b04cf2cbc571ed7d)), closes [#423](https://github.com/stephenh/ts-proto/issues/423)
14
+
15
+ ## [1.93.2](https://github.com/stephenh/ts-proto/compare/v1.93.1...v1.93.2) (2021-12-09)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * standalone dockerized protoc alias ([#438](https://github.com/stephenh/ts-proto/issues/438)) ([466f7d9](https://github.com/stephenh/ts-proto/commit/466f7d91551c6297fc1b9677f7a5839f8cdba0c6))
21
+
22
+ ## [1.93.1](https://github.com/stephenh/ts-proto/compare/v1.93.0...v1.93.1) (2021-12-08)
23
+
24
+
25
+ ### Bug Fixes
26
+
27
+ * Unwrap google.protobuf.BytesValue to Buffer when env=node ([#439](https://github.com/stephenh/ts-proto/issues/439)) ([73aa836](https://github.com/stephenh/ts-proto/commit/73aa8368300818068f3cddc5f046d990c66ab4f2))
28
+
1
29
  # [1.93.0](https://github.com/stephenh/ts-proto/compare/v1.92.2...v1.93.0) (2021-12-08)
2
30
 
3
31
 
package/README.markdown CHANGED
@@ -289,9 +289,7 @@ Generated code will be placed in the Gradle build directory.
289
289
 
290
290
  - With `--ts_proto_opt=lowerCaseServiceMethods=true`, the method names of service methods will be lowered/camel-case, i.e. `service.findFoo` instead of `service.FindFoo`.
291
291
 
292
- - With `--ts_proto_opt=snakeToCamel=false`, fields will be kept snake case.
293
-
294
- Defaults to `true`.
292
+ - With `--ts_proto_opt=snakeToCamel=false`, fields will be kept snake case. `snakeToCamel` can also be set as string with `--ts_proto_opt=snakeToCamel=keys,json`. `keys` will keep field names as camelCase and `json` will keep json field names as camelCase. Empty string will keep field names as snake_case.
295
293
 
296
294
  - With `--ts_proto_opt=outputEncodeMethods=false`, the `Message.encode` and `Message.decode` methods for working with protobuf-encoded/binary data will not be output.
297
295
 
package/build/case.js CHANGED
@@ -2,7 +2,7 @@
2
2
  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
- if (options.snakeToCamel && s.includes('_')) {
5
+ if (options.snakeToCamel.includes('keys') && s.includes('_')) {
6
6
  return s
7
7
  .split('_')
8
8
  .map((word, i) => {
package/build/main.js CHANGED
@@ -980,6 +980,9 @@ function generateToJson(ctx, fullName, messageDesc) {
980
980
  else if (types_1.isLong(valueType) && options.forceLong === options_1.LongOption.LONG) {
981
981
  return ts_poet_1.code `${from}.toString()`;
982
982
  }
983
+ else if (types_1.isWholeNumber(valueType) && !(types_1.isLong(valueType) && options.forceLong === options_1.LongOption.STRING)) {
984
+ return ts_poet_1.code `Math.round(${from})`;
985
+ }
983
986
  else if (types_1.isScalar(valueType) || types_1.isValueType(ctx, valueType)) {
984
987
  return ts_poet_1.code `${from}`;
985
988
  }
@@ -1010,6 +1013,9 @@ function generateToJson(ctx, fullName, messageDesc) {
1010
1013
  const v = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1011
1014
  return ts_poet_1.code `(${from} || ${v}).toString()`;
1012
1015
  }
1016
+ else if (types_1.isWholeNumber(field) && !(types_1.isLong(field) && options.forceLong === options_1.LongOption.STRING)) {
1017
+ return ts_poet_1.code `Math.round(${from})`;
1018
+ }
1013
1019
  else {
1014
1020
  return ts_poet_1.code `${from}`;
1015
1021
  }
package/build/options.js CHANGED
@@ -34,7 +34,7 @@ var ServiceOption;
34
34
  function defaultOptions() {
35
35
  return {
36
36
  context: false,
37
- snakeToCamel: true,
37
+ snakeToCamel: ['json', 'keys'],
38
38
  forceLong: LongOption.NUMBER,
39
39
  useOptionals: false,
40
40
  useDate: DateOption.DATE,
@@ -100,6 +100,12 @@ function optionsFromParameter(parameter) {
100
100
  // Treat useDate=false as TIMESTAMP
101
101
  options.useDate = DateOption.TIMESTAMP;
102
102
  }
103
+ if (options.snakeToCamel === false) {
104
+ options.snakeToCamel = [];
105
+ }
106
+ else if (options.snakeToCamel === true) {
107
+ options.snakeToCamel = ['keys', 'json'];
108
+ }
103
109
  return options;
104
110
  }
105
111
  exports.optionsFromParameter = optionsFromParameter;
package/build/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.rawRequestType = exports.detectMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isMapType = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
3
+ exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.rawRequestType = exports.detectMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isMapType = exports.isWholeNumber = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
4
4
  const ts_proto_descriptors_1 = require("ts-proto-descriptors");
5
5
  const ts_poet_1 = require("ts-poet");
6
6
  const options_1 = require("./options");
@@ -347,6 +347,19 @@ function isLong(field) {
347
347
  return basicLongWireType(field.type) !== undefined;
348
348
  }
349
349
  exports.isLong = isLong;
350
+ function isWholeNumber(field) {
351
+ return (field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_INT32 ||
352
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_INT64 ||
353
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_UINT32 ||
354
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_UINT64 ||
355
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SINT32 ||
356
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SINT64 ||
357
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FIXED32 ||
358
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FIXED64 ||
359
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SFIXED32 ||
360
+ field.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SFIXED64);
361
+ }
362
+ exports.isWholeNumber = isWholeNumber;
350
363
  function isMapType(ctx, messageDesc, field) {
351
364
  return detectMapType(ctx, messageDesc, field) !== undefined;
352
365
  }
@@ -411,7 +424,7 @@ function valueTypeName(ctx, typeName) {
411
424
  case '.google.protobuf.BoolValue':
412
425
  return ts_poet_1.code `boolean`;
413
426
  case '.google.protobuf.BytesValue':
414
- return ts_poet_1.code `Uint8Array`;
427
+ return ctx.options.env === options_1.EnvOption.NODE ? ts_poet_1.code `Buffer` : ts_poet_1.code `Uint8Array`;
415
428
  case '.google.protobuf.ListValue':
416
429
  return ts_poet_1.code `Array<any>`;
417
430
  case '.google.protobuf.Value':
package/build/utils.js CHANGED
@@ -145,12 +145,14 @@ class FormattedMethodDescriptor {
145
145
  }
146
146
  exports.FormattedMethodDescriptor = FormattedMethodDescriptor;
147
147
  function determineFieldJsonName(field, options) {
148
- // By default jsonName is camelCased by the protocol compilier unless the user has
149
- // set a "json_name" option on this field.
150
- if (field.jsonName.length > 0) {
148
+ // jsonName will be camelCased by the protocol compiler, plus can be overridden by the user,
149
+ // so just use that instead of our own maybeSnakeToCamel
150
+ if (options.snakeToCamel.includes('json')) {
151
151
  return field.jsonName;
152
152
  }
153
- return case_1.maybeSnakeToCamel(field.name, options);
153
+ else {
154
+ return field.name;
155
+ }
154
156
  }
155
157
  exports.determineFieldJsonName = determineFieldJsonName;
156
158
  function impProto(options, module, type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.93.0",
3
+ "version": "1.94.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",