@bufbuild/protoc-gen-es 0.2.0 → 0.3.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.2.0"
23
+ "@bufbuild/protoplugin": "0.3.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.2.0"
26
+ "@bufbuild/protobuf": "0.3.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -15,7 +15,6 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateDts = void 0;
17
17
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
18
- const match_wkt_js_1 = require("./match-wkt.js");
19
18
  function generateDts(schema) {
20
19
  for (const file of schema.files) {
21
20
  const f = schema.generateFile(file.name + "_pb.d.ts");
@@ -119,18 +118,20 @@ function generateField(schema, f, field) {
119
118
  }
120
119
  // prettier-ignore
121
120
  function generateWktMethods(schema, f, message) {
122
- const ref = (0, match_wkt_js_1.matchWkt)(message);
121
+ const ref = (0, ecmascript_1.reifyWkt)(message);
123
122
  if (ref === undefined) {
124
123
  return;
125
124
  }
126
- const { Message, MessageType, } = schema.runtime;
125
+ const { Message, MessageType, IMessageTypeRegistry } = schema.runtime;
127
126
  switch (ref.typeName) {
128
127
  case "google.protobuf.Any":
129
128
  f.print(" packFrom(message: ", Message, "): void;");
130
129
  f.print();
131
130
  f.print(" unpackTo(target: ", Message, "): boolean;");
132
131
  f.print();
133
- f.print(" is(type: ", MessageType, "): boolean;");
132
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): Message | undefined;");
133
+ f.print();
134
+ f.print(" is(type: ", MessageType, " | string): boolean;");
134
135
  f.print();
135
136
  f.print(" private typeNameToUrl(name: string): string;");
136
137
  f.print();
@@ -160,7 +161,7 @@ function generateWktMethods(schema, f, message) {
160
161
  }
161
162
  // prettier-ignore
162
163
  function generateWktStaticMethods(schema, f, message) {
163
- const ref = (0, match_wkt_js_1.matchWkt)(message);
164
+ const ref = (0, ecmascript_1.reifyWkt)(message);
164
165
  if (ref === undefined) {
165
166
  return;
166
167
  }
@@ -186,7 +187,7 @@ function generateWktStaticMethods(schema, f, message) {
186
187
  case "google.protobuf.BytesValue": {
187
188
  const { typing } = (0, ecmascript_1.getFieldTyping)(ref.value, f);
188
189
  f.print(" static readonly fieldWrapper: {");
189
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, ",");
190
+ f.print(" wrapField(value: ", typing, "): ", message, ",");
190
191
  f.print(" unwrapField(value: ", message, "): ", typing, ",");
191
192
  f.print(" };");
192
193
  f.print();
@@ -16,7 +16,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateFieldInfo = exports.generateJs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
- const match_wkt_js_1 = require("./match-wkt.js");
20
19
  function generateJs(schema) {
21
20
  for (const file of schema.files) {
22
21
  const f = schema.generateFile(file.name + "_pb.js");
@@ -148,7 +147,7 @@ exports.generateFieldInfo = generateFieldInfo;
148
147
  // prettier-ignore
149
148
  function generateWktMethods(schema, f, message) {
150
149
  var _a;
151
- const ref = (0, match_wkt_js_1.matchWkt)(message);
150
+ const ref = (0, ecmascript_1.reifyWkt)(message);
152
151
  if (ref === undefined) {
153
152
  return;
154
153
  }
@@ -178,6 +177,9 @@ function generateWktMethods(schema, f, message) {
178
177
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
179
178
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
180
179
  f.print(" }");
180
+ f.print(` if (Object.keys(json).length == 0) {`);
181
+ f.print(` return this;`);
182
+ f.print(` }`);
181
183
  f.print(` const typeUrl = json["@type"];`);
182
184
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
183
185
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -211,8 +213,29 @@ function generateWktMethods(schema, f, message) {
211
213
  f.print(" return true;");
212
214
  f.print("};");
213
215
  f.print();
216
+ f.print(message, ".prototype.unpack = function unpack(registry) {");
217
+ f.print(" if (this.", (0, ecmascript_1.localName)(ref.typeUrl), ` === "") {`);
218
+ f.print(" return undefined;");
219
+ f.print(" }");
220
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), "));");
221
+ f.print(" if (!messageType) {");
222
+ f.print(" return undefined;");
223
+ f.print(" }");
224
+ f.print(" return messageType.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
225
+ f.print(" }");
226
+ f.print();
214
227
  f.print(message, ".prototype.is = function is(type) {");
215
- f.print(" return this.", (0, ecmascript_1.localName)(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
228
+ f.print(" if (this.typeUrl === '') {");
229
+ f.print(" return false;");
230
+ f.print(" }");
231
+ f.print(" const name = this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), ");");
232
+ f.print(" let typeName = '';");
233
+ f.print(" if (typeof type === 'string') {");
234
+ f.print(" typeName = type;");
235
+ f.print(" } else {");
236
+ f.print(" typeName = type.typeName;");
237
+ f.print(" }");
238
+ f.print(" return name === typeName;");
216
239
  f.print("};");
217
240
  f.print();
218
241
  f.print(message, ".prototype.typeNameToUrl = function typeNameToUrl(name) {");
@@ -500,7 +523,7 @@ function generateWktMethods(schema, f, message) {
500
523
  }
501
524
  // prettier-ignore
502
525
  function generateWktStaticMethods(schema, f, message) {
503
- const ref = (0, match_wkt_js_1.matchWkt)(message);
526
+ const ref = (0, ecmascript_1.reifyWkt)(message);
504
527
  if (ref === undefined) {
505
528
  return;
506
529
  }
@@ -539,7 +562,7 @@ function generateWktStaticMethods(schema, f, message) {
539
562
  case "google.protobuf.BytesValue": {
540
563
  f.print(message, ".fieldWrapper = {");
541
564
  f.print(" wrapField(value) {");
542
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});");
565
+ f.print(" return new ", message, "({value});");
543
566
  f.print(" },");
544
567
  f.print(" unwrapField(value) {");
545
568
  f.print(" return value.", (0, ecmascript_1.localName)(ref.value), ";");
@@ -16,7 +16,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateTs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
- const match_wkt_js_1 = require("./match-wkt.js");
20
19
  const javascript_js_1 = require("./javascript.js");
21
20
  const ecmascript_2 = require("@bufbuild/protoplugin/ecmascript");
22
21
  function generateTs(schema) {
@@ -149,11 +148,11 @@ function generateField(schema, f, field) {
149
148
  // prettier-ignore
150
149
  function generateWktMethods(schema, f, message) {
151
150
  var _a;
152
- const ref = (0, match_wkt_js_1.matchWkt)(message);
151
+ const ref = (0, ecmascript_1.reifyWkt)(message);
153
152
  if (ref === undefined) {
154
153
  return;
155
154
  }
156
- const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, ScalarType: rtScalarType, protoInt64, } = schema.runtime;
155
+ const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, IMessageTypeRegistry, ScalarType: rtScalarType, protoInt64, } = schema.runtime;
157
156
  const protoN = schema.runtime[message.file.syntax];
158
157
  switch (ref.typeName) {
159
158
  case "google.protobuf.Any":
@@ -179,6 +178,9 @@ function generateWktMethods(schema, f, message) {
179
178
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
180
179
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
181
180
  f.print(" }");
181
+ f.print(` if (Object.keys(json).length == 0) {`);
182
+ f.print(` return this;`);
183
+ f.print(` }`);
182
184
  f.print(` const typeUrl = json["@type"];`);
183
185
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
184
186
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -212,8 +214,29 @@ function generateWktMethods(schema, f, message) {
212
214
  f.print(" return true;");
213
215
  f.print(" }");
214
216
  f.print();
215
- f.print(" is(type: ", MessageType, "): boolean {");
216
- f.print(" return this.", (0, ecmascript_1.localName)(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
217
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): ", Message, " | undefined {");
218
+ f.print(" if (this.", (0, ecmascript_1.localName)(ref.typeUrl), ` === "") {`);
219
+ f.print(" return undefined;");
220
+ f.print(" }");
221
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), "));");
222
+ f.print(" if (!messageType) {");
223
+ f.print(" return undefined;");
224
+ f.print(" }");
225
+ f.print(" return messageType.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
226
+ f.print(" }");
227
+ f.print();
228
+ f.print(" is(type: ", MessageType, " | string): boolean {");
229
+ f.print(" if (this.typeUrl === '') {");
230
+ f.print(" return false;");
231
+ f.print(" }");
232
+ f.print(" const name = this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), ");");
233
+ f.print(" let typeName = '';");
234
+ f.print(" if (typeof type === 'string') {");
235
+ f.print(" typeName = type;");
236
+ f.print(" } else {");
237
+ f.print(" typeName = type.typeName;");
238
+ f.print(" }");
239
+ f.print(" return name === typeName;");
217
240
  f.print(" }");
218
241
  f.print();
219
242
  f.print(" private typeNameToUrl(name: string): string {");
@@ -501,7 +524,7 @@ function generateWktMethods(schema, f, message) {
501
524
  }
502
525
  // prettier-ignore
503
526
  function generateWktStaticMethods(schema, f, message) {
504
- const ref = (0, match_wkt_js_1.matchWkt)(message);
527
+ const ref = (0, ecmascript_1.reifyWkt)(message);
505
528
  if (ref === undefined) {
506
529
  return;
507
530
  }
@@ -540,8 +563,8 @@ function generateWktStaticMethods(schema, f, message) {
540
563
  case "google.protobuf.BytesValue": {
541
564
  const { typing } = (0, ecmascript_1.getFieldTyping)(ref.value, f);
542
565
  f.print(" static readonly fieldWrapper = {");
543
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, " {");
544
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});");
566
+ f.print(" wrapField(value: ", typing, "): ", message, " {");
567
+ f.print(" return new ", message, "({value});");
545
568
  f.print(" },");
546
569
  f.print(" unwrapField(value: ", message, "): ", typing, " {");
547
570
  f.print(" return value.", (0, ecmascript_1.localName)(ref.value), ";");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.2.0"
23
+ "@bufbuild/protoplugin": "0.3.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.2.0"
26
+ "@bufbuild/protobuf": "0.3.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -28,8 +28,8 @@ import {
28
28
  literalString,
29
29
  localName,
30
30
  makeJsDoc,
31
+ reifyWkt,
31
32
  } from "@bufbuild/protoplugin/ecmascript";
32
- import { matchWkt } from "./match-wkt.js";
33
33
 
34
34
  export function generateDts(schema: Schema) {
35
35
  for (const file of schema.files) {
@@ -145,13 +145,14 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
145
145
 
146
146
  // prettier-ignore
147
147
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
148
- const ref = matchWkt(message);
148
+ const ref = reifyWkt(message);
149
149
  if (ref === undefined) {
150
150
  return;
151
151
  }
152
152
  const {
153
153
  Message,
154
154
  MessageType,
155
+ IMessageTypeRegistry
155
156
  } = schema.runtime;
156
157
  switch (ref.typeName) {
157
158
  case "google.protobuf.Any":
@@ -159,7 +160,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
159
160
  f.print();
160
161
  f.print(" unpackTo(target: ", Message, "): boolean;");
161
162
  f.print();
162
- f.print(" is(type: ", MessageType, "): boolean;");
163
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): Message | undefined;");
164
+ f.print();
165
+ f.print(" is(type: ", MessageType, " | string): boolean;");
163
166
  f.print();
164
167
  f.print(" private typeNameToUrl(name: string): string;");
165
168
  f.print();
@@ -190,7 +193,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
190
193
 
191
194
  // prettier-ignore
192
195
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
193
- const ref = matchWkt(message);
196
+ const ref = reifyWkt(message);
194
197
  if (ref === undefined) {
195
198
  return;
196
199
  }
@@ -216,7 +219,7 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
216
219
  case "google.protobuf.BytesValue": {
217
220
  const {typing} = getFieldTyping(ref.value, f);
218
221
  f.print(" static readonly fieldWrapper: {")
219
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, ",")
222
+ f.print(" wrapField(value: ", typing, "): ", message, ",")
220
223
  f.print(" unwrapField(value: ", message, "): ", typing, ",")
221
224
  f.print(" };")
222
225
  f.print()
package/src/javascript.ts CHANGED
@@ -24,8 +24,8 @@ import {
24
24
  literalString,
25
25
  localName,
26
26
  makeJsDoc,
27
+ reifyWkt,
27
28
  } from "@bufbuild/protoplugin/ecmascript";
28
- import { matchWkt } from "./match-wkt.js";
29
29
 
30
30
  export function generateJs(schema: Schema) {
31
31
  for (const file of schema.files) {
@@ -157,7 +157,7 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
157
157
 
158
158
  // prettier-ignore
159
159
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
160
- const ref = matchWkt(message);
160
+ const ref = reifyWkt(message);
161
161
  if (ref === undefined) {
162
162
  return;
163
163
  }
@@ -190,6 +190,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
190
190
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
191
191
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
192
192
  f.print(" }");
193
+ f.print(` if (Object.keys(json).length == 0) {`);
194
+ f.print(` return this;`);
195
+ f.print(` }`);
193
196
  f.print(` const typeUrl = json["@type"];`);
194
197
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
195
198
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -223,8 +226,29 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
223
226
  f.print(" return true;");
224
227
  f.print("};");
225
228
  f.print();
229
+ f.print(message, ".prototype.unpack = function unpack(registry) {")
230
+ f.print(" if (this.", localName(ref.typeUrl), ` === "") {`);
231
+ f.print(" return undefined;");
232
+ f.print(" }");
233
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", localName(ref.typeUrl), "));");
234
+ f.print(" if (!messageType) {");
235
+ f.print(" return undefined;");
236
+ f.print(" }");
237
+ f.print(" return messageType.fromBinary(this.", localName(ref.value), ");");
238
+ f.print(" }");
239
+ f.print();
226
240
  f.print(message, ".prototype.is = function is(type) {")
227
- f.print(" return this.", localName(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
241
+ f.print(" if (this.typeUrl === '') {");
242
+ f.print(" return false;");
243
+ f.print(" }");
244
+ f.print(" const name = this.typeUrlToName(this.", localName(ref.typeUrl), ");");
245
+ f.print(" let typeName = '';");
246
+ f.print(" if (typeof type === 'string') {");
247
+ f.print(" typeName = type;");
248
+ f.print(" } else {");
249
+ f.print(" typeName = type.typeName;");
250
+ f.print(" }");
251
+ f.print(" return name === typeName;");
228
252
  f.print("};");
229
253
  f.print();
230
254
  f.print(message, ".prototype.typeNameToUrl = function typeNameToUrl(name) {")
@@ -513,7 +537,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
513
537
 
514
538
  // prettier-ignore
515
539
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
516
- const ref = matchWkt(message);
540
+ const ref = reifyWkt(message);
517
541
  if (ref === undefined) {
518
542
  return;
519
543
  }
@@ -554,7 +578,7 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
554
578
  case "google.protobuf.BytesValue": {
555
579
  f.print(message, ".fieldWrapper = {")
556
580
  f.print(" wrapField(value) {")
557
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});")
581
+ f.print(" return new ", message, "({value});")
558
582
  f.print(" },")
559
583
  f.print(" unwrapField(value) {")
560
584
  f.print(" return value.", localName(ref.value), ";")
package/src/typescript.ts CHANGED
@@ -29,8 +29,8 @@ import {
29
29
  getFieldIntrinsicDefaultValue,
30
30
  getFieldTyping,
31
31
  makeJsDoc,
32
+ reifyWkt,
32
33
  } from "@bufbuild/protoplugin/ecmascript";
33
- import { matchWkt } from "./match-wkt.js";
34
34
  import { generateFieldInfo } from "./javascript.js";
35
35
  import { literalString } from "@bufbuild/protoplugin/ecmascript";
36
36
 
@@ -175,7 +175,7 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
175
175
 
176
176
  // prettier-ignore
177
177
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
178
- const ref = matchWkt(message);
178
+ const ref = reifyWkt(message);
179
179
  if (ref === undefined) {
180
180
  return;
181
181
  }
@@ -186,6 +186,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
186
186
  JsonWriteOptions,
187
187
  JsonObject,
188
188
  MessageType,
189
+ IMessageTypeRegistry,
189
190
  ScalarType: rtScalarType,
190
191
  protoInt64,
191
192
  } = schema.runtime;
@@ -214,6 +215,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
214
215
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
215
216
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
216
217
  f.print(" }");
218
+ f.print(` if (Object.keys(json).length == 0) {`);
219
+ f.print(` return this;`);
220
+ f.print(` }`);
217
221
  f.print(` const typeUrl = json["@type"];`);
218
222
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
219
223
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -247,8 +251,29 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
247
251
  f.print(" return true;");
248
252
  f.print(" }");
249
253
  f.print();
250
- f.print(" is(type: ", MessageType, "): boolean {");
251
- f.print(" return this.", localName(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
254
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): ", Message, " | undefined {");
255
+ f.print(" if (this.", localName(ref.typeUrl), ` === "") {`);
256
+ f.print(" return undefined;");
257
+ f.print(" }");
258
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", localName(ref.typeUrl), "));");
259
+ f.print(" if (!messageType) {");
260
+ f.print(" return undefined;");
261
+ f.print(" }");
262
+ f.print(" return messageType.fromBinary(this.", localName(ref.value), ");");
263
+ f.print(" }");
264
+ f.print();
265
+ f.print(" is(type: ", MessageType, " | string): boolean {");
266
+ f.print(" if (this.typeUrl === '') {");
267
+ f.print(" return false;");
268
+ f.print(" }");
269
+ f.print(" const name = this.typeUrlToName(this.", localName(ref.typeUrl), ");");
270
+ f.print(" let typeName = '';");
271
+ f.print(" if (typeof type === 'string') {");
272
+ f.print(" typeName = type;");
273
+ f.print(" } else {");
274
+ f.print(" typeName = type.typeName;");
275
+ f.print(" }");
276
+ f.print(" return name === typeName;");
252
277
  f.print(" }");
253
278
  f.print();
254
279
  f.print(" private typeNameToUrl(name: string): string {");
@@ -537,7 +562,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
537
562
 
538
563
  // prettier-ignore
539
564
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
540
- const ref = matchWkt(message);
565
+ const ref = reifyWkt(message);
541
566
  if (ref === undefined) {
542
567
  return;
543
568
  }
@@ -578,8 +603,8 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
578
603
  case "google.protobuf.BytesValue": {
579
604
  const {typing} = getFieldTyping(ref.value, f);
580
605
  f.print(" static readonly fieldWrapper = {")
581
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, " {")
582
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});")
606
+ f.print(" wrapField(value: ", typing, "): ", message, " {")
607
+ f.print(" return new ", message, "({value});")
583
608
  f.print(" },")
584
609
  f.print(" unwrapField(value: ", message, "): ", typing, " {")
585
610
  f.print(" return value.", localName(ref.value), ";")
@@ -1,159 +0,0 @@
1
- "use strict";
2
- // Copyright 2021-2022 Buf Technologies, Inc.
3
- //
4
- // Licensed under the Apache License, Version 2.0 (the "License");
5
- // you may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- //
8
- // http://www.apache.org/licenses/LICENSE-2.0
9
- //
10
- // Unless required by applicable law or agreed to in writing, software
11
- // distributed under the License is distributed on an "AS IS" BASIS,
12
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- // See the License for the specific language governing permissions and
14
- // limitations under the License.
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.matchWkt = void 0;
17
- const protobuf_1 = require("@bufbuild/protobuf");
18
- function matchWkt(message) {
19
- switch (message.typeName) {
20
- case "google.protobuf.Any": {
21
- const typeUrl = message.fields.find((f) => f.number == 1 &&
22
- f.fieldKind == "scalar" &&
23
- f.scalar === protobuf_1.ScalarType.STRING);
24
- const value = message.fields.find((f) => f.number == 2 &&
25
- f.fieldKind == "scalar" &&
26
- f.scalar === protobuf_1.ScalarType.BYTES);
27
- if (typeUrl && value) {
28
- return {
29
- typeName: message.typeName,
30
- typeUrl,
31
- value,
32
- };
33
- }
34
- break;
35
- }
36
- case "google.protobuf.Timestamp": {
37
- const seconds = message.fields.find((f) => f.number == 1 &&
38
- f.fieldKind == "scalar" &&
39
- f.scalar === protobuf_1.ScalarType.INT64);
40
- const nanos = message.fields.find((f) => f.number == 2 &&
41
- f.fieldKind == "scalar" &&
42
- f.scalar === protobuf_1.ScalarType.INT32);
43
- if (seconds && nanos) {
44
- return {
45
- typeName: message.typeName,
46
- seconds,
47
- nanos,
48
- };
49
- }
50
- break;
51
- }
52
- case "google.protobuf.Duration": {
53
- const seconds = message.fields.find((f) => f.number == 1 &&
54
- f.fieldKind == "scalar" &&
55
- f.scalar === protobuf_1.ScalarType.INT64);
56
- const nanos = message.fields.find((f) => f.number == 2 &&
57
- f.fieldKind == "scalar" &&
58
- f.scalar === protobuf_1.ScalarType.INT32);
59
- if (seconds && nanos) {
60
- return {
61
- typeName: message.typeName,
62
- seconds,
63
- nanos,
64
- };
65
- }
66
- break;
67
- }
68
- case "google.protobuf.Struct": {
69
- const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
70
- if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
71
- fields.mapValue.kind !== "message" ||
72
- fields.mapValue.message.typeName !== "google.protobuf.Value") {
73
- break;
74
- }
75
- return { typeName: message.typeName, fields };
76
- }
77
- case "google.protobuf.Value": {
78
- const kind = message.oneofs.find((o) => o.name === "kind");
79
- const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
80
- if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
81
- nullValue.enum.typeName !== "google.protobuf.NullValue") {
82
- return undefined;
83
- }
84
- const numberValue = message.fields.find((f) => f.number == 2 &&
85
- f.fieldKind == "scalar" &&
86
- f.scalar === protobuf_1.ScalarType.DOUBLE &&
87
- f.oneof === kind);
88
- const stringValue = message.fields.find((f) => f.number == 3 &&
89
- f.fieldKind == "scalar" &&
90
- f.scalar === protobuf_1.ScalarType.STRING &&
91
- f.oneof === kind);
92
- const boolValue = message.fields.find((f) => f.number == 4 &&
93
- f.fieldKind == "scalar" &&
94
- f.scalar === protobuf_1.ScalarType.BOOL &&
95
- f.oneof === kind);
96
- const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
97
- if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
98
- structValue.message.typeName !== "google.protobuf.Struct") {
99
- return undefined;
100
- }
101
- const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
102
- if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
103
- listValue.message.typeName !== "google.protobuf.ListValue") {
104
- return undefined;
105
- }
106
- if (kind && numberValue && stringValue && boolValue) {
107
- return {
108
- typeName: message.typeName,
109
- kind,
110
- nullValue,
111
- numberValue,
112
- stringValue,
113
- boolValue,
114
- structValue,
115
- listValue,
116
- };
117
- }
118
- break;
119
- }
120
- case "google.protobuf.ListValue": {
121
- const values = message.fields.find((f) => f.number == 1 && f.repeated);
122
- if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
123
- values.message.typeName !== "google.protobuf.Value") {
124
- break;
125
- }
126
- return { typeName: message.typeName, values };
127
- }
128
- case "google.protobuf.FieldMask": {
129
- const paths = message.fields.find((f) => f.number == 1 &&
130
- f.fieldKind == "scalar" &&
131
- f.scalar === protobuf_1.ScalarType.STRING &&
132
- f.repeated);
133
- if (paths) {
134
- return { typeName: message.typeName, paths };
135
- }
136
- break;
137
- }
138
- case "google.protobuf.DoubleValue":
139
- case "google.protobuf.FloatValue":
140
- case "google.protobuf.Int64Value":
141
- case "google.protobuf.UInt64Value":
142
- case "google.protobuf.Int32Value":
143
- case "google.protobuf.UInt32Value":
144
- case "google.protobuf.BoolValue":
145
- case "google.protobuf.StringValue":
146
- case "google.protobuf.BytesValue": {
147
- const value = message.fields.find((f) => f.number == 1 && f.name == "value");
148
- if (!value) {
149
- break;
150
- }
151
- if (value.fieldKind !== "scalar") {
152
- break;
153
- }
154
- return { typeName: message.typeName, value };
155
- }
156
- }
157
- return undefined;
158
- }
159
- exports.matchWkt = matchWkt;
package/src/match-wkt.ts DELETED
@@ -1,285 +0,0 @@
1
- // Copyright 2021-2022 Buf Technologies, Inc.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- DescField,
17
- DescMessage,
18
- DescOneof,
19
- ScalarType,
20
- } from "@bufbuild/protobuf";
21
-
22
- type DescWkt =
23
- | {
24
- typeName: "google.protobuf.Any";
25
- typeUrl: DescField;
26
- value: DescField;
27
- }
28
- | {
29
- typeName: "google.protobuf.Timestamp";
30
- seconds: DescField;
31
- nanos: DescField;
32
- }
33
- | {
34
- typeName: "google.protobuf.Duration";
35
- seconds: DescField;
36
- nanos: DescField;
37
- }
38
- | {
39
- typeName: "google.protobuf.Struct";
40
- fields: DescField & { fieldKind: "map" };
41
- }
42
- | {
43
- typeName: "google.protobuf.Value";
44
- kind: DescOneof;
45
- nullValue: DescField & { fieldKind: "enum" };
46
- numberValue: DescField;
47
- stringValue: DescField;
48
- boolValue: DescField;
49
- structValue: DescField & { fieldKind: "message" };
50
- listValue: DescField & { fieldKind: "message" };
51
- }
52
- | {
53
- typeName: "google.protobuf.ListValue";
54
- values: DescField & { fieldKind: "message" };
55
- }
56
- | {
57
- typeName: "google.protobuf.FieldMask";
58
- paths: DescField;
59
- }
60
- | {
61
- typeName: "google.protobuf.DoubleValue";
62
- value: DescField & { fieldKind: "scalar" };
63
- }
64
- | {
65
- typeName: "google.protobuf.FloatValue";
66
- value: DescField & { fieldKind: "scalar" };
67
- }
68
- | {
69
- typeName: "google.protobuf.Int64Value";
70
- value: DescField & { fieldKind: "scalar" };
71
- }
72
- | {
73
- typeName: "google.protobuf.UInt64Value";
74
- value: DescField & { fieldKind: "scalar" };
75
- }
76
- | {
77
- typeName: "google.protobuf.Int32Value";
78
- value: DescField & { fieldKind: "scalar" };
79
- }
80
- | {
81
- typeName: "google.protobuf.UInt32Value";
82
- value: DescField & { fieldKind: "scalar" };
83
- }
84
- | {
85
- typeName: "google.protobuf.BoolValue";
86
- value: DescField & { fieldKind: "scalar" };
87
- }
88
- | {
89
- typeName: "google.protobuf.StringValue";
90
- value: DescField & { fieldKind: "scalar" };
91
- }
92
- | {
93
- typeName: "google.protobuf.BytesValue";
94
- value: DescField & { fieldKind: "scalar" };
95
- };
96
-
97
- export function matchWkt(message: DescMessage): DescWkt | undefined {
98
- switch (message.typeName) {
99
- case "google.protobuf.Any": {
100
- const typeUrl = message.fields.find(
101
- (f) =>
102
- f.number == 1 &&
103
- f.fieldKind == "scalar" &&
104
- f.scalar === ScalarType.STRING
105
- );
106
- const value = message.fields.find(
107
- (f) =>
108
- f.number == 2 &&
109
- f.fieldKind == "scalar" &&
110
- f.scalar === ScalarType.BYTES
111
- );
112
- if (typeUrl && value) {
113
- return {
114
- typeName: message.typeName,
115
- typeUrl,
116
- value,
117
- };
118
- }
119
- break;
120
- }
121
- case "google.protobuf.Timestamp": {
122
- const seconds = message.fields.find(
123
- (f) =>
124
- f.number == 1 &&
125
- f.fieldKind == "scalar" &&
126
- f.scalar === ScalarType.INT64
127
- );
128
- const nanos = message.fields.find(
129
- (f) =>
130
- f.number == 2 &&
131
- f.fieldKind == "scalar" &&
132
- f.scalar === ScalarType.INT32
133
- );
134
- if (seconds && nanos) {
135
- return {
136
- typeName: message.typeName,
137
- seconds,
138
- nanos,
139
- };
140
- }
141
- break;
142
- }
143
- case "google.protobuf.Duration": {
144
- const seconds = message.fields.find(
145
- (f) =>
146
- f.number == 1 &&
147
- f.fieldKind == "scalar" &&
148
- f.scalar === ScalarType.INT64
149
- );
150
- const nanos = message.fields.find(
151
- (f) =>
152
- f.number == 2 &&
153
- f.fieldKind == "scalar" &&
154
- f.scalar === ScalarType.INT32
155
- );
156
- if (seconds && nanos) {
157
- return {
158
- typeName: message.typeName,
159
- seconds,
160
- nanos,
161
- };
162
- }
163
- break;
164
- }
165
- case "google.protobuf.Struct": {
166
- const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
167
- if (
168
- fields?.fieldKind !== "map" ||
169
- fields.mapValue.kind !== "message" ||
170
- fields.mapValue.message.typeName !== "google.protobuf.Value"
171
- ) {
172
- break;
173
- }
174
- return { typeName: message.typeName, fields };
175
- }
176
- case "google.protobuf.Value": {
177
- const kind = message.oneofs.find((o) => o.name === "kind");
178
- const nullValue = message.fields.find(
179
- (f) => f.number == 1 && f.oneof === kind
180
- );
181
- if (
182
- nullValue?.fieldKind !== "enum" ||
183
- nullValue.enum.typeName !== "google.protobuf.NullValue"
184
- ) {
185
- return undefined;
186
- }
187
- const numberValue = message.fields.find(
188
- (f) =>
189
- f.number == 2 &&
190
- f.fieldKind == "scalar" &&
191
- f.scalar === ScalarType.DOUBLE &&
192
- f.oneof === kind
193
- );
194
- const stringValue = message.fields.find(
195
- (f) =>
196
- f.number == 3 &&
197
- f.fieldKind == "scalar" &&
198
- f.scalar === ScalarType.STRING &&
199
- f.oneof === kind
200
- );
201
- const boolValue = message.fields.find(
202
- (f) =>
203
- f.number == 4 &&
204
- f.fieldKind == "scalar" &&
205
- f.scalar === ScalarType.BOOL &&
206
- f.oneof === kind
207
- );
208
- const structValue = message.fields.find(
209
- (f) => f.number == 5 && f.oneof === kind
210
- );
211
- if (
212
- structValue?.fieldKind !== "message" ||
213
- structValue.message.typeName !== "google.protobuf.Struct"
214
- ) {
215
- return undefined;
216
- }
217
- const listValue = message.fields.find(
218
- (f) => f.number == 6 && f.oneof === kind
219
- );
220
- if (
221
- listValue?.fieldKind !== "message" ||
222
- listValue.message.typeName !== "google.protobuf.ListValue"
223
- ) {
224
- return undefined;
225
- }
226
- if (kind && numberValue && stringValue && boolValue) {
227
- return {
228
- typeName: message.typeName,
229
- kind,
230
- nullValue,
231
- numberValue,
232
- stringValue,
233
- boolValue,
234
- structValue,
235
- listValue,
236
- };
237
- }
238
- break;
239
- }
240
- case "google.protobuf.ListValue": {
241
- const values = message.fields.find((f) => f.number == 1 && f.repeated);
242
- if (
243
- values?.fieldKind != "message" ||
244
- values.message.typeName !== "google.protobuf.Value"
245
- ) {
246
- break;
247
- }
248
- return { typeName: message.typeName, values };
249
- }
250
- case "google.protobuf.FieldMask": {
251
- const paths = message.fields.find(
252
- (f) =>
253
- f.number == 1 &&
254
- f.fieldKind == "scalar" &&
255
- f.scalar === ScalarType.STRING &&
256
- f.repeated
257
- );
258
- if (paths) {
259
- return { typeName: message.typeName, paths };
260
- }
261
- break;
262
- }
263
- case "google.protobuf.DoubleValue":
264
- case "google.protobuf.FloatValue":
265
- case "google.protobuf.Int64Value":
266
- case "google.protobuf.UInt64Value":
267
- case "google.protobuf.Int32Value":
268
- case "google.protobuf.UInt32Value":
269
- case "google.protobuf.BoolValue":
270
- case "google.protobuf.StringValue":
271
- case "google.protobuf.BytesValue": {
272
- const value = message.fields.find(
273
- (f) => f.number == 1 && f.name == "value"
274
- );
275
- if (!value) {
276
- break;
277
- }
278
- if (value.fieldKind !== "scalar") {
279
- break;
280
- }
281
- return { typeName: message.typeName, value };
282
- }
283
- }
284
- return undefined;
285
- }