ts-proto 1.120.0 → 1.121.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.121.0](https://github.com/stephenh/ts-proto/compare/v1.120.0...v1.121.0) (2022-07-28)
2
+
3
+
4
+ ### Features
5
+
6
+ * Add use-numeric-enum-json option. ([#625](https://github.com/stephenh/ts-proto/issues/625)) ([cd53d8c](https://github.com/stephenh/ts-proto/commit/cd53d8cacd6b4b8fa6517242020b216dd18eebdf))
7
+
1
8
  # [1.120.0](https://github.com/stephenh/ts-proto/compare/v1.119.0...v1.120.0) (2022-07-21)
2
9
 
3
10
 
package/README.markdown CHANGED
@@ -435,6 +435,8 @@ Generated code will be placed in the Gradle build directory.
435
435
  Requires `onlyTypes=true`. Implies `useDate=string` and `stringEnums=true`. This option is to generate types that can be directly used with marshalling/unmarshalling Protobuf messages serialized as JSON.
436
436
  You may also want to set `useOptionals=all`, as gRPC gateways are not required to send default value for scalar values.
437
437
 
438
+ - With '--ts_proto_opt=useNumericEnumForJson=true`, the JSON converter (`toJSON`) will encode enum values as int, rather than a string literal.
439
+
438
440
  ### NestJS Support
439
441
 
440
442
  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/enums.js CHANGED
@@ -91,17 +91,31 @@ function generateEnumToJson(ctx, fullName, enumDesc) {
91
91
  const { options, utils } = ctx;
92
92
  const chunks = [];
93
93
  const functionName = (0, case_1.camelCase)(fullName) + 'ToJSON';
94
- chunks.push((0, ts_poet_1.code) `export function ${(0, ts_poet_1.def)(functionName)}(object: ${fullName}): string {`);
94
+ chunks.push((0, ts_poet_1.code) `export function ${(0, ts_poet_1.def)(functionName)}(object: ${fullName}): ${ctx.options.useNumericEnumForJson ? 'number' : 'string'} {`);
95
95
  chunks.push((0, ts_poet_1.code) `switch (object) {`);
96
96
  for (const valueDesc of enumDesc.value) {
97
- chunks.push((0, ts_poet_1.code) `case ${fullName}.${valueDesc.name}: return "${valueDesc.name}";`);
97
+ if (ctx.options.useNumericEnumForJson) {
98
+ chunks.push((0, ts_poet_1.code) `case ${fullName}.${valueDesc.name}: return ${valueDesc.number};`);
99
+ }
100
+ else {
101
+ chunks.push((0, ts_poet_1.code) `case ${fullName}.${valueDesc.name}: return "${valueDesc.name}";`);
102
+ }
98
103
  }
99
104
  if (options.unrecognizedEnum) {
100
105
  chunks.push((0, ts_poet_1.code) `
101
- case ${fullName}.${UNRECOGNIZED_ENUM_NAME}:
106
+ case ${fullName}.${UNRECOGNIZED_ENUM_NAME}:`);
107
+ if (ctx.options.useNumericEnumForJson) {
108
+ chunks.push((0, ts_poet_1.code) `
109
+ default:
110
+ return ${UNRECOGNIZED_ENUM_VALUE};
111
+ `);
112
+ }
113
+ else {
114
+ chunks.push((0, ts_poet_1.code) `
102
115
  default:
103
116
  return "${UNRECOGNIZED_ENUM_NAME}";
104
117
  `);
118
+ }
105
119
  }
106
120
  else {
107
121
  // We use globalThis to avoid conflicts on protobuf types named `Error`.
package/build/main.js CHANGED
@@ -1481,7 +1481,11 @@ function generateWrap(ctx, fullProtoTypeName, fieldNames) {
1481
1481
  }
1482
1482
  if ((0, types_1.isFieldMaskTypeName)(fullProtoTypeName)) {
1483
1483
  chunks.push((0, ts_poet_1.code) `wrap(paths: string[]): FieldMask {
1484
- return {paths: paths};
1484
+ const result = createBaseFieldMask();
1485
+
1486
+ result.paths = paths;
1487
+
1488
+ return result;
1485
1489
  }`);
1486
1490
  }
1487
1491
  return chunks;
package/build/options.js CHANGED
@@ -70,6 +70,7 @@ function defaultOptions() {
70
70
  unknownFields: false,
71
71
  usePrototypeForDefaults: false,
72
72
  useJsonWireFormat: false,
73
+ useNumericEnumForJson: false,
73
74
  };
74
75
  }
75
76
  exports.defaultOptions = defaultOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.120.0",
3
+ "version": "1.121.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",