ts-proto 2.15.2 → 2.16.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/README.markdown CHANGED
@@ -612,6 +612,8 @@ With `--ts_proto_opt=protoJsonFormat=false`, strict proto3 compliance is disable
612
612
 
613
613
  - With `--ts_proto_opt=typeSuffix=MySuffix`, the generated interfaces, enums, and factories will have a suffix of `MySuffix` in their names.
614
614
 
615
+ - With `--ts_proto_opt=messagePrefix=MyPrefix` / `messageSuffix=MySuffix`, only the generated message interfaces and factories get the prefix/suffix, and with `enumPrefix=MyPrefix` / `enumSuffix=MySuffix` only the enums do. They are added inside `typePrefix`/`typeSuffix` when both are set, e.g. `typePrefix=T,messagePrefix=M` gives `TMFoo`.
616
+
615
617
  ```protobuf
616
618
  message ProfileInfo {
617
619
  int32 id = 1;
@@ -48,7 +48,7 @@ function generateWrapDeep(ctx, fullProtoTypeName, fieldNames) {
48
48
  chunks.push((0, ts_poet_1.code) `wrap(value: any): ${(0, utils_1.wrapTypeName)(ctx.options, "Value")} {
49
49
  const result = {} as any;
50
50
  if (value === null) {
51
- result.${fieldNames.nullValue} = ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue")}.NULL_VALUE;
51
+ result.${fieldNames.nullValue} = ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue", "enum")}.NULL_VALUE;
52
52
  } else if (typeof value === 'boolean') {
53
53
  result.${fieldNames.boolValue} = value;
54
54
  } else if (typeof value === 'number') {
@@ -180,7 +180,7 @@ function generateWrapShallow(ctx, fullProtoTypeName, fieldNames) {
180
180
  chunks.push((0, ts_poet_1.code) `wrap(value: any): ${(0, utils_1.wrapTypeName)(ctx.options, "Value")} {
181
181
  const result = createBase${(0, utils_1.wrapTypeName)(ctx.options, "Value")}()${maybeAsAny(ctx.options)};
182
182
  if (value === null) {
183
- result.kind = {$case: '${fieldNames.nullValue}', ${fieldNames.nullValue}: ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue")}.NULL_VALUE};
183
+ result.kind = {$case: '${fieldNames.nullValue}', ${fieldNames.nullValue}: ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue", "enum")}.NULL_VALUE};
184
184
  } else if (typeof value === 'boolean') {
185
185
  result.kind = {$case: '${fieldNames.boolValue}', ${fieldNames.boolValue}: value};
186
186
  } else if (typeof value === 'number') {
@@ -201,7 +201,7 @@ function generateWrapShallow(ctx, fullProtoTypeName, fieldNames) {
201
201
  chunks.push((0, ts_poet_1.code) `wrap(value: any): ${(0, utils_1.wrapTypeName)(ctx.options, "Value")} {
202
202
  const result = createBase${(0, utils_1.wrapTypeName)(ctx.options, "Value")}()${maybeAsAny(ctx.options)};
203
203
  if (value === null) {
204
- result.kind = {$case: '${fieldNames.nullValue}', value: ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue")}.NULL_VALUE};
204
+ result.kind = {$case: '${fieldNames.nullValue}', value: ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue", "enum")}.NULL_VALUE};
205
205
  } else if (typeof value === 'boolean') {
206
206
  result.kind = {$case: '${fieldNames.boolValue}', value };
207
207
  } else if (typeof value === 'number') {
@@ -222,7 +222,7 @@ function generateWrapShallow(ctx, fullProtoTypeName, fieldNames) {
222
222
  chunks.push((0, ts_poet_1.code) `wrap(value: any): ${(0, utils_1.wrapTypeName)(ctx.options, "Value")} {
223
223
  const result = createBase${(0, utils_1.wrapTypeName)(ctx.options, "Value")}()${maybeAsAny(ctx.options)};
224
224
  if (value === null) {
225
- result.${fieldNames.nullValue} = ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue")}.NULL_VALUE;
225
+ result.${fieldNames.nullValue} = ${(0, utils_1.wrapTypeName)(ctx.options, "NullValue", "enum")}.NULL_VALUE;
226
226
  } else if (typeof value === 'boolean') {
227
227
  result.${fieldNames.boolValue} = value;
228
228
  } else if (typeof value === 'number') {
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) {
@@ -121,6 +121,10 @@ export type Options = {
121
121
  bigIntLiteral: boolean;
122
122
  typePrefix: string;
123
123
  typeSuffix: string;
124
+ messagePrefix: string;
125
+ messageSuffix: string;
126
+ enumPrefix: string;
127
+ enumSuffix: string;
124
128
  useDuration: DurationOption;
125
129
  anyType: AnyTypeOption;
126
130
  };
@@ -135,6 +135,10 @@ function defaultOptions() {
135
135
  bigIntLiteral: true,
136
136
  typePrefix: "",
137
137
  typeSuffix: "",
138
+ messagePrefix: "",
139
+ messageSuffix: "",
140
+ enumPrefix: "",
141
+ enumSuffix: "",
138
142
  useDuration: DurationOption.DURATION,
139
143
  anyType: AnyTypeOption.AT_TYPE,
140
144
  };
@@ -80,4 +80,4 @@ export declare function getVersions(request: CodeGeneratorRequest): Promise<{
80
80
  protocVersion: string;
81
81
  tsProtoVersion: string;
82
82
  }>;
83
- export declare function wrapTypeName(options: Options, name: string): string;
83
+ export declare function wrapTypeName(options: Options, name: string, kind?: "message" | "enum"): string;
@@ -308,6 +308,7 @@ async function readPackageJson() {
308
308
  return await Promise.resolve(`${"../package.json"}`).then(s => require(s));
309
309
  }
310
310
  }
311
- function wrapTypeName(options, name) {
312
- return options.typePrefix + name + options.typeSuffix;
311
+ function wrapTypeName(options, name, kind = "message") {
312
+ const [prefix, suffix] = kind === "enum" ? [options.enumPrefix, options.enumSuffix] : [options.messagePrefix, options.messageSuffix];
313
+ return options.typePrefix + prefix + name + suffix + options.typeSuffix;
313
314
  }
@@ -13,7 +13,7 @@ function visit(proto, sourceInfo, messageFn, options, enumFn = () => { }, tsPref
13
13
  const protoFullName = protoPrefix + enumDesc.name;
14
14
  // I.e. FooBar_ZazInner
15
15
  const tsFullName = tsPrefix + (0, case_1.maybeSnakeToCamel)(enumDesc.name, options);
16
- const tsFullNameWithAffixes = messageName((0, utils_1.wrapTypeName)(options, tsFullName));
16
+ const tsFullNameWithAffixes = messageName((0, utils_1.wrapTypeName)(options, tsFullName, "enum"));
17
17
  const nestedSourceInfo = sourceInfo.open(childEnumType, index);
18
18
  enumFn(tsFullNameWithAffixes, enumDesc, nestedSourceInfo, protoFullName);
19
19
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.15.2",
3
+ "version": "2.16.0",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",