@bufbuild/protoc-gen-es 1.9.0 → 2.0.0-alpha.2
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 +22 -24
- package/dist/cjs/package.json +9 -5
- package/dist/cjs/src/protoc-gen-es-plugin.js +320 -6
- package/dist/cjs/src/util.js +65 -195
- package/package.json +9 -5
- 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/protoc-gen-es-plugin.ts +0 -27
- package/src/typescript.ts +0 -673
- package/src/util.ts +0 -315
- package/tsconfig.json +0 -12
package/dist/cjs/src/util.js
CHANGED
|
@@ -13,56 +13,67 @@
|
|
|
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.
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
function
|
|
16
|
+
exports.arrayLiteral = exports.functionCall = exports.fieldTypeScriptType = void 0;
|
|
17
|
+
const reflect_1 = require("@bufbuild/protobuf/reflect");
|
|
18
|
+
const wkt_1 = require("@bufbuild/protobuf/wkt");
|
|
19
|
+
function fieldTypeScriptType(field) {
|
|
20
20
|
const typing = [];
|
|
21
|
-
let typingInferrableFromZeroValue;
|
|
22
21
|
let optional = false;
|
|
23
22
|
switch (field.fieldKind) {
|
|
24
23
|
case "scalar":
|
|
25
|
-
typing.push(scalarTypeScriptType(field.scalar, field.longType));
|
|
26
|
-
optional =
|
|
27
|
-
field.optional ||
|
|
28
|
-
field.proto.label === protobuf_1.FieldDescriptorProto_Label.REQUIRED;
|
|
29
|
-
typingInferrableFromZeroValue = true;
|
|
24
|
+
typing.push((0, reflect_1.scalarTypeScriptType)(field.scalar, field.longType));
|
|
25
|
+
optional = field.proto.proto3Optional;
|
|
30
26
|
break;
|
|
31
27
|
case "message": {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
typing.push(scalarTypeScriptType(baseType,
|
|
28
|
+
if (!field.oneof && (0, wkt_1.isWrapperDesc)(field.message)) {
|
|
29
|
+
const baseType = field.message.fields[0].scalar;
|
|
30
|
+
typing.push((0, reflect_1.scalarTypeScriptType)(baseType, reflect_1.LongType.BIGINT));
|
|
35
31
|
}
|
|
36
32
|
else {
|
|
37
33
|
typing.push({
|
|
38
|
-
kind: "
|
|
39
|
-
|
|
40
|
-
typeOnly: true,
|
|
34
|
+
kind: "es_shape_ref",
|
|
35
|
+
desc: field.message,
|
|
41
36
|
});
|
|
42
37
|
}
|
|
43
38
|
optional = true;
|
|
44
|
-
typingInferrableFromZeroValue = true;
|
|
45
39
|
break;
|
|
46
40
|
}
|
|
47
41
|
case "enum":
|
|
48
42
|
typing.push({
|
|
49
|
-
kind: "
|
|
50
|
-
|
|
51
|
-
typeOnly: true,
|
|
43
|
+
kind: "es_shape_ref",
|
|
44
|
+
desc: field.enum,
|
|
52
45
|
});
|
|
53
|
-
optional =
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
optional = field.proto.proto3Optional;
|
|
47
|
+
break;
|
|
48
|
+
case "list":
|
|
49
|
+
optional = false;
|
|
50
|
+
switch (field.listKind) {
|
|
51
|
+
case "enum":
|
|
52
|
+
typing.push({
|
|
53
|
+
kind: "es_shape_ref",
|
|
54
|
+
desc: field.enum,
|
|
55
|
+
}, "[]");
|
|
56
|
+
break;
|
|
57
|
+
case "scalar":
|
|
58
|
+
typing.push((0, reflect_1.scalarTypeScriptType)(field.scalar, field.longType), "[]");
|
|
59
|
+
break;
|
|
60
|
+
case "message": {
|
|
61
|
+
typing.push({
|
|
62
|
+
kind: "es_shape_ref",
|
|
63
|
+
desc: field.message,
|
|
64
|
+
}, "[]");
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
57
68
|
break;
|
|
58
69
|
case "map": {
|
|
59
70
|
let keyType;
|
|
60
71
|
switch (field.mapKey) {
|
|
61
|
-
case
|
|
62
|
-
case
|
|
63
|
-
case
|
|
64
|
-
case
|
|
65
|
-
case
|
|
72
|
+
case reflect_1.ScalarType.INT32:
|
|
73
|
+
case reflect_1.ScalarType.FIXED32:
|
|
74
|
+
case reflect_1.ScalarType.UINT32:
|
|
75
|
+
case reflect_1.ScalarType.SFIXED32:
|
|
76
|
+
case reflect_1.ScalarType.SINT32:
|
|
66
77
|
keyType = "number";
|
|
67
78
|
break;
|
|
68
79
|
default:
|
|
@@ -70,191 +81,50 @@ function getFieldTypeInfo(field) {
|
|
|
70
81
|
break;
|
|
71
82
|
}
|
|
72
83
|
let valueType;
|
|
73
|
-
switch (field.
|
|
84
|
+
switch (field.mapKind) {
|
|
74
85
|
case "scalar":
|
|
75
|
-
valueType = scalarTypeScriptType(field.
|
|
86
|
+
valueType = (0, reflect_1.scalarTypeScriptType)(field.scalar, reflect_1.LongType.BIGINT);
|
|
76
87
|
break;
|
|
77
88
|
case "message":
|
|
78
89
|
valueType = {
|
|
79
|
-
kind: "
|
|
80
|
-
|
|
81
|
-
typeOnly: true,
|
|
90
|
+
kind: "es_shape_ref",
|
|
91
|
+
desc: field.message,
|
|
82
92
|
};
|
|
83
93
|
break;
|
|
84
94
|
case "enum":
|
|
85
95
|
valueType = {
|
|
86
|
-
kind: "
|
|
87
|
-
|
|
88
|
-
typeOnly: true,
|
|
96
|
+
kind: "es_shape_ref",
|
|
97
|
+
desc: field.enum,
|
|
89
98
|
};
|
|
90
99
|
break;
|
|
91
100
|
}
|
|
92
101
|
typing.push("{ [key: ", keyType, "]: ", valueType, " }");
|
|
93
|
-
typingInferrableFromZeroValue = false;
|
|
94
102
|
optional = false;
|
|
95
103
|
break;
|
|
96
104
|
}
|
|
97
105
|
}
|
|
98
|
-
|
|
99
|
-
typing.push("[]");
|
|
100
|
-
optional = false;
|
|
101
|
-
typingInferrableFromZeroValue = false;
|
|
102
|
-
}
|
|
103
|
-
return { typing, optional, typingInferrableFromZeroValue };
|
|
106
|
+
return { typing, optional };
|
|
104
107
|
}
|
|
105
|
-
exports.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
function getFieldDefaultValueExpression(field, enumAs = "enum_value_as_is") {
|
|
111
|
-
if (field.repeated) {
|
|
112
|
-
return undefined;
|
|
113
|
-
}
|
|
114
|
-
if (field.fieldKind !== "enum" && field.fieldKind !== "scalar") {
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
const defaultValue = field.getDefaultValue();
|
|
118
|
-
if (defaultValue === undefined) {
|
|
119
|
-
return undefined;
|
|
120
|
-
}
|
|
121
|
-
switch (field.fieldKind) {
|
|
122
|
-
case "enum": {
|
|
123
|
-
const enumValue = field.enum.values.find((value) => value.number === defaultValue);
|
|
124
|
-
if (enumValue === undefined) {
|
|
125
|
-
throw new Error(`invalid enum default value: ${String(defaultValue)} for ${enumValue}`);
|
|
126
|
-
}
|
|
127
|
-
return literalEnumValue(enumValue, enumAs);
|
|
128
|
-
}
|
|
129
|
-
case "scalar":
|
|
130
|
-
return literalScalarValue(defaultValue, field);
|
|
108
|
+
exports.fieldTypeScriptType = fieldTypeScriptType;
|
|
109
|
+
function functionCall(fn, args, typeParams) {
|
|
110
|
+
let tp = [];
|
|
111
|
+
if (typeParams !== undefined && typeParams.length > 0) {
|
|
112
|
+
tp = ["<", commaSeparate(typeParams), ">"];
|
|
131
113
|
}
|
|
114
|
+
return [fn, ...tp, "(", commaSeparate(args), ")"];
|
|
132
115
|
}
|
|
133
|
-
exports.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
*
|
|
137
|
-
* Returns either:
|
|
138
|
-
* - empty array literal for repeated fields
|
|
139
|
-
* - empty object literal for maps
|
|
140
|
-
* - undefined for message fields
|
|
141
|
-
* - an enums first value
|
|
142
|
-
* - scalar zero value
|
|
143
|
-
*/
|
|
144
|
-
function getFieldZeroValueExpression(field, enumAs = "enum_value_as_is") {
|
|
145
|
-
if (field.repeated) {
|
|
146
|
-
return "[]";
|
|
147
|
-
}
|
|
148
|
-
switch (field.fieldKind) {
|
|
149
|
-
case "message":
|
|
150
|
-
return undefined;
|
|
151
|
-
case "map":
|
|
152
|
-
return "{}";
|
|
153
|
-
case "enum": {
|
|
154
|
-
// In proto3, the first enum value must be zero.
|
|
155
|
-
// In proto2, protobuf-go returns the first value as the default.
|
|
156
|
-
if (field.enum.values.length < 1) {
|
|
157
|
-
throw new Error("invalid enum: missing at least one value");
|
|
158
|
-
}
|
|
159
|
-
const zeroValue = field.enum.values[0];
|
|
160
|
-
return literalEnumValue(zeroValue, enumAs);
|
|
161
|
-
}
|
|
162
|
-
case "scalar": {
|
|
163
|
-
const defaultValue = protobuf_1.codegenInfo.scalarZeroValue(field.scalar, field.longType);
|
|
164
|
-
return literalScalarValue(defaultValue, field);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
exports.getFieldZeroValueExpression = getFieldZeroValueExpression;
|
|
169
|
-
function literalScalarValue(value, field) {
|
|
170
|
-
switch (field.scalar) {
|
|
171
|
-
case protobuf_1.ScalarType.DOUBLE:
|
|
172
|
-
case protobuf_1.ScalarType.FLOAT:
|
|
173
|
-
case protobuf_1.ScalarType.INT32:
|
|
174
|
-
case protobuf_1.ScalarType.FIXED32:
|
|
175
|
-
case protobuf_1.ScalarType.UINT32:
|
|
176
|
-
case protobuf_1.ScalarType.SFIXED32:
|
|
177
|
-
case protobuf_1.ScalarType.SINT32:
|
|
178
|
-
if (typeof value != "number") {
|
|
179
|
-
throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
|
|
180
|
-
}
|
|
181
|
-
return value;
|
|
182
|
-
case protobuf_1.ScalarType.BOOL:
|
|
183
|
-
if (typeof value != "boolean") {
|
|
184
|
-
throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
|
|
185
|
-
}
|
|
186
|
-
return value;
|
|
187
|
-
case protobuf_1.ScalarType.STRING:
|
|
188
|
-
if (typeof value != "string") {
|
|
189
|
-
throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
|
|
190
|
-
}
|
|
191
|
-
return { kind: "es_string", value };
|
|
192
|
-
case protobuf_1.ScalarType.BYTES:
|
|
193
|
-
if (!(value instanceof Uint8Array)) {
|
|
194
|
-
throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
|
|
195
|
-
}
|
|
196
|
-
return value;
|
|
197
|
-
case protobuf_1.ScalarType.INT64:
|
|
198
|
-
case protobuf_1.ScalarType.SINT64:
|
|
199
|
-
case protobuf_1.ScalarType.SFIXED64:
|
|
200
|
-
case protobuf_1.ScalarType.UINT64:
|
|
201
|
-
case protobuf_1.ScalarType.FIXED64:
|
|
202
|
-
if (typeof value != "bigint" && typeof value != "string") {
|
|
203
|
-
throw new Error(`Unexpected value for ${protobuf_1.ScalarType[field.scalar]} ${field.toString()}: ${String(value)}`);
|
|
204
|
-
}
|
|
205
|
-
return {
|
|
206
|
-
kind: "es_proto_int64",
|
|
207
|
-
type: field.scalar,
|
|
208
|
-
longType: field.longType,
|
|
209
|
-
value,
|
|
210
|
-
};
|
|
211
|
-
}
|
|
116
|
+
exports.functionCall = functionCall;
|
|
117
|
+
function arrayLiteral(elements) {
|
|
118
|
+
return ["[", commaSeparate(elements), "]"];
|
|
212
119
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
case "enum_value_as_integer":
|
|
222
|
-
return [
|
|
223
|
-
value.number,
|
|
224
|
-
" /* ",
|
|
225
|
-
value.parent.typeName,
|
|
226
|
-
".",
|
|
227
|
-
value.name,
|
|
228
|
-
" */",
|
|
229
|
-
];
|
|
230
|
-
case "enum_value_as_cast_integer":
|
|
231
|
-
return [
|
|
232
|
-
value.number,
|
|
233
|
-
" as ",
|
|
234
|
-
{ kind: "es_ref_enum", type: value.parent, typeOnly: true },
|
|
235
|
-
".",
|
|
236
|
-
(0, ecmascript_1.localName)(value),
|
|
237
|
-
];
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
function scalarTypeScriptType(type, longType) {
|
|
241
|
-
switch (type) {
|
|
242
|
-
case protobuf_1.ScalarType.STRING:
|
|
243
|
-
return "string";
|
|
244
|
-
case protobuf_1.ScalarType.BOOL:
|
|
245
|
-
return "boolean";
|
|
246
|
-
case protobuf_1.ScalarType.UINT64:
|
|
247
|
-
case protobuf_1.ScalarType.SFIXED64:
|
|
248
|
-
case protobuf_1.ScalarType.FIXED64:
|
|
249
|
-
case protobuf_1.ScalarType.SINT64:
|
|
250
|
-
case protobuf_1.ScalarType.INT64:
|
|
251
|
-
if (longType === protobuf_1.LongType.STRING) {
|
|
252
|
-
return "string";
|
|
253
|
-
}
|
|
254
|
-
return "bigint";
|
|
255
|
-
case protobuf_1.ScalarType.BYTES:
|
|
256
|
-
return "Uint8Array";
|
|
257
|
-
default:
|
|
258
|
-
return "number";
|
|
120
|
+
exports.arrayLiteral = arrayLiteral;
|
|
121
|
+
function commaSeparate(elements) {
|
|
122
|
+
const r = [];
|
|
123
|
+
for (let i = 0; i < elements.length; i++) {
|
|
124
|
+
r.push(elements[i]);
|
|
125
|
+
if (i < elements.length - 1) {
|
|
126
|
+
r.push(", ");
|
|
127
|
+
}
|
|
259
128
|
}
|
|
129
|
+
return r;
|
|
260
130
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-alpha.2",
|
|
4
4
|
"description": "Protocol Buffers code generator for ECMAScript",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -20,15 +20,19 @@
|
|
|
20
20
|
},
|
|
21
21
|
"preferUnplugged": true,
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@bufbuild/protobuf": "^
|
|
24
|
-
"@bufbuild/protoplugin": "
|
|
23
|
+
"@bufbuild/protobuf": "^2.0.0-alpha.2",
|
|
24
|
+
"@bufbuild/protoplugin": "2.0.0-alpha.2"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@bufbuild/protobuf": "
|
|
27
|
+
"@bufbuild/protobuf": "2.0.0-alpha.2"
|
|
28
28
|
},
|
|
29
29
|
"peerDependenciesMeta": {
|
|
30
30
|
"@bufbuild/protobuf": {
|
|
31
31
|
"optional": true
|
|
32
32
|
}
|
|
33
|
-
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist/**",
|
|
36
|
+
"bin/**"
|
|
37
|
+
]
|
|
34
38
|
}
|
|
@@ -1,218 +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.generateDts = void 0;
|
|
17
|
-
const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
|
|
18
|
-
const editions_js_1 = require("./editions.js");
|
|
19
|
-
const util_js_1 = require("./util.js");
|
|
20
|
-
function generateDts(schema) {
|
|
21
|
-
for (const file of schema.files) {
|
|
22
|
-
const f = schema.generateFile(file.name + "_pb.d.ts");
|
|
23
|
-
f.preamble(file);
|
|
24
|
-
for (const enumeration of file.enums) {
|
|
25
|
-
generateEnum(schema, f, enumeration);
|
|
26
|
-
}
|
|
27
|
-
for (const message of file.messages) {
|
|
28
|
-
generateMessage(schema, f, message);
|
|
29
|
-
}
|
|
30
|
-
for (const extension of file.extensions) {
|
|
31
|
-
generateExtension(schema, f, extension);
|
|
32
|
-
}
|
|
33
|
-
// We do not generate anything for services
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.generateDts = generateDts;
|
|
37
|
-
// prettier-ignore
|
|
38
|
-
function generateEnum(schema, f, enumeration) {
|
|
39
|
-
f.print(f.jsDoc(enumeration));
|
|
40
|
-
f.print(f.exportDecl("declare enum", (0, ecmascript_1.localName)(enumeration)), " {");
|
|
41
|
-
for (const value of enumeration.values) {
|
|
42
|
-
if (enumeration.values.indexOf(value) > 0) {
|
|
43
|
-
f.print();
|
|
44
|
-
}
|
|
45
|
-
f.print(f.jsDoc(value, " "));
|
|
46
|
-
f.print(" ", (0, ecmascript_1.localName)(value), " = ", value.number, ",");
|
|
47
|
-
}
|
|
48
|
-
f.print("}");
|
|
49
|
-
f.print();
|
|
50
|
-
}
|
|
51
|
-
// prettier-ignore
|
|
52
|
-
function generateMessage(schema, f, message) {
|
|
53
|
-
const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
|
|
54
|
-
const { PartialMessage, FieldList, Message, PlainMessage, BinaryReadOptions, JsonReadOptions, JsonValue } = schema.runtime;
|
|
55
|
-
const m = (0, ecmascript_1.localName)(message);
|
|
56
|
-
f.print(f.jsDoc(message));
|
|
57
|
-
f.print(f.exportDecl("declare class", m), " extends ", Message, "<", m, "> {");
|
|
58
|
-
for (const member of message.members) {
|
|
59
|
-
switch (member.kind) {
|
|
60
|
-
case "oneof":
|
|
61
|
-
generateOneof(schema, f, member);
|
|
62
|
-
break;
|
|
63
|
-
default:
|
|
64
|
-
generateField(schema, f, member);
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
f.print();
|
|
68
|
-
}
|
|
69
|
-
f.print(" constructor(data?: ", PartialMessage, "<", m, ">);");
|
|
70
|
-
f.print();
|
|
71
|
-
generateWktMethods(schema, f, message);
|
|
72
|
-
f.print(" static readonly runtime: typeof ", protoN, ";");
|
|
73
|
-
f.print(' static readonly typeName = ', f.string(message.typeName), ';');
|
|
74
|
-
f.print(" static readonly fields: ", FieldList, ";");
|
|
75
|
-
// In case we start supporting options, we have to surface them here
|
|
76
|
-
//f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
|
|
77
|
-
f.print();
|
|
78
|
-
generateWktStaticMethods(schema, f, message);
|
|
79
|
-
f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", m, ";");
|
|
80
|
-
f.print();
|
|
81
|
-
f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", m, ";");
|
|
82
|
-
f.print();
|
|
83
|
-
f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", m, ";");
|
|
84
|
-
f.print();
|
|
85
|
-
f.print(" static equals(a: ", m, " | ", PlainMessage, "<", m, "> | undefined, b: ", m, " | ", PlainMessage, "<", m, "> | undefined): boolean;");
|
|
86
|
-
f.print("}");
|
|
87
|
-
f.print();
|
|
88
|
-
for (const nestedEnum of message.nestedEnums) {
|
|
89
|
-
generateEnum(schema, f, nestedEnum);
|
|
90
|
-
}
|
|
91
|
-
for (const nestedMessage of message.nestedMessages) {
|
|
92
|
-
generateMessage(schema, f, nestedMessage);
|
|
93
|
-
}
|
|
94
|
-
for (const nestedExtension of message.nestedExtensions) {
|
|
95
|
-
generateExtension(schema, f, nestedExtension);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
// prettier-ignore
|
|
99
|
-
function generateOneof(schema, f, oneof) {
|
|
100
|
-
f.print(f.jsDoc(oneof, " "));
|
|
101
|
-
f.print(" ", (0, ecmascript_1.localName)(oneof), ": {");
|
|
102
|
-
for (const field of oneof.fields) {
|
|
103
|
-
if (oneof.fields.indexOf(field) > 0) {
|
|
104
|
-
f.print(` } | {`);
|
|
105
|
-
}
|
|
106
|
-
f.print(f.jsDoc(field, " "));
|
|
107
|
-
const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
108
|
-
f.print(` value: `, typing, `;`);
|
|
109
|
-
f.print(` case: "`, (0, ecmascript_1.localName)(field), `";`);
|
|
110
|
-
}
|
|
111
|
-
f.print(` } | { case: undefined; value?: undefined };`);
|
|
112
|
-
}
|
|
113
|
-
// prettier-ignore
|
|
114
|
-
function generateField(schema, f, field) {
|
|
115
|
-
f.print(f.jsDoc(field, " "));
|
|
116
|
-
const e = [];
|
|
117
|
-
const { typing, optional } = (0, util_js_1.getFieldTypeInfo)(field);
|
|
118
|
-
if (!optional) {
|
|
119
|
-
e.push(" ", (0, ecmascript_1.localName)(field), ": ", typing, ";");
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
e.push(" ", (0, ecmascript_1.localName)(field), "?: ", typing, ";");
|
|
123
|
-
}
|
|
124
|
-
f.print(e);
|
|
125
|
-
}
|
|
126
|
-
// prettier-ignore
|
|
127
|
-
function generateExtension(schema, f, ext) {
|
|
128
|
-
const { typing } = (0, util_js_1.getFieldTypeInfo)(ext);
|
|
129
|
-
const e = f.import(ext.extendee).toTypeOnly();
|
|
130
|
-
f.print(f.jsDoc(ext));
|
|
131
|
-
f.print(f.exportDecl("declare const", (0, ecmascript_1.localName)(ext)), ": ", schema.runtime.Extension, "<", e, ", ", typing, ">;");
|
|
132
|
-
f.print();
|
|
133
|
-
}
|
|
134
|
-
// prettier-ignore
|
|
135
|
-
function generateWktMethods(schema, f, message) {
|
|
136
|
-
const ref = (0, ecmascript_1.reifyWkt)(message);
|
|
137
|
-
if (ref === undefined) {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
const { Message, MessageType, IMessageTypeRegistry } = schema.runtime;
|
|
141
|
-
switch (ref.typeName) {
|
|
142
|
-
case "google.protobuf.Any":
|
|
143
|
-
f.print(" packFrom(message: ", Message, "): void;");
|
|
144
|
-
f.print();
|
|
145
|
-
f.print(" unpackTo(target: ", Message, "): boolean;");
|
|
146
|
-
f.print();
|
|
147
|
-
f.print(" unpack(registry: ", IMessageTypeRegistry, "): Message | undefined;");
|
|
148
|
-
f.print();
|
|
149
|
-
f.print(" is(type: ", MessageType, " | string): boolean;");
|
|
150
|
-
f.print();
|
|
151
|
-
f.print(" private typeNameToUrl(name: string): string;");
|
|
152
|
-
f.print();
|
|
153
|
-
f.print(" private typeUrlToName(url: string): string;");
|
|
154
|
-
f.print();
|
|
155
|
-
break;
|
|
156
|
-
case "google.protobuf.Timestamp":
|
|
157
|
-
f.print(" toDate(): Date;");
|
|
158
|
-
f.print();
|
|
159
|
-
break;
|
|
160
|
-
case "google.protobuf.Duration":
|
|
161
|
-
case "google.protobuf.Struct":
|
|
162
|
-
case "google.protobuf.Value":
|
|
163
|
-
case "google.protobuf.ListValue":
|
|
164
|
-
case "google.protobuf.FieldMask":
|
|
165
|
-
case "google.protobuf.DoubleValue":
|
|
166
|
-
case "google.protobuf.FloatValue":
|
|
167
|
-
case "google.protobuf.Int64Value":
|
|
168
|
-
case "google.protobuf.UInt64Value":
|
|
169
|
-
case "google.protobuf.Int32Value":
|
|
170
|
-
case "google.protobuf.UInt32Value":
|
|
171
|
-
case "google.protobuf.BoolValue":
|
|
172
|
-
case "google.protobuf.StringValue":
|
|
173
|
-
case "google.protobuf.BytesValue":
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
// prettier-ignore
|
|
178
|
-
function generateWktStaticMethods(schema, f, message) {
|
|
179
|
-
const ref = (0, ecmascript_1.reifyWkt)(message);
|
|
180
|
-
if (ref === undefined) {
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
switch (ref.typeName) {
|
|
184
|
-
case "google.protobuf.Any":
|
|
185
|
-
f.print(" static pack(message: Message): ", message, ";");
|
|
186
|
-
f.print();
|
|
187
|
-
break;
|
|
188
|
-
case "google.protobuf.Timestamp":
|
|
189
|
-
f.print(" static now(): ", message, ";");
|
|
190
|
-
f.print();
|
|
191
|
-
f.print(" static fromDate(date: Date): ", message, ";");
|
|
192
|
-
f.print();
|
|
193
|
-
break;
|
|
194
|
-
case "google.protobuf.DoubleValue":
|
|
195
|
-
case "google.protobuf.FloatValue":
|
|
196
|
-
case "google.protobuf.Int64Value":
|
|
197
|
-
case "google.protobuf.UInt64Value":
|
|
198
|
-
case "google.protobuf.Int32Value":
|
|
199
|
-
case "google.protobuf.UInt32Value":
|
|
200
|
-
case "google.protobuf.BoolValue":
|
|
201
|
-
case "google.protobuf.StringValue":
|
|
202
|
-
case "google.protobuf.BytesValue": {
|
|
203
|
-
const { typing } = (0, util_js_1.getFieldTypeInfo)(ref.value);
|
|
204
|
-
f.print(" static readonly fieldWrapper: {");
|
|
205
|
-
f.print(" wrapField(value: ", typing, "): ", message, ",");
|
|
206
|
-
f.print(" unwrapField(value: ", message, "): ", typing, ",");
|
|
207
|
-
f.print(" };");
|
|
208
|
-
f.print();
|
|
209
|
-
break;
|
|
210
|
-
}
|
|
211
|
-
case "google.protobuf.Duration":
|
|
212
|
-
case "google.protobuf.Struct":
|
|
213
|
-
case "google.protobuf.Value":
|
|
214
|
-
case "google.protobuf.ListValue":
|
|
215
|
-
case "google.protobuf.FieldMask":
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
}
|
package/dist/cjs/src/editions.js
DELETED
|
@@ -1,34 +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.getNonEditionRuntime = void 0;
|
|
17
|
-
/**
|
|
18
|
-
* Temporary function to retrieve the import symbol for the proto2 or proto3
|
|
19
|
-
* runtime.
|
|
20
|
-
*
|
|
21
|
-
* For syntax "editions", this function raises and error.
|
|
22
|
-
*
|
|
23
|
-
* Once support for "editions" is implemented in the runtime, this function can
|
|
24
|
-
* be removed.
|
|
25
|
-
*/
|
|
26
|
-
function getNonEditionRuntime(schema, file) {
|
|
27
|
-
var _a;
|
|
28
|
-
if (file.syntax === "editions") {
|
|
29
|
-
// TODO support editions
|
|
30
|
-
throw new Error(`${(_a = file.proto.name) !== null && _a !== void 0 ? _a : ""}: syntax "editions" is not supported yet`);
|
|
31
|
-
}
|
|
32
|
-
return schema.runtime[file.syntax];
|
|
33
|
-
}
|
|
34
|
-
exports.getNonEditionRuntime = getNonEditionRuntime;
|