ts-proto 1.112.1 → 1.112.2

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.112.2](https://github.com/stephenh/ts-proto/compare/v1.112.1...v1.112.2) (2022-05-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * enum type returns 'UNRECOGNIZED' or '-1' in xxxToJSON/xxxToNumber ([#566](https://github.com/stephenh/ts-proto/issues/566)) ([19911a1](https://github.com/stephenh/ts-proto/commit/19911a1c62e3fe5c8a0eb0a795489340512898da))
7
+
1
8
  ## [1.112.1](https://github.com/stephenh/ts-proto/compare/v1.112.0...v1.112.1) (2022-05-06)
2
9
 
3
10
 
package/build/enums.js CHANGED
@@ -43,11 +43,11 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
43
43
  }
44
44
  if (options.outputJsonMethods) {
45
45
  chunks.push(ts_poet_1.code `\n`);
46
- chunks.push(generateEnumToJson(fullName, enumDesc));
46
+ chunks.push(generateEnumToJson(ctx, fullName, enumDesc));
47
47
  }
48
48
  if (options.stringEnums && options.outputEncodeMethods) {
49
49
  chunks.push(ts_poet_1.code `\n`);
50
- chunks.push(generateEnumToNumber(fullName, enumDesc));
50
+ chunks.push(generateEnumToNumber(ctx, fullName, enumDesc));
51
51
  }
52
52
  return ts_poet_1.joinCode(chunks, { on: '\n' });
53
53
  }
@@ -87,7 +87,8 @@ function generateEnumFromJson(ctx, fullName, enumDesc) {
87
87
  }
88
88
  exports.generateEnumFromJson = generateEnumFromJson;
89
89
  /** Generates a function with a big switch statement to encode our enum -> JSON. */
90
- function generateEnumToJson(fullName, enumDesc) {
90
+ function generateEnumToJson(ctx, fullName, enumDesc) {
91
+ const { options, utils } = ctx;
91
92
  const chunks = [];
92
93
  const functionName = case_1.camelCase(fullName) + 'ToJSON';
93
94
  chunks.push(ts_poet_1.code `export function ${ts_poet_1.def(functionName)}(object: ${fullName}): string {`);
@@ -95,14 +96,28 @@ function generateEnumToJson(fullName, enumDesc) {
95
96
  for (const valueDesc of enumDesc.value) {
96
97
  chunks.push(ts_poet_1.code `case ${fullName}.${valueDesc.name}: return "${valueDesc.name}";`);
97
98
  }
98
- chunks.push(ts_poet_1.code `default: return "UNKNOWN";`);
99
+ if (options.unrecognizedEnum) {
100
+ chunks.push(ts_poet_1.code `
101
+ case ${fullName}.${UNRECOGNIZED_ENUM_NAME}:
102
+ default:
103
+ return "${UNRECOGNIZED_ENUM_NAME}";
104
+ `);
105
+ }
106
+ else {
107
+ // We use globalThis to avoid conflicts on protobuf types named `Error`.
108
+ chunks.push(ts_poet_1.code `
109
+ default:
110
+ throw new ${utils.globalThis}.Error("Unrecognized enum value " + object + " for enum ${fullName}");
111
+ `);
112
+ }
99
113
  chunks.push(ts_poet_1.code `}`);
100
114
  chunks.push(ts_poet_1.code `}`);
101
115
  return ts_poet_1.joinCode(chunks, { on: '\n' });
102
116
  }
103
117
  exports.generateEnumToJson = generateEnumToJson;
104
118
  /** Generates a function with a big switch statement to encode our string enum -> int value. */
105
- function generateEnumToNumber(fullName, enumDesc) {
119
+ function generateEnumToNumber(ctx, fullName, enumDesc) {
120
+ const { options, utils } = ctx;
106
121
  const chunks = [];
107
122
  const functionName = case_1.camelCase(fullName) + 'ToNumber';
108
123
  chunks.push(ts_poet_1.code `export function ${ts_poet_1.def(functionName)}(object: ${fullName}): number {`);
@@ -110,7 +125,20 @@ function generateEnumToNumber(fullName, enumDesc) {
110
125
  for (const valueDesc of enumDesc.value) {
111
126
  chunks.push(ts_poet_1.code `case ${fullName}.${valueDesc.name}: return ${valueDesc.number};`);
112
127
  }
113
- chunks.push(ts_poet_1.code `default: return 0;`);
128
+ if (options.unrecognizedEnum) {
129
+ chunks.push(ts_poet_1.code `
130
+ case ${fullName}.${UNRECOGNIZED_ENUM_NAME}:
131
+ default:
132
+ return ${UNRECOGNIZED_ENUM_VALUE};
133
+ `);
134
+ }
135
+ else {
136
+ // We use globalThis to avoid conflicts on protobuf types named `Error`.
137
+ chunks.push(ts_poet_1.code `
138
+ default:
139
+ throw new ${utils.globalThis}.Error("Unrecognized enum value " + object + " for enum ${fullName}");
140
+ `);
141
+ }
114
142
  chunks.push(ts_poet_1.code `}`);
115
143
  chunks.push(ts_poet_1.code `}`);
116
144
  return ts_poet_1.joinCode(chunks, { on: '\n' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.112.1",
3
+ "version": "1.112.2",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",