ts-proto 1.83.3 → 1.84.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.84.0](https://github.com/stephenh/ts-proto/compare/v1.83.3...v1.84.0) (2021-11-02)
2
+
3
+
4
+ ### Features
5
+
6
+ * Reduce code size by using nullish coalescing operator in fromPartial ([#376](https://github.com/stephenh/ts-proto/issues/376)) ([19d2ded](https://github.com/stephenh/ts-proto/commit/19d2deda2cf7c47b1b56bfc65cf58653291dba4a))
7
+
1
8
  ## [1.83.3](https://github.com/stephenh/ts-proto/compare/v1.83.2...v1.83.3) (2021-10-28)
2
9
 
3
10
 
package/build/main.js CHANGED
@@ -1002,12 +1002,6 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1002
1002
  fromPartial(${messageDesc.field.length > 0 ? 'object' : '_'}: ${utils.DeepPartial}<${fullName}>): ${fullName} {
1003
1003
  const message = { ...base${fullName} } as ${fullName};
1004
1004
  `);
1005
- // initialize all lists
1006
- messageDesc.field.filter(types_1.isRepeated).forEach((field) => {
1007
- const value = types_1.isMapType(ctx, messageDesc, field) ? '{}' : '[]';
1008
- const name = case_1.maybeSnakeToCamel(field.name, options);
1009
- chunks.push(ts_poet_1.code `message.${name} = ${value};`);
1010
- });
1011
1005
  // add a check for each incoming field
1012
1006
  messageDesc.field.forEach((field) => {
1013
1007
  const fieldName = case_1.maybeSnakeToCamel(field.name, options);
@@ -1055,6 +1049,9 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1055
1049
  };
1056
1050
  // and then use the snippet to handle repeated fields if necessary
1057
1051
  if (types_1.isRepeated(field)) {
1052
+ const value = types_1.isMapType(ctx, messageDesc, field) ? '{}' : '[]';
1053
+ const name = case_1.maybeSnakeToCamel(field.name, options);
1054
+ chunks.push(ts_poet_1.code `message.${name} = ${value};`);
1058
1055
  chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
1059
1056
  if (types_1.isMapType(ctx, messageDesc, field)) {
1060
1057
  const i = maybeCastToNumber(ctx, messageDesc, field, 'key');
@@ -1073,6 +1070,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1073
1070
  }
1074
1071
  `);
1075
1072
  }
1073
+ chunks.push(ts_poet_1.code `}`);
1076
1074
  }
1077
1075
  else if (types_1.isWithinOneOfThatShouldBeUnion(options, field)) {
1078
1076
  let oneofName = case_1.maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
@@ -1084,25 +1082,34 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1084
1082
  && object.${oneofName}?.${fieldName} !== null
1085
1083
  ) {
1086
1084
  message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${v} };
1085
+ }
1087
1086
  `);
1088
1087
  }
1089
- else {
1088
+ else if ((types_1.isLong(field) || types_1.isLongValueType(field)) && options.forceLong === options_1.LongOption.LONG) {
1089
+ const v = readSnippet(`object.${fieldName}`);
1090
+ const type = types_1.basicTypeName(ctx, field);
1090
1091
  chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
1091
- if ((types_1.isLong(field) || types_1.isLongValueType(field)) && options.forceLong === options_1.LongOption.LONG) {
1092
- const v = readSnippet(`object.${fieldName}`);
1093
- const type = types_1.basicTypeName(ctx, field);
1094
- chunks.push(ts_poet_1.code `message.${fieldName} = ${v} as ${type};`);
1095
- }
1096
- else {
1097
- chunks.push(ts_poet_1.code `message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
1098
- }
1092
+ chunks.push(ts_poet_1.code `message.${fieldName} = ${v} as ${type};`);
1093
+ chunks.push(ts_poet_1.code `} else {`);
1094
+ const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1095
+ chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback}`);
1096
+ chunks.push(ts_poet_1.code `}`);
1099
1097
  }
1100
- if (!types_1.isRepeated(field) && !types_1.isWithinOneOfThatShouldBeUnion(options, field)) {
1098
+ else if (types_1.isPrimitive(field) ||
1099
+ (types_1.isTimestamp(field) && (options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) ||
1100
+ types_1.isValueType(ctx, field)) {
1101
+ // An optimized case of the else below that works when `readSnippet` returns the plain input
1102
+ const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1103
+ chunks.push(ts_poet_1.code `message.${fieldName} = object.${fieldName} ?? ${fallback};`);
1104
+ }
1105
+ else {
1106
+ chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
1107
+ chunks.push(ts_poet_1.code `message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
1101
1108
  chunks.push(ts_poet_1.code `} else {`);
1102
- const v = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1103
- chunks.push(ts_poet_1.code `message.${fieldName} = ${v}`);
1109
+ const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1110
+ chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback}`);
1111
+ chunks.push(ts_poet_1.code `}`);
1104
1112
  }
1105
- chunks.push(ts_poet_1.code `}`);
1106
1113
  });
1107
1114
  // and then wrap up the switch/while/return
1108
1115
  chunks.push(ts_poet_1.code `return message;`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.83.3",
3
+ "version": "1.84.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",