ts-proto 1.106.0 → 1.106.1

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.106.1](https://github.com/stephenh/ts-proto/compare/v1.106.0...v1.106.1) (2022-02-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * support json_name containing hyphen on all field types ([#521](https://github.com/stephenh/ts-proto/issues/521)) ([8d9e78e](https://github.com/stephenh/ts-proto/commit/8d9e78eb39c460f6727458f6a2dd149deb983668))
7
+
1
8
  # [1.106.0](https://github.com/stephenh/ts-proto/compare/v1.105.2...v1.106.0) (2022-02-21)
2
9
 
3
10
 
package/build/main.js CHANGED
@@ -897,6 +897,7 @@ function generateFromJson(ctx, fullName, messageDesc) {
897
897
  const fieldName = case_1.maybeSnakeToCamel(field.name, options);
898
898
  const jsonName = utils_1.getFieldJsonName(field, options);
899
899
  const jsonProperty = utils_1.getPropertyAccessor('object', jsonName);
900
+ const jsonPropertyOptional = utils_1.getPropertyAccessor('object', jsonName, true);
900
901
  // get code that extracts value from incoming object
901
902
  const readSnippet = (from) => {
902
903
  if (types_1.isEnum(field)) {
@@ -1025,12 +1026,12 @@ function generateFromJson(ctx, fullName, messageDesc) {
1025
1026
  else {
1026
1027
  const readValueSnippet = readSnippet('e');
1027
1028
  if (readValueSnippet.toString() === ts_poet_1.code `e`.toString()) {
1028
- chunks.push(ts_poet_1.code `${fieldName}: Array.isArray(object?.${jsonName}) ? [...${jsonProperty}] : [],`);
1029
+ chunks.push(ts_poet_1.code `${fieldName}: Array.isArray(${jsonPropertyOptional}) ? [...${jsonProperty}] : [],`);
1029
1030
  }
1030
1031
  else {
1031
1032
  // Explicit `any` type required to make TS with noImplicitAny happy. `object` is also `any` here.
1032
1033
  chunks.push(ts_poet_1.code `
1033
- ${fieldName}: Array.isArray(object?.${jsonName}) ? ${jsonProperty}.map((e: any) => ${readValueSnippet}): [],
1034
+ ${fieldName}: Array.isArray(${jsonPropertyOptional}) ? ${jsonProperty}.map((e: any) => ${readValueSnippet}): [],
1034
1035
  `);
1035
1036
  }
1036
1037
  }
@@ -1051,7 +1052,7 @@ function generateFromJson(ctx, fullName, messageDesc) {
1051
1052
  }
1052
1053
  }
1053
1054
  else if (types_1.isAnyValueType(field)) {
1054
- chunks.push(ts_poet_1.code `${fieldName}: ${ctx.utils.isSet}(object?.${jsonName})
1055
+ chunks.push(ts_poet_1.code `${fieldName}: ${ctx.utils.isSet}(${jsonPropertyOptional})
1055
1056
  ? ${readSnippet(`${jsonProperty}`)}
1056
1057
  : undefined,
1057
1058
  `);
@@ -1185,10 +1186,10 @@ function generateToJson(ctx, fullName, messageDesc) {
1185
1186
  if (types_1.isMapType(ctx, messageDesc, field)) {
1186
1187
  // Maps might need their values transformed, i.e. bytes --> base64
1187
1188
  chunks.push(ts_poet_1.code `
1188
- obj.${jsonName} = {};
1189
+ ${jsonProperty} = {};
1189
1190
  if (message.${fieldName}) {
1190
1191
  Object.entries(message.${fieldName}).forEach(([k, v]) => {
1191
- obj.${jsonName}[k] = ${readSnippet('v')};
1192
+ ${jsonProperty}[k] = ${readSnippet('v')};
1192
1193
  });
1193
1194
  }
1194
1195
  `);
@@ -1197,9 +1198,9 @@ function generateToJson(ctx, fullName, messageDesc) {
1197
1198
  // Arrays might need their elements transformed
1198
1199
  chunks.push(ts_poet_1.code `
1199
1200
  if (message.${fieldName}) {
1200
- obj.${jsonName} = message.${fieldName}.map(e => ${readSnippet('e')});
1201
+ ${jsonProperty} = message.${fieldName}.map(e => ${readSnippet('e')});
1201
1202
  } else {
1202
- obj.${jsonName} = [];
1203
+ ${jsonProperty} = [];
1203
1204
  }
1204
1205
  `);
1205
1206
  }
package/build/utils.js CHANGED
@@ -160,12 +160,13 @@ exports.getFieldJsonName = getFieldJsonName;
160
160
  * For simplicity, we don't match the ECMA 5/6 rules for valid identifiers exactly, and return array syntax liberally.
161
161
  * @param objectName
162
162
  * @param propertyName
163
+ * @param optional
163
164
  */
164
- function getPropertyAccessor(objectName, propertyName) {
165
+ function getPropertyAccessor(objectName, propertyName, optional = false) {
165
166
  let validIdentifier = /^[a-zA-Z_$][\w$]*$/;
166
167
  return validIdentifier.test(propertyName)
167
- ? `${objectName}.${propertyName}`
168
- : `${objectName}[${JSON.stringify(propertyName)}]`;
168
+ ? `${objectName}${optional ? '?' : ''}.${propertyName}`
169
+ : `${objectName}${optional ? '?.' : ''}[${JSON.stringify(propertyName)}]`;
169
170
  }
170
171
  exports.getPropertyAccessor = getPropertyAccessor;
171
172
  function impProto(options, module, type) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.106.0",
3
+ "version": "1.106.1",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",