ts-proto 1.162.1 → 1.163.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/build/encode.js CHANGED
@@ -18,7 +18,7 @@ function generateEncoder(ctx, typeName) {
18
18
  }
19
19
  return (0, ts_poet_1.code) `${TimestampValue}.encode(${value}).finish()`;
20
20
  }
21
- if (name == "Struct") {
21
+ if (name == "Struct" || name == "Value") {
22
22
  const StructType = (0, utils_1.impProto)(ctx.options, "google/protobuf/struct", name);
23
23
  return (0, ts_poet_1.code) `${StructType}.encode(${StructType}.wrap(value)).finish()`;
24
24
  }
@@ -64,7 +64,7 @@ function generateDecoder(ctx, typeName) {
64
64
  }
65
65
  return decoder;
66
66
  }
67
- if (name == "Struct" || name == "ListValue") {
67
+ if (name == "Struct" || name == "ListValue" || name == "Value") {
68
68
  TypeValue = (0, utils_1.impProto)(ctx.options, "google/protobuf/struct", name);
69
69
  return (0, ts_poet_1.code) `${TypeValue}.unwrap(${TypeValue}.decode(value))`;
70
70
  }
package/build/enums.js CHANGED
@@ -38,6 +38,16 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
38
38
  chunks.push((0, ts_poet_1.code) `} as const`);
39
39
  chunks.push((0, ts_poet_1.code) `\n`);
40
40
  chunks.push((0, ts_poet_1.code) `export type ${(0, ts_poet_1.def)(fullName)} = typeof ${(0, ts_poet_1.def)(fullName)}[keyof typeof ${(0, ts_poet_1.def)(fullName)}]`);
41
+ chunks.push((0, ts_poet_1.code) `\n`);
42
+ chunks.push((0, ts_poet_1.code) `export namespace ${(0, ts_poet_1.def)(fullName)} {`);
43
+ enumDesc.value.forEach((valueDesc) => {
44
+ const memberName = getMemberName(ctx, enumDesc, valueDesc);
45
+ chunks.push((0, ts_poet_1.code) `export type ${memberName} = typeof ${(0, ts_poet_1.def)(fullName)}.${memberName};`);
46
+ });
47
+ if (options.unrecognizedEnum && !unrecognizedEnum.present) {
48
+ chunks.push((0, ts_poet_1.code) `export type ${options.unrecognizedEnumName} = typeof ${(0, ts_poet_1.def)(fullName)}.${options.unrecognizedEnumName};`);
49
+ }
50
+ chunks.push((0, ts_poet_1.code) `}`);
41
51
  }
42
52
  else {
43
53
  chunks.push((0, ts_poet_1.code) `}`);
@@ -134,9 +134,7 @@ function generateUnwrapDeep(ctx, fullProtoTypeName, fieldNames) {
134
134
  }`);
135
135
  }
136
136
  if ((0, types_1.isFieldMaskTypeName)(fullProtoTypeName)) {
137
- chunks.push((0, ts_poet_1.code) `unwrap(message: ${ctx.options.useReadonlyTypes ? "any" : "FieldMask"}): string[] {
138
- return message.paths;
139
- }`);
137
+ chunks.push(generateFieldMaskUnwrap(ctx));
140
138
  }
141
139
  return chunks;
142
140
  }
@@ -303,13 +301,18 @@ function generateUnwrapShallow(ctx, fullProtoTypeName, fieldNames) {
303
301
  }`);
304
302
  }
305
303
  if ((0, types_1.isFieldMaskTypeName)(fullProtoTypeName)) {
306
- chunks.push((0, ts_poet_1.code) `unwrap(message: ${ctx.options.useReadonlyTypes ? "any" : "FieldMask"}): string[] {
307
- return message.paths;
308
- }`);
304
+ chunks.push(generateFieldMaskUnwrap(ctx));
309
305
  }
310
306
  return chunks;
311
307
  }
312
308
  exports.generateUnwrapShallow = generateUnwrapShallow;
309
+ function generateFieldMaskUnwrap(ctx) {
310
+ const returnType = ctx.options.useOptionals === "all" ? "string[] | undefined" : "string[]";
311
+ const pathModifier = ctx.options.useOptionals === "all" ? "?" : "";
312
+ return (0, ts_poet_1.code) `unwrap(message: ${ctx.options.useReadonlyTypes ? "any" : "FieldMask"}): ${returnType} {
313
+ return message${pathModifier}.paths;
314
+ }`;
315
+ }
313
316
  function maybeReadonly(options) {
314
317
  return options.useReadonlyTypes ? "readonly " : "";
315
318
  }
package/build/main.js CHANGED
@@ -1687,11 +1687,13 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1687
1687
  chunks.push((0, ts_poet_1.code) `}`);
1688
1688
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
1689
1689
  }
1690
- function generateCanonicalToJson(fullName, fullProtobufTypeName) {
1690
+ function generateCanonicalToJson(fullName, fullProtobufTypeName, { useOptionals }) {
1691
1691
  if ((0, types_1.isFieldMaskTypeName)(fullProtobufTypeName)) {
1692
+ const returnType = useOptionals === "all" ? "string | undefined" : "string";
1693
+ const pathModifier = useOptionals === "all" ? "?" : "";
1692
1694
  return (0, ts_poet_1.code) `
1693
- toJSON(message: ${fullName}): string {
1694
- return message.paths.join(',');
1695
+ toJSON(message: ${fullName}): ${returnType} {
1696
+ return message.paths${pathModifier}.join(',');
1695
1697
  }
1696
1698
  `;
1697
1699
  }
@@ -1700,7 +1702,7 @@ function generateCanonicalToJson(fullName, fullProtobufTypeName) {
1700
1702
  function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
1701
1703
  const { options, utils, typeMap } = ctx;
1702
1704
  const chunks = [];
1703
- const canonicalToJson = generateCanonicalToJson(fullName, fullProtobufTypeName);
1705
+ const canonicalToJson = generateCanonicalToJson(fullName, fullProtobufTypeName, options);
1704
1706
  if (canonicalToJson) {
1705
1707
  chunks.push(canonicalToJson);
1706
1708
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
package/build/types.js CHANGED
@@ -512,6 +512,7 @@ function wrapperTypeName(typeName) {
512
512
  case ".google.protobuf.ListValue":
513
513
  case ".google.protobuf.Timestamp":
514
514
  case ".google.protobuf.Struct":
515
+ case ".google.protobuf.Value":
515
516
  return typeName.split(".")[3];
516
517
  default:
517
518
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.162.1",
3
+ "version": "1.163.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",