ts-proto 1.156.5 → 1.156.7

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/build/main.js CHANGED
@@ -614,9 +614,6 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
614
614
  const name = (0, case_1.maybeSnakeToCamel)(fieldDesc.name, options);
615
615
  const isOptional = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options);
616
616
  const type = (0, types_1.toTypeName)(ctx, messageDesc, fieldDesc, isOptional);
617
- if (isOptional && !type.toString().includes("undefined")) {
618
- console.warn(name, type);
619
- }
620
617
  chunks.push((0, ts_poet_1.code) `${maybeReadonly(options)}${name}${isOptional ? "?" : ""}: ${type}, `);
621
618
  });
622
619
  if (ctx.options.unknownFields) {
@@ -680,7 +677,7 @@ function generateBaseInstanceFactory(ctx, fullName, messageDesc, fullTypeName) {
680
677
  const val = (0, types_1.isWithinOneOf)(field)
681
678
  ? "undefined"
682
679
  : (0, types_1.isMapType)(ctx, messageDesc, field)
683
- ? ctx.options.useMapType
680
+ ? (0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)
684
681
  ? "new Map()"
685
682
  : "{}"
686
683
  : (0, types_1.isRepeated)(field)
@@ -808,16 +805,22 @@ function generateDecode(ctx, fullName, messageDesc) {
808
805
  const initializerNecessary = !options.initializeFieldsAsUndefined && (0, types_1.isOptionalProperty)(field, messageDesc.options, options);
809
806
  if ((0, types_1.isRepeated)(field)) {
810
807
  const maybeNonNullAssertion = ctx.options.useOptionals === "all" ? "!" : "";
811
- if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
808
+ const mapType = (0, types_1.detectMapType)(ctx, messageDesc, field);
809
+ if (mapType) {
812
810
  // We need a unique const within the `cast` statement
813
811
  const varName = `entry${field.number}`;
814
- const valueSetterSnippet = ctx.options.useMapType
815
- ? `message.${fieldName}${maybeNonNullAssertion}.set(${varName}.key, ${varName}.value)`
816
- : `message.${fieldName}${maybeNonNullAssertion}[${varName}.key] = ${varName}.value`;
812
+ const generateMapType = (0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field);
813
+ let valueSetterSnippet;
814
+ if (generateMapType) {
815
+ valueSetterSnippet = `message.${fieldName}${maybeNonNullAssertion}.set(${varName}.key, ${varName}.value)`;
816
+ }
817
+ else {
818
+ valueSetterSnippet = `message.${fieldName}${maybeNonNullAssertion}[${varName}.key] = ${varName}.value`;
819
+ }
817
820
  const initializerSnippet = initializerNecessary
818
821
  ? `
819
822
  if (message.${fieldName} === undefined) {
820
- message.${fieldName} = ${ctx.options.useMapType ? "new Map()" : "{}"};
823
+ message.${fieldName} = ${generateMapType ? "new Map()" : "{}"};
821
824
  }`
822
825
  : "";
823
826
  chunks.push((0, ts_poet_1.code) `
@@ -1011,8 +1014,9 @@ function generateEncode(ctx, fullName, messageDesc) {
1011
1014
  }
1012
1015
  `
1013
1016
  : writeSnippet(`{ ${maybeTypeField} key: key as any, value }`);
1014
- const optionalAlternative = isOptional ? (ctx.options.useMapType ? " || new Map()" : " || {}") : "";
1015
- if (ctx.options.useMapType) {
1017
+ const useMapType = (0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field);
1018
+ const optionalAlternative = isOptional ? (useMapType ? " || new Map()" : " || {}") : "";
1019
+ if (useMapType) {
1016
1020
  chunks.push((0, ts_poet_1.code) `
1017
1021
  (message.${fieldName}${optionalAlternative}).forEach((value, key) => {
1018
1022
  ${entryWriteSnippet}
@@ -1535,7 +1539,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1535
1539
  if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
1536
1540
  const fieldType = (0, types_1.toTypeName)(ctx, messageDesc, field);
1537
1541
  const i = convertFromObjectKey(ctx, messageDesc, field, "key");
1538
- if (ctx.options.useMapType) {
1542
+ if ((0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)) {
1539
1543
  const fallback = noDefaultValue ? "undefined" : "new Map()";
1540
1544
  chunks.push((0, ts_poet_1.code) `
1541
1545
  ${fieldName}: ${ctx.utils.isObject}(${jsonProperty})
@@ -1736,7 +1740,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
1736
1740
  if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
1737
1741
  // Maps might need their values transformed, i.e. bytes --> base64
1738
1742
  const i = convertToObjectKey(ctx, messageDesc, field, "k");
1739
- if (ctx.options.useMapType) {
1743
+ if ((0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)) {
1740
1744
  chunks.push((0, ts_poet_1.code) `
1741
1745
  if (message.${fieldName}?.size) {
1742
1746
  ${jsonProperty} = {};
@@ -1904,7 +1908,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
1904
1908
  const noValueSnippet = noDefaultValue
1905
1909
  ? `(object.${fieldName} === undefined || object.${fieldName} === null) ? undefined : `
1906
1910
  : "";
1907
- if (ctx.options.useMapType) {
1911
+ if ((0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)) {
1908
1912
  chunks.push((0, ts_poet_1.code) `
1909
1913
  message.${fieldName} = ${noValueSnippet} (() => {
1910
1914
  const m = new Map();
@@ -1973,7 +1977,7 @@ function convertFromObjectKey(ctx, messageDesc, field, variableName) {
1973
1977
  if (keyType.toCodeString([]) === "string") {
1974
1978
  return (0, ts_poet_1.code) `${variableName}`;
1975
1979
  }
1976
- else if ((0, types_1.isLong)(keyField) && ctx.options.useMapType) {
1980
+ else if ((0, types_1.isLong)(keyField) && (0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)) {
1977
1981
  if (ctx.options.forceLong === options_1.LongOption.LONG) {
1978
1982
  return (0, ts_poet_1.code) `${(0, case_1.capitalize)(keyType.toCodeString([]))}.fromValue(${variableName})`;
1979
1983
  }
@@ -1996,7 +2000,7 @@ function convertToObjectKey(ctx, messageDesc, field, variableName) {
1996
2000
  if (keyType.toCodeString([]) === "string") {
1997
2001
  return (0, ts_poet_1.code) `${variableName}`;
1998
2002
  }
1999
- else if ((0, types_1.isLong)(keyField) && ctx.options.useMapType) {
2003
+ else if ((0, types_1.isLong)(keyField) && (0, types_1.shouldGenerateJSMapType)(ctx, messageDesc, field)) {
2000
2004
  if (ctx.options.forceLong === options_1.LongOption.LONG) {
2001
2005
  return (0, ts_poet_1.code) `${ctx.utils.longToNumber}(${variableName})`;
2002
2006
  }
package/build/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.observableType = exports.rawRequestType = exports.detectMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isFieldMaskTypeName = exports.isFieldMaskType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isObjectId = exports.isMapType = exports.isWholeNumber = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isOptionalProperty = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
3
+ exports.detectBatchMethod = exports.responsePromiseOrObservable = exports.responseObservable = exports.responsePromise = exports.responseType = exports.requestType = exports.observableType = exports.rawRequestType = exports.detectMapType = exports.shouldGenerateJSMapType = exports.toTypeName = exports.getEnumMethod = exports.messageToTypeName = exports.wrapperTypeName = exports.valueTypeName = exports.isEmptyType = exports.isLongValueType = exports.isStructTypeName = exports.isStructType = exports.isListValueTypeName = exports.isListValueType = exports.isFieldMaskTypeName = exports.isFieldMaskType = exports.isBytesValueType = exports.isAnyValueTypeName = exports.isAnyValueType = exports.isValueType = exports.isTimestamp = exports.isObjectId = exports.isMapType = exports.isWholeNumber = exports.isLong = exports.isRepeated = exports.isWithinOneOfThatShouldBeUnion = exports.isWithinOneOf = exports.isEnum = exports.isMessage = exports.isBytes = exports.isPrimitive = exports.isOptionalProperty = exports.isScalar = exports.createTypeMap = exports.notDefaultCheck = exports.defaultValue = exports.packedType = exports.toReaderCall = exports.basicTypeName = exports.basicLongWireType = exports.basicWireType = void 0;
4
4
  const ts_proto_descriptors_1 = require("ts-proto-descriptors");
5
5
  const ts_poet_1 = require("ts-poet");
6
6
  const options_1 = require("./options");
@@ -587,7 +587,7 @@ function toTypeName(ctx, messageDesc, field, ensureOptional = false) {
587
587
  const mapType = messageDesc ? detectMapType(ctx, messageDesc, field) : false;
588
588
  if (mapType) {
589
589
  const { keyType, valueType } = mapType;
590
- if (ctx.options.useMapType) {
590
+ if (shouldGenerateJSMapType(ctx, messageDesc, field)) {
591
591
  return finalize((0, ts_poet_1.code) `Map<${keyType}, ${valueType}>`, ensureOptional);
592
592
  }
593
593
  return finalize((0, ts_poet_1.code) `{ [key: ${keyType} ]: ${valueType} }`, ensureOptional);
@@ -622,6 +622,29 @@ function toTypeName(ctx, messageDesc, field, ensureOptional = false) {
622
622
  ensureOptional);
623
623
  }
624
624
  exports.toTypeName = toTypeName;
625
+ /**
626
+ * For a protobuf map field, if the generated code should use the javascript Map type.
627
+ *
628
+ * If the type of a protobuf map key corresponds to the Long type, we always use the Map type. This avoids generating
629
+ * invalid code such as below (using Long as key of a javascript object):
630
+ *
631
+ * export interface Foo {
632
+ * bar: { [key: Long]: Long }
633
+ * }
634
+ *
635
+ * See https://github.com/stephenh/ts-proto/issues/708 for more details.
636
+ */
637
+ function shouldGenerateJSMapType(ctx, message, field) {
638
+ if (ctx.options.useMapType) {
639
+ return true;
640
+ }
641
+ const mapType = detectMapType(ctx, message, field);
642
+ if (!mapType) {
643
+ return false;
644
+ }
645
+ return isLong(mapType.keyField) && ctx.options.forceLong === options_1.LongOption.LONG;
646
+ }
647
+ exports.shouldGenerateJSMapType = shouldGenerateJSMapType;
625
648
  function detectMapType(ctx, messageDesc, fieldDesc) {
626
649
  var _a;
627
650
  const { typeMap } = ctx;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.156.5",
3
+ "version": "1.156.7",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",
@@ -54,7 +54,7 @@
54
54
  "mongodb": "^5.7.0",
55
55
  "nice-grpc": "^2.1.4",
56
56
  "object-hash": "^3.0.0",
57
- "prettier": "^3.0.0",
57
+ "prettier": "^2.8.8",
58
58
  "protobufjs-cli": "^1.1.1",
59
59
  "reflect-metadata": "^0.1.13",
60
60
  "rxjs": "^7.8.1",