@bufbuild/protoc-gen-es 2.0.0-alpha.3 → 2.0.0-beta.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 +5 -0
- package/dist/cjs/package.json +4 -4
- package/dist/cjs/src/protoc-gen-es-plugin.js +175 -42
- package/dist/cjs/src/util.js +100 -20
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -135,3 +135,8 @@ settings.
|
|
|
135
135
|
In case you use compiler settings that yield an error for generated code, you
|
|
136
136
|
can set the plugin option `ts_nocheck=true`. This will generate an annotation at
|
|
137
137
|
the top of each file to skip type checks: `// @ts-nocheck`.
|
|
138
|
+
|
|
139
|
+
### `json_types=true`
|
|
140
|
+
|
|
141
|
+
Generates JSON types for every Protobuf message and enumeration. Calling `toJson()`
|
|
142
|
+
will automatically return the JSON type if available.
|
package/dist/cjs/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
4
|
"description": "Protocol Buffers code generator for ECMAScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
},
|
|
21
21
|
"preferUnplugged": true,
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@bufbuild/protobuf": "^2.0.0-
|
|
24
|
-
"@bufbuild/protoplugin": "2.0.0-
|
|
23
|
+
"@bufbuild/protobuf": "^2.0.0-beta.1",
|
|
24
|
+
"@bufbuild/protoplugin": "2.0.0-beta.1"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@bufbuild/protobuf": "2.0.0-
|
|
27
|
+
"@bufbuild/protobuf": "2.0.0-beta.1"
|
|
28
28
|
},
|
|
29
29
|
"peerDependenciesMeta": {
|
|
30
30
|
"@bufbuild/protobuf": {
|
|
@@ -16,16 +16,34 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
16
16
|
exports.protocGenEs = void 0;
|
|
17
17
|
const reflect_1 = require("@bufbuild/protobuf/reflect");
|
|
18
18
|
const codegenv1_1 = require("@bufbuild/protobuf/codegenv1");
|
|
19
|
+
const wkt_1 = require("@bufbuild/protobuf/wkt");
|
|
19
20
|
const protoplugin_1 = require("@bufbuild/protoplugin");
|
|
20
21
|
const util_1 = require("./util");
|
|
21
22
|
const package_json_1 = require("../package.json");
|
|
22
23
|
exports.protocGenEs = (0, protoplugin_1.createEcmaScriptPlugin)({
|
|
23
24
|
name: "protoc-gen-es",
|
|
24
25
|
version: `v${String(package_json_1.version)}`,
|
|
26
|
+
parseOptions,
|
|
25
27
|
generateTs,
|
|
26
28
|
generateJs,
|
|
27
29
|
generateDts,
|
|
28
30
|
});
|
|
31
|
+
function parseOptions(options) {
|
|
32
|
+
let jsonTypes = false;
|
|
33
|
+
for (const { key, value } of options) {
|
|
34
|
+
switch (key) {
|
|
35
|
+
case "json_types":
|
|
36
|
+
if (!["true", "1", "false", "0"].includes(value)) {
|
|
37
|
+
throw "please provide true or false";
|
|
38
|
+
}
|
|
39
|
+
jsonTypes = ["true", "1"].includes(value);
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
throw new Error();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { jsonTypes };
|
|
46
|
+
}
|
|
29
47
|
// This annotation informs bundlers that the succeeding function call is free of
|
|
30
48
|
// side effects. This means the symbol can be removed from the module during
|
|
31
49
|
// tree-shaking if it is unused.
|
|
@@ -37,7 +55,7 @@ function generateTs(schema) {
|
|
|
37
55
|
const f = schema.generateFile(file.name + "_pb.ts");
|
|
38
56
|
f.preamble(file);
|
|
39
57
|
const { GenDescFile } = f.runtime.codegen;
|
|
40
|
-
const fileDesc = f.
|
|
58
|
+
const fileDesc = f.importSchema(file);
|
|
41
59
|
generateDescDoc(f, file);
|
|
42
60
|
f.print(f.export("const", fileDesc.name), ": ", GenDescFile, " = ", pure);
|
|
43
61
|
f.print(" ", getFileDescCall(f, file, schema), ";");
|
|
@@ -46,33 +64,51 @@ function generateTs(schema) {
|
|
|
46
64
|
switch (desc.kind) {
|
|
47
65
|
case "message": {
|
|
48
66
|
generateMessageShape(f, desc, "ts");
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
67
|
+
if (schema.options.jsonTypes) {
|
|
68
|
+
generateMessageJsonShape(f, desc, "ts");
|
|
69
|
+
}
|
|
52
70
|
generateDescDoc(f, desc);
|
|
71
|
+
const name = f.importSchema(desc).name;
|
|
72
|
+
const Shape = f.importShape(desc);
|
|
73
|
+
const { GenDescMessage, messageDesc } = f.runtime.codegen;
|
|
74
|
+
if (schema.options.jsonTypes) {
|
|
75
|
+
const JsonType = f.importJson(desc);
|
|
76
|
+
f.print(f.export("const", name), ": ", GenDescMessage, "<", Shape, ", ", JsonType, ">", " = ", pure);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
f.print(f.export("const", name), ": ", GenDescMessage, "<", Shape, ">", " = ", pure);
|
|
80
|
+
}
|
|
53
81
|
const call = (0, util_1.functionCall)(messageDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
54
|
-
f.print(f.export("const", name), ": ", GenDescMessage, "<", MessageShape, ">", " = ", pure);
|
|
55
82
|
f.print(" ", call, ";");
|
|
56
83
|
f.print();
|
|
57
84
|
break;
|
|
58
85
|
}
|
|
59
86
|
case "enum": {
|
|
60
87
|
generateEnumShape(f, desc);
|
|
61
|
-
|
|
62
|
-
|
|
88
|
+
if (schema.options.jsonTypes) {
|
|
89
|
+
generateEnumJsonShape(f, desc, "ts");
|
|
90
|
+
}
|
|
63
91
|
generateDescDoc(f, desc);
|
|
64
|
-
const name = f.
|
|
92
|
+
const name = f.importSchema(desc).name;
|
|
93
|
+
const Shape = f.importShape(desc);
|
|
94
|
+
const { GenDescEnum, enumDesc } = f.runtime.codegen;
|
|
95
|
+
if (schema.options.jsonTypes) {
|
|
96
|
+
const JsonType = f.importJson(desc);
|
|
97
|
+
f.print(f.export("const", name), ": ", GenDescEnum, "<", Shape, ", ", JsonType, ">", " = ", pure);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
f.print(f.export("const", name), ": ", GenDescEnum, "<", Shape, ">", " = ", pure);
|
|
101
|
+
}
|
|
65
102
|
const call = (0, util_1.functionCall)(enumDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
66
|
-
f.print(f.export("const", name), ": ", GenDescEnum, "<", EnumShape, ">", " = ", pure);
|
|
67
103
|
f.print(" ", call, ";");
|
|
68
104
|
f.print();
|
|
69
105
|
break;
|
|
70
106
|
}
|
|
71
107
|
case "extension": {
|
|
72
108
|
const { GenDescExtension, extDesc } = f.runtime.codegen;
|
|
73
|
-
const name = f.
|
|
109
|
+
const name = f.importSchema(desc).name;
|
|
74
110
|
const E = f.importShape(desc.extendee);
|
|
75
|
-
const V = (0, util_1.fieldTypeScriptType)(desc).typing;
|
|
111
|
+
const V = (0, util_1.fieldTypeScriptType)(desc, f.runtime).typing;
|
|
76
112
|
const call = (0, util_1.functionCall)(extDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
77
113
|
f.print(f.jsDoc(desc));
|
|
78
114
|
f.print(f.export("const", name), ": ", GenDescExtension, "<", E, ", ", V, ">", " = ", pure);
|
|
@@ -82,7 +118,7 @@ function generateTs(schema) {
|
|
|
82
118
|
}
|
|
83
119
|
case "service": {
|
|
84
120
|
const { GenDescService, serviceDesc } = f.runtime.codegen;
|
|
85
|
-
const name = f.
|
|
121
|
+
const name = f.importSchema(desc).name;
|
|
86
122
|
const call = (0, util_1.functionCall)(serviceDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
87
123
|
f.print(f.jsDoc(desc));
|
|
88
124
|
f.print(f.export("const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), "> = ", pure);
|
|
@@ -99,7 +135,7 @@ function generateJs(schema) {
|
|
|
99
135
|
for (const file of schema.files) {
|
|
100
136
|
const f = schema.generateFile(file.name + "_pb.js");
|
|
101
137
|
f.preamble(file);
|
|
102
|
-
const fileDesc = f.
|
|
138
|
+
const fileDesc = f.importSchema(file);
|
|
103
139
|
generateDescDoc(f, file);
|
|
104
140
|
f.print(f.export("const", fileDesc.name), " = ", pure);
|
|
105
141
|
f.print(" ", getFileDescCall(f, file, schema), ";");
|
|
@@ -107,9 +143,9 @@ function generateJs(schema) {
|
|
|
107
143
|
for (const desc of schema.typesInFile(file)) {
|
|
108
144
|
switch (desc.kind) {
|
|
109
145
|
case "message": {
|
|
110
|
-
const
|
|
111
|
-
const name = f.importDesc(desc).name;
|
|
146
|
+
const name = f.importSchema(desc).name;
|
|
112
147
|
generateDescDoc(f, desc);
|
|
148
|
+
const { messageDesc } = f.runtime.codegen;
|
|
113
149
|
const call = (0, util_1.functionCall)(messageDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
114
150
|
f.print(f.export("const", name), " = ", pure);
|
|
115
151
|
f.print(" ", call, ";");
|
|
@@ -119,41 +155,41 @@ function generateJs(schema) {
|
|
|
119
155
|
case "enum": {
|
|
120
156
|
// generate descriptor
|
|
121
157
|
{
|
|
122
|
-
const { enumDesc } = f.runtime.codegen;
|
|
123
158
|
generateDescDoc(f, desc);
|
|
124
|
-
const name = f.
|
|
125
|
-
const call = (0, util_1.functionCall)(enumDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
159
|
+
const name = f.importSchema(desc).name;
|
|
126
160
|
f.print(f.export("const", name), " = ", pure);
|
|
161
|
+
const { enumDesc } = f.runtime.codegen;
|
|
162
|
+
const call = (0, util_1.functionCall)(enumDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
127
163
|
f.print(" ", call, ";");
|
|
128
164
|
f.print();
|
|
129
165
|
}
|
|
130
166
|
// declare TypeScript enum
|
|
131
167
|
{
|
|
132
168
|
f.print(f.jsDoc(desc));
|
|
133
|
-
const { tsEnum } = f.runtime.codegen;
|
|
134
|
-
const call = (0, util_1.functionCall)(tsEnum, [f.importDesc(desc)]);
|
|
135
169
|
f.print(f.export("const", f.importShape(desc).name), " = ", pure);
|
|
170
|
+
const { tsEnum } = f.runtime.codegen;
|
|
171
|
+
const call = (0, util_1.functionCall)(tsEnum, [f.importSchema(desc)]);
|
|
136
172
|
f.print(" ", call, ";");
|
|
137
173
|
f.print();
|
|
138
174
|
}
|
|
139
175
|
break;
|
|
140
176
|
}
|
|
141
177
|
case "extension": {
|
|
142
|
-
const { extDesc } = f.runtime.codegen;
|
|
143
|
-
const name = f.importDesc(desc).name;
|
|
144
|
-
const call = (0, util_1.functionCall)(extDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
145
178
|
f.print(f.jsDoc(desc));
|
|
179
|
+
const name = f.importSchema(desc).name;
|
|
146
180
|
f.print(f.export("const", name), " = ", pure);
|
|
181
|
+
const { extDesc } = f.runtime.codegen;
|
|
182
|
+
const call = (0, util_1.functionCall)(extDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
147
183
|
f.print(" ", call, ";");
|
|
148
184
|
f.print();
|
|
149
185
|
break;
|
|
150
186
|
}
|
|
151
187
|
case "service": {
|
|
152
|
-
const { serviceDesc } = f.runtime.codegen;
|
|
153
|
-
const name = f.importDesc(desc).name;
|
|
154
188
|
f.print(f.jsDoc(desc));
|
|
155
|
-
const
|
|
189
|
+
const name = f.importSchema(desc).name;
|
|
156
190
|
f.print(f.export("const", name), " = ", pure);
|
|
191
|
+
const { serviceDesc } = f.runtime.codegen;
|
|
192
|
+
const call = (0, util_1.functionCall)(serviceDesc, [fileDesc, ...(0, codegenv1_1.pathInFileDesc)(desc)]);
|
|
157
193
|
f.print(" ", call, ";");
|
|
158
194
|
f.print();
|
|
159
195
|
break;
|
|
@@ -168,7 +204,7 @@ function generateDts(schema) {
|
|
|
168
204
|
const f = schema.generateFile(file.name + "_pb.d.ts");
|
|
169
205
|
f.preamble(file);
|
|
170
206
|
const { GenDescFile } = f.runtime.codegen;
|
|
171
|
-
const fileDesc = f.
|
|
207
|
+
const fileDesc = f.importSchema(file);
|
|
172
208
|
generateDescDoc(f, file);
|
|
173
209
|
f.print(f.export("declare const", fileDesc.name), ": ", GenDescFile, ";");
|
|
174
210
|
f.print();
|
|
@@ -176,29 +212,47 @@ function generateDts(schema) {
|
|
|
176
212
|
switch (desc.kind) {
|
|
177
213
|
case "message": {
|
|
178
214
|
generateMessageShape(f, desc, "dts");
|
|
215
|
+
if (schema.options.jsonTypes) {
|
|
216
|
+
generateMessageJsonShape(f, desc, "dts");
|
|
217
|
+
}
|
|
218
|
+
const name = f.importSchema(desc).name;
|
|
219
|
+
const Shape = f.importShape(desc);
|
|
179
220
|
const { GenDescMessage } = f.runtime.codegen;
|
|
180
|
-
const MessageShape = f.importShape(desc);
|
|
181
|
-
const name = f.importDesc(desc).name;
|
|
182
221
|
generateDescDoc(f, desc);
|
|
183
|
-
|
|
222
|
+
if (schema.options.jsonTypes) {
|
|
223
|
+
const JsonType = f.importJson(desc);
|
|
224
|
+
f.print(f.export("declare const", name), ": ", GenDescMessage, "<", Shape, ", ", JsonType, ">", ";");
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
f.print(f.export("declare const", name), ": ", GenDescMessage, "<", Shape, ">", ";");
|
|
228
|
+
}
|
|
184
229
|
f.print();
|
|
185
230
|
break;
|
|
186
231
|
}
|
|
187
232
|
case "enum": {
|
|
188
233
|
generateEnumShape(f, desc);
|
|
189
|
-
|
|
190
|
-
|
|
234
|
+
if (schema.options.jsonTypes) {
|
|
235
|
+
generateEnumJsonShape(f, desc, "dts");
|
|
236
|
+
}
|
|
191
237
|
generateDescDoc(f, desc);
|
|
192
|
-
const name = f.
|
|
193
|
-
|
|
238
|
+
const name = f.importSchema(desc).name;
|
|
239
|
+
const Shape = f.importShape(desc);
|
|
240
|
+
const { GenDescEnum } = f.runtime.codegen;
|
|
241
|
+
if (schema.options.jsonTypes) {
|
|
242
|
+
const JsonType = f.importJson(desc);
|
|
243
|
+
f.print(f.export("declare const", name), ": ", GenDescEnum, "<", Shape, ", ", JsonType, ">;");
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
f.print(f.export("declare const", name), ": ", GenDescEnum, "<", Shape, ">;");
|
|
247
|
+
}
|
|
194
248
|
f.print();
|
|
195
249
|
break;
|
|
196
250
|
}
|
|
197
251
|
case "extension": {
|
|
198
252
|
const { GenDescExtension } = f.runtime.codegen;
|
|
199
|
-
const name = f.
|
|
253
|
+
const name = f.importSchema(desc).name;
|
|
200
254
|
const E = f.importShape(desc.extendee);
|
|
201
|
-
const V = (0, util_1.fieldTypeScriptType)(desc).typing;
|
|
255
|
+
const V = (0, util_1.fieldTypeScriptType)(desc, f.runtime).typing;
|
|
202
256
|
f.print(f.jsDoc(desc));
|
|
203
257
|
f.print(f.export("declare const", name), ": ", GenDescExtension, "<", E, ", ", V, ">;");
|
|
204
258
|
f.print();
|
|
@@ -206,7 +260,7 @@ function generateDts(schema) {
|
|
|
206
260
|
}
|
|
207
261
|
case "service": {
|
|
208
262
|
const { GenDescService } = f.runtime.codegen;
|
|
209
|
-
const name = f.
|
|
263
|
+
const name = f.importSchema(desc).name;
|
|
210
264
|
f.print(f.jsDoc(desc));
|
|
211
265
|
f.print(f.export("declare const", name), ": ", GenDescService, "<", getServiceShapeExpr(f, desc), ">;");
|
|
212
266
|
f.print();
|
|
@@ -225,7 +279,7 @@ function generateDescDoc(f, desc) {
|
|
|
225
279
|
case "message":
|
|
226
280
|
lines = [
|
|
227
281
|
`Describes the ${desc.toString()}.`,
|
|
228
|
-
`Use \`create(${f.
|
|
282
|
+
`Use \`create(${f.importSchema(desc).name})\` to create a new message.`,
|
|
229
283
|
];
|
|
230
284
|
break;
|
|
231
285
|
case "enum":
|
|
@@ -278,8 +332,8 @@ function getServiceShapeExpr(f, service) {
|
|
|
278
332
|
print(f.jsDoc(method, " "));
|
|
279
333
|
print(" ", method.localName, ": {");
|
|
280
334
|
print(" methodKind: ", f.string(method.methodKind), ";");
|
|
281
|
-
print(" input: typeof ", f.
|
|
282
|
-
print(" output: typeof ", f.
|
|
335
|
+
print(" input: typeof ", f.importSchema(method.input, true), ";");
|
|
336
|
+
print(" output: typeof ", f.importSchema(method.output, true), ";");
|
|
283
337
|
print(" },");
|
|
284
338
|
}
|
|
285
339
|
print("}");
|
|
@@ -300,6 +354,25 @@ function generateEnumShape(f, enumeration) {
|
|
|
300
354
|
f.print();
|
|
301
355
|
}
|
|
302
356
|
// prettier-ignore
|
|
357
|
+
function generateEnumJsonShape(f, enumeration, target) {
|
|
358
|
+
f.print(f.jsDoc(`JSON type for the ${enumeration.toString()}.`));
|
|
359
|
+
const declaration = target == "ts" ? "type" : "declare type";
|
|
360
|
+
const values = [];
|
|
361
|
+
if (enumeration.typeName == "google.protobuf.NullValue") {
|
|
362
|
+
values.push("null");
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
for (const v of enumeration.values) {
|
|
366
|
+
if (enumeration.values.indexOf(v) > 0) {
|
|
367
|
+
values.push(" | ");
|
|
368
|
+
}
|
|
369
|
+
values.push(f.string(v.name));
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
f.print(f.export(declaration, f.importJson(enumeration).name), " = ", values, ";");
|
|
373
|
+
f.print();
|
|
374
|
+
}
|
|
375
|
+
// prettier-ignore
|
|
303
376
|
function generateMessageShape(f, message, target) {
|
|
304
377
|
const { Message } = f.runtime;
|
|
305
378
|
const declaration = target == "ts" ? "type" : "declare type";
|
|
@@ -315,7 +388,7 @@ function generateMessageShape(f, message, target) {
|
|
|
315
388
|
f.print(` } | {`);
|
|
316
389
|
}
|
|
317
390
|
f.print(f.jsDoc(field, " "));
|
|
318
|
-
const { typing } = (0, util_1.fieldTypeScriptType)(field);
|
|
391
|
+
const { typing } = (0, util_1.fieldTypeScriptType)(field, f.runtime);
|
|
319
392
|
f.print(` value: `, typing, `;`);
|
|
320
393
|
f.print(` case: "`, field.localName, `";`);
|
|
321
394
|
}
|
|
@@ -323,7 +396,7 @@ function generateMessageShape(f, message, target) {
|
|
|
323
396
|
break;
|
|
324
397
|
default: {
|
|
325
398
|
f.print(f.jsDoc(member, " "));
|
|
326
|
-
const { typing, optional } = (0, util_1.fieldTypeScriptType)(member);
|
|
399
|
+
const { typing, optional } = (0, util_1.fieldTypeScriptType)(member, f.runtime);
|
|
327
400
|
if (optional) {
|
|
328
401
|
f.print(" ", member.localName, "?: ", typing, ";");
|
|
329
402
|
}
|
|
@@ -340,3 +413,63 @@ function generateMessageShape(f, message, target) {
|
|
|
340
413
|
f.print("};");
|
|
341
414
|
f.print();
|
|
342
415
|
}
|
|
416
|
+
// prettier-ignore
|
|
417
|
+
function generateMessageJsonShape(f, message, target) {
|
|
418
|
+
const exp = f.export(target == "ts" ? "type" : "declare type", f.importJson(message).name);
|
|
419
|
+
f.print(f.jsDoc(`JSON type for the ${message.toString()}.`));
|
|
420
|
+
switch (message.typeName) {
|
|
421
|
+
case "google.protobuf.Any":
|
|
422
|
+
f.print(exp, " = {");
|
|
423
|
+
f.print(` "@type"?: string`);
|
|
424
|
+
f.print("};");
|
|
425
|
+
break;
|
|
426
|
+
case "google.protobuf.Timestamp":
|
|
427
|
+
f.print(exp, " = string;");
|
|
428
|
+
break;
|
|
429
|
+
case "google.protobuf.Duration":
|
|
430
|
+
f.print(exp, " = string;");
|
|
431
|
+
break;
|
|
432
|
+
case "google.protobuf.FieldMask":
|
|
433
|
+
f.print(exp, " = string;");
|
|
434
|
+
break;
|
|
435
|
+
case "google.protobuf.Struct":
|
|
436
|
+
f.print(exp, " = ", f.runtime.JsonObject, ";");
|
|
437
|
+
break;
|
|
438
|
+
case "google.protobuf.Value":
|
|
439
|
+
f.print(exp, " = ", f.runtime.JsonValue, ";");
|
|
440
|
+
break;
|
|
441
|
+
case "google.protobuf.ListValue":
|
|
442
|
+
f.print(exp, " = ", f.runtime.JsonValue, "[];");
|
|
443
|
+
break;
|
|
444
|
+
case "google.protobuf.Empty":
|
|
445
|
+
f.print(exp, " = Record<string, never>;");
|
|
446
|
+
break;
|
|
447
|
+
default:
|
|
448
|
+
if ((0, wkt_1.isWrapperDesc)(message)) {
|
|
449
|
+
f.print(exp, " = ", (0, util_1.fieldJsonType)(message.fields[0]), ";");
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
f.print(exp, " = {");
|
|
453
|
+
for (const field of message.fields) {
|
|
454
|
+
switch (field.kind) {
|
|
455
|
+
default:
|
|
456
|
+
f.print(f.jsDoc(`@generated from field: ${(0, protoplugin_1.getDeclarationString)(field)};`, " "));
|
|
457
|
+
// eslint-disable-next-line no-case-declarations
|
|
458
|
+
let jsonName = field.jsonName;
|
|
459
|
+
if (jsonName === ""
|
|
460
|
+
|| /^[0-9]/.test(jsonName)
|
|
461
|
+
|| jsonName.indexOf("@") > -1) {
|
|
462
|
+
jsonName = f.string(jsonName);
|
|
463
|
+
}
|
|
464
|
+
f.print(" ", jsonName, "?: ", (0, util_1.fieldJsonType)(field), ";");
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
if (message.fields.indexOf(field) < message.fields.length - 1) {
|
|
468
|
+
f.print();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
f.print("};");
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
f.print();
|
|
475
|
+
}
|
package/dist/cjs/src/util.js
CHANGED
|
@@ -13,11 +13,11 @@
|
|
|
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.arrayLiteral = exports.functionCall = exports.fieldTypeScriptType = void 0;
|
|
16
|
+
exports.arrayLiteral = exports.functionCall = exports.fieldJsonType = exports.fieldTypeScriptType = void 0;
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
18
|
const codegenv1_1 = require("@bufbuild/protobuf/codegenv1");
|
|
19
19
|
const wkt_1 = require("@bufbuild/protobuf/wkt");
|
|
20
|
-
function fieldTypeScriptType(field) {
|
|
20
|
+
function fieldTypeScriptType(field, imports) {
|
|
21
21
|
const typing = [];
|
|
22
22
|
let optional = false;
|
|
23
23
|
switch (field.fieldKind) {
|
|
@@ -26,16 +26,7 @@ function fieldTypeScriptType(field) {
|
|
|
26
26
|
optional = field.proto.proto3Optional;
|
|
27
27
|
break;
|
|
28
28
|
case "message": {
|
|
29
|
-
|
|
30
|
-
const baseType = field.message.fields[0].scalar;
|
|
31
|
-
typing.push((0, codegenv1_1.scalarTypeScriptType)(baseType, false));
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
typing.push({
|
|
35
|
-
kind: "es_shape_ref",
|
|
36
|
-
desc: field.message,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
29
|
+
typing.push(messageFieldTypeScriptType(field, imports));
|
|
39
30
|
optional = true;
|
|
40
31
|
break;
|
|
41
32
|
}
|
|
@@ -59,10 +50,7 @@ function fieldTypeScriptType(field) {
|
|
|
59
50
|
typing.push((0, codegenv1_1.scalarTypeScriptType)(field.scalar, field.longAsString), "[]");
|
|
60
51
|
break;
|
|
61
52
|
case "message": {
|
|
62
|
-
typing.push(
|
|
63
|
-
kind: "es_shape_ref",
|
|
64
|
-
desc: field.message,
|
|
65
|
-
}, "[]");
|
|
53
|
+
typing.push(messageFieldTypeScriptType(field, imports), "[]");
|
|
66
54
|
break;
|
|
67
55
|
}
|
|
68
56
|
}
|
|
@@ -87,10 +75,7 @@ function fieldTypeScriptType(field) {
|
|
|
87
75
|
valueType = (0, codegenv1_1.scalarTypeScriptType)(field.scalar, false);
|
|
88
76
|
break;
|
|
89
77
|
case "message":
|
|
90
|
-
valueType =
|
|
91
|
-
kind: "es_shape_ref",
|
|
92
|
-
desc: field.message,
|
|
93
|
-
};
|
|
78
|
+
valueType = messageFieldTypeScriptType(field, imports);
|
|
94
79
|
break;
|
|
95
80
|
case "enum":
|
|
96
81
|
valueType = {
|
|
@@ -107,6 +92,101 @@ function fieldTypeScriptType(field) {
|
|
|
107
92
|
return { typing, optional };
|
|
108
93
|
}
|
|
109
94
|
exports.fieldTypeScriptType = fieldTypeScriptType;
|
|
95
|
+
function messageFieldTypeScriptType(field, imports) {
|
|
96
|
+
var _a;
|
|
97
|
+
if ((0, wkt_1.isWrapperDesc)(field.message) &&
|
|
98
|
+
!field.oneof &&
|
|
99
|
+
field.fieldKind == "message") {
|
|
100
|
+
const baseType = field.message.fields[0].scalar;
|
|
101
|
+
return (0, codegenv1_1.scalarTypeScriptType)(baseType, false);
|
|
102
|
+
}
|
|
103
|
+
if (field.message.typeName == wkt_1.StructSchema.typeName &&
|
|
104
|
+
((_a = field.parent) === null || _a === void 0 ? void 0 : _a.typeName) != wkt_1.ValueSchema.typeName) {
|
|
105
|
+
return imports.JsonObject;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
kind: "es_shape_ref",
|
|
109
|
+
desc: field.message,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function fieldJsonType(field) {
|
|
113
|
+
switch (field.fieldKind) {
|
|
114
|
+
case "scalar":
|
|
115
|
+
return (0, codegenv1_1.scalarJsonType)(field.scalar);
|
|
116
|
+
case "message":
|
|
117
|
+
return {
|
|
118
|
+
kind: "es_json_type_ref",
|
|
119
|
+
desc: field.message,
|
|
120
|
+
};
|
|
121
|
+
case "enum":
|
|
122
|
+
return {
|
|
123
|
+
kind: "es_json_type_ref",
|
|
124
|
+
desc: field.enum,
|
|
125
|
+
};
|
|
126
|
+
case "list":
|
|
127
|
+
switch (field.listKind) {
|
|
128
|
+
case "enum":
|
|
129
|
+
return [
|
|
130
|
+
{
|
|
131
|
+
kind: "es_json_type_ref",
|
|
132
|
+
desc: field.enum,
|
|
133
|
+
},
|
|
134
|
+
"[]",
|
|
135
|
+
];
|
|
136
|
+
case "scalar": {
|
|
137
|
+
const t = (0, codegenv1_1.scalarJsonType)(field.scalar);
|
|
138
|
+
if (t.includes("|")) {
|
|
139
|
+
return ["(", t, ")[]"];
|
|
140
|
+
}
|
|
141
|
+
return [t, "[]"];
|
|
142
|
+
}
|
|
143
|
+
case "message":
|
|
144
|
+
return [
|
|
145
|
+
{
|
|
146
|
+
kind: "es_json_type_ref",
|
|
147
|
+
desc: field.message,
|
|
148
|
+
},
|
|
149
|
+
"[]",
|
|
150
|
+
];
|
|
151
|
+
}
|
|
152
|
+
break;
|
|
153
|
+
case "map": {
|
|
154
|
+
let keyType;
|
|
155
|
+
switch (field.mapKey) {
|
|
156
|
+
case protobuf_1.ScalarType.INT32:
|
|
157
|
+
case protobuf_1.ScalarType.FIXED32:
|
|
158
|
+
case protobuf_1.ScalarType.UINT32:
|
|
159
|
+
case protobuf_1.ScalarType.SFIXED32:
|
|
160
|
+
case protobuf_1.ScalarType.SINT32:
|
|
161
|
+
keyType = "number";
|
|
162
|
+
break;
|
|
163
|
+
default:
|
|
164
|
+
keyType = "string";
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
let valueType;
|
|
168
|
+
switch (field.mapKind) {
|
|
169
|
+
case "scalar":
|
|
170
|
+
valueType = (0, codegenv1_1.scalarJsonType)(field.scalar);
|
|
171
|
+
break;
|
|
172
|
+
case "message":
|
|
173
|
+
valueType = {
|
|
174
|
+
kind: "es_json_type_ref",
|
|
175
|
+
desc: field.message,
|
|
176
|
+
};
|
|
177
|
+
break;
|
|
178
|
+
case "enum":
|
|
179
|
+
valueType = {
|
|
180
|
+
kind: "es_json_type_ref",
|
|
181
|
+
desc: field.enum,
|
|
182
|
+
};
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
return ["{ [key: ", keyType, "]: ", valueType, " }"];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
exports.fieldJsonType = fieldJsonType;
|
|
110
190
|
function functionCall(fn, args, typeParams) {
|
|
111
191
|
let tp = [];
|
|
112
192
|
if (typeParams !== undefined && typeParams.length > 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
4
|
"description": "Protocol Buffers code generator for ECMAScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
},
|
|
21
21
|
"preferUnplugged": true,
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@bufbuild/protobuf": "^2.0.0-
|
|
24
|
-
"@bufbuild/protoplugin": "2.0.0-
|
|
23
|
+
"@bufbuild/protobuf": "^2.0.0-beta.1",
|
|
24
|
+
"@bufbuild/protoplugin": "2.0.0-beta.1"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@bufbuild/protobuf": "2.0.0-
|
|
27
|
+
"@bufbuild/protobuf": "2.0.0-beta.1"
|
|
28
28
|
},
|
|
29
29
|
"peerDependenciesMeta": {
|
|
30
30
|
"@bufbuild/protobuf": {
|