ts-proto 1.86.0 → 1.87.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.87.0](https://github.com/stephenh/ts-proto/compare/v1.86.0...v1.87.0) (2021-11-16)
2
+
3
+
4
+ ### Features
5
+
6
+ * Use ternary operator for conditional assignments ([#394](https://github.com/stephenh/ts-proto/issues/394)) ([d84c084](https://github.com/stephenh/ts-proto/commit/d84c084fb56c958c184f8971479979b8bfb17ccc))
7
+
1
8
  # [1.86.0](https://github.com/stephenh/ts-proto/compare/v1.85.0...v1.86.0) (2021-11-15)
2
9
 
3
10
 
package/build/main.js CHANGED
@@ -860,12 +860,12 @@ function generateFromJson(ctx, fullName, messageDesc) {
860
860
  chunks.push(ts_poet_1.code `}`);
861
861
  }
862
862
  else {
863
- chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
864
- chunks.push(ts_poet_1.code `message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
865
- chunks.push(ts_poet_1.code `} else {`);
866
863
  const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
867
- chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback};`);
868
- chunks.push(ts_poet_1.code `}`);
864
+ chunks.push(ts_poet_1.code `
865
+ message.${fieldName} = (object.${fieldName} !== undefined && object.${fieldName} !== null)
866
+ ? ${readSnippet(`object.${fieldName}`)}
867
+ : ${fallback};
868
+ `);
869
869
  }
870
870
  });
871
871
  // and then wrap up the switch/while/return
@@ -1081,20 +1081,18 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1081
1081
  chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback}`);
1082
1082
  chunks.push(ts_poet_1.code `}`);
1083
1083
  }
1084
- else if (types_1.isPrimitive(field) ||
1085
- (types_1.isTimestamp(field) && (options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) ||
1086
- types_1.isValueType(ctx, field)) {
1084
+ else if (readSnippet(`x`).toCodeString() == 'x') {
1087
1085
  // An optimized case of the else below that works when `readSnippet` returns the plain input
1088
1086
  const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1089
1087
  chunks.push(ts_poet_1.code `message.${fieldName} = object.${fieldName} ?? ${fallback};`);
1090
1088
  }
1091
1089
  else {
1092
- chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
1093
- chunks.push(ts_poet_1.code `message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
1094
- chunks.push(ts_poet_1.code `} else {`);
1095
1090
  const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1096
- chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback};`);
1097
- chunks.push(ts_poet_1.code `}`);
1091
+ chunks.push(ts_poet_1.code `
1092
+ message.${fieldName} = (object.${fieldName} !== undefined && object.${fieldName} !== null)
1093
+ ? ${readSnippet(`object.${fieldName}`)}
1094
+ : ${fallback};
1095
+ `);
1098
1096
  }
1099
1097
  });
1100
1098
  // and then wrap up the switch/while/return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.86.0",
3
+ "version": "1.87.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",