ts-proto 1.85.0 → 1.86.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.86.0](https://github.com/stephenh/ts-proto/compare/v1.85.0...v1.86.0) (2021-11-15)
2
+
3
+
4
+ ### Features
5
+
6
+ * Initialize lists with map ([#387](https://github.com/stephenh/ts-proto/issues/387)) ([200e674](https://github.com/stephenh/ts-proto/commit/200e674e4baf2640d67720ad535fe042d291d4a0))
7
+
1
8
  # [1.85.0](https://github.com/stephenh/ts-proto/compare/v1.84.0...v1.85.0) (2021-11-02)
2
9
 
3
10
 
package/build/main.js CHANGED
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.contextTypeVar = exports.makeUtils = exports.generateFile = void 0;
4
4
  const ts_poet_1 = require("ts-poet");
5
- const ts_proto_descriptors_1 = require("ts-proto-descriptors");
6
5
  const types_1 = require("./types");
7
6
  const sourceInfo_1 = require("./sourceInfo");
8
7
  const utils_1 = require("./utils");
@@ -739,20 +738,6 @@ function generateFromJson(ctx, fullName, messageDesc) {
739
738
  fromJSON(${messageDesc.field.length > 0 ? 'object' : '_'}: any): ${fullName} {
740
739
  const message = { ...base${fullName} } as ${fullName};
741
740
  `);
742
- // initialize all lists
743
- messageDesc.field.filter(types_1.isRepeated).forEach((field) => {
744
- const value = types_1.isMapType(ctx, messageDesc, field) ? '{}' : '[]';
745
- const name = case_1.maybeSnakeToCamel(field.name, options);
746
- chunks.push(ts_poet_1.code `message.${name} = ${value};`);
747
- });
748
- // initialize all buffers
749
- messageDesc.field
750
- .filter((field) => !types_1.isRepeated(field) && !types_1.isWithinOneOf(field) && types_1.isBytes(field))
751
- .forEach((field) => {
752
- const value = options.env === options_1.EnvOption.NODE ? 'Buffer.alloc(0)' : 'new Uint8Array()';
753
- const name = case_1.maybeSnakeToCamel(field.name, options);
754
- chunks.push(ts_poet_1.code `message.${name} = ${value};`);
755
- });
756
741
  // add a check for each incoming field
757
742
  messageDesc.field.forEach((field) => {
758
743
  const fieldName = case_1.maybeSnakeToCamel(field.name, options);
@@ -847,42 +832,41 @@ function generateFromJson(ctx, fullName, messageDesc) {
847
832
  }
848
833
  };
849
834
  // and then use the snippet to handle repeated fields if necessary
850
- chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
851
835
  if (types_1.isRepeated(field)) {
852
836
  if (types_1.isMapType(ctx, messageDesc, field)) {
837
+ chunks.push(ts_poet_1.code `message.${fieldName} = {};`);
838
+ chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
853
839
  const i = maybeCastToNumber(ctx, messageDesc, field, 'key');
854
840
  chunks.push(ts_poet_1.code `
855
841
  Object.entries(object.${fieldName}).forEach(([key, value]) => {
856
842
  message.${fieldName}[${i}] = ${readSnippet('value')};
857
843
  });
858
844
  `);
845
+ chunks.push(ts_poet_1.code `}`);
859
846
  }
860
847
  else {
848
+ // Explicit `any` type required to make TS with noImplicitAny happy. `object` is also `any` here.
861
849
  chunks.push(ts_poet_1.code `
862
- for (const e of object.${fieldName}) {
863
- message.${fieldName}.push(${readSnippet('e')});
864
- }
850
+ message.${fieldName} = (object.${fieldName} ?? []).map((e: any) => ${readSnippet('e')});
865
851
  `);
866
852
  }
867
853
  }
868
854
  else if (types_1.isWithinOneOfThatShouldBeUnion(options, field)) {
855
+ chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
869
856
  const oneofName = case_1.maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
870
857
  chunks.push(ts_poet_1.code `
871
858
  message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${readSnippet(`object.${fieldName}`)} }
872
859
  `);
860
+ chunks.push(ts_poet_1.code `}`);
873
861
  }
874
862
  else {
863
+ chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
875
864
  chunks.push(ts_poet_1.code `message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
876
- }
877
- // set the default value (TODO Support bytes)
878
- if (!types_1.isRepeated(field) &&
879
- field.type !== ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BYTES &&
880
- options.oneof !== options_1.OneofOption.UNIONS) {
881
- const v = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
882
865
  chunks.push(ts_poet_1.code `} else {`);
883
- chunks.push(ts_poet_1.code `message.${fieldName} = ${v};`);
866
+ 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 `}`);
884
869
  }
885
- chunks.push(ts_poet_1.code `}`);
886
870
  });
887
871
  // and then wrap up the switch/while/return
888
872
  chunks.push(ts_poet_1.code `return message`);
@@ -1055,11 +1039,9 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1055
1039
  };
1056
1040
  // and then use the snippet to handle repeated fields if necessary
1057
1041
  if (types_1.isRepeated(field)) {
1058
- const value = types_1.isMapType(ctx, messageDesc, field) ? '{}' : '[]';
1059
- const name = case_1.maybeSnakeToCamel(field.name, options);
1060
- chunks.push(ts_poet_1.code `message.${name} = ${value};`);
1061
- chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
1062
1042
  if (types_1.isMapType(ctx, messageDesc, field)) {
1043
+ chunks.push(ts_poet_1.code `message.${fieldName} = {};`);
1044
+ chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
1063
1045
  const i = maybeCastToNumber(ctx, messageDesc, field, 'key');
1064
1046
  chunks.push(ts_poet_1.code `
1065
1047
  Object.entries(object.${fieldName}).forEach(([key, value]) => {
@@ -1068,15 +1050,13 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1068
1050
  }
1069
1051
  });
1070
1052
  `);
1053
+ chunks.push(ts_poet_1.code `}`);
1071
1054
  }
1072
1055
  else {
1073
1056
  chunks.push(ts_poet_1.code `
1074
- for (const e of object.${fieldName}) {
1075
- message.${fieldName}.push(${readSnippet('e')});
1076
- }
1057
+ message.${fieldName} = (object.${fieldName} ?? []).map((e) => ${readSnippet('e')});
1077
1058
  `);
1078
1059
  }
1079
- chunks.push(ts_poet_1.code `}`);
1080
1060
  }
1081
1061
  else if (types_1.isWithinOneOfThatShouldBeUnion(options, field)) {
1082
1062
  let oneofName = case_1.maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
@@ -1113,7 +1093,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1113
1093
  chunks.push(ts_poet_1.code `message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
1114
1094
  chunks.push(ts_poet_1.code `} else {`);
1115
1095
  const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
1116
- chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback}`);
1096
+ chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback};`);
1117
1097
  chunks.push(ts_poet_1.code `}`);
1118
1098
  }
1119
1099
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.85.0",
3
+ "version": "1.86.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",