ts-proto 1.167.8 → 1.168.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
@@ -432,7 +432,7 @@ Generated code will be placed in the Gradle build directory.
432
432
 
433
433
  - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.
434
434
 
435
- - With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name. You can use `--ts_proto_opt=outputTypeAnnotations=static-only` to omit it from the `interface` declaration.
435
+ - With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name. You can use `--ts_proto_opt=outputTypeAnnotations=static-only` to omit it from the `interface` declaration, or `--ts_proto_opt=outputTypeAnnotations=optional` to make it an optional property on the `interface` definition. The latter option may be useful if you want to use the `$type` field for runtime type checking on responses from a server.
436
436
 
437
437
  - With `--ts_proto_opt=outputTypeRegistry=true`, the type registry will be generated that can be used to resolve message types by fully-qualified name. Also, each message will be given a `$type` field containing its fully-qualified name.
438
438
 
@@ -21,15 +21,20 @@ function generateWrapDeep(ctx, fullProtoTypeName, fieldNames) {
21
21
  const chunks = [];
22
22
  if ((0, types_1.isStructTypeName)(fullProtoTypeName)) {
23
23
  let setStatement = "struct.fields[key] = Value.wrap(object[key]);";
24
+ let defaultFields = "struct.fields ??= {};";
24
25
  if (ctx.options.useMapType) {
25
26
  setStatement = "struct.fields.set(key, Value.wrap(object[key]));";
27
+ defaultFields = "struct.fields ??= new Map<string, any | undefined>();";
26
28
  }
29
+ if (ctx.options.useOptionals !== "all")
30
+ defaultFields = "";
27
31
  chunks.push((0, ts_poet_1.code) `wrap(object: {[key: string]: any} | undefined): Struct {
28
32
  const struct = createBaseStruct();
33
+ ${defaultFields}
29
34
  if (object !== undefined) {
30
- Object.keys(object).forEach(key => {
35
+ for (const key of Object.keys(object)) {
31
36
  ${setStatement}
32
- });
37
+ }
33
38
  }
34
39
  return struct;
35
40
  }`);
@@ -85,9 +90,11 @@ function generateUnwrapDeep(ctx, fullProtoTypeName, fieldNames) {
85
90
  if (ctx.options.useMapType) {
86
91
  chunks.push((0, ts_poet_1.code) `unwrap(message: Struct): {[key: string]: any} {
87
92
  const object: { [key: string]: any } = {};
88
- [...message.fields.keys()].forEach((key) => {
89
- object[key] = Value.unwrap(message.fields.get(key));
90
- });
93
+ if (message.fields) {
94
+ for (const key of message.fields.keys()) {
95
+ object[key] = Value.unwrap(message.fields.get(key));
96
+ }
97
+ }
91
98
  return object;
92
99
  }`);
93
100
  }
@@ -95,9 +102,9 @@ function generateUnwrapDeep(ctx, fullProtoTypeName, fieldNames) {
95
102
  chunks.push((0, ts_poet_1.code) `unwrap(message: Struct): {[key: string]: any} {
96
103
  const object: { [key: string]: any } = {};
97
104
  if (message.fields) {
98
- Object.keys(message.fields).forEach(key => {
105
+ for (const key of Object.keys(message.fields)) {
99
106
  object[key] = Value.unwrap(message.fields[key]);
100
- });
107
+ }
101
108
  }
102
109
  return object;
103
110
  }`);
@@ -148,15 +155,20 @@ function generateWrapShallow(ctx, fullProtoTypeName, fieldNames) {
148
155
  const chunks = [];
149
156
  if ((0, types_1.isStructTypeName)(fullProtoTypeName)) {
150
157
  let setStatement = "struct.fields[key] = object[key];";
158
+ let defaultFields = "struct.fields ??= {};";
151
159
  if (ctx.options.useMapType) {
152
160
  setStatement = "struct.fields.set(key, object[key]);";
161
+ defaultFields = "struct.fields ??= new Map<string, any | undefined>();";
153
162
  }
163
+ if (ctx.options.useOptionals !== "all")
164
+ defaultFields = "";
154
165
  chunks.push((0, ts_poet_1.code) `wrap(object: {[key: string]: any} | undefined): Struct {
155
166
  const struct = createBaseStruct();
167
+ ${defaultFields}
156
168
  if (object !== undefined) {
157
- Object.keys(object).forEach(key => {
169
+ for (const key of Object.keys(object)) {
158
170
  ${setStatement}
159
- });
171
+ }
160
172
  }
161
173
  return struct;
162
174
  }`);
@@ -234,9 +246,11 @@ function generateUnwrapShallow(ctx, fullProtoTypeName, fieldNames) {
234
246
  if (ctx.options.useMapType) {
235
247
  chunks.push((0, ts_poet_1.code) `unwrap(message: Struct): {[key: string]: any} {
236
248
  const object: { [key: string]: any } = {};
237
- [...message.fields.keys()].forEach((key) => {
238
- object[key] = message.fields.get(key);
239
- });
249
+ if (message.fields) {
250
+ for (const key of message.fields.keys()) {
251
+ object[key] = message.fields.get(key);
252
+ }
253
+ }
240
254
  return object;
241
255
  }`);
242
256
  }
@@ -244,9 +258,9 @@ function generateUnwrapShallow(ctx, fullProtoTypeName, fieldNames) {
244
258
  chunks.push((0, ts_poet_1.code) `unwrap(message: Struct): {[key: string]: any} {
245
259
  const object: { [key: string]: any } = {};
246
260
  if (message.fields) {
247
- Object.keys(message.fields).forEach(key => {
261
+ for (const key of Object.keys(message.fields)) {
248
262
  object[key] = message.fields[key];
249
- });
263
+ }
250
264
  }
251
265
  return object;
252
266
  }`);
package/build/main.js CHANGED
@@ -678,7 +678,7 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
678
678
  // interface name should be defined to avoid import collisions
679
679
  chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(fullName)} {`);
680
680
  if ((0, options_1.addTypeToMessages)(options)) {
681
- chunks.push((0, ts_poet_1.code) `$type: '${fullTypeName}',`);
681
+ chunks.push((0, ts_poet_1.code) `$type${options.outputTypeAnnotations === "optional" ? "?" : ""}: '${fullTypeName}',`);
682
682
  }
683
683
  // When oneof=unions, we generate a single property with an ADT per `oneof` clause.
684
684
  const processedOneofs = new Set();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.167.8",
3
+ "version": "1.168.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",