@bufbuild/protoc-gen-es 0.1.1 → 0.2.1

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.md CHANGED
@@ -124,3 +124,11 @@ By default, we generate JavaScript and TypeScript declaration files, which
124
124
  produces the smallest code size. If you prefer to generate TypeScript, use
125
125
  `target=ts`.
126
126
 
127
+ ### `keep_empty_files=true`
128
+
129
+ By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
130
+ (and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin))
131
+ omit empty files from the plugin output. This option disables pruning of
132
+ empty files, to allow for smooth interoperation with Bazel and similar
133
+ tooling that requires all output files to be declared ahead of time.
134
+ Unless you use Bazel, it is very unlikely that you need this option.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
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.1.1"
23
+ "@bufbuild/protoplugin": "0.2.1"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.1.1"
26
+ "@bufbuild/protobuf": "0.2.1"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -13,15 +13,22 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.declaration = void 0;
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
- exports.declaration = {
20
- target: "dts",
21
- extension: "_pb.d.ts",
22
- generateEnum,
23
- generateMessage,
24
- };
18
+ function generateDts(schema) {
19
+ for (const file of schema.files) {
20
+ const f = schema.generateFile(file.name + "_pb.d.ts");
21
+ f.preamble(file);
22
+ for (const enumeration of file.enums) {
23
+ generateEnum(schema, f, enumeration);
24
+ }
25
+ for (const message of file.messages) {
26
+ generateMessage(schema, f, message);
27
+ }
28
+ // We do not generate anything for services, and we do not support extensions at this time
29
+ }
30
+ }
31
+ exports.generateDts = generateDts;
25
32
  // prettier-ignore
26
33
  function generateEnum(schema, f, enumeration) {
27
34
  f.print((0, ecmascript_1.makeJsDoc)(enumeration));
@@ -111,7 +118,7 @@ function generateField(schema, f, field) {
111
118
  }
112
119
  // prettier-ignore
113
120
  function generateWktMethods(schema, f, message) {
114
- const ref = (0, match_wkt_js_1.matchWkt)(message);
121
+ const ref = (0, ecmascript_1.reifyWkt)(message);
115
122
  if (ref === undefined) {
116
123
  return;
117
124
  }
@@ -152,7 +159,7 @@ function generateWktMethods(schema, f, message) {
152
159
  }
153
160
  // prettier-ignore
154
161
  function generateWktStaticMethods(schema, f, message) {
155
- const ref = (0, match_wkt_js_1.matchWkt)(message);
162
+ const ref = (0, ecmascript_1.reifyWkt)(message);
156
163
  if (ref === undefined) {
157
164
  return;
158
165
  }
@@ -13,16 +13,23 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.generateFieldInfo = exports.javascript = void 0;
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
- exports.javascript = {
21
- target: "js",
22
- extension: "_pb.js",
23
- generateEnum,
24
- generateMessage,
25
- };
19
+ function generateJs(schema) {
20
+ for (const file of schema.files) {
21
+ const f = schema.generateFile(file.name + "_pb.js");
22
+ f.preamble(file);
23
+ for (const enumeration of file.enums) {
24
+ generateEnum(schema, f, enumeration);
25
+ }
26
+ for (const message of file.messages) {
27
+ generateMessage(schema, f, message);
28
+ }
29
+ // We do not generate anything for services, and we do not support extensions at this time
30
+ }
31
+ }
32
+ exports.generateJs = generateJs;
26
33
  // prettier-ignore
27
34
  function generateEnum(schema, f, enumeration) {
28
35
  const protoN = schema.runtime[enumeration.file.syntax];
@@ -88,11 +95,11 @@ function generateFieldInfo(schema, f, field) {
88
95
  if (field.jsonName !== undefined) {
89
96
  e.push(`jsonName: "`, field.jsonName, `", `);
90
97
  }
91
- switch (field.kind) {
92
- case "scalar_field":
98
+ switch (field.fieldKind) {
99
+ case "scalar":
93
100
  e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
94
101
  break;
95
- case "map_field":
102
+ case "map":
96
103
  e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
97
104
  switch (field.mapValue.kind) {
98
105
  case "scalar":
@@ -106,10 +113,10 @@ function generateFieldInfo(schema, f, field) {
106
113
  break;
107
114
  }
108
115
  break;
109
- case "message_field":
116
+ case "message":
110
117
  e.push(`kind: "message", T: `, field.message, `, `);
111
118
  break;
112
- case "enum_field":
119
+ case "enum":
113
120
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
114
121
  break;
115
122
  }
@@ -140,7 +147,7 @@ exports.generateFieldInfo = generateFieldInfo;
140
147
  // prettier-ignore
141
148
  function generateWktMethods(schema, f, message) {
142
149
  var _a;
143
- const ref = (0, match_wkt_js_1.matchWkt)(message);
150
+ const ref = (0, ecmascript_1.reifyWkt)(message);
144
151
  if (ref === undefined) {
145
152
  return;
146
153
  }
@@ -170,6 +177,9 @@ function generateWktMethods(schema, f, message) {
170
177
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
171
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}`);');
172
179
  f.print(" }");
180
+ f.print(` if (Object.keys(json).length == 0) {`);
181
+ f.print(` return this;`);
182
+ f.print(` }`);
173
183
  f.print(` const typeUrl = json["@type"];`);
174
184
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
175
185
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -492,7 +502,7 @@ function generateWktMethods(schema, f, message) {
492
502
  }
493
503
  // prettier-ignore
494
504
  function generateWktStaticMethods(schema, f, message) {
495
- const ref = (0, match_wkt_js_1.matchWkt)(message);
505
+ const ref = (0, ecmascript_1.reifyWkt)(message);
496
506
  if (ref === undefined) {
497
507
  return;
498
508
  }
@@ -17,24 +17,12 @@ exports.protocGenEs = void 0;
17
17
  const protoplugin_1 = require("@bufbuild/protoplugin");
18
18
  const typescript_js_1 = require("./typescript.js");
19
19
  const javascript_js_1 = require("./javascript.js");
20
- const declaration_1 = require("./declaration");
20
+ const declaration_js_1 = require("./declaration.js");
21
21
  const package_json_1 = require("../package.json");
22
22
  exports.protocGenEs = (0, protoplugin_1.createEcmaScriptPlugin)({
23
23
  name: "protoc-gen-es",
24
24
  version: `v${String(package_json_1.version)}`,
25
- }, (schema) => {
26
- const targets = [typescript_js_1.typescript, javascript_js_1.javascript, declaration_1.declaration].filter((gen) => schema.targets.includes(gen.target));
27
- for (const target of targets) {
28
- for (const file of schema.files) {
29
- const f = schema.generateFile(file.name + target.extension);
30
- f.preamble(file);
31
- for (const enumeration of file.enums) {
32
- target.generateEnum(schema, f, enumeration);
33
- }
34
- for (const message of file.messages) {
35
- target.generateMessage(schema, f, message);
36
- }
37
- // We do not generate anything for services, and we do not support extensions at this time
38
- }
39
- }
25
+ generateTs: typescript_js_1.generateTs,
26
+ generateJs: javascript_js_1.generateJs,
27
+ generateDts: declaration_js_1.generateDts,
40
28
  });
@@ -13,18 +13,25 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.typescript = void 0;
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
- exports.typescript = {
23
- target: "ts",
24
- extension: "_pb.ts",
25
- generateEnum,
26
- generateMessage,
27
- };
21
+ function generateTs(schema) {
22
+ for (const file of schema.files) {
23
+ const f = schema.generateFile(file.name + "_pb.ts");
24
+ f.preamble(file);
25
+ for (const enumeration of file.enums) {
26
+ generateEnum(schema, f, enumeration);
27
+ }
28
+ for (const message of file.messages) {
29
+ generateMessage(schema, f, message);
30
+ }
31
+ // We do not generate anything for services, and we do not support extensions at this time
32
+ }
33
+ }
34
+ exports.generateTs = generateTs;
28
35
  // prettier-ignore
29
36
  function generateEnum(schema, f, enumeration) {
30
37
  const protoN = schema.runtime[enumeration.file.syntax];
@@ -141,7 +148,7 @@ function generateField(schema, f, field) {
141
148
  // prettier-ignore
142
149
  function generateWktMethods(schema, f, message) {
143
150
  var _a;
144
- const ref = (0, match_wkt_js_1.matchWkt)(message);
151
+ const ref = (0, ecmascript_1.reifyWkt)(message);
145
152
  if (ref === undefined) {
146
153
  return;
147
154
  }
@@ -171,6 +178,9 @@ function generateWktMethods(schema, f, message) {
171
178
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
172
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}`);');
173
180
  f.print(" }");
181
+ f.print(` if (Object.keys(json).length == 0) {`);
182
+ f.print(` return this;`);
183
+ f.print(` }`);
174
184
  f.print(` const typeUrl = json["@type"];`);
175
185
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
176
186
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -493,7 +503,7 @@ function generateWktMethods(schema, f, message) {
493
503
  }
494
504
  // prettier-ignore
495
505
  function generateWktStaticMethods(schema, f, message) {
496
- const ref = (0, match_wkt_js_1.matchWkt)(message);
506
+ const ref = (0, ecmascript_1.reifyWkt)(message);
497
507
  if (ref === undefined) {
498
508
  return;
499
509
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
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.1.1"
23
+ "@bufbuild/protoplugin": "0.2.1"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.1.1"
26
+ "@bufbuild/protobuf": "0.2.1"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -18,21 +18,32 @@ import type {
18
18
  DescMessage,
19
19
  DescOneof,
20
20
  } from "@bufbuild/protobuf";
21
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
21
+ import type {
22
+ GeneratedFile,
23
+ Printable,
24
+ Schema,
25
+ } from "@bufbuild/protoplugin/ecmascript";
22
26
  import {
23
27
  getFieldTyping,
24
28
  literalString,
25
29
  localName,
26
30
  makeJsDoc,
31
+ reifyWkt,
27
32
  } from "@bufbuild/protoplugin/ecmascript";
28
- import { matchWkt } from "./match-wkt.js";
29
33
 
30
- export const declaration = {
31
- target: "dts",
32
- extension: "_pb.d.ts",
33
- generateEnum,
34
- generateMessage,
35
- } as const;
34
+ export function generateDts(schema: Schema) {
35
+ for (const file of schema.files) {
36
+ const f = schema.generateFile(file.name + "_pb.d.ts");
37
+ f.preamble(file);
38
+ for (const enumeration of file.enums) {
39
+ generateEnum(schema, f, enumeration);
40
+ }
41
+ for (const message of file.messages) {
42
+ generateMessage(schema, f, message);
43
+ }
44
+ // We do not generate anything for services, and we do not support extensions at this time
45
+ }
46
+ }
36
47
 
37
48
  // prettier-ignore
38
49
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -120,7 +131,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
120
131
 
121
132
  function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
122
133
  f.print(makeJsDoc(field, " "));
123
- const e: Parameters<typeof f.print> = [];
134
+ const e: Printable = [];
124
135
  e.push(" ", localName(field));
125
136
  const { typing, optional } = getFieldTyping(field, f);
126
137
  if (optional) {
@@ -134,7 +145,7 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
134
145
 
135
146
  // prettier-ignore
136
147
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
137
- const ref = matchWkt(message);
148
+ const ref = reifyWkt(message);
138
149
  if (ref === undefined) {
139
150
  return;
140
151
  }
@@ -179,7 +190,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
179
190
 
180
191
  // prettier-ignore
181
192
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
182
- const ref = matchWkt(message);
193
+ const ref = reifyWkt(message);
183
194
  if (ref === undefined) {
184
195
  return;
185
196
  }
package/src/javascript.ts CHANGED
@@ -14,21 +14,32 @@
14
14
 
15
15
  import type { DescEnum, DescField, DescMessage } from "@bufbuild/protobuf";
16
16
  import { proto2, proto3, ScalarType } from "@bufbuild/protobuf";
17
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
17
+ import type {
18
+ GeneratedFile,
19
+ Printable,
20
+ Schema,
21
+ } from "@bufbuild/protoplugin/ecmascript";
18
22
  import {
19
23
  getFieldExplicitDefaultValue,
20
24
  literalString,
21
25
  localName,
22
26
  makeJsDoc,
27
+ reifyWkt,
23
28
  } from "@bufbuild/protoplugin/ecmascript";
24
- import { matchWkt } from "./match-wkt.js";
25
29
 
26
- export const javascript = {
27
- target: "js",
28
- extension: "_pb.js",
29
- generateEnum,
30
- generateMessage,
31
- } as const;
30
+ export function generateJs(schema: Schema) {
31
+ for (const file of schema.files) {
32
+ const f = schema.generateFile(file.name + "_pb.js");
33
+ f.preamble(file);
34
+ for (const enumeration of file.enums) {
35
+ generateEnum(schema, f, enumeration);
36
+ }
37
+ for (const message of file.messages) {
38
+ generateMessage(schema, f, message);
39
+ }
40
+ // We do not generate anything for services, and we do not support extensions at this time
41
+ }
42
+ }
32
43
 
33
44
  // prettier-ignore
34
45
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -90,16 +101,16 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
90
101
  // prettier-ignore
91
102
  export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescField) {
92
103
  const protoN = schema.runtime[field.parent.file.syntax];
93
- const e: Parameters<typeof f.print> = [];
104
+ const e: Printable = [];
94
105
  e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
95
106
  if (field.jsonName !== undefined) {
96
107
  e.push(`jsonName: "`, field.jsonName, `", `);
97
108
  }
98
- switch (field.kind) {
99
- case "scalar_field":
109
+ switch (field.fieldKind) {
110
+ case "scalar":
100
111
  e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, ScalarType[field.scalar], ` */, `);
101
112
  break;
102
- case "map_field":
113
+ case "map":
103
114
  e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, ScalarType[field.mapKey], ` */, `);
104
115
  switch (field.mapValue.kind) {
105
116
  case "scalar":
@@ -113,10 +124,10 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
113
124
  break;
114
125
  }
115
126
  break;
116
- case "message_field":
127
+ case "message":
117
128
  e.push(`kind: "message", T: `, field.message, `, `);
118
129
  break;
119
- case "enum_field":
130
+ case "enum":
120
131
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
121
132
  break;
122
133
  }
@@ -146,7 +157,7 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
146
157
 
147
158
  // prettier-ignore
148
159
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
149
- const ref = matchWkt(message);
160
+ const ref = reifyWkt(message);
150
161
  if (ref === undefined) {
151
162
  return;
152
163
  }
@@ -179,6 +190,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
179
190
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
180
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}`);');
181
192
  f.print(" }");
193
+ f.print(` if (Object.keys(json).length == 0) {`);
194
+ f.print(` return this;`);
195
+ f.print(` }`);
182
196
  f.print(` const typeUrl = json["@type"];`);
183
197
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
184
198
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -502,7 +516,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
502
516
 
503
517
  // prettier-ignore
504
518
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
505
- const ref = matchWkt(message);
519
+ const ref = reifyWkt(message);
506
520
  if (ref === undefined) {
507
521
  return;
508
522
  }
@@ -13,32 +13,15 @@
13
13
  // limitations under the License.
14
14
 
15
15
  import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
16
- import { typescript } from "./typescript.js";
17
- import { javascript } from "./javascript.js";
18
- import { declaration } from "./declaration";
16
+ import { generateTs } from "./typescript.js";
17
+ import { generateJs } from "./javascript.js";
18
+ import { generateDts } from "./declaration.js";
19
19
  import { version } from "../package.json";
20
20
 
21
- export const protocGenEs = createEcmaScriptPlugin(
22
- {
23
- name: "protoc-gen-es",
24
- version: `v${String(version)}`,
25
- },
26
- (schema) => {
27
- const targets = [typescript, javascript, declaration].filter((gen) =>
28
- schema.targets.includes(gen.target)
29
- );
30
- for (const target of targets) {
31
- for (const file of schema.files) {
32
- const f = schema.generateFile(file.name + target.extension);
33
- f.preamble(file);
34
- for (const enumeration of file.enums) {
35
- target.generateEnum(schema, f, enumeration);
36
- }
37
- for (const message of file.messages) {
38
- target.generateMessage(schema, f, message);
39
- }
40
- // We do not generate anything for services, and we do not support extensions at this time
41
- }
42
- }
43
- }
44
- );
21
+ export const protocGenEs = createEcmaScriptPlugin({
22
+ name: "protoc-gen-es",
23
+ version: `v${String(version)}`,
24
+ generateTs,
25
+ generateJs,
26
+ generateDts,
27
+ });
package/src/typescript.ts CHANGED
@@ -19,23 +19,34 @@ import type {
19
19
  DescOneof,
20
20
  } from "@bufbuild/protobuf";
21
21
  import { ScalarType } from "@bufbuild/protobuf";
22
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
22
+ import type {
23
+ GeneratedFile,
24
+ Printable,
25
+ Schema,
26
+ } from "@bufbuild/protoplugin/ecmascript";
23
27
  import {
24
28
  localName,
25
29
  getFieldIntrinsicDefaultValue,
26
30
  getFieldTyping,
27
31
  makeJsDoc,
32
+ reifyWkt,
28
33
  } from "@bufbuild/protoplugin/ecmascript";
29
- import { matchWkt } from "./match-wkt.js";
30
34
  import { generateFieldInfo } from "./javascript.js";
31
35
  import { literalString } from "@bufbuild/protoplugin/ecmascript";
32
36
 
33
- export const typescript = {
34
- target: "ts",
35
- extension: "_pb.ts",
36
- generateEnum,
37
- generateMessage,
38
- } as const;
37
+ export function generateTs(schema: Schema) {
38
+ for (const file of schema.files) {
39
+ const f = schema.generateFile(file.name + "_pb.ts");
40
+ f.preamble(file);
41
+ for (const enumeration of file.enums) {
42
+ generateEnum(schema, f, enumeration);
43
+ }
44
+ for (const message of file.messages) {
45
+ generateMessage(schema, f, message);
46
+ }
47
+ // We do not generate anything for services, and we do not support extensions at this time
48
+ }
49
+ }
39
50
 
40
51
  // prettier-ignore
41
52
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -145,7 +156,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
145
156
 
146
157
  function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
147
158
  f.print(makeJsDoc(field, " "));
148
- const e: Parameters<typeof f.print> = [];
159
+ const e: Printable = [];
149
160
  e.push(" ", localName(field));
150
161
  const { defaultValue, typingInferrable } =
151
162
  getFieldIntrinsicDefaultValue(field);
@@ -164,7 +175,7 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
164
175
 
165
176
  // prettier-ignore
166
177
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
167
- const ref = matchWkt(message);
178
+ const ref = reifyWkt(message);
168
179
  if (ref === undefined) {
169
180
  return;
170
181
  }
@@ -203,6 +214,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
203
214
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
204
215
  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}`);');
205
216
  f.print(" }");
217
+ f.print(` if (Object.keys(json).length == 0) {`);
218
+ f.print(` return this;`);
219
+ f.print(` }`);
206
220
  f.print(` const typeUrl = json["@type"];`);
207
221
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
208
222
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -526,7 +540,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
526
540
 
527
541
  // prettier-ignore
528
542
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
529
- const ref = matchWkt(message);
543
+ const ref = reifyWkt(message);
530
544
  if (ref === undefined) {
531
545
  return;
532
546
  }
@@ -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.kind == "scalar_field" &&
23
- f.scalar === protobuf_1.ScalarType.STRING);
24
- const value = message.fields.find((f) => f.number == 2 &&
25
- f.kind == "scalar_field" &&
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.kind == "scalar_field" &&
39
- f.scalar === protobuf_1.ScalarType.INT64);
40
- const nanos = message.fields.find((f) => f.number == 2 &&
41
- f.kind == "scalar_field" &&
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.kind == "scalar_field" &&
55
- f.scalar === protobuf_1.ScalarType.INT64);
56
- const nanos = message.fields.find((f) => f.number == 2 &&
57
- f.kind == "scalar_field" &&
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.kind) !== "map_field" ||
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.kind) !== "enum_field" ||
81
- nullValue.enum.typeName !== "google.protobuf.NullValue") {
82
- return undefined;
83
- }
84
- const numberValue = message.fields.find((f) => f.number == 2 &&
85
- f.kind == "scalar_field" &&
86
- f.scalar === protobuf_1.ScalarType.DOUBLE &&
87
- f.oneof === kind);
88
- const stringValue = message.fields.find((f) => f.number == 3 &&
89
- f.kind == "scalar_field" &&
90
- f.scalar === protobuf_1.ScalarType.STRING &&
91
- f.oneof === kind);
92
- const boolValue = message.fields.find((f) => f.number == 4 &&
93
- f.kind == "scalar_field" &&
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.kind) !== "message_field" ||
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.kind) !== "message_field" ||
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.kind) != "message_field" ||
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.kind == "scalar_field" &&
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.kind !== "scalar_field") {
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 & { kind: "map_field" };
41
- }
42
- | {
43
- typeName: "google.protobuf.Value";
44
- kind: DescOneof;
45
- nullValue: DescField & { kind: "enum_field" };
46
- numberValue: DescField;
47
- stringValue: DescField;
48
- boolValue: DescField;
49
- structValue: DescField & { kind: "message_field" };
50
- listValue: DescField & { kind: "message_field" };
51
- }
52
- | {
53
- typeName: "google.protobuf.ListValue";
54
- values: DescField & { kind: "message_field" };
55
- }
56
- | {
57
- typeName: "google.protobuf.FieldMask";
58
- paths: DescField;
59
- }
60
- | {
61
- typeName: "google.protobuf.DoubleValue";
62
- value: DescField & { kind: "scalar_field" };
63
- }
64
- | {
65
- typeName: "google.protobuf.FloatValue";
66
- value: DescField & { kind: "scalar_field" };
67
- }
68
- | {
69
- typeName: "google.protobuf.Int64Value";
70
- value: DescField & { kind: "scalar_field" };
71
- }
72
- | {
73
- typeName: "google.protobuf.UInt64Value";
74
- value: DescField & { kind: "scalar_field" };
75
- }
76
- | {
77
- typeName: "google.protobuf.Int32Value";
78
- value: DescField & { kind: "scalar_field" };
79
- }
80
- | {
81
- typeName: "google.protobuf.UInt32Value";
82
- value: DescField & { kind: "scalar_field" };
83
- }
84
- | {
85
- typeName: "google.protobuf.BoolValue";
86
- value: DescField & { kind: "scalar_field" };
87
- }
88
- | {
89
- typeName: "google.protobuf.StringValue";
90
- value: DescField & { kind: "scalar_field" };
91
- }
92
- | {
93
- typeName: "google.protobuf.BytesValue";
94
- value: DescField & { kind: "scalar_field" };
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.kind == "scalar_field" &&
104
- f.scalar === ScalarType.STRING
105
- );
106
- const value = message.fields.find(
107
- (f) =>
108
- f.number == 2 &&
109
- f.kind == "scalar_field" &&
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.kind == "scalar_field" &&
126
- f.scalar === ScalarType.INT64
127
- );
128
- const nanos = message.fields.find(
129
- (f) =>
130
- f.number == 2 &&
131
- f.kind == "scalar_field" &&
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.kind == "scalar_field" &&
148
- f.scalar === ScalarType.INT64
149
- );
150
- const nanos = message.fields.find(
151
- (f) =>
152
- f.number == 2 &&
153
- f.kind == "scalar_field" &&
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?.kind !== "map_field" ||
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?.kind !== "enum_field" ||
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.kind == "scalar_field" &&
191
- f.scalar === ScalarType.DOUBLE &&
192
- f.oneof === kind
193
- );
194
- const stringValue = message.fields.find(
195
- (f) =>
196
- f.number == 3 &&
197
- f.kind == "scalar_field" &&
198
- f.scalar === ScalarType.STRING &&
199
- f.oneof === kind
200
- );
201
- const boolValue = message.fields.find(
202
- (f) =>
203
- f.number == 4 &&
204
- f.kind == "scalar_field" &&
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?.kind !== "message_field" ||
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?.kind !== "message_field" ||
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?.kind != "message_field" ||
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.kind == "scalar_field" &&
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.kind !== "scalar_field") {
279
- break;
280
- }
281
- return { typeName: message.typeName, value };
282
- }
283
- }
284
- return undefined;
285
- }