@bufbuild/protoc-gen-es 0.0.6 → 0.0.9
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 +30 -30
- package/bin/protoc-gen-es +3 -92
- package/dist/cjs/package.json +33 -0
- package/dist/cjs/src/declaration.js +194 -0
- package/dist/cjs/src/javascript.js +550 -0
- package/dist/cjs/src/match-wkt.js +159 -0
- package/dist/cjs/src/protoc-gen-es-plugin.js +40 -0
- package/dist/cjs/src/typescript.js +552 -0
- package/package.json +15 -19
- package/src/declaration.ts +221 -0
- package/src/javascript.ts +562 -0
- package/src/match-wkt.ts +285 -0
- package/src/protoc-gen-es-plugin.ts +44 -0
- package/src/typescript.ts +587 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,550 @@
|
|
|
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.generateFieldInfo = exports.javascript = void 0;
|
|
17
|
+
const protobuf_1 = require("@bufbuild/protobuf");
|
|
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
|
+
};
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
function generateEnum(schema, f, enumeration) {
|
|
28
|
+
const protoN = schema.runtime[enumeration.file.syntax];
|
|
29
|
+
f.print((0, ecmascript_1.makeJsDoc)(enumeration));
|
|
30
|
+
f.print("export const ", enumeration, " = ", protoN, ".makeEnum(");
|
|
31
|
+
f.print(` "`, enumeration.typeName, `",`);
|
|
32
|
+
f.print(` [`);
|
|
33
|
+
if (enumeration.sharedPrefix === undefined) {
|
|
34
|
+
for (const value of enumeration.values) {
|
|
35
|
+
f.print(" {no: ", value.number, ", name: ", (0, ecmascript_1.literalString)(value.name), "},");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
for (const value of enumeration.values) {
|
|
40
|
+
const localName = value.name.substring(enumeration.sharedPrefix.length);
|
|
41
|
+
f.print(" {no: ", value.number, ", name: ", (0, ecmascript_1.literalString)(value.name), ", localName: ", (0, ecmascript_1.literalString)(localName), "},");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
f.print(` ],`);
|
|
45
|
+
f.print(");");
|
|
46
|
+
f.print();
|
|
47
|
+
}
|
|
48
|
+
// prettier-ignore
|
|
49
|
+
function generateMessage(schema, f, message) {
|
|
50
|
+
const protoN = schema.runtime[message.file.syntax];
|
|
51
|
+
f.print((0, ecmascript_1.makeJsDoc)(message));
|
|
52
|
+
f.print("export const ", message, " = ", protoN, ".makeMessageType(");
|
|
53
|
+
f.print(` `, (0, ecmascript_1.literalString)(message.typeName), `,`);
|
|
54
|
+
if (message.fields.length == 0) {
|
|
55
|
+
f.print(" [],");
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
f.print(" () => [");
|
|
59
|
+
for (const field of message.fields) {
|
|
60
|
+
generateFieldInfo(schema, f, field);
|
|
61
|
+
}
|
|
62
|
+
f.print(" ],");
|
|
63
|
+
}
|
|
64
|
+
const needsLocalName = (0, ecmascript_1.localName)(message) !==
|
|
65
|
+
(message.file.syntax == "proto3" ? protobuf_1.proto2 : protobuf_1.proto3)
|
|
66
|
+
.makeMessageType(message.typeName, []).name;
|
|
67
|
+
if (needsLocalName) {
|
|
68
|
+
// local name is not inferrable from the type name, we need to provide it
|
|
69
|
+
f.print(` {localName: `, (0, ecmascript_1.literalString)((0, ecmascript_1.localName)(message)), `},`);
|
|
70
|
+
}
|
|
71
|
+
f.print(");");
|
|
72
|
+
f.print();
|
|
73
|
+
generateWktMethods(schema, f, message);
|
|
74
|
+
generateWktStaticMethods(schema, f, message);
|
|
75
|
+
for (const nestedEnum of message.nestedEnums) {
|
|
76
|
+
generateEnum(schema, f, nestedEnum);
|
|
77
|
+
}
|
|
78
|
+
for (const nestedMessage of message.nestedMessages) {
|
|
79
|
+
generateMessage(schema, f, nestedMessage);
|
|
80
|
+
}
|
|
81
|
+
// We do not support extensions at this time
|
|
82
|
+
}
|
|
83
|
+
// prettier-ignore
|
|
84
|
+
function generateFieldInfo(schema, f, field) {
|
|
85
|
+
const protoN = schema.runtime[field.parent.file.syntax];
|
|
86
|
+
const e = [];
|
|
87
|
+
e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
|
|
88
|
+
if (field.jsonName !== undefined) {
|
|
89
|
+
e.push(`jsonName: "`, field.jsonName, `", `);
|
|
90
|
+
}
|
|
91
|
+
switch (field.kind) {
|
|
92
|
+
case "scalar_field":
|
|
93
|
+
e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
|
|
94
|
+
break;
|
|
95
|
+
case "map_field":
|
|
96
|
+
e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
|
|
97
|
+
switch (field.mapValue.kind) {
|
|
98
|
+
case "scalar":
|
|
99
|
+
e.push(`V: {kind: "scalar", T: `, field.mapValue.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapValue.scalar], ` */}, `);
|
|
100
|
+
break;
|
|
101
|
+
case "message":
|
|
102
|
+
e.push(`V: {kind: "message", T: `, field.mapValue.message, `}, `);
|
|
103
|
+
break;
|
|
104
|
+
case "enum":
|
|
105
|
+
e.push(`V: {kind: "enum", T: `, protoN, `.getEnumType(`, field.mapValue.enum, `)}, `);
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
case "message_field":
|
|
110
|
+
e.push(`kind: "message", T: `, field.message, `, `);
|
|
111
|
+
break;
|
|
112
|
+
case "enum_field":
|
|
113
|
+
e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
if (field.repeated) {
|
|
117
|
+
e.push(`repeated: true, `);
|
|
118
|
+
if (field.packed !== field.packedByDefault) {
|
|
119
|
+
e.push(`packed: `, field.packed, `, `);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (field.optional) {
|
|
123
|
+
e.push(`opt: true, `);
|
|
124
|
+
}
|
|
125
|
+
const defaultValue = (0, ecmascript_1.getFieldExplicitDefaultValue)(field, schema.runtime.protoInt64);
|
|
126
|
+
if (defaultValue !== undefined) {
|
|
127
|
+
e.push(`default: `, defaultValue, `, `);
|
|
128
|
+
}
|
|
129
|
+
if (field.oneof) {
|
|
130
|
+
e.push(`oneof: "`, field.oneof.name, `", `);
|
|
131
|
+
}
|
|
132
|
+
const lastE = e[e.length - 1];
|
|
133
|
+
if (typeof lastE == "string" && lastE.endsWith(", ")) {
|
|
134
|
+
e[e.length - 1] = lastE.substring(0, lastE.length - 2);
|
|
135
|
+
}
|
|
136
|
+
e.push(" },");
|
|
137
|
+
f.print(...e);
|
|
138
|
+
}
|
|
139
|
+
exports.generateFieldInfo = generateFieldInfo;
|
|
140
|
+
// prettier-ignore
|
|
141
|
+
function generateWktMethods(schema, f, message) {
|
|
142
|
+
var _a;
|
|
143
|
+
const ref = (0, match_wkt_js_1.matchWkt)(message);
|
|
144
|
+
if (ref === undefined) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const { ScalarType: rtScalarType, protoInt64, } = schema.runtime;
|
|
148
|
+
const protoN = schema.runtime[message.file.syntax];
|
|
149
|
+
switch (ref.typeName) {
|
|
150
|
+
case "google.protobuf.Any":
|
|
151
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
152
|
+
f.print(` if (this.`, (0, ecmascript_1.localName)(ref.typeUrl), ` === "") {`);
|
|
153
|
+
f.print(" return {};");
|
|
154
|
+
f.print(" }");
|
|
155
|
+
f.print(" const typeName = this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), ");");
|
|
156
|
+
f.print(" const messageType = options?.typeRegistry?.findMessage(typeName);");
|
|
157
|
+
f.print(" if (!messageType) {");
|
|
158
|
+
f.print(" throw new Error(`cannot encode message ", message.typeName, ' to JSON: "${this.', (0, ecmascript_1.localName)(ref.typeUrl), '}" is not in the type registry`);');
|
|
159
|
+
f.print(" }");
|
|
160
|
+
f.print(" const message = messageType.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
|
|
161
|
+
f.print(" let json = message.toJson(options);");
|
|
162
|
+
f.print(` if (typeName.startsWith("google.protobuf.") || (json === null || Array.isArray(json) || typeof json !== "object")) {`);
|
|
163
|
+
f.print(" json = {value: json};");
|
|
164
|
+
f.print(" }");
|
|
165
|
+
f.print(` json["@type"] = this.`, (0, ecmascript_1.localName)(ref.typeUrl), `;`);
|
|
166
|
+
f.print(" return json;");
|
|
167
|
+
f.print("};");
|
|
168
|
+
f.print();
|
|
169
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
170
|
+
f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
|
|
171
|
+
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
|
+
f.print(" }");
|
|
173
|
+
f.print(` const typeUrl = json["@type"];`);
|
|
174
|
+
f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
|
|
175
|
+
f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
|
|
176
|
+
f.print(" }");
|
|
177
|
+
f.print(" const typeName = this.typeUrlToName(typeUrl), messageType = options?.typeRegistry?.findMessage(typeName);");
|
|
178
|
+
f.print(" if (!messageType) {");
|
|
179
|
+
f.print(" throw new Error(`cannot decode message ", message.typeName, " from JSON: ${typeUrl} is not in the type registry`);");
|
|
180
|
+
f.print(" }");
|
|
181
|
+
f.print(" let message;");
|
|
182
|
+
f.print(` if (typeName.startsWith("google.protobuf.") && Object.prototype.hasOwnProperty.call(json, "value")) {`);
|
|
183
|
+
f.print(` message = messageType.fromJson(json["value"], options);`);
|
|
184
|
+
f.print(" } else {");
|
|
185
|
+
f.print(" const copy = Object.assign({}, json);");
|
|
186
|
+
f.print(` delete copy["@type"];`);
|
|
187
|
+
f.print(" message = messageType.fromJson(copy, options);");
|
|
188
|
+
f.print(" }");
|
|
189
|
+
f.print(" this.packFrom(message);");
|
|
190
|
+
f.print(" return this;");
|
|
191
|
+
f.print("};");
|
|
192
|
+
f.print();
|
|
193
|
+
f.print(message, ".prototype.packFrom = function packFrom(message) {");
|
|
194
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.value), " = message.toBinary();");
|
|
195
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.typeUrl), " = this.typeNameToUrl(message.getType().typeName);");
|
|
196
|
+
f.print("};");
|
|
197
|
+
f.print();
|
|
198
|
+
f.print(message, ".prototype.unpackTo = function unpackTo(target) {");
|
|
199
|
+
f.print(" if (!this.is(target.getType())) {");
|
|
200
|
+
f.print(" return false;");
|
|
201
|
+
f.print(" }");
|
|
202
|
+
f.print(" target.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
|
|
203
|
+
f.print(" return true;");
|
|
204
|
+
f.print("};");
|
|
205
|
+
f.print();
|
|
206
|
+
f.print(message, ".prototype.is = function is(type) {");
|
|
207
|
+
f.print(" return this.", (0, ecmascript_1.localName)(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
|
|
208
|
+
f.print("};");
|
|
209
|
+
f.print();
|
|
210
|
+
f.print(message, ".prototype.typeNameToUrl = function typeNameToUrl(name) {");
|
|
211
|
+
f.print(" return `type.googleapis.com/${name}`;");
|
|
212
|
+
f.print("};");
|
|
213
|
+
f.print();
|
|
214
|
+
f.print(message, ".prototype.typeUrlToName = function typeUrlToName(url) {");
|
|
215
|
+
f.print(" if (!url.length) {");
|
|
216
|
+
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
217
|
+
f.print(" }");
|
|
218
|
+
f.print(` const slash = url.lastIndexOf("/");`);
|
|
219
|
+
f.print(" const name = slash > 0 ? url.substring(slash + 1) : url;");
|
|
220
|
+
f.print(" if (!name.length) {");
|
|
221
|
+
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
222
|
+
f.print(" }");
|
|
223
|
+
f.print(" return name;");
|
|
224
|
+
f.print("};");
|
|
225
|
+
f.print();
|
|
226
|
+
break;
|
|
227
|
+
case "google.protobuf.Timestamp":
|
|
228
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
229
|
+
f.print(` if (typeof json !== "string") {`);
|
|
230
|
+
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);");
|
|
231
|
+
f.print(" }");
|
|
232
|
+
f.print(` const matches = json.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|\\.([0-9]{3,9})Z|([+-][0-9][0-9]:[0-9][0-9]))$/);`);
|
|
233
|
+
f.print(" if (!matches) {");
|
|
234
|
+
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: invalid RFC 3339 string`);");
|
|
235
|
+
f.print(" }");
|
|
236
|
+
f.print(` const ms = Date.parse(matches[1] + "-" + matches[2] + "-" + matches[3] + "T" + matches[4] + ":" + matches[5] + ":" + matches[6] + (matches[8] ? matches[8] : "Z"));`);
|
|
237
|
+
f.print(" if (Number.isNaN(ms)) {");
|
|
238
|
+
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: invalid RFC 3339 string`);");
|
|
239
|
+
f.print(" }");
|
|
240
|
+
f.print(` if (ms < Date.parse("0001-01-01T00:00:00Z") || ms > Date.parse("9999-12-31T23:59:59Z")) {`);
|
|
241
|
+
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`);");
|
|
242
|
+
f.print(" }");
|
|
243
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(ms / 1000);");
|
|
244
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = 0;");
|
|
245
|
+
f.print(" if (matches[7]) {");
|
|
246
|
+
f.print(` this.`, (0, ecmascript_1.localName)(ref.nanos), ` = (parseInt("1" + matches[7] + "0".repeat(9 - matches[7].length)) - 1000000000);`);
|
|
247
|
+
f.print(" }");
|
|
248
|
+
f.print(" return this;");
|
|
249
|
+
f.print("};");
|
|
250
|
+
f.print();
|
|
251
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
252
|
+
f.print(" const ms = Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") * 1000;");
|
|
253
|
+
f.print(` if (ms < Date.parse("0001-01-01T00:00:00Z") || ms > Date.parse("9999-12-31T23:59:59Z")) {`);
|
|
254
|
+
f.print(" throw new Error(`cannot encode ", message.typeName, " to JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);");
|
|
255
|
+
f.print(" }");
|
|
256
|
+
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " < 0) {");
|
|
257
|
+
f.print(" throw new Error(`cannot encode ", message.typeName, " to JSON: nanos must not be negative`);");
|
|
258
|
+
f.print(" }");
|
|
259
|
+
f.print(` let z = "Z";`);
|
|
260
|
+
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " > 0) {");
|
|
261
|
+
f.print(" const nanosStr = (this.", (0, ecmascript_1.localName)(ref.nanos), " + 1000000000).toString().substring(1);");
|
|
262
|
+
f.print(` if (nanosStr.substring(3) === "000000") {`);
|
|
263
|
+
f.print(` z = "." + nanosStr.substring(0, 3) + "Z";`);
|
|
264
|
+
f.print(` } else if (nanosStr.substring(6) === "000") {`);
|
|
265
|
+
f.print(` z = "." + nanosStr.substring(0, 6) + "Z";`);
|
|
266
|
+
f.print(" } else {");
|
|
267
|
+
f.print(` z = "." + nanosStr + "Z";`);
|
|
268
|
+
f.print(" }");
|
|
269
|
+
f.print(" }");
|
|
270
|
+
f.print(` return new Date(ms).toISOString().replace(".000Z", z);`);
|
|
271
|
+
f.print("};");
|
|
272
|
+
f.print();
|
|
273
|
+
f.print(message, ".prototype.toDate = function toDate() {");
|
|
274
|
+
f.print(" return new Date(Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") * 1000 + Math.ceil(this.", (0, ecmascript_1.localName)(ref.nanos), " / 1000000));");
|
|
275
|
+
f.print("};");
|
|
276
|
+
f.print();
|
|
277
|
+
break;
|
|
278
|
+
case "google.protobuf.Duration":
|
|
279
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
280
|
+
f.print(` if (typeof json !== "string") {`);
|
|
281
|
+
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${proto3.json.debug(json)}`);");
|
|
282
|
+
f.print(" }");
|
|
283
|
+
f.print(` const match = json.match(/^(-?[0-9]+)(?:\\.([0-9]+))?s/);`);
|
|
284
|
+
f.print(" if (match === null) {");
|
|
285
|
+
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);");
|
|
286
|
+
f.print(" }");
|
|
287
|
+
f.print(" const longSeconds = Number(match[1]);");
|
|
288
|
+
f.print(" if (longSeconds > 315576000000 || longSeconds < -315576000000) {");
|
|
289
|
+
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);");
|
|
290
|
+
f.print(" }");
|
|
291
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(longSeconds);");
|
|
292
|
+
f.print(` if (typeof match[2] == "string") {`);
|
|
293
|
+
f.print(` const nanosStr = match[2] + "0".repeat(9 - match[2].length);`);
|
|
294
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = parseInt(nanosStr);");
|
|
295
|
+
f.print(" if (longSeconds < ", protoInt64, ".zero) {");
|
|
296
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = -this.", (0, ecmascript_1.localName)(ref.nanos), ";");
|
|
297
|
+
f.print(" }");
|
|
298
|
+
f.print(" }");
|
|
299
|
+
f.print(" return this;");
|
|
300
|
+
f.print("};");
|
|
301
|
+
f.print();
|
|
302
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
303
|
+
f.print(" if (Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") > 315576000000 || Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") < -315576000000) {");
|
|
304
|
+
f.print(" throw new Error(`cannot encode ", message.typeName, " to JSON: value out of range`);");
|
|
305
|
+
f.print(" }");
|
|
306
|
+
f.print(" let text = this.", (0, ecmascript_1.localName)(ref.seconds), ".toString();");
|
|
307
|
+
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " !== 0) {");
|
|
308
|
+
f.print(" let nanosStr = Math.abs(this.", (0, ecmascript_1.localName)(ref.nanos), ").toString();");
|
|
309
|
+
f.print(` nanosStr = "0".repeat(9 - nanosStr.length) + nanosStr;`);
|
|
310
|
+
f.print(` if (nanosStr.substring(3) === "000000") {`);
|
|
311
|
+
f.print(" nanosStr = nanosStr.substring(0, 3);");
|
|
312
|
+
f.print(` } else if (nanosStr.substring(6) === "000") {`);
|
|
313
|
+
f.print(" nanosStr = nanosStr.substring(0, 6);");
|
|
314
|
+
f.print(` }`);
|
|
315
|
+
f.print(` text += "." + nanosStr;`);
|
|
316
|
+
f.print(" }");
|
|
317
|
+
f.print(` return text + "s";`);
|
|
318
|
+
f.print("};");
|
|
319
|
+
f.print();
|
|
320
|
+
break;
|
|
321
|
+
case "google.protobuf.Struct":
|
|
322
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
323
|
+
f.print(" const json = {}");
|
|
324
|
+
f.print(" for (const [k, v] of Object.entries(this.", (0, ecmascript_1.localName)(ref.fields), ")) {");
|
|
325
|
+
f.print(" json[k] = v.toJson(options);");
|
|
326
|
+
f.print(" }");
|
|
327
|
+
f.print(" return json;");
|
|
328
|
+
f.print("};");
|
|
329
|
+
f.print();
|
|
330
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
331
|
+
f.print(` if (typeof json != "object" || json == null || Array.isArray(json)) {`);
|
|
332
|
+
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON " + `, protoN, `.json.debug(json));`);
|
|
333
|
+
f.print(" }");
|
|
334
|
+
f.print(" for (const [k, v] of Object.entries(json)) {");
|
|
335
|
+
f.print(" this.", (0, ecmascript_1.localName)(ref.fields), "[k] = ", (_a = ref.fields.mapValue.message) !== null && _a !== void 0 ? _a : "", ".fromJson(v);");
|
|
336
|
+
f.print(" }");
|
|
337
|
+
f.print(" return this;");
|
|
338
|
+
f.print("};");
|
|
339
|
+
f.print();
|
|
340
|
+
break;
|
|
341
|
+
case "google.protobuf.Value":
|
|
342
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
343
|
+
f.print(" switch (this.", (0, ecmascript_1.localName)(ref.kind), ".case) {");
|
|
344
|
+
f.print(` case "`, (0, ecmascript_1.localName)(ref.nullValue), `":`);
|
|
345
|
+
f.print(" return null;");
|
|
346
|
+
f.print(` case "`, (0, ecmascript_1.localName)(ref.boolValue), `":`);
|
|
347
|
+
f.print(` case "`, (0, ecmascript_1.localName)(ref.numberValue), `":`);
|
|
348
|
+
f.print(` case "`, (0, ecmascript_1.localName)(ref.stringValue), `":`);
|
|
349
|
+
f.print(" return this.", (0, ecmascript_1.localName)(ref.kind), ".value;");
|
|
350
|
+
f.print(` case "`, (0, ecmascript_1.localName)(ref.structValue), `":`);
|
|
351
|
+
f.print(` case "`, (0, ecmascript_1.localName)(ref.listValue), `":`);
|
|
352
|
+
f.print(` return this.`, (0, ecmascript_1.localName)(ref.kind), `.value.toJson({...options, emitDefaultValues: true});`);
|
|
353
|
+
f.print(" }");
|
|
354
|
+
f.print(` throw new Error("`, message.typeName, ` must have a value");`);
|
|
355
|
+
f.print("};");
|
|
356
|
+
f.print();
|
|
357
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
358
|
+
f.print(" switch (typeof json) {");
|
|
359
|
+
f.print(` case "number":`);
|
|
360
|
+
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.numberValue), `", value: json };`);
|
|
361
|
+
f.print(" break;");
|
|
362
|
+
f.print(` case "string":`);
|
|
363
|
+
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.stringValue), `", value: json };`);
|
|
364
|
+
f.print(" break;");
|
|
365
|
+
f.print(` case "boolean":`);
|
|
366
|
+
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.boolValue), `", value: json };`);
|
|
367
|
+
f.print(" break;");
|
|
368
|
+
f.print(` case "object":`);
|
|
369
|
+
f.print(" if (json === null) {");
|
|
370
|
+
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.nullValue), `", value: `, ref.nullValue.enum, `.`, (0, ecmascript_1.localName)(ref.nullValue.enum.values[0]), ` };`);
|
|
371
|
+
f.print(" } else if (Array.isArray(json)) {");
|
|
372
|
+
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.listValue), `", value: `, ref.listValue.message, `.fromJson(json) };`);
|
|
373
|
+
f.print(" } else {");
|
|
374
|
+
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.structValue), `", value: `, ref.structValue.message, `.fromJson(json) };`);
|
|
375
|
+
f.print(" }");
|
|
376
|
+
f.print(" break;");
|
|
377
|
+
f.print(" default:");
|
|
378
|
+
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON " + `, protoN, `.json.debug(json));`);
|
|
379
|
+
f.print(" }");
|
|
380
|
+
f.print(" return this;");
|
|
381
|
+
f.print("};");
|
|
382
|
+
f.print();
|
|
383
|
+
break;
|
|
384
|
+
case "google.protobuf.ListValue":
|
|
385
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
386
|
+
f.print(` return this.`, (0, ecmascript_1.localName)(ref.values), `.map(v => v.toJson());`);
|
|
387
|
+
f.print(`}`);
|
|
388
|
+
f.print();
|
|
389
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
390
|
+
f.print(` if (!Array.isArray(json)) {`);
|
|
391
|
+
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON " + `, protoN, `.json.debug(json));`);
|
|
392
|
+
f.print(` }`);
|
|
393
|
+
f.print(` for (let e of json) {`);
|
|
394
|
+
f.print(` this.`, (0, ecmascript_1.localName)(ref.values), `.push(`, ref.values.message, `.fromJson(e));`);
|
|
395
|
+
f.print(` }`);
|
|
396
|
+
f.print(` return this;`);
|
|
397
|
+
f.print("};");
|
|
398
|
+
f.print();
|
|
399
|
+
break;
|
|
400
|
+
case "google.protobuf.FieldMask":
|
|
401
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
402
|
+
f.print(` // Converts snake_case to protoCamelCase according to the convention`);
|
|
403
|
+
f.print(` // used by protoc to convert a field name to a JSON name.`);
|
|
404
|
+
f.print(` function protoCamelCase(snakeCase) {`);
|
|
405
|
+
f.print(` let capNext = false;`);
|
|
406
|
+
f.print(` const b = [];`);
|
|
407
|
+
f.print(` for (let i = 0; i < snakeCase.length; i++) {`);
|
|
408
|
+
f.print(` let c = snakeCase.charAt(i);`);
|
|
409
|
+
f.print(` switch (c) {`);
|
|
410
|
+
f.print(` case '_':`);
|
|
411
|
+
f.print(` capNext = true;`);
|
|
412
|
+
f.print(` break;`);
|
|
413
|
+
f.print(` case '0':`);
|
|
414
|
+
f.print(` case '1':`);
|
|
415
|
+
f.print(` case '2':`);
|
|
416
|
+
f.print(` case '3':`);
|
|
417
|
+
f.print(` case '4':`);
|
|
418
|
+
f.print(` case '5':`);
|
|
419
|
+
f.print(` case '6':`);
|
|
420
|
+
f.print(` case '7':`);
|
|
421
|
+
f.print(` case '8':`);
|
|
422
|
+
f.print(` case '9':`);
|
|
423
|
+
f.print(` b.push(c);`);
|
|
424
|
+
f.print(` capNext = false;`);
|
|
425
|
+
f.print(` break;`);
|
|
426
|
+
f.print(` default:`);
|
|
427
|
+
f.print(` if (capNext) {`);
|
|
428
|
+
f.print(` capNext = false;`);
|
|
429
|
+
f.print(` c = c.toUpperCase();`);
|
|
430
|
+
f.print(` }`);
|
|
431
|
+
f.print(` b.push(c);`);
|
|
432
|
+
f.print(` break;`);
|
|
433
|
+
f.print(` }`);
|
|
434
|
+
f.print(` }`);
|
|
435
|
+
f.print(` return b.join('');`);
|
|
436
|
+
f.print(` }`);
|
|
437
|
+
f.print(` return this.`, (0, ecmascript_1.localName)(ref.paths), `.map(p => {`);
|
|
438
|
+
f.print(` if (p.match(/_[0-9]?_/g) || p.match(/[A-Z]/g)) {`);
|
|
439
|
+
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON: lowerCamelCase of path name \\"" + p + "\\" is irreversible");`);
|
|
440
|
+
f.print(` }`);
|
|
441
|
+
f.print(` return protoCamelCase(p);`);
|
|
442
|
+
f.print(` }).join(",");`);
|
|
443
|
+
f.print("};");
|
|
444
|
+
f.print();
|
|
445
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
446
|
+
f.print(` if (typeof json !== "string") {`);
|
|
447
|
+
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON: " + proto3.json.debug(json));`);
|
|
448
|
+
f.print(` }`);
|
|
449
|
+
f.print(` if (json === "") {`);
|
|
450
|
+
f.print(` return this;`);
|
|
451
|
+
f.print(` }`);
|
|
452
|
+
f.print(` function camelToSnake (str) {`);
|
|
453
|
+
f.print(` if (str.includes("_")) {`);
|
|
454
|
+
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON: path names must be lowerCamelCase");`);
|
|
455
|
+
f.print(` }`);
|
|
456
|
+
f.print(` const sc = str.replace(/[A-Z]/g, letter => "_" + letter.toLowerCase());`);
|
|
457
|
+
f.print(` return (sc[0] === "_") ? sc.substring(1) : sc;`);
|
|
458
|
+
f.print(` }`);
|
|
459
|
+
f.print(` this.`, (0, ecmascript_1.localName)(ref.paths), ` = json.split(",").map(camelToSnake);`);
|
|
460
|
+
f.print(` return this;`);
|
|
461
|
+
f.print("};");
|
|
462
|
+
f.print();
|
|
463
|
+
break;
|
|
464
|
+
case "google.protobuf.DoubleValue":
|
|
465
|
+
case "google.protobuf.FloatValue":
|
|
466
|
+
case "google.protobuf.Int64Value":
|
|
467
|
+
case "google.protobuf.UInt64Value":
|
|
468
|
+
case "google.protobuf.Int32Value":
|
|
469
|
+
case "google.protobuf.UInt32Value":
|
|
470
|
+
case "google.protobuf.BoolValue":
|
|
471
|
+
case "google.protobuf.StringValue":
|
|
472
|
+
case "google.protobuf.BytesValue":
|
|
473
|
+
f.print(message, ".prototype.toJson = function toJson(options) {");
|
|
474
|
+
f.print(" return proto3.json.writeScalar(", rtScalarType, ".", protobuf_1.ScalarType[ref.value.scalar], ", this.value, true);");
|
|
475
|
+
f.print("}");
|
|
476
|
+
f.print();
|
|
477
|
+
f.print(message, ".prototype.fromJson = function fromJson(json, options) {");
|
|
478
|
+
f.print(" try {");
|
|
479
|
+
f.print(" this.value = ", protoN, ".json.readScalar(", rtScalarType, ".", protobuf_1.ScalarType[ref.value.scalar], ", json);");
|
|
480
|
+
f.print(" } catch (e) {");
|
|
481
|
+
f.print(" let m = `cannot decode message ", message.typeName, " from JSON\"`;");
|
|
482
|
+
f.print(" if (e instanceof Error && e.message.length > 0) {");
|
|
483
|
+
f.print(" m += `: ${e.message}`");
|
|
484
|
+
f.print(" }");
|
|
485
|
+
f.print(" throw new Error(m);");
|
|
486
|
+
f.print(" }");
|
|
487
|
+
f.print(" return this;");
|
|
488
|
+
f.print("};");
|
|
489
|
+
f.print();
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
// prettier-ignore
|
|
494
|
+
function generateWktStaticMethods(schema, f, message) {
|
|
495
|
+
const ref = (0, match_wkt_js_1.matchWkt)(message);
|
|
496
|
+
if (ref === undefined) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
const { protoInt64, } = schema.runtime;
|
|
500
|
+
switch (ref.typeName) {
|
|
501
|
+
case "google.protobuf.Any":
|
|
502
|
+
f.print(message, ".pack = function pack(message) {");
|
|
503
|
+
f.print(" const any = new ", message, "();");
|
|
504
|
+
f.print(" any.packFrom(message);");
|
|
505
|
+
f.print(" return any;");
|
|
506
|
+
f.print("};");
|
|
507
|
+
f.print();
|
|
508
|
+
break;
|
|
509
|
+
case "google.protobuf.Timestamp":
|
|
510
|
+
f.print(message, ".now = function now() {");
|
|
511
|
+
f.print(" return ", message, ".fromDate(new Date())");
|
|
512
|
+
f.print("};");
|
|
513
|
+
f.print();
|
|
514
|
+
f.print(message, ".fromDate = function fromDate(date) {");
|
|
515
|
+
f.print(" const ms = date.getTime();");
|
|
516
|
+
f.print(" return new ", message, "({");
|
|
517
|
+
f.print(" ", (0, ecmascript_1.localName)(ref.seconds), ": ", protoInt64, ".parse(Math.floor(ms / 1000)),");
|
|
518
|
+
f.print(" ", (0, ecmascript_1.localName)(ref.nanos), ": (ms % 1000) * 1000000,");
|
|
519
|
+
f.print(" });");
|
|
520
|
+
f.print("};");
|
|
521
|
+
f.print();
|
|
522
|
+
break;
|
|
523
|
+
case "google.protobuf.DoubleValue":
|
|
524
|
+
case "google.protobuf.FloatValue":
|
|
525
|
+
case "google.protobuf.Int64Value":
|
|
526
|
+
case "google.protobuf.UInt64Value":
|
|
527
|
+
case "google.protobuf.Int32Value":
|
|
528
|
+
case "google.protobuf.UInt32Value":
|
|
529
|
+
case "google.protobuf.BoolValue":
|
|
530
|
+
case "google.protobuf.StringValue":
|
|
531
|
+
case "google.protobuf.BytesValue": {
|
|
532
|
+
f.print(message, ".fieldWrapper = {");
|
|
533
|
+
f.print(" wrapField(value) {");
|
|
534
|
+
f.print(" return value instanceof ", message, " ? value : new ", message, "({value});");
|
|
535
|
+
f.print(" },");
|
|
536
|
+
f.print(" unwrapField(value) {");
|
|
537
|
+
f.print(" return value.", (0, ecmascript_1.localName)(ref.value), ";");
|
|
538
|
+
f.print(" }");
|
|
539
|
+
f.print("};");
|
|
540
|
+
f.print();
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
case "google.protobuf.Duration":
|
|
544
|
+
case "google.protobuf.Struct":
|
|
545
|
+
case "google.protobuf.Value":
|
|
546
|
+
case "google.protobuf.ListValue":
|
|
547
|
+
case "google.protobuf.FieldMask":
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
}
|