@bufbuild/protoc-gen-es 1.9.0 → 2.0.0-alpha.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 +16 -17
- package/dist/cjs/package.json +4 -4
- package/dist/cjs/src/protoc-gen-es-plugin.js +291 -6
- package/dist/cjs/src/util.js +81 -187
- package/package.json +4 -4
- package/src/protoc-gen-es-plugin.ts +312 -3
- package/src/util.ts +86 -225
- package/dist/cjs/src/declaration.js +0 -218
- package/dist/cjs/src/editions.js +0 -34
- package/dist/cjs/src/javascript.js +0 -625
- package/dist/cjs/src/typescript.js +0 -639
- package/src/declaration.ts +0 -250
- package/src/editions.ts +0 -38
- package/src/javascript.ts +0 -649
- package/src/typescript.ts +0 -673
|
@@ -1,639 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Copyright 2021-2024 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.generateTs = void 0;
|
|
17
|
-
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
-
const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
|
|
19
|
-
const javascript_js_1 = require("./javascript.js");
|
|
20
|
-
const editions_js_1 = require("./editions.js");
|
|
21
|
-
const util_js_1 = require("./util.js");
|
|
22
|
-
function generateTs(schema) {
|
|
23
|
-
for (const file of schema.files) {
|
|
24
|
-
const f = schema.generateFile(file.name + "_pb.ts");
|
|
25
|
-
f.preamble(file);
|
|
26
|
-
for (const enumeration of file.enums) {
|
|
27
|
-
generateEnum(schema, f, enumeration);
|
|
28
|
-
}
|
|
29
|
-
for (const message of file.messages) {
|
|
30
|
-
generateMessage(schema, f, message);
|
|
31
|
-
}
|
|
32
|
-
for (const extension of file.extensions) {
|
|
33
|
-
generateExtension(schema, f, extension);
|
|
34
|
-
}
|
|
35
|
-
// We do not generate anything for services
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
exports.generateTs = generateTs;
|
|
39
|
-
// prettier-ignore
|
|
40
|
-
function generateEnum(schema, f, enumeration) {
|
|
41
|
-
const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, enumeration.file);
|
|
42
|
-
f.print(f.jsDoc(enumeration));
|
|
43
|
-
f.print(f.exportDecl("enum", enumeration), " {");
|
|
44
|
-
for (const value of enumeration.values) {
|
|
45
|
-
if (enumeration.values.indexOf(value) > 0) {
|
|
46
|
-
f.print();
|
|
47
|
-
}
|
|
48
|
-
f.print(f.jsDoc(value, " "));
|
|
49
|
-
f.print(" ", (0, ecmascript_1.localName)(value), " = ", value.number, ",");
|
|
50
|
-
}
|
|
51
|
-
f.print("}");
|
|
52
|
-
f.print("// Retrieve enum metadata with: ", enumeration.file.syntax, ".getEnumType(", enumeration, ")");
|
|
53
|
-
f.print(protoN, `.util.setEnumType(`, enumeration, `, "`, enumeration.typeName, `", [`);
|
|
54
|
-
for (const value of enumeration.values) {
|
|
55
|
-
f.print(" { no: ", value.number, ', name: "', value.name, '" },');
|
|
56
|
-
}
|
|
57
|
-
f.print("]);");
|
|
58
|
-
f.print();
|
|
59
|
-
}
|
|
60
|
-
// prettier-ignore
|
|
61
|
-
function generateMessage(schema, f, message) {
|
|
62
|
-
const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
|
|
63
|
-
const { PartialMessage, FieldList, Message, PlainMessage, BinaryReadOptions, JsonReadOptions, JsonValue } = schema.runtime;
|
|
64
|
-
f.print(f.jsDoc(message));
|
|
65
|
-
f.print(f.exportDecl("class", message), " extends ", Message, "<", message, "> {");
|
|
66
|
-
for (const member of message.members) {
|
|
67
|
-
switch (member.kind) {
|
|
68
|
-
case "oneof":
|
|
69
|
-
generateOneof(schema, f, member);
|
|
70
|
-
break;
|
|
71
|
-
default:
|
|
72
|
-
generateField(schema, f, member);
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
f.print();
|
|
76
|
-
}
|
|
77
|
-
f.print(" constructor(data?: ", PartialMessage, "<", message, ">) {");
|
|
78
|
-
f.print(" super();");
|
|
79
|
-
f.print(" ", protoN, ".util.initPartial(data, this);");
|
|
80
|
-
f.print(" }");
|
|
81
|
-
f.print();
|
|
82
|
-
generateWktMethods(schema, f, message);
|
|
83
|
-
f.print(" static readonly runtime: typeof ", protoN, " = ", protoN, ";");
|
|
84
|
-
f.print(' static readonly typeName = ', f.string(message.typeName), ';');
|
|
85
|
-
f.print(" static readonly fields: ", FieldList, " = ", protoN, ".util.newFieldList(() => [");
|
|
86
|
-
for (const field of message.fields) {
|
|
87
|
-
(0, javascript_js_1.generateFieldInfo)(schema, f, field);
|
|
88
|
-
}
|
|
89
|
-
f.print(" ]);");
|
|
90
|
-
// In case we start supporting options, we have to surface them here
|
|
91
|
-
//f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
|
|
92
|
-
f.print();
|
|
93
|
-
generateWktStaticMethods(schema, f, message);
|
|
94
|
-
f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", message, " {");
|
|
95
|
-
f.print(" return new ", message, "().fromBinary(bytes, options);");
|
|
96
|
-
f.print(" }");
|
|
97
|
-
f.print();
|
|
98
|
-
f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", message, " {");
|
|
99
|
-
f.print(" return new ", message, "().fromJson(jsonValue, options);");
|
|
100
|
-
f.print(" }");
|
|
101
|
-
f.print();
|
|
102
|
-
f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", message, " {");
|
|
103
|
-
f.print(" return new ", message, "().fromJsonString(jsonString, options);");
|
|
104
|
-
f.print(" }");
|
|
105
|
-
f.print();
|
|
106
|
-
f.print(" static equals(a: ", message, " | ", PlainMessage, "<", message, "> | undefined, b: ", message, " | ", PlainMessage, "<", message, "> | undefined): boolean {");
|
|
107
|
-
f.print(" return ", protoN, ".util.equals(", message, ", a, b);");
|
|
108
|
-
f.print(" }");
|
|
109
|
-
f.print("}");
|
|
110
|
-
f.print();
|
|
111
|
-
for (const nestedEnum of message.nestedEnums) {
|
|
112
|
-
generateEnum(schema, f, nestedEnum);
|
|
113
|
-
}
|
|
114
|
-
for (const nestedMessage of message.nestedMessages) {
|
|
115
|
-
generateMessage(schema, f, nestedMessage);
|
|
116
|
-
}
|
|
117
|
-
for (const nestedExtension of message.nestedExtensions) {
|
|
118
|
-
generateExtension(schema, f, nestedExtension);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
// prettier-ignore
|
|
122
|
-
function generateOneof(schema, f, oneof) {
|
|
123
|
-
f.print(f.jsDoc(oneof, " "));
|
|
124
|
-
f.print(" ", (0, ecmascript_1.localName)(oneof), ": {");
|
|
125
|
-
for (const field of oneof.fields) {
|
|
126
|
-
if (oneof.fields.indexOf(field) > 0) {
|
|
127
|
-
f.print(` } | {`);
|
|
128
|
-
}
|
|
129
|
-
f.print(f.jsDoc(field, " "));
|
|
130
|
-
const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
131
|
-
f.print(` value: `, typing, `;`);
|
|
132
|
-
f.print(` case: "`, (0, ecmascript_1.localName)(field), `";`);
|
|
133
|
-
}
|
|
134
|
-
f.print(` } | { case: undefined; value?: undefined } = { case: undefined };`);
|
|
135
|
-
}
|
|
136
|
-
// prettier-ignore
|
|
137
|
-
function generateField(schema, f, field) {
|
|
138
|
-
f.print(f.jsDoc(field, " "));
|
|
139
|
-
const e = [];
|
|
140
|
-
const { typing, optional, typingInferrableFromZeroValue } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
141
|
-
if (optional) {
|
|
142
|
-
e.push(" ", (0, ecmascript_1.localName)(field), "?: ", typing, ";");
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
if (typingInferrableFromZeroValue) {
|
|
146
|
-
e.push(" ", (0, ecmascript_1.localName)(field));
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
e.push(" ", (0, ecmascript_1.localName)(field), ": ", typing);
|
|
150
|
-
}
|
|
151
|
-
const zeroValue = (0, util_js_1.getFieldZeroValueExpression)(field);
|
|
152
|
-
if (zeroValue !== undefined) {
|
|
153
|
-
e.push(" = ", zeroValue);
|
|
154
|
-
}
|
|
155
|
-
e.push(";");
|
|
156
|
-
}
|
|
157
|
-
f.print(e);
|
|
158
|
-
}
|
|
159
|
-
// prettier-ignore
|
|
160
|
-
function generateExtension(schema, f, ext) {
|
|
161
|
-
const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, ext.file);
|
|
162
|
-
const { typing } = (0, util_js_1.getFieldTypeInfo)(ext);
|
|
163
|
-
f.print(f.jsDoc(ext));
|
|
164
|
-
f.print(f.exportDecl("const", ext), " = ", protoN, ".makeExtension<", ext.extendee, ", ", typing, ">(");
|
|
165
|
-
f.print(" ", f.string(ext.typeName), ", ");
|
|
166
|
-
f.print(" ", ext.extendee, ", ");
|
|
167
|
-
if (ext.fieldKind == "scalar") {
|
|
168
|
-
f.print(" ", (0, javascript_js_1.getFieldInfoLiteral)(schema, ext), ",");
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
f.print(" () => (", (0, javascript_js_1.getFieldInfoLiteral)(schema, ext), "),");
|
|
172
|
-
}
|
|
173
|
-
f.print(");");
|
|
174
|
-
f.print();
|
|
175
|
-
}
|
|
176
|
-
// prettier-ignore
|
|
177
|
-
function generateWktMethods(schema, f, message) {
|
|
178
|
-
var _a;
|
|
179
|
-
const ref = (0, ecmascript_1.reifyWkt)(message);
|
|
180
|
-
if (ref === undefined) {
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, IMessageTypeRegistry, ScalarType: rtScalarType, LongType: rtLongType, protoInt64, } = schema.runtime;
|
|
184
|
-
const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
|
|
185
|
-
switch (ref.typeName) {
|
|
186
|
-
case "google.protobuf.Any":
|
|
187
|
-
f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): ", JsonValue, " {");
|
|
188
|
-
f.print(` if (this.`, (0, ecmascript_1.localName)(ref.typeUrl), ` === "") {`);
|
|
189
|
-
f.print(" return {};");
|
|
190
|
-
f.print(" }");
|
|
191
|
-
f.print(" const typeName = this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), ");");
|
|
192
|
-
f.print(" const messageType = options?.typeRegistry?.findMessage(typeName);");
|
|
193
|
-
f.print(" if (!messageType) {");
|
|
194
|
-
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`);');
|
|
195
|
-
f.print(" }");
|
|
196
|
-
f.print(" const message = messageType.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
|
|
197
|
-
f.print(" let json = message.toJson(options);");
|
|
198
|
-
f.print(` if (typeName.startsWith("google.protobuf.") || (json === null || Array.isArray(json) || typeof json !== "object")) {`);
|
|
199
|
-
f.print(" json = {value: json};");
|
|
200
|
-
f.print(" }");
|
|
201
|
-
f.print(` json["@type"] = this.`, (0, ecmascript_1.localName)(ref.typeUrl), `;`);
|
|
202
|
-
f.print(" return json;");
|
|
203
|
-
f.print(" }");
|
|
204
|
-
f.print();
|
|
205
|
-
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {");
|
|
206
|
-
f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
|
|
207
|
-
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}`);');
|
|
208
|
-
f.print(" }");
|
|
209
|
-
f.print(` if (Object.keys(json).length == 0) {`);
|
|
210
|
-
f.print(` return this;`);
|
|
211
|
-
f.print(` }`);
|
|
212
|
-
f.print(` const typeUrl = json["@type"];`);
|
|
213
|
-
f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
|
|
214
|
-
f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
|
|
215
|
-
f.print(" }");
|
|
216
|
-
f.print(" const typeName = this.typeUrlToName(typeUrl), messageType = options?.typeRegistry?.findMessage(typeName);");
|
|
217
|
-
f.print(" if (!messageType) {");
|
|
218
|
-
f.print(" throw new Error(`cannot decode message ", message.typeName, " from JSON: ${typeUrl} is not in the type registry`);");
|
|
219
|
-
f.print(" }");
|
|
220
|
-
f.print(" let message;");
|
|
221
|
-
f.print(` if (typeName.startsWith("google.protobuf.") && Object.prototype.hasOwnProperty.call(json, "value")) {`);
|
|
222
|
-
f.print(` message = messageType.fromJson(json["value"], options);`);
|
|
223
|
-
f.print(" } else {");
|
|
224
|
-
f.print(" const copy = Object.assign({}, json);");
|
|
225
|
-
f.print(` delete copy["@type"];`);
|
|
226
|
-
f.print(" message = messageType.fromJson(copy, options);");
|
|
227
|
-
f.print(" }");
|
|
228
|
-
f.print(" this.packFrom(message);");
|
|
229
|
-
f.print(" return this;");
|
|
230
|
-
f.print(" }");
|
|
231
|
-
f.print();
|
|
232
|
-
f.print(" packFrom(message: ", Message, "): void {");
|
|
233
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.value), " = message.toBinary();");
|
|
234
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.typeUrl), " = this.typeNameToUrl(message.getType().typeName);");
|
|
235
|
-
f.print(" }");
|
|
236
|
-
f.print();
|
|
237
|
-
f.print(" unpackTo(target: ", Message, "): boolean {");
|
|
238
|
-
f.print(" if (!this.is(target.getType())) {");
|
|
239
|
-
f.print(" return false;");
|
|
240
|
-
f.print(" }");
|
|
241
|
-
f.print(" target.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
|
|
242
|
-
f.print(" return true;");
|
|
243
|
-
f.print(" }");
|
|
244
|
-
f.print();
|
|
245
|
-
f.print(" unpack(registry: ", IMessageTypeRegistry, "): ", Message, " | undefined {");
|
|
246
|
-
f.print(" if (this.", (0, ecmascript_1.localName)(ref.typeUrl), ` === "") {`);
|
|
247
|
-
f.print(" return undefined;");
|
|
248
|
-
f.print(" }");
|
|
249
|
-
f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), "));");
|
|
250
|
-
f.print(" if (!messageType) {");
|
|
251
|
-
f.print(" return undefined;");
|
|
252
|
-
f.print(" }");
|
|
253
|
-
f.print(" return messageType.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
|
|
254
|
-
f.print(" }");
|
|
255
|
-
f.print();
|
|
256
|
-
f.print(" is(type: ", MessageType, " | string): boolean {");
|
|
257
|
-
f.print(" if (this.typeUrl === '') {");
|
|
258
|
-
f.print(" return false;");
|
|
259
|
-
f.print(" }");
|
|
260
|
-
f.print(" const name = this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), ");");
|
|
261
|
-
f.print(" let typeName = '';");
|
|
262
|
-
f.print(" if (typeof type === 'string') {");
|
|
263
|
-
f.print(" typeName = type;");
|
|
264
|
-
f.print(" } else {");
|
|
265
|
-
f.print(" typeName = type.typeName;");
|
|
266
|
-
f.print(" }");
|
|
267
|
-
f.print(" return name === typeName;");
|
|
268
|
-
f.print(" }");
|
|
269
|
-
f.print();
|
|
270
|
-
f.print(" private typeNameToUrl(name: string): string {");
|
|
271
|
-
f.print(" return `type.googleapis.com/${name}`;");
|
|
272
|
-
f.print(" }");
|
|
273
|
-
f.print();
|
|
274
|
-
f.print(" private typeUrlToName(url: string): string {");
|
|
275
|
-
f.print(" if (!url.length) {");
|
|
276
|
-
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
277
|
-
f.print(" }");
|
|
278
|
-
f.print(` const slash = url.lastIndexOf("/");`);
|
|
279
|
-
f.print(" const name = slash >= 0 ? url.substring(slash + 1) : url;");
|
|
280
|
-
f.print(" if (!name.length) {");
|
|
281
|
-
f.print(" throw new Error(`invalid type url: ${url}`);");
|
|
282
|
-
f.print(" }");
|
|
283
|
-
f.print(" return name;");
|
|
284
|
-
f.print(" }");
|
|
285
|
-
f.print();
|
|
286
|
-
break;
|
|
287
|
-
case "google.protobuf.Timestamp":
|
|
288
|
-
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {");
|
|
289
|
-
f.print(` if (typeof json !== "string") {`);
|
|
290
|
-
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);");
|
|
291
|
-
f.print(" }");
|
|
292
|
-
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]))$/);`);
|
|
293
|
-
f.print(" if (!matches) {");
|
|
294
|
-
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: invalid RFC 3339 string`);");
|
|
295
|
-
f.print(" }");
|
|
296
|
-
f.print(` const ms = Date.parse(matches[1] + "-" + matches[2] + "-" + matches[3] + "T" + matches[4] + ":" + matches[5] + ":" + matches[6] + (matches[8] ? matches[8] : "Z"));`);
|
|
297
|
-
f.print(" if (Number.isNaN(ms)) {");
|
|
298
|
-
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: invalid RFC 3339 string`);");
|
|
299
|
-
f.print(" }");
|
|
300
|
-
f.print(` if (ms < Date.parse("0001-01-01T00:00:00Z") || ms > Date.parse("9999-12-31T23:59:59Z")) {`);
|
|
301
|
-
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`);");
|
|
302
|
-
f.print(" }");
|
|
303
|
-
if (ref.seconds.longType === protobuf_1.LongType.STRING) {
|
|
304
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(ms / 1000).toString();");
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(ms / 1000);");
|
|
308
|
-
}
|
|
309
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = 0;");
|
|
310
|
-
f.print(" if (matches[7]) {");
|
|
311
|
-
f.print(` this.`, (0, ecmascript_1.localName)(ref.nanos), ` = (parseInt("1" + matches[7] + "0".repeat(9 - matches[7].length)) - 1000000000);`);
|
|
312
|
-
f.print(" }");
|
|
313
|
-
f.print(" return this;");
|
|
314
|
-
f.print(" }");
|
|
315
|
-
f.print();
|
|
316
|
-
f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): JsonValue {");
|
|
317
|
-
f.print(" const ms = Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") * 1000;");
|
|
318
|
-
f.print(` if (ms < Date.parse("0001-01-01T00:00:00Z") || ms > Date.parse("9999-12-31T23:59:59Z")) {`);
|
|
319
|
-
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`);");
|
|
320
|
-
f.print(" }");
|
|
321
|
-
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " < 0) {");
|
|
322
|
-
f.print(" throw new Error(`cannot encode ", message.typeName, " to JSON: nanos must not be negative`);");
|
|
323
|
-
f.print(" }");
|
|
324
|
-
f.print(` let z = "Z";`);
|
|
325
|
-
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " > 0) {");
|
|
326
|
-
f.print(" const nanosStr = (this.", (0, ecmascript_1.localName)(ref.nanos), " + 1000000000).toString().substring(1);");
|
|
327
|
-
f.print(` if (nanosStr.substring(3) === "000000") {`);
|
|
328
|
-
f.print(` z = "." + nanosStr.substring(0, 3) + "Z";`);
|
|
329
|
-
f.print(` } else if (nanosStr.substring(6) === "000") {`);
|
|
330
|
-
f.print(` z = "." + nanosStr.substring(0, 6) + "Z";`);
|
|
331
|
-
f.print(" } else {");
|
|
332
|
-
f.print(` z = "." + nanosStr + "Z";`);
|
|
333
|
-
f.print(" }");
|
|
334
|
-
f.print(" }");
|
|
335
|
-
f.print(` return new Date(ms).toISOString().replace(".000Z", z);`);
|
|
336
|
-
f.print(" }");
|
|
337
|
-
f.print();
|
|
338
|
-
f.print(" toDate(): Date {");
|
|
339
|
-
f.print(" return new Date(Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") * 1000 + Math.ceil(this.", (0, ecmascript_1.localName)(ref.nanos), " / 1000000));");
|
|
340
|
-
f.print(" }");
|
|
341
|
-
f.print();
|
|
342
|
-
break;
|
|
343
|
-
case "google.protobuf.Duration":
|
|
344
|
-
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {");
|
|
345
|
-
f.print(` if (typeof json !== "string") {`);
|
|
346
|
-
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${proto3.json.debug(json)}`);");
|
|
347
|
-
f.print(" }");
|
|
348
|
-
f.print(` const match = json.match(/^(-?[0-9]+)(?:\\.([0-9]+))?s/);`);
|
|
349
|
-
f.print(" if (match === null) {");
|
|
350
|
-
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);");
|
|
351
|
-
f.print(" }");
|
|
352
|
-
f.print(" const longSeconds = Number(match[1]);");
|
|
353
|
-
f.print(" if (longSeconds > 315576000000 || longSeconds < -315576000000) {");
|
|
354
|
-
f.print(" throw new Error(`cannot decode ", message.typeName, " from JSON: ${", protoN, ".json.debug(json)}`);");
|
|
355
|
-
f.print(" }");
|
|
356
|
-
if (ref.seconds.longType === protobuf_1.LongType.STRING) {
|
|
357
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(longSeconds).toString();");
|
|
358
|
-
}
|
|
359
|
-
else {
|
|
360
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.seconds), " = ", protoInt64, ".parse(longSeconds);");
|
|
361
|
-
}
|
|
362
|
-
f.print(` if (typeof match[2] == "string") {`);
|
|
363
|
-
f.print(` const nanosStr = match[2] + "0".repeat(9 - match[2].length);`);
|
|
364
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = parseInt(nanosStr);");
|
|
365
|
-
f.print(" if (longSeconds < 0 || Object.is(longSeconds, -0)) {");
|
|
366
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.nanos), " = -this.", (0, ecmascript_1.localName)(ref.nanos), ";");
|
|
367
|
-
f.print(" }");
|
|
368
|
-
f.print(" }");
|
|
369
|
-
f.print(" return this;");
|
|
370
|
-
f.print(" }");
|
|
371
|
-
f.print();
|
|
372
|
-
f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): JsonValue {");
|
|
373
|
-
f.print(" if (Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") > 315576000000 || Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") < -315576000000) {");
|
|
374
|
-
f.print(" throw new Error(`cannot encode ", message.typeName, " to JSON: value out of range`);");
|
|
375
|
-
f.print(" }");
|
|
376
|
-
f.print(" let text = this.", (0, ecmascript_1.localName)(ref.seconds), ".toString();");
|
|
377
|
-
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " !== 0) {");
|
|
378
|
-
f.print(" let nanosStr = Math.abs(this.", (0, ecmascript_1.localName)(ref.nanos), ").toString();");
|
|
379
|
-
f.print(` nanosStr = "0".repeat(9 - nanosStr.length) + nanosStr;`);
|
|
380
|
-
f.print(` if (nanosStr.substring(3) === "000000") {`);
|
|
381
|
-
f.print(" nanosStr = nanosStr.substring(0, 3);");
|
|
382
|
-
f.print(` } else if (nanosStr.substring(6) === "000") {`);
|
|
383
|
-
f.print(" nanosStr = nanosStr.substring(0, 6);");
|
|
384
|
-
f.print(` }`);
|
|
385
|
-
f.print(` text += "." + nanosStr;`);
|
|
386
|
-
f.print(" if (this.", (0, ecmascript_1.localName)(ref.nanos), " < 0 && Number(this.", (0, ecmascript_1.localName)(ref.seconds), ") == 0) {");
|
|
387
|
-
f.print(` text = "-" + text;`);
|
|
388
|
-
f.print(` }`);
|
|
389
|
-
f.print(" }");
|
|
390
|
-
f.print(` return text + "s";`);
|
|
391
|
-
f.print(" }");
|
|
392
|
-
f.print();
|
|
393
|
-
break;
|
|
394
|
-
case "google.protobuf.Struct":
|
|
395
|
-
f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): ", JsonValue, " {");
|
|
396
|
-
f.print(" const json: ", JsonObject, " = {}");
|
|
397
|
-
f.print(" for (const [k, v] of Object.entries(this.", (0, ecmascript_1.localName)(ref.fields), ")) {");
|
|
398
|
-
f.print(" json[k] = v.toJson(options);");
|
|
399
|
-
f.print(" }");
|
|
400
|
-
f.print(" return json;");
|
|
401
|
-
f.print(" }");
|
|
402
|
-
f.print();
|
|
403
|
-
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {");
|
|
404
|
-
f.print(` if (typeof json != "object" || json == null || Array.isArray(json)) {`);
|
|
405
|
-
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON " + `, protoN, `.json.debug(json));`);
|
|
406
|
-
f.print(" }");
|
|
407
|
-
f.print(" for (const [k, v] of Object.entries(json)) {");
|
|
408
|
-
f.print(" this.", (0, ecmascript_1.localName)(ref.fields), "[k] = ", (_a = ref.fields.mapValue.message) !== null && _a !== void 0 ? _a : "", ".fromJson(v);");
|
|
409
|
-
f.print(" }");
|
|
410
|
-
f.print(" return this;");
|
|
411
|
-
f.print(" }");
|
|
412
|
-
f.print();
|
|
413
|
-
break;
|
|
414
|
-
case "google.protobuf.Value":
|
|
415
|
-
f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): ", JsonValue, " {");
|
|
416
|
-
f.print(" switch (this.", (0, ecmascript_1.localName)(ref.kind), ".case) {");
|
|
417
|
-
f.print(` case "`, (0, ecmascript_1.localName)(ref.nullValue), `":`);
|
|
418
|
-
f.print(" return null;");
|
|
419
|
-
f.print(` case "`, (0, ecmascript_1.localName)(ref.numberValue), `":`);
|
|
420
|
-
f.print(` if (!Number.isFinite(this.`, (0, ecmascript_1.localName)(ref.kind), `.value)) {`);
|
|
421
|
-
f.print(` throw new Error("google.protobuf.Value cannot be NaN or Infinity");`);
|
|
422
|
-
f.print(` }`);
|
|
423
|
-
f.print(` return this.`, (0, ecmascript_1.localName)(ref.kind), `.value;`);
|
|
424
|
-
f.print(` case "`, (0, ecmascript_1.localName)(ref.boolValue), `":`);
|
|
425
|
-
f.print(` return this.`, (0, ecmascript_1.localName)(ref.kind), `.value;`);
|
|
426
|
-
f.print(` case "`, (0, ecmascript_1.localName)(ref.stringValue), `":`);
|
|
427
|
-
f.print(" return this.", (0, ecmascript_1.localName)(ref.kind), ".value;");
|
|
428
|
-
f.print(` case "`, (0, ecmascript_1.localName)(ref.structValue), `":`);
|
|
429
|
-
f.print(` case "`, (0, ecmascript_1.localName)(ref.listValue), `":`);
|
|
430
|
-
f.print(` return this.`, (0, ecmascript_1.localName)(ref.kind), `.value.toJson({...options, emitDefaultValues: true});`);
|
|
431
|
-
f.print(" }");
|
|
432
|
-
f.print(` throw new Error("`, message.typeName, ` must have a value");`);
|
|
433
|
-
f.print(" }");
|
|
434
|
-
f.print();
|
|
435
|
-
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {");
|
|
436
|
-
f.print(" switch (typeof json) {");
|
|
437
|
-
f.print(` case "number":`);
|
|
438
|
-
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.numberValue), `", value: json };`);
|
|
439
|
-
f.print(" break;");
|
|
440
|
-
f.print(` case "string":`);
|
|
441
|
-
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.stringValue), `", value: json };`);
|
|
442
|
-
f.print(" break;");
|
|
443
|
-
f.print(` case "boolean":`);
|
|
444
|
-
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.boolValue), `", value: json };`);
|
|
445
|
-
f.print(" break;");
|
|
446
|
-
f.print(` case "object":`);
|
|
447
|
-
f.print(" if (json === null) {");
|
|
448
|
-
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.nullValue), `", value: `, ref.nullValue.enum, `.`, (0, ecmascript_1.localName)(ref.nullValue.enum.values[0]), ` };`);
|
|
449
|
-
f.print(" } else if (Array.isArray(json)) {");
|
|
450
|
-
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.listValue), `", value: `, ref.listValue.message, `.fromJson(json) };`);
|
|
451
|
-
f.print(" } else {");
|
|
452
|
-
f.print(` this.kind = { case: "`, (0, ecmascript_1.localName)(ref.structValue), `", value: `, ref.structValue.message, `.fromJson(json) };`);
|
|
453
|
-
f.print(" }");
|
|
454
|
-
f.print(" break;");
|
|
455
|
-
f.print(" default:");
|
|
456
|
-
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON " + `, protoN, `.json.debug(json));`);
|
|
457
|
-
f.print(" }");
|
|
458
|
-
f.print(" return this;");
|
|
459
|
-
f.print(" }");
|
|
460
|
-
f.print();
|
|
461
|
-
break;
|
|
462
|
-
case "google.protobuf.ListValue":
|
|
463
|
-
f.print(` override toJson(options?: Partial<`, JsonWriteOptions, `>): `, JsonValue, ` {`);
|
|
464
|
-
f.print(` return this.`, (0, ecmascript_1.localName)(ref.values), `.map(v => v.toJson());`);
|
|
465
|
-
f.print(` }`);
|
|
466
|
-
f.print();
|
|
467
|
-
f.print(` override fromJson(json: `, JsonValue, `, options?: Partial<`, JsonReadOptions, `>): this {`);
|
|
468
|
-
f.print(` if (!Array.isArray(json)) {`);
|
|
469
|
-
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON " + `, protoN, `.json.debug(json));`);
|
|
470
|
-
f.print(` }`);
|
|
471
|
-
f.print(` for (let e of json) {`);
|
|
472
|
-
f.print(` this.`, (0, ecmascript_1.localName)(ref.values), `.push(`, ref.values.message, `.fromJson(e));`);
|
|
473
|
-
f.print(` }`);
|
|
474
|
-
f.print(` return this;`);
|
|
475
|
-
f.print(` }`);
|
|
476
|
-
f.print();
|
|
477
|
-
break;
|
|
478
|
-
case "google.protobuf.FieldMask":
|
|
479
|
-
f.print(` override toJson(options?: Partial<`, JsonWriteOptions, `>): `, JsonValue, ` {`);
|
|
480
|
-
f.print(` // Converts snake_case to protoCamelCase according to the convention`);
|
|
481
|
-
f.print(` // used by protoc to convert a field name to a JSON name.`);
|
|
482
|
-
f.print(` function protoCamelCase(snakeCase: string): string {`);
|
|
483
|
-
f.print(` let capNext = false;`);
|
|
484
|
-
f.print(` const b = [];`);
|
|
485
|
-
f.print(` for (let i = 0; i < snakeCase.length; i++) {`);
|
|
486
|
-
f.print(` let c = snakeCase.charAt(i);`);
|
|
487
|
-
f.print(` switch (c) {`);
|
|
488
|
-
f.print(` case '_':`);
|
|
489
|
-
f.print(` capNext = true;`);
|
|
490
|
-
f.print(` break;`);
|
|
491
|
-
f.print(` case '0':`);
|
|
492
|
-
f.print(` case '1':`);
|
|
493
|
-
f.print(` case '2':`);
|
|
494
|
-
f.print(` case '3':`);
|
|
495
|
-
f.print(` case '4':`);
|
|
496
|
-
f.print(` case '5':`);
|
|
497
|
-
f.print(` case '6':`);
|
|
498
|
-
f.print(` case '7':`);
|
|
499
|
-
f.print(` case '8':`);
|
|
500
|
-
f.print(` case '9':`);
|
|
501
|
-
f.print(` b.push(c);`);
|
|
502
|
-
f.print(` capNext = false;`);
|
|
503
|
-
f.print(` break;`);
|
|
504
|
-
f.print(` default:`);
|
|
505
|
-
f.print(` if (capNext) {`);
|
|
506
|
-
f.print(` capNext = false;`);
|
|
507
|
-
f.print(` c = c.toUpperCase();`);
|
|
508
|
-
f.print(` }`);
|
|
509
|
-
f.print(` b.push(c);`);
|
|
510
|
-
f.print(` break;`);
|
|
511
|
-
f.print(` }`);
|
|
512
|
-
f.print(` }`);
|
|
513
|
-
f.print(` return b.join('');`);
|
|
514
|
-
f.print(` }`);
|
|
515
|
-
f.print(` return this.`, (0, ecmascript_1.localName)(ref.paths), `.map(p => {`);
|
|
516
|
-
f.print(` if (p.match(/_[0-9]?_/g) || p.match(/[A-Z]/g)) {`);
|
|
517
|
-
f.print(` throw new Error("cannot encode `, message.typeName, ` to JSON: lowerCamelCase of path name \\"" + p + "\\" is irreversible");`);
|
|
518
|
-
f.print(` }`);
|
|
519
|
-
f.print(` return protoCamelCase(p);`);
|
|
520
|
-
f.print(` }).join(",");`);
|
|
521
|
-
f.print(` }`);
|
|
522
|
-
f.print();
|
|
523
|
-
f.print(` override fromJson(json: `, JsonValue, `, options?: Partial<`, JsonReadOptions, `>): this {`);
|
|
524
|
-
f.print(` if (typeof json !== "string") {`);
|
|
525
|
-
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON: " + proto3.json.debug(json));`);
|
|
526
|
-
f.print(` }`);
|
|
527
|
-
f.print(` if (json === "") {`);
|
|
528
|
-
f.print(` return this;`);
|
|
529
|
-
f.print(` }`);
|
|
530
|
-
f.print(` function camelToSnake (str: string) {`);
|
|
531
|
-
f.print(` if (str.includes("_")) {`);
|
|
532
|
-
f.print(` throw new Error("cannot decode `, message.typeName, ` from JSON: path names must be lowerCamelCase");`);
|
|
533
|
-
f.print(` }`);
|
|
534
|
-
f.print(` const sc = str.replace(/[A-Z]/g, letter => "_" + letter.toLowerCase());`);
|
|
535
|
-
f.print(` return (sc[0] === "_") ? sc.substring(1) : sc;`);
|
|
536
|
-
f.print(` }`);
|
|
537
|
-
f.print(` this.`, (0, ecmascript_1.localName)(ref.paths), ` = json.split(",").map(camelToSnake);`);
|
|
538
|
-
f.print(` return this;`);
|
|
539
|
-
f.print(` }`);
|
|
540
|
-
f.print();
|
|
541
|
-
break;
|
|
542
|
-
case "google.protobuf.DoubleValue":
|
|
543
|
-
case "google.protobuf.FloatValue":
|
|
544
|
-
case "google.protobuf.Int64Value":
|
|
545
|
-
case "google.protobuf.UInt64Value":
|
|
546
|
-
case "google.protobuf.Int32Value":
|
|
547
|
-
case "google.protobuf.UInt32Value":
|
|
548
|
-
case "google.protobuf.BoolValue":
|
|
549
|
-
case "google.protobuf.StringValue":
|
|
550
|
-
case "google.protobuf.BytesValue":
|
|
551
|
-
f.print(" override toJson(options?: Partial<", JsonWriteOptions, ">): ", JsonValue, " {");
|
|
552
|
-
f.print(" return proto3.json.writeScalar(", rtScalarType, ".", protobuf_1.ScalarType[ref.value.scalar], ", this.value, true)!;");
|
|
553
|
-
f.print(" }");
|
|
554
|
-
f.print();
|
|
555
|
-
f.print(" override fromJson(json: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): this {");
|
|
556
|
-
f.print(" try {");
|
|
557
|
-
if (ref.value.longType === protobuf_1.LongType.STRING) {
|
|
558
|
-
f.print(" this.value = ", protoN, ".json.readScalar(", rtScalarType, ".", protobuf_1.ScalarType[ref.value.scalar], ", json, ", rtLongType, ".", protobuf_1.LongType[ref.value.longType], ");");
|
|
559
|
-
}
|
|
560
|
-
else {
|
|
561
|
-
f.print(" this.value = ", protoN, ".json.readScalar(", rtScalarType, ".", protobuf_1.ScalarType[ref.value.scalar], ", json);");
|
|
562
|
-
}
|
|
563
|
-
f.print(" } catch (e) {");
|
|
564
|
-
f.print(" let m = `cannot decode message ", message.typeName, " from JSON\"`;");
|
|
565
|
-
f.print(" if (e instanceof Error && e.message.length > 0) {");
|
|
566
|
-
f.print(" m += `: ${e.message}`");
|
|
567
|
-
f.print(" }");
|
|
568
|
-
f.print(" throw new Error(m);");
|
|
569
|
-
f.print(" }");
|
|
570
|
-
f.print(" return this;");
|
|
571
|
-
f.print(" }");
|
|
572
|
-
f.print();
|
|
573
|
-
break;
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
// prettier-ignore
|
|
577
|
-
function generateWktStaticMethods(schema, f, message) {
|
|
578
|
-
const ref = (0, ecmascript_1.reifyWkt)(message);
|
|
579
|
-
if (ref === undefined) {
|
|
580
|
-
return;
|
|
581
|
-
}
|
|
582
|
-
const { protoInt64, } = schema.runtime;
|
|
583
|
-
switch (ref.typeName) {
|
|
584
|
-
case "google.protobuf.Any":
|
|
585
|
-
f.print(" static pack(message: Message): ", message, " {");
|
|
586
|
-
f.print(" const any = new ", message, "();");
|
|
587
|
-
f.print(" any.packFrom(message);");
|
|
588
|
-
f.print(" return any;");
|
|
589
|
-
f.print(" }");
|
|
590
|
-
f.print();
|
|
591
|
-
break;
|
|
592
|
-
case "google.protobuf.Timestamp":
|
|
593
|
-
f.print(" static now(): ", message, " {");
|
|
594
|
-
f.print(" return ", message, ".fromDate(new Date())");
|
|
595
|
-
f.print(" }");
|
|
596
|
-
f.print();
|
|
597
|
-
f.print(" static fromDate(date: Date): ", message, " {");
|
|
598
|
-
f.print(" const ms = date.getTime();");
|
|
599
|
-
f.print(" return new ", message, "({");
|
|
600
|
-
if (ref.seconds.longType === protobuf_1.LongType.STRING) {
|
|
601
|
-
f.print(" ", (0, ecmascript_1.localName)(ref.seconds), ": ", protoInt64, ".parse(Math.floor(ms / 1000)).toString(),");
|
|
602
|
-
}
|
|
603
|
-
else {
|
|
604
|
-
f.print(" ", (0, ecmascript_1.localName)(ref.seconds), ": ", protoInt64, ".parse(Math.floor(ms / 1000)),");
|
|
605
|
-
}
|
|
606
|
-
f.print(" ", (0, ecmascript_1.localName)(ref.nanos), ": (ms % 1000) * 1000000,");
|
|
607
|
-
f.print(" });");
|
|
608
|
-
f.print(" }");
|
|
609
|
-
f.print();
|
|
610
|
-
break;
|
|
611
|
-
case "google.protobuf.DoubleValue":
|
|
612
|
-
case "google.protobuf.FloatValue":
|
|
613
|
-
case "google.protobuf.Int64Value":
|
|
614
|
-
case "google.protobuf.UInt64Value":
|
|
615
|
-
case "google.protobuf.Int32Value":
|
|
616
|
-
case "google.protobuf.UInt32Value":
|
|
617
|
-
case "google.protobuf.BoolValue":
|
|
618
|
-
case "google.protobuf.StringValue":
|
|
619
|
-
case "google.protobuf.BytesValue": {
|
|
620
|
-
const { typing } = (0, util_js_1.getFieldTypeInfo)(ref.value);
|
|
621
|
-
f.print(" static readonly fieldWrapper = {");
|
|
622
|
-
f.print(" wrapField(value: ", typing, "): ", message, " {");
|
|
623
|
-
f.print(" return new ", message, "({value});");
|
|
624
|
-
f.print(" },");
|
|
625
|
-
f.print(" unwrapField(value: ", message, "): ", typing, " {");
|
|
626
|
-
f.print(" return value.", (0, ecmascript_1.localName)(ref.value), ";");
|
|
627
|
-
f.print(" }");
|
|
628
|
-
f.print(" };");
|
|
629
|
-
f.print();
|
|
630
|
-
break;
|
|
631
|
-
}
|
|
632
|
-
case "google.protobuf.Duration":
|
|
633
|
-
case "google.protobuf.Struct":
|
|
634
|
-
case "google.protobuf.Value":
|
|
635
|
-
case "google.protobuf.ListValue":
|
|
636
|
-
case "google.protobuf.FieldMask":
|
|
637
|
-
break;
|
|
638
|
-
}
|
|
639
|
-
}
|