@bufbuild/protoplugin 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 +38 -0
- package/dist/cjs/create-es-plugin.js +117 -0
- package/dist/cjs/ecmascript/gencommon.js +338 -0
- package/dist/cjs/ecmascript/generated-file.js +300 -0
- package/dist/cjs/ecmascript/import-symbol.js +34 -0
- package/dist/cjs/ecmascript/index.js +30 -0
- package/dist/cjs/ecmascript/runtime-imports.js +46 -0
- package/dist/cjs/ecmascript/schema.js +84 -0
- package/dist/cjs/ecmascript/target.js +15 -0
- package/dist/cjs/error.js +34 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/plugin.js +15 -0
- package/dist/cjs/run-node.js +84 -0
- package/dist/esm/create-es-plugin.js +113 -0
- package/dist/esm/ecmascript/gencommon.js +327 -0
- package/dist/esm/ecmascript/generated-file.js +296 -0
- package/dist/esm/ecmascript/import-symbol.js +30 -0
- package/dist/esm/ecmascript/index.js +21 -0
- package/dist/esm/ecmascript/runtime-imports.js +42 -0
- package/dist/esm/ecmascript/schema.js +80 -0
- package/dist/esm/ecmascript/target.js +14 -0
- package/dist/esm/error.js +29 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/plugin.js +14 -0
- package/dist/esm/run-node.js +80 -0
- package/dist/types/create-es-plugin.d.ts +21 -0
- package/dist/types/ecmascript/gencommon.d.ts +23 -0
- package/dist/types/ecmascript/generated-file.d.ts +63 -0
- package/dist/types/ecmascript/import-symbol.d.ts +39 -0
- package/dist/types/ecmascript/index.d.ts +7 -0
- package/dist/types/ecmascript/runtime-imports.d.ts +21 -0
- package/dist/types/ecmascript/schema.d.ts +41 -0
- package/dist/types/ecmascript/target.d.ts +4 -0
- package/dist/types/error.d.ts +4 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/plugin.d.ts +18 -0
- package/dist/types/run-node.d.ts +12 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# @bufbuild/protoplugin
|
|
2
|
+
|
|
3
|
+
This package helps to create your own code generator plugin.
|
|
4
|
+
|
|
5
|
+
## Protocol Buffers for ECMAScript
|
|
6
|
+
|
|
7
|
+
A complete implementation of [Protocol Buffers](https://developers.google.com/protocol-buffers)
|
|
8
|
+
in TypeScript, suitable for web browsers and Node.js.
|
|
9
|
+
Learn more at [github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es).
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
It is a complete implementation of [Protocol Buffers](https://developers.google.com/protocol-buffers)
|
|
13
|
+
in TypeScript, suitable for web browsers and Node.js.
|
|
14
|
+
|
|
15
|
+
For example, the following definition:
|
|
16
|
+
|
|
17
|
+
```protobuf
|
|
18
|
+
message Person {
|
|
19
|
+
string name = 1;
|
|
20
|
+
int32 id = 2; // Unique ID number for this person.
|
|
21
|
+
string email = 3;
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Is compiled to an ECMAScript class that can be used like this:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
let pete = new Person({
|
|
29
|
+
name: "pete",
|
|
30
|
+
id: 123
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
let bytes = pete.toBinary();
|
|
34
|
+
pete = Person.fromBinary(bytes);
|
|
35
|
+
pete = Person.fromJsonString('{"name": "pete", "id": 123}');
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Learn more at [github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es).
|
|
@@ -0,0 +1,117 @@
|
|
|
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.createEcmaScriptPlugin = void 0;
|
|
17
|
+
const schema_js_1 = require("./ecmascript/schema.js");
|
|
18
|
+
const protobuf_1 = require("@bufbuild/protobuf");
|
|
19
|
+
const error_js_1 = require("./error.js");
|
|
20
|
+
/**
|
|
21
|
+
* Create a new code generator plugin for ECMAScript.
|
|
22
|
+
* The plugin can generate JavaScript, TypeScript, or TypeScript declaration
|
|
23
|
+
* files.
|
|
24
|
+
*/
|
|
25
|
+
function createEcmaScriptPlugin(init, generateFn) {
|
|
26
|
+
return {
|
|
27
|
+
name: init.name,
|
|
28
|
+
version: init.version,
|
|
29
|
+
run(req) {
|
|
30
|
+
const { targets, tsNocheck, bootstrapWkt } = parseParameter(req.parameter, init.parseOption);
|
|
31
|
+
const { schema, toResponse } = (0, schema_js_1.createSchema)(req, targets, init.name, init.version, tsNocheck, bootstrapWkt);
|
|
32
|
+
generateFn(schema);
|
|
33
|
+
const res = new protobuf_1.CodeGeneratorResponse();
|
|
34
|
+
toResponse(res);
|
|
35
|
+
return res;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
exports.createEcmaScriptPlugin = createEcmaScriptPlugin;
|
|
40
|
+
function parseParameter(parameter, parseOption) {
|
|
41
|
+
let targets = ["js", "dts"];
|
|
42
|
+
let tsNocheck = true;
|
|
43
|
+
let bootstrapWkt = false;
|
|
44
|
+
for (const { key, value } of splitParameter(parameter)) {
|
|
45
|
+
switch (key) {
|
|
46
|
+
case "target":
|
|
47
|
+
targets = [];
|
|
48
|
+
for (const rawTarget of value.split("+")) {
|
|
49
|
+
switch (rawTarget) {
|
|
50
|
+
case "js":
|
|
51
|
+
case "ts":
|
|
52
|
+
case "dts":
|
|
53
|
+
if (targets.indexOf(rawTarget) < 0) {
|
|
54
|
+
targets.push(rawTarget);
|
|
55
|
+
}
|
|
56
|
+
break;
|
|
57
|
+
default:
|
|
58
|
+
throw new error_js_1.PluginOptionError(`${key}=${value}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
value.split("+");
|
|
62
|
+
break;
|
|
63
|
+
case "ts_nocheck":
|
|
64
|
+
switch (value) {
|
|
65
|
+
case "true":
|
|
66
|
+
case "1":
|
|
67
|
+
tsNocheck = true;
|
|
68
|
+
break;
|
|
69
|
+
case "false":
|
|
70
|
+
case "0":
|
|
71
|
+
tsNocheck = false;
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
throw new error_js_1.PluginOptionError(`${key}=${value}`);
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case "bootstrap_wkt":
|
|
78
|
+
switch (value) {
|
|
79
|
+
case "true":
|
|
80
|
+
case "1":
|
|
81
|
+
bootstrapWkt = true;
|
|
82
|
+
break;
|
|
83
|
+
case "false":
|
|
84
|
+
case "0":
|
|
85
|
+
bootstrapWkt = false;
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
throw new error_js_1.PluginOptionError(`${key}=${value}`);
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
if (parseOption === undefined) {
|
|
93
|
+
throw new error_js_1.PluginOptionError(`${key}=${value}`);
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
parseOption(key, value);
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw new error_js_1.PluginOptionError(`${key}=${value}`, e);
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { targets, tsNocheck, bootstrapWkt };
|
|
105
|
+
}
|
|
106
|
+
function splitParameter(parameter) {
|
|
107
|
+
if (parameter == undefined) {
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
return parameter.split(",").map((pair) => {
|
|
111
|
+
const i = pair.indexOf("=");
|
|
112
|
+
return {
|
|
113
|
+
key: i === -1 ? pair : pair.substring(0, i),
|
|
114
|
+
value: i === -1 ? "" : pair.substring(i + 1),
|
|
115
|
+
};
|
|
116
|
+
});
|
|
117
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
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.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.literalString = exports.scalarTypeScriptType = exports.getFieldTyping = exports.makeJsDoc = exports.createJsDocBlock = exports.makeFilePreamble = void 0;
|
|
17
|
+
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
+
const { localName, getUnwrappedFieldType, scalarDefaultValue } = protobuf_1.codegenInfo;
|
|
19
|
+
function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
|
|
20
|
+
const builder = [];
|
|
21
|
+
const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
|
|
22
|
+
const writeLeadingComments = (comments) => {
|
|
23
|
+
for (let comment of comments.leadingDetached) {
|
|
24
|
+
comment = trimSuffix(comment, "\n");
|
|
25
|
+
for (const line of comment.split("\n")) {
|
|
26
|
+
builder.push(`//${line}\n`);
|
|
27
|
+
}
|
|
28
|
+
builder.push("\n");
|
|
29
|
+
}
|
|
30
|
+
if (comments.leading !== undefined) {
|
|
31
|
+
const comment = trimSuffix(comments.leading, "\n");
|
|
32
|
+
for (const line of comment.split("\n")) {
|
|
33
|
+
builder.push(`//${line}\n`);
|
|
34
|
+
}
|
|
35
|
+
builder.push("\n");
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
writeLeadingComments(file.getSyntaxComments());
|
|
39
|
+
builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
|
|
40
|
+
if (parameter !== undefined) {
|
|
41
|
+
builder.push(` with parameter "${parameter}"`);
|
|
42
|
+
}
|
|
43
|
+
builder.push("\n");
|
|
44
|
+
builder.push(`// @generated from file ${file.name}.proto (`);
|
|
45
|
+
if (file.proto.package !== undefined) {
|
|
46
|
+
builder.push(`package ${file.proto.package}, `);
|
|
47
|
+
}
|
|
48
|
+
builder.push(`syntax ${file.syntax})\n`);
|
|
49
|
+
builder.push("/* eslint-disable */\n");
|
|
50
|
+
if (tsNoCheck) {
|
|
51
|
+
builder.push("/* @ts-nocheck */\n");
|
|
52
|
+
}
|
|
53
|
+
builder.push("\n");
|
|
54
|
+
writeLeadingComments(file.getPackageComments());
|
|
55
|
+
return trimSuffix(builder.join(""), "\n");
|
|
56
|
+
}
|
|
57
|
+
exports.makeFilePreamble = makeFilePreamble;
|
|
58
|
+
function createJsDocBlock(text, indentation = "") {
|
|
59
|
+
if (text.trim().length == 0) {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
let lines = text.split("\n");
|
|
63
|
+
if (lines.length === 0) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
lines = lines.map((l) => l.split("*/").join("*\\/"));
|
|
67
|
+
lines = lines.map((l) => (l.length > 0 ? " " + l : l));
|
|
68
|
+
// prettier-ignore
|
|
69
|
+
return [
|
|
70
|
+
`${indentation}/**\n`,
|
|
71
|
+
...lines.map((l) => `${indentation} *${l}\n`),
|
|
72
|
+
`${indentation} */`
|
|
73
|
+
];
|
|
74
|
+
}
|
|
75
|
+
exports.createJsDocBlock = createJsDocBlock;
|
|
76
|
+
function makeJsDoc(desc, indentation = "") {
|
|
77
|
+
var _a, _b;
|
|
78
|
+
const comments = desc.getComments();
|
|
79
|
+
let text = "";
|
|
80
|
+
if (comments.leading !== undefined) {
|
|
81
|
+
text += comments.leading;
|
|
82
|
+
if (text.endsWith("\n")) {
|
|
83
|
+
text = text.substring(0, text.length - 1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (comments.trailing !== undefined) {
|
|
87
|
+
if (text.length > 0) {
|
|
88
|
+
text += "\n\n";
|
|
89
|
+
}
|
|
90
|
+
text += comments.trailing;
|
|
91
|
+
if (text.endsWith("\n")) {
|
|
92
|
+
text = text.substring(0, text.length - 1);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (text.length > 0) {
|
|
96
|
+
text += "\n\n";
|
|
97
|
+
}
|
|
98
|
+
text = text
|
|
99
|
+
.split("\n")
|
|
100
|
+
.map((line) => (line.startsWith(" ") ? line.substring(1) : line))
|
|
101
|
+
.join("\n");
|
|
102
|
+
switch (desc.kind) {
|
|
103
|
+
case "enum_value":
|
|
104
|
+
text += `@generated from enum value: ${desc.declarationString()};`;
|
|
105
|
+
break;
|
|
106
|
+
case "scalar_field":
|
|
107
|
+
case "enum_field":
|
|
108
|
+
case "message_field":
|
|
109
|
+
case "map_field":
|
|
110
|
+
text += `@generated from field: ${desc.declarationString()};`;
|
|
111
|
+
break;
|
|
112
|
+
default:
|
|
113
|
+
text += `@generated from ${desc.toString()}`;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
let deprecated = desc.deprecated;
|
|
117
|
+
switch (desc.kind) {
|
|
118
|
+
case "enum":
|
|
119
|
+
case "message":
|
|
120
|
+
case "service":
|
|
121
|
+
deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (deprecated) {
|
|
127
|
+
text += "\n@deprecated";
|
|
128
|
+
}
|
|
129
|
+
if (text.length > 0) {
|
|
130
|
+
return createJsDocBlock(text, indentation);
|
|
131
|
+
}
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
exports.makeJsDoc = makeJsDoc;
|
|
135
|
+
/**
|
|
136
|
+
* Returns an expression for the TypeScript typing of a field,
|
|
137
|
+
* and whether the property should be optional.
|
|
138
|
+
*/
|
|
139
|
+
function getFieldTyping(field, file) {
|
|
140
|
+
const typing = [];
|
|
141
|
+
let optional = false;
|
|
142
|
+
switch (field.kind) {
|
|
143
|
+
case "scalar_field":
|
|
144
|
+
typing.push(scalarTypeScriptType(field.scalar));
|
|
145
|
+
optional = field.optional;
|
|
146
|
+
break;
|
|
147
|
+
case "message_field": {
|
|
148
|
+
const baseType = getUnwrappedFieldType(field);
|
|
149
|
+
if (baseType !== undefined) {
|
|
150
|
+
typing.push(scalarTypeScriptType(baseType));
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
typing.push(file.import(field.message).toTypeOnly());
|
|
154
|
+
}
|
|
155
|
+
optional = true;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case "enum_field":
|
|
159
|
+
typing.push(file.import(field.enum).toTypeOnly());
|
|
160
|
+
optional = field.optional;
|
|
161
|
+
break;
|
|
162
|
+
case "map_field": {
|
|
163
|
+
let keyType;
|
|
164
|
+
switch (field.mapKey) {
|
|
165
|
+
case protobuf_1.ScalarType.INT32:
|
|
166
|
+
case protobuf_1.ScalarType.FIXED32:
|
|
167
|
+
case protobuf_1.ScalarType.UINT32:
|
|
168
|
+
case protobuf_1.ScalarType.SFIXED32:
|
|
169
|
+
case protobuf_1.ScalarType.SINT32:
|
|
170
|
+
keyType = "number";
|
|
171
|
+
break;
|
|
172
|
+
default:
|
|
173
|
+
keyType = "string";
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
let valueType;
|
|
177
|
+
switch (field.mapValue.kind) {
|
|
178
|
+
case "scalar":
|
|
179
|
+
valueType = scalarTypeScriptType(field.mapValue.scalar);
|
|
180
|
+
break;
|
|
181
|
+
case "message":
|
|
182
|
+
valueType = file.import(field.mapValue.message).toTypeOnly();
|
|
183
|
+
break;
|
|
184
|
+
case "enum":
|
|
185
|
+
valueType = file.import(field.mapValue.enum).toTypeOnly();
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
typing.push("{ [key: ", keyType, "]: ", valueType, " }");
|
|
189
|
+
optional = false;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (field.repeated) {
|
|
194
|
+
typing.push("[]");
|
|
195
|
+
optional = false;
|
|
196
|
+
}
|
|
197
|
+
return { typing, optional };
|
|
198
|
+
}
|
|
199
|
+
exports.getFieldTyping = getFieldTyping;
|
|
200
|
+
function scalarTypeScriptType(type) {
|
|
201
|
+
switch (type) {
|
|
202
|
+
case protobuf_1.ScalarType.STRING:
|
|
203
|
+
return "string";
|
|
204
|
+
case protobuf_1.ScalarType.BOOL:
|
|
205
|
+
return "boolean";
|
|
206
|
+
case protobuf_1.ScalarType.UINT64:
|
|
207
|
+
case protobuf_1.ScalarType.SFIXED64:
|
|
208
|
+
case protobuf_1.ScalarType.FIXED64:
|
|
209
|
+
case protobuf_1.ScalarType.SINT64:
|
|
210
|
+
case protobuf_1.ScalarType.INT64:
|
|
211
|
+
return "bigint";
|
|
212
|
+
case protobuf_1.ScalarType.BYTES:
|
|
213
|
+
return "Uint8Array";
|
|
214
|
+
default:
|
|
215
|
+
return "number";
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
exports.scalarTypeScriptType = scalarTypeScriptType;
|
|
219
|
+
function literalString(value) {
|
|
220
|
+
return ('"' +
|
|
221
|
+
value
|
|
222
|
+
.split("\\")
|
|
223
|
+
.join("\\\\")
|
|
224
|
+
.split('"')
|
|
225
|
+
.join('\\"')
|
|
226
|
+
.split("\r")
|
|
227
|
+
.join("\\r")
|
|
228
|
+
.split("\n")
|
|
229
|
+
.join("\\n") +
|
|
230
|
+
'"');
|
|
231
|
+
}
|
|
232
|
+
exports.literalString = literalString;
|
|
233
|
+
function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
|
|
234
|
+
switch (field.kind) {
|
|
235
|
+
case "enum_field": {
|
|
236
|
+
const value = field.enum.values.find((v) => v.number === field.getDefaultValue());
|
|
237
|
+
if (value !== undefined) {
|
|
238
|
+
return [value.parent, ".", localName(value)];
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case "scalar_field": {
|
|
243
|
+
const defaultValue = field.getDefaultValue();
|
|
244
|
+
if (defaultValue === undefined) {
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
switch (field.scalar) {
|
|
248
|
+
case protobuf_1.ScalarType.FLOAT:
|
|
249
|
+
case protobuf_1.ScalarType.DOUBLE: {
|
|
250
|
+
return defaultValue;
|
|
251
|
+
}
|
|
252
|
+
case protobuf_1.ScalarType.INT64:
|
|
253
|
+
case protobuf_1.ScalarType.SINT64:
|
|
254
|
+
case protobuf_1.ScalarType.SFIXED64:
|
|
255
|
+
return [protoInt64Symbol, `.parse("${defaultValue.toString()}")`];
|
|
256
|
+
case protobuf_1.ScalarType.UINT64:
|
|
257
|
+
case protobuf_1.ScalarType.FIXED64:
|
|
258
|
+
return [protoInt64Symbol, `.uParse("${defaultValue.toString()}")`];
|
|
259
|
+
case protobuf_1.ScalarType.INT32:
|
|
260
|
+
case protobuf_1.ScalarType.FIXED32:
|
|
261
|
+
case protobuf_1.ScalarType.UINT32:
|
|
262
|
+
case protobuf_1.ScalarType.SFIXED32:
|
|
263
|
+
case protobuf_1.ScalarType.SINT32:
|
|
264
|
+
return defaultValue;
|
|
265
|
+
case protobuf_1.ScalarType.BOOL: {
|
|
266
|
+
return defaultValue;
|
|
267
|
+
}
|
|
268
|
+
case protobuf_1.ScalarType.STRING: {
|
|
269
|
+
if (typeof defaultValue == "string") {
|
|
270
|
+
return literalString(defaultValue);
|
|
271
|
+
}
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
case protobuf_1.ScalarType.BYTES: {
|
|
275
|
+
if (defaultValue instanceof Uint8Array) {
|
|
276
|
+
return defaultValue;
|
|
277
|
+
}
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
default:
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
return undefined;
|
|
287
|
+
}
|
|
288
|
+
exports.getFieldExplicitDefaultValue = getFieldExplicitDefaultValue;
|
|
289
|
+
function getFieldIntrinsicDefaultValue(field) {
|
|
290
|
+
if (field.repeated) {
|
|
291
|
+
return {
|
|
292
|
+
defaultValue: "[]",
|
|
293
|
+
typingInferrable: false,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
if (field.kind == "map_field") {
|
|
297
|
+
return {
|
|
298
|
+
defaultValue: "{}",
|
|
299
|
+
typingInferrable: false,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
let defaultValue = undefined;
|
|
303
|
+
let typingInferrable = false;
|
|
304
|
+
if (field.parent.file.syntax == "proto3") {
|
|
305
|
+
switch (field.kind) {
|
|
306
|
+
case "enum_field": {
|
|
307
|
+
if (!field.optional) {
|
|
308
|
+
const zeroValue = field.enum.values.find((v) => v.number === 0);
|
|
309
|
+
if (zeroValue === undefined) {
|
|
310
|
+
throw new Error("invalid proto3 enum: missing 0 value");
|
|
311
|
+
}
|
|
312
|
+
defaultValue = [field.enum, ".", localName(zeroValue)];
|
|
313
|
+
typingInferrable = true;
|
|
314
|
+
}
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case "scalar_field":
|
|
318
|
+
if (!field.optional) {
|
|
319
|
+
typingInferrable = true;
|
|
320
|
+
if (field.scalar === protobuf_1.ScalarType.STRING) {
|
|
321
|
+
defaultValue = literalString("");
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
|
|
325
|
+
defaultValue = scalarDefaultValue(field.scalar);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
break;
|
|
329
|
+
default:
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
defaultValue,
|
|
335
|
+
typingInferrable,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
exports.getFieldIntrinsicDefaultValue = getFieldIntrinsicDefaultValue;
|