ts-proto 2.15.2 → 2.15.3

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.
Files changed (2) hide show
  1. package/build/src/main.js +32 -13
  2. package/package.json +1 -1
package/build/src/main.js CHANGED
@@ -1900,6 +1900,27 @@ function generateExtension(ctx, message, extension) {
1900
1900
  extensionInfo: (0, ts_poet_1.joinCode)(chunks, { on: "\n" }),
1901
1901
  };
1902
1902
  }
1903
+ /** Converts the JSON of a wrapper type, such as `google.protobuf.Int64Value`, to the unwrapped value. */
1904
+ function valueTypeFromJson(ctx, field, from) {
1905
+ const { options } = ctx;
1906
+ const valueType = (0, types_1.valueTypeName)(ctx, field.typeName);
1907
+ if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.LONG) {
1908
+ return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString([]))}.fromValue(${from})`;
1909
+ }
1910
+ else if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.BIGINT) {
1911
+ return (0, ts_poet_1.code) `BigInt(${from})`;
1912
+ }
1913
+ else if ((0, types_1.isBytesValueType)(field)) {
1914
+ return (0, ts_poet_1.code) `new ${(0, case_1.capitalize)(valueType.toCodeString([]))}(${from})`;
1915
+ }
1916
+ else {
1917
+ return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString([]))}(${from})`;
1918
+ }
1919
+ }
1920
+ /** Whether `field` is a 64-bit wrapper type unwrapped to a `Long` or `bigint`, which JSON writes as a string. */
1921
+ function isLongValueTypeAsObject(options, field) {
1922
+ return (0, types_1.isLongValueType)(field) && (options.forceLong === options_1.LongOption.LONG || options.forceLong === options_1.LongOption.BIGINT);
1923
+ }
1903
1924
  /**
1904
1925
  * Creates a function to decode a message from JSON.
1905
1926
  *
@@ -2042,19 +2063,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
2042
2063
  return (0, ts_poet_1.code) `[...${from}]`;
2043
2064
  }
2044
2065
  else if ((0, types_1.isValueType)(ctx, field)) {
2045
- const valueType = (0, types_1.valueTypeName)(ctx, field.typeName);
2046
- if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.LONG) {
2047
- return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString([]))}.fromValue(${from})`;
2048
- }
2049
- else if ((0, types_1.isLongValueType)(field) && options.forceLong === options_1.LongOption.BIGINT) {
2050
- return (0, ts_poet_1.code) `BigInt(${from})`;
2051
- }
2052
- else if ((0, types_1.isBytesValueType)(field)) {
2053
- return (0, ts_poet_1.code) `new ${(0, case_1.capitalize)(valueType.toCodeString([]))}(${from})`;
2054
- }
2055
- else {
2056
- return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString([]))}(${from})`;
2057
- }
2066
+ return valueTypeFromJson(ctx, field, from);
2058
2067
  }
2059
2068
  else if ((0, types_1.isMessage)(field)) {
2060
2069
  if ((0, types_1.isRepeated)(field) && (0, types_1.isMapType)(ctx, messageDesc, field)) {
@@ -2097,6 +2106,9 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
2097
2106
  options.useDate === options_1.DateOption.TIMESTAMP)) {
2098
2107
  return (0, ts_poet_1.code) `${utils.fromJsonTimestamp}(${from})`;
2099
2108
  }
2109
+ else if ((0, types_1.isLongValueType)(valueField)) {
2110
+ return valueTypeFromJson(ctx, valueField, from);
2111
+ }
2100
2112
  else if ((0, types_1.isValueType)(ctx, valueField)) {
2101
2113
  return (0, ts_poet_1.code) `${from} as ${valueType}`;
2102
2114
  }
@@ -2496,6 +2508,10 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2496
2508
  else if ((0, types_1.isFloatingPoint)(valueType)) {
2497
2509
  return (0, ts_poet_1.code) `${utils.floatToJson}(${from})`;
2498
2510
  }
2511
+ else if (isLongValueTypeAsObject(options, valueType)) {
2512
+ // Map values of wrapper types are declared `| undefined`.
2513
+ return (0, ts_poet_1.code) `${from}?.toString()`;
2514
+ }
2499
2515
  else if ((0, types_1.isScalar)(valueType) || (0, types_1.isValueType)(ctx, valueType)) {
2500
2516
  return (0, ts_poet_1.code) `${from}`;
2501
2517
  }
@@ -2521,6 +2537,9 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
2521
2537
  else if ((0, types_1.isBytes)(field)) {
2522
2538
  return (0, ts_poet_1.code) `${utils.base64FromBytes}(${from})`;
2523
2539
  }
2540
+ else if (isLongValueTypeAsObject(options, field)) {
2541
+ return (0, ts_poet_1.code) `${from}.toString()`;
2542
+ }
2524
2543
  else if ((0, types_1.isLong)(field) && (0, types_1.isJsTypeFieldOption)(options, field)) {
2525
2544
  const fieldType = (0, types_1.getFieldOptionsJsType)(field, ctx.options) ?? field.type;
2526
2545
  if (!fieldType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.15.2",
3
+ "version": "2.15.3",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",