@bufbuild/protoc-gen-es 1.3.3 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/package.json +3 -3
- package/dist/cjs/src/javascript.js +3 -0
- package/dist/cjs/src/typescript.js +27 -8
- package/package.json +3 -3
- package/src/javascript.ts +4 -1
- package/src/typescript.ts +25 -8
package/dist/cjs/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.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": "1.
|
|
23
|
+
"@bufbuild/protoplugin": "1.4.0"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@bufbuild/protobuf": "1.
|
|
26
|
+
"@bufbuild/protobuf": "1.4.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependenciesMeta": {
|
|
29
29
|
"@bufbuild/protobuf": {
|
|
@@ -98,6 +98,9 @@ function generateFieldInfo(schema, f, field) {
|
|
|
98
98
|
switch (field.fieldKind) {
|
|
99
99
|
case "scalar":
|
|
100
100
|
e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
|
|
101
|
+
if (field.longType != protobuf_1.LongType.BIGINT) {
|
|
102
|
+
e.push(`L: `, field.longType, ` /* LongType.`, protobuf_1.LongType[field.longType], ` */, `);
|
|
103
|
+
}
|
|
101
104
|
break;
|
|
102
105
|
case "map":
|
|
103
106
|
e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
|
|
@@ -17,7 +17,6 @@ exports.generateTs = void 0;
|
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
18
|
const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
|
|
19
19
|
const javascript_js_1 = require("./javascript.js");
|
|
20
|
-
const ecmascript_2 = require("@bufbuild/protoplugin/ecmascript");
|
|
21
20
|
function generateTs(schema) {
|
|
22
21
|
for (const file of schema.files) {
|
|
23
22
|
const f = schema.generateFile(file.name + "_pb.ts");
|
|
@@ -77,7 +76,7 @@ function generateMessage(schema, f, message) {
|
|
|
77
76
|
f.print();
|
|
78
77
|
generateWktMethods(schema, f, message);
|
|
79
78
|
f.print(" static readonly runtime: typeof ", protoN, " = ", protoN, ";");
|
|
80
|
-
f.print(' static readonly typeName = ', (0,
|
|
79
|
+
f.print(' static readonly typeName = ', (0, ecmascript_1.literalString)(message.typeName), ';');
|
|
81
80
|
f.print(" static readonly fields: ", FieldList, " = ", protoN, ".util.newFieldList(() => [");
|
|
82
81
|
for (const field of message.fields) {
|
|
83
82
|
(0, javascript_js_1.generateFieldInfo)(schema, f, field);
|
|
@@ -152,7 +151,7 @@ function generateWktMethods(schema, f, message) {
|
|
|
152
151
|
if (ref === undefined) {
|
|
153
152
|
return;
|
|
154
153
|
}
|
|
155
|
-
const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, IMessageTypeRegistry, ScalarType: rtScalarType, protoInt64, } = schema.runtime;
|
|
154
|
+
const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, IMessageTypeRegistry, ScalarType: rtScalarType, LongType: rtLongType, protoInt64, } = schema.runtime;
|
|
156
155
|
const protoN = schema.runtime[message.file.syntax];
|
|
157
156
|
switch (ref.typeName) {
|
|
158
157
|
case "google.protobuf.Any":
|
|
@@ -272,7 +271,12 @@ function generateWktMethods(schema, f, message) {
|
|
|
272
271
|
f.print(` if (ms < Date.parse("0001-01-01T00:00:00Z") || ms > Date.parse("9999-12-31T23:59:59Z")) {`);
|
|
273
272
|
f.print(" throw new Error(`cannot decode message ", message.typeName, " from JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);");
|
|
274
273
|
f.print(" }");
|
|
275
|
-
|
|
274
|
+
if (ref.seconds.longType === protobuf_1.LongType.STRING) {
|
|
275
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(ms / 1000).toString();");
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(ms / 1000);");
|
|
279
|
+
}
|
|
276
280
|
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = 0;");
|
|
277
281
|
f.print(" if (matches[7]) {");
|
|
278
282
|
f.print(` this.`, (0, ecmascript_1.localName)(ref.nanos), ` = (parseInt("1" + matches[7] + "0".repeat(9 - matches[7].length)) - 1000000000);`);
|
|
@@ -320,7 +324,12 @@ function generateWktMethods(schema, f, message) {
|
|
|
320
324
|
f.print(" if (longSeconds > 315576000000 || longSeconds < -315576000000) {");
|
|
321
325
|
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);");
|
|
322
326
|
f.print(" }");
|
|
323
|
-
|
|
327
|
+
if (ref.seconds.longType === protobuf_1.LongType.STRING) {
|
|
328
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(longSeconds).toString();");
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(longSeconds);");
|
|
332
|
+
}
|
|
324
333
|
f.print(` if (typeof match[2] == "string") {`);
|
|
325
334
|
f.print(` const nanosStr = match[2] + "0".repeat(9 - match[2].length);`);
|
|
326
335
|
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = parseInt(nanosStr);");
|
|
@@ -345,7 +354,7 @@ function generateWktMethods(schema, f, message) {
|
|
|
345
354
|
f.print(" nanosStr = nanosStr.substring(0, 6);");
|
|
346
355
|
f.print(` }`);
|
|
347
356
|
f.print(` text += "." + nanosStr;`);
|
|
348
|
-
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " < 0 && this.", (0, ecmascript_1.localName)(ref.seconds), "
|
|
357
|
+
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " < 0 && Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") == 0) {");
|
|
349
358
|
f.print(` text = "-" + text;`);
|
|
350
359
|
f.print(` }`);
|
|
351
360
|
f.print(" }");
|
|
@@ -516,7 +525,12 @@ function generateWktMethods(schema, f, message) {
|
|
|
516
525
|
f.print();
|
|
517
526
|
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {");
|
|
518
527
|
f.print(" try {");
|
|
519
|
-
|
|
528
|
+
if (ref.value.longType === protobuf_1.LongType.STRING) {
|
|
529
|
+
f.print(" this.value = ", protoN, ".json.readScalar(", rtScalarType, ".", protobuf_1.ScalarType[ref.value.scalar], ", json, ", rtLongType, ".", protobuf_1.LongType[ref.value.longType], ");");
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
f.print(" this.value = ", protoN, ".json.readScalar(", rtScalarType, ".", protobuf_1.ScalarType[ref.value.scalar], ", json);");
|
|
533
|
+
}
|
|
520
534
|
f.print(" } catch (e) {");
|
|
521
535
|
f.print(" let m = `cannot decode message ", message.typeName, " from JSON\"`;");
|
|
522
536
|
f.print(" if (e instanceof Error && e.message.length > 0) {");
|
|
@@ -554,7 +568,12 @@ function generateWktStaticMethods(schema, f, message) {
|
|
|
554
568
|
f.print(" static fromDate(date: Date): ", message, " {");
|
|
555
569
|
f.print(" const ms = date.getTime();");
|
|
556
570
|
f.print(" return new ", message, "({");
|
|
557
|
-
|
|
571
|
+
if (ref.seconds.longType === protobuf_1.LongType.STRING) {
|
|
572
|
+
f.print(" ", (0, ecmascript_1.localName)(ref.seconds), ": ", protoInt64, ".parse(Math.floor(ms / 1000)).toString(),");
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
f.print(" ", (0, ecmascript_1.localName)(ref.seconds), ": ", protoInt64, ".parse(Math.floor(ms / 1000)),");
|
|
576
|
+
}
|
|
558
577
|
f.print(" ", (0, ecmascript_1.localName)(ref.nanos), ": (ms % 1000) * 1000000,");
|
|
559
578
|
f.print(" });");
|
|
560
579
|
f.print(" }");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.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": "1.
|
|
23
|
+
"@bufbuild/protoplugin": "1.4.0"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@bufbuild/protobuf": "1.
|
|
26
|
+
"@bufbuild/protobuf": "1.4.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependenciesMeta": {
|
|
29
29
|
"@bufbuild/protobuf": {
|
package/src/javascript.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
15
|
import type { DescEnum, DescField, DescMessage } from "@bufbuild/protobuf";
|
|
16
|
-
import { proto2, proto3, ScalarType } from "@bufbuild/protobuf";
|
|
16
|
+
import { LongType, proto2, proto3, ScalarType } from "@bufbuild/protobuf";
|
|
17
17
|
import type {
|
|
18
18
|
GeneratedFile,
|
|
19
19
|
Printable,
|
|
@@ -109,6 +109,9 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
|
|
|
109
109
|
switch (field.fieldKind) {
|
|
110
110
|
case "scalar":
|
|
111
111
|
e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, ScalarType[field.scalar], ` */, `);
|
|
112
|
+
if (field.longType != LongType.BIGINT) {
|
|
113
|
+
e.push(`L: `, field.longType, ` /* LongType.`, LongType[field.longType], ` */, `);
|
|
114
|
+
}
|
|
112
115
|
break;
|
|
113
116
|
case "map":
|
|
114
117
|
e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, ScalarType[field.mapKey], ` */, `);
|
package/src/typescript.ts
CHANGED
|
@@ -18,21 +18,21 @@ import type {
|
|
|
18
18
|
DescMessage,
|
|
19
19
|
DescOneof,
|
|
20
20
|
} from "@bufbuild/protobuf";
|
|
21
|
-
import { ScalarType } from "@bufbuild/protobuf";
|
|
21
|
+
import { LongType, ScalarType } from "@bufbuild/protobuf";
|
|
22
22
|
import type {
|
|
23
23
|
GeneratedFile,
|
|
24
24
|
Printable,
|
|
25
25
|
Schema,
|
|
26
26
|
} from "@bufbuild/protoplugin/ecmascript";
|
|
27
27
|
import {
|
|
28
|
-
localName,
|
|
29
28
|
getFieldIntrinsicDefaultValue,
|
|
30
29
|
getFieldTyping,
|
|
30
|
+
literalString,
|
|
31
|
+
localName,
|
|
31
32
|
makeJsDoc,
|
|
32
33
|
reifyWkt,
|
|
33
34
|
} from "@bufbuild/protoplugin/ecmascript";
|
|
34
35
|
import { generateFieldInfo } from "./javascript.js";
|
|
35
|
-
import { literalString } from "@bufbuild/protoplugin/ecmascript";
|
|
36
36
|
|
|
37
37
|
export function generateTs(schema: Schema) {
|
|
38
38
|
for (const file of schema.files) {
|
|
@@ -188,6 +188,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
|
|
|
188
188
|
MessageType,
|
|
189
189
|
IMessageTypeRegistry,
|
|
190
190
|
ScalarType: rtScalarType,
|
|
191
|
+
LongType: rtLongType,
|
|
191
192
|
protoInt64,
|
|
192
193
|
} = schema.runtime;
|
|
193
194
|
const protoN = schema.runtime[message.file.syntax];
|
|
@@ -309,7 +310,11 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
|
|
|
309
310
|
f.print(` if (ms < Date.parse("0001-01-01T00:00:00Z") || ms > Date.parse("9999-12-31T23:59:59Z")) {`);
|
|
310
311
|
f.print(" throw new Error(`cannot decode message ", message.typeName, " from JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);");
|
|
311
312
|
f.print(" }");
|
|
312
|
-
|
|
313
|
+
if (ref.seconds.longType === LongType.STRING) {
|
|
314
|
+
f.print(" this.", localName(ref.seconds), " = ", protoInt64, ".parse(ms / 1000).toString();");
|
|
315
|
+
} else {
|
|
316
|
+
f.print(" this.", localName(ref.seconds), " = ", protoInt64, ".parse(ms / 1000);");
|
|
317
|
+
}
|
|
313
318
|
f.print(" this.", localName(ref.nanos), " = 0;");
|
|
314
319
|
f.print(" if (matches[7]) {");
|
|
315
320
|
f.print(` this.`, localName(ref.nanos), ` = (parseInt("1" + matches[7] + "0".repeat(9 - matches[7].length)) - 1000000000);` );
|
|
@@ -357,7 +362,11 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
|
|
|
357
362
|
f.print(" if (longSeconds > 315576000000 || longSeconds < -315576000000) {")
|
|
358
363
|
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);")
|
|
359
364
|
f.print(" }")
|
|
360
|
-
|
|
365
|
+
if (ref.seconds.longType === LongType.STRING) {
|
|
366
|
+
f.print(" this.", localName(ref.seconds), " = ", protoInt64, ".parse(longSeconds).toString();")
|
|
367
|
+
} else {
|
|
368
|
+
f.print(" this.", localName(ref.seconds), " = ", protoInt64, ".parse(longSeconds);")
|
|
369
|
+
}
|
|
361
370
|
f.print(` if (typeof match[2] == "string") {`)
|
|
362
371
|
f.print(` const nanosStr = match[2] + "0".repeat(9 - match[2].length);`)
|
|
363
372
|
f.print(" this.", localName(ref.nanos), " = parseInt(nanosStr);")
|
|
@@ -382,7 +391,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
|
|
|
382
391
|
f.print(" nanosStr = nanosStr.substring(0, 6);")
|
|
383
392
|
f.print(` }`)
|
|
384
393
|
f.print(` text += "." + nanosStr;`)
|
|
385
|
-
f.print(" if (this.", localName(ref.nanos), " < 0 && this.", localName(ref.seconds), "
|
|
394
|
+
f.print(" if (this.", localName(ref.nanos), " < 0 && Number(this.", localName(ref.seconds), ") == 0) {");
|
|
386
395
|
f.print(` text = "-" + text;`);
|
|
387
396
|
f.print(` }`);
|
|
388
397
|
f.print(" }")
|
|
@@ -553,7 +562,11 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
|
|
|
553
562
|
f.print()
|
|
554
563
|
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {")
|
|
555
564
|
f.print(" try {")
|
|
556
|
-
|
|
565
|
+
if (ref.value.longType === LongType.STRING) {
|
|
566
|
+
f.print(" this.value = ", protoN, ".json.readScalar(", rtScalarType, ".", ScalarType[ref.value.scalar], ", json, ", rtLongType, ".", LongType[ref.value.longType] ,");")
|
|
567
|
+
} else {
|
|
568
|
+
f.print(" this.value = ", protoN, ".json.readScalar(", rtScalarType, ".", ScalarType[ref.value.scalar], ", json);")
|
|
569
|
+
}
|
|
557
570
|
f.print(" } catch (e) {")
|
|
558
571
|
f.print(" let m = `cannot decode message ", message.typeName, " from JSON\"`;")
|
|
559
572
|
f.print(" if (e instanceof Error && e.message.length > 0) {")
|
|
@@ -594,7 +607,11 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
|
|
|
594
607
|
f.print(" static fromDate(date: Date): ", message, " {")
|
|
595
608
|
f.print(" const ms = date.getTime();")
|
|
596
609
|
f.print(" return new ", message, "({")
|
|
597
|
-
|
|
610
|
+
if (ref.seconds.longType === LongType.STRING) {
|
|
611
|
+
f.print(" ", localName(ref.seconds), ": ", protoInt64, ".parse(Math.floor(ms / 1000)).toString(),")
|
|
612
|
+
} else {
|
|
613
|
+
f.print(" ", localName(ref.seconds), ": ", protoInt64, ".parse(Math.floor(ms / 1000)),")
|
|
614
|
+
}
|
|
598
615
|
f.print(" ", localName(ref.nanos), ": (ms % 1000) * 1000000,")
|
|
599
616
|
f.print(" });")
|
|
600
617
|
f.print(" }")
|