@bufbuild/protoplugin 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/dist/cjs/create-es-plugin.d.ts +7 -11
- package/dist/cjs/create-es-plugin.js +15 -13
- package/dist/cjs/ecmascript/file-preamble.js +11 -7
- package/dist/cjs/ecmascript/generated-file.d.ts +19 -20
- package/dist/cjs/ecmascript/generated-file.js +95 -108
- package/dist/cjs/ecmascript/import-path.d.ts +0 -6
- package/dist/cjs/ecmascript/import-path.js +1 -22
- package/dist/cjs/ecmascript/import-symbol.d.ts +1 -3
- package/dist/cjs/ecmascript/import-symbol.js +3 -5
- package/dist/cjs/ecmascript/index.d.ts +3 -16
- package/dist/cjs/ecmascript/index.js +3 -29
- package/dist/cjs/ecmascript/jsdoc.d.ts +2 -13
- package/dist/cjs/ecmascript/jsdoc.js +39 -35
- package/dist/cjs/ecmascript/names.d.ts +4 -0
- package/dist/cjs/ecmascript/names.js +119 -0
- package/dist/cjs/ecmascript/parameter.d.ts +1 -1
- package/dist/cjs/ecmascript/parameter.js +3 -3
- package/dist/cjs/ecmascript/printable.d.ts +36 -0
- package/dist/cjs/ecmascript/runtime-imports.d.ts +12 -23
- package/dist/cjs/ecmascript/runtime-imports.js +28 -29
- package/dist/cjs/ecmascript/safe-identifier.d.ts +6 -0
- package/dist/cjs/ecmascript/safe-identifier.js +90 -0
- package/dist/cjs/ecmascript/schema.d.ts +9 -8
- package/dist/cjs/ecmascript/schema.js +72 -20
- package/dist/cjs/index.d.ts +1 -5
- package/dist/cjs/index.js +6 -1
- package/dist/cjs/plugin.d.ts +1 -1
- package/dist/cjs/run-node.js +4 -3
- package/dist/cjs/source-code-info.d.ts +23 -0
- package/dist/cjs/source-code-info.js +286 -0
- package/dist/esm/create-es-plugin.d.ts +7 -11
- package/dist/esm/create-es-plugin.js +15 -13
- package/dist/esm/ecmascript/file-preamble.js +8 -4
- package/dist/esm/ecmascript/generated-file.d.ts +19 -20
- package/dist/esm/ecmascript/generated-file.js +97 -110
- package/dist/esm/ecmascript/import-path.d.ts +0 -6
- package/dist/esm/ecmascript/import-path.js +0 -20
- package/dist/esm/ecmascript/import-symbol.d.ts +1 -3
- package/dist/esm/ecmascript/import-symbol.js +3 -5
- package/dist/esm/ecmascript/index.d.ts +3 -16
- package/dist/esm/ecmascript/index.js +1 -18
- package/dist/esm/ecmascript/jsdoc.d.ts +2 -13
- package/dist/esm/ecmascript/jsdoc.js +36 -33
- package/dist/esm/ecmascript/names.d.ts +4 -0
- package/dist/esm/ecmascript/names.js +113 -0
- package/dist/esm/ecmascript/parameter.d.ts +1 -1
- package/dist/esm/ecmascript/parameter.js +3 -3
- package/dist/esm/ecmascript/printable.d.ts +36 -0
- package/dist/esm/ecmascript/{opaque-printables.js → printable.js} +1 -1
- package/dist/esm/ecmascript/runtime-imports.d.ts +12 -23
- package/dist/esm/ecmascript/runtime-imports.js +28 -29
- package/dist/esm/ecmascript/safe-identifier.d.ts +6 -0
- package/dist/esm/ecmascript/safe-identifier.js +86 -0
- package/dist/esm/ecmascript/schema.d.ts +9 -8
- package/dist/esm/ecmascript/schema.js +74 -22
- package/dist/esm/index.d.ts +1 -5
- package/dist/esm/index.js +1 -0
- package/dist/esm/plugin.d.ts +1 -1
- package/dist/esm/run-node.js +4 -3
- package/dist/esm/source-code-info.d.ts +23 -0
- package/dist/esm/source-code-info.js +278 -0
- package/package.json +2 -2
- package/dist/cjs/ecmascript/legacy-custom-options.d.ts +0 -40
- package/dist/cjs/ecmascript/legacy-custom-options.js +0 -121
- package/dist/cjs/ecmascript/legacy-gencommon.d.ts +0 -25
- package/dist/cjs/ecmascript/legacy-gencommon.js +0 -237
- package/dist/cjs/ecmascript/opaque-printables.d.ts +0 -27
- package/dist/cjs/ecmascript/reify-wkt.d.ts +0 -100
- package/dist/cjs/ecmascript/reify-wkt.js +0 -170
- package/dist/esm/ecmascript/legacy-custom-options.d.ts +0 -40
- package/dist/esm/ecmascript/legacy-custom-options.js +0 -115
- package/dist/esm/ecmascript/legacy-gencommon.d.ts +0 -25
- package/dist/esm/ecmascript/legacy-gencommon.js +0 -230
- package/dist/esm/ecmascript/opaque-printables.d.ts +0 -27
- package/dist/esm/ecmascript/reify-wkt.d.ts +0 -100
- package/dist/esm/ecmascript/reify-wkt.js +0 -166
- /package/dist/cjs/ecmascript/{opaque-printables.js → printable.js} +0 -0
|
@@ -1,237 +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.getFieldIntrinsicDefaultValue = exports.getFieldExplicitDefaultValue = exports.getFieldTyping = exports.literalString = void 0;
|
|
17
|
-
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
-
const { localName, getUnwrappedFieldType, scalarZeroValue } = protobuf_1.codegenInfo;
|
|
19
|
-
/**
|
|
20
|
-
* @deprecated Please use GeneratedFile.string() instead
|
|
21
|
-
*/
|
|
22
|
-
function literalString(value) {
|
|
23
|
-
return ('"' +
|
|
24
|
-
value
|
|
25
|
-
.split("\\")
|
|
26
|
-
.join("\\\\")
|
|
27
|
-
.split('"')
|
|
28
|
-
.join('\\"')
|
|
29
|
-
.split("\r")
|
|
30
|
-
.join("\\r")
|
|
31
|
-
.split("\n")
|
|
32
|
-
.join("\\n") +
|
|
33
|
-
'"');
|
|
34
|
-
}
|
|
35
|
-
exports.literalString = literalString;
|
|
36
|
-
/**
|
|
37
|
-
* @deprecated
|
|
38
|
-
*/
|
|
39
|
-
function getFieldTyping(field, file) {
|
|
40
|
-
const typing = [];
|
|
41
|
-
let optional = false;
|
|
42
|
-
switch (field.fieldKind) {
|
|
43
|
-
case "scalar":
|
|
44
|
-
typing.push(scalarTypeScriptType(field.scalar, field.longType));
|
|
45
|
-
optional = field.optional;
|
|
46
|
-
break;
|
|
47
|
-
case "message": {
|
|
48
|
-
const baseType = getUnwrappedFieldType(field);
|
|
49
|
-
if (baseType !== undefined) {
|
|
50
|
-
typing.push(scalarTypeScriptType(baseType, protobuf_1.LongType.BIGINT));
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
typing.push(file.import(field.message).toTypeOnly());
|
|
54
|
-
}
|
|
55
|
-
optional = true;
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
case "enum":
|
|
59
|
-
typing.push(file.import(field.enum).toTypeOnly());
|
|
60
|
-
optional = field.optional;
|
|
61
|
-
break;
|
|
62
|
-
case "map": {
|
|
63
|
-
let keyType;
|
|
64
|
-
switch (field.mapKey) {
|
|
65
|
-
case protobuf_1.ScalarType.INT32:
|
|
66
|
-
case protobuf_1.ScalarType.FIXED32:
|
|
67
|
-
case protobuf_1.ScalarType.UINT32:
|
|
68
|
-
case protobuf_1.ScalarType.SFIXED32:
|
|
69
|
-
case protobuf_1.ScalarType.SINT32:
|
|
70
|
-
keyType = "number";
|
|
71
|
-
break;
|
|
72
|
-
default:
|
|
73
|
-
keyType = "string";
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
let valueType;
|
|
77
|
-
switch (field.mapValue.kind) {
|
|
78
|
-
case "scalar":
|
|
79
|
-
valueType = scalarTypeScriptType(field.mapValue.scalar, protobuf_1.LongType.BIGINT);
|
|
80
|
-
break;
|
|
81
|
-
case "message":
|
|
82
|
-
valueType = file.import(field.mapValue.message).toTypeOnly();
|
|
83
|
-
break;
|
|
84
|
-
case "enum":
|
|
85
|
-
valueType = file.import(field.mapValue.enum).toTypeOnly();
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
typing.push("{ [key: ", keyType, "]: ", valueType, " }");
|
|
89
|
-
optional = false;
|
|
90
|
-
break;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
if (field.repeated) {
|
|
94
|
-
typing.push("[]");
|
|
95
|
-
optional = false;
|
|
96
|
-
}
|
|
97
|
-
return { typing, optional };
|
|
98
|
-
}
|
|
99
|
-
exports.getFieldTyping = getFieldTyping;
|
|
100
|
-
/**
|
|
101
|
-
* @deprecated
|
|
102
|
-
*/
|
|
103
|
-
function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
|
|
104
|
-
switch (field.fieldKind) {
|
|
105
|
-
case "enum": {
|
|
106
|
-
const value = field.enum.values.find((v) => v.number === field.getDefaultValue());
|
|
107
|
-
if (value !== undefined) {
|
|
108
|
-
return [value.parent, ".", localName(value)];
|
|
109
|
-
}
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
case "scalar": {
|
|
113
|
-
const defaultValue = field.getDefaultValue();
|
|
114
|
-
if (defaultValue === undefined) {
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
switch (field.scalar) {
|
|
118
|
-
case protobuf_1.ScalarType.FLOAT:
|
|
119
|
-
case protobuf_1.ScalarType.DOUBLE: {
|
|
120
|
-
return defaultValue;
|
|
121
|
-
}
|
|
122
|
-
case protobuf_1.ScalarType.INT64:
|
|
123
|
-
case protobuf_1.ScalarType.SINT64:
|
|
124
|
-
case protobuf_1.ScalarType.SFIXED64:
|
|
125
|
-
return [protoInt64Symbol, `.parse("${defaultValue.toString()}")`];
|
|
126
|
-
case protobuf_1.ScalarType.UINT64:
|
|
127
|
-
case protobuf_1.ScalarType.FIXED64:
|
|
128
|
-
return [protoInt64Symbol, `.uParse("${defaultValue.toString()}")`];
|
|
129
|
-
case protobuf_1.ScalarType.INT32:
|
|
130
|
-
case protobuf_1.ScalarType.FIXED32:
|
|
131
|
-
case protobuf_1.ScalarType.UINT32:
|
|
132
|
-
case protobuf_1.ScalarType.SFIXED32:
|
|
133
|
-
case protobuf_1.ScalarType.SINT32:
|
|
134
|
-
return defaultValue;
|
|
135
|
-
case protobuf_1.ScalarType.BOOL: {
|
|
136
|
-
return defaultValue;
|
|
137
|
-
}
|
|
138
|
-
case protobuf_1.ScalarType.STRING: {
|
|
139
|
-
if (typeof defaultValue == "string") {
|
|
140
|
-
return literalString(defaultValue);
|
|
141
|
-
}
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
case protobuf_1.ScalarType.BYTES: {
|
|
145
|
-
if (defaultValue instanceof Uint8Array) {
|
|
146
|
-
return defaultValue;
|
|
147
|
-
}
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
break;
|
|
152
|
-
}
|
|
153
|
-
default:
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
exports.getFieldExplicitDefaultValue = getFieldExplicitDefaultValue;
|
|
159
|
-
/**
|
|
160
|
-
* @deprecated
|
|
161
|
-
*/
|
|
162
|
-
function getFieldIntrinsicDefaultValue(field) {
|
|
163
|
-
if (field.repeated) {
|
|
164
|
-
return {
|
|
165
|
-
defaultValue: "[]",
|
|
166
|
-
typingInferrable: false,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
if (field.fieldKind == "map") {
|
|
170
|
-
return {
|
|
171
|
-
defaultValue: "{}",
|
|
172
|
-
typingInferrable: false,
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
let defaultValue = undefined;
|
|
176
|
-
let typingInferrable = false;
|
|
177
|
-
if (field.parent.file.syntax == "proto3") {
|
|
178
|
-
switch (field.fieldKind) {
|
|
179
|
-
case "enum": {
|
|
180
|
-
if (!field.optional) {
|
|
181
|
-
const zeroValue = field.enum.values.find((v) => v.number === 0);
|
|
182
|
-
if (zeroValue === undefined) {
|
|
183
|
-
throw new Error("invalid proto3 enum: missing 0 value");
|
|
184
|
-
}
|
|
185
|
-
defaultValue = [field.enum, ".", localName(zeroValue)];
|
|
186
|
-
typingInferrable = true;
|
|
187
|
-
}
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
case "scalar":
|
|
191
|
-
if (!field.optional) {
|
|
192
|
-
typingInferrable = true;
|
|
193
|
-
if (field.scalar === protobuf_1.ScalarType.STRING) {
|
|
194
|
-
defaultValue = literalString("");
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
defaultValue = scalarZeroValue(field.scalar, field.longType);
|
|
198
|
-
if (typeof defaultValue === "string") {
|
|
199
|
-
defaultValue = literalString(defaultValue);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
break;
|
|
204
|
-
default:
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
return {
|
|
209
|
-
defaultValue,
|
|
210
|
-
typingInferrable,
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
exports.getFieldIntrinsicDefaultValue = getFieldIntrinsicDefaultValue;
|
|
214
|
-
/**
|
|
215
|
-
* @deprecated
|
|
216
|
-
*/
|
|
217
|
-
function scalarTypeScriptType(type, longType) {
|
|
218
|
-
switch (type) {
|
|
219
|
-
case protobuf_1.ScalarType.STRING:
|
|
220
|
-
return "string";
|
|
221
|
-
case protobuf_1.ScalarType.BOOL:
|
|
222
|
-
return "boolean";
|
|
223
|
-
case protobuf_1.ScalarType.UINT64:
|
|
224
|
-
case protobuf_1.ScalarType.SFIXED64:
|
|
225
|
-
case protobuf_1.ScalarType.FIXED64:
|
|
226
|
-
case protobuf_1.ScalarType.SINT64:
|
|
227
|
-
case protobuf_1.ScalarType.INT64:
|
|
228
|
-
if (longType === protobuf_1.LongType.STRING) {
|
|
229
|
-
return "string";
|
|
230
|
-
}
|
|
231
|
-
return "bigint";
|
|
232
|
-
case protobuf_1.ScalarType.BYTES:
|
|
233
|
-
return "Uint8Array";
|
|
234
|
-
default:
|
|
235
|
-
return "number";
|
|
236
|
-
}
|
|
237
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { DescEnum, DescExtension, DescMessage } from "@bufbuild/protobuf";
|
|
2
|
-
import { LongType, ScalarType } from "@bufbuild/protobuf";
|
|
3
|
-
export type LiteralProtoInt64 = {
|
|
4
|
-
readonly kind: "es_proto_int64";
|
|
5
|
-
type: ScalarType.INT64 | ScalarType.SINT64 | ScalarType.SFIXED64 | ScalarType.UINT64 | ScalarType.FIXED64;
|
|
6
|
-
longType: LongType;
|
|
7
|
-
value: bigint | string;
|
|
8
|
-
};
|
|
9
|
-
export type LiteralString = {
|
|
10
|
-
readonly kind: "es_string";
|
|
11
|
-
value: string;
|
|
12
|
-
};
|
|
13
|
-
export type RefDescMessage = {
|
|
14
|
-
readonly kind: "es_ref_message";
|
|
15
|
-
type: DescMessage;
|
|
16
|
-
typeOnly: boolean;
|
|
17
|
-
};
|
|
18
|
-
export type RefDescEnum = {
|
|
19
|
-
readonly kind: "es_ref_enum";
|
|
20
|
-
type: DescEnum;
|
|
21
|
-
typeOnly: boolean;
|
|
22
|
-
};
|
|
23
|
-
export type ExportDeclaration = {
|
|
24
|
-
readonly kind: "es_export_decl";
|
|
25
|
-
declaration: string;
|
|
26
|
-
name: string | DescMessage | DescEnum | DescExtension;
|
|
27
|
-
};
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import type { DescField, DescMessage, DescOneof } from "@bufbuild/protobuf";
|
|
2
|
-
type DescWkt = {
|
|
3
|
-
typeName: "google.protobuf.Any";
|
|
4
|
-
typeUrl: DescField;
|
|
5
|
-
value: DescField;
|
|
6
|
-
} | {
|
|
7
|
-
typeName: "google.protobuf.Timestamp";
|
|
8
|
-
seconds: DescField;
|
|
9
|
-
nanos: DescField;
|
|
10
|
-
} | {
|
|
11
|
-
typeName: "google.protobuf.Duration";
|
|
12
|
-
seconds: DescField;
|
|
13
|
-
nanos: DescField;
|
|
14
|
-
} | {
|
|
15
|
-
typeName: "google.protobuf.Struct";
|
|
16
|
-
fields: DescField & {
|
|
17
|
-
fieldKind: "map";
|
|
18
|
-
};
|
|
19
|
-
} | {
|
|
20
|
-
typeName: "google.protobuf.Value";
|
|
21
|
-
kind: DescOneof;
|
|
22
|
-
nullValue: DescField & {
|
|
23
|
-
fieldKind: "enum";
|
|
24
|
-
};
|
|
25
|
-
numberValue: DescField;
|
|
26
|
-
stringValue: DescField;
|
|
27
|
-
boolValue: DescField;
|
|
28
|
-
structValue: DescField & {
|
|
29
|
-
fieldKind: "message";
|
|
30
|
-
};
|
|
31
|
-
listValue: DescField & {
|
|
32
|
-
fieldKind: "message";
|
|
33
|
-
};
|
|
34
|
-
} | {
|
|
35
|
-
typeName: "google.protobuf.ListValue";
|
|
36
|
-
values: DescField & {
|
|
37
|
-
fieldKind: "message";
|
|
38
|
-
};
|
|
39
|
-
} | {
|
|
40
|
-
typeName: "google.protobuf.FieldMask";
|
|
41
|
-
paths: DescField;
|
|
42
|
-
} | {
|
|
43
|
-
typeName: "google.protobuf.DoubleValue";
|
|
44
|
-
value: DescField & {
|
|
45
|
-
fieldKind: "scalar";
|
|
46
|
-
};
|
|
47
|
-
} | {
|
|
48
|
-
typeName: "google.protobuf.FloatValue";
|
|
49
|
-
value: DescField & {
|
|
50
|
-
fieldKind: "scalar";
|
|
51
|
-
};
|
|
52
|
-
} | {
|
|
53
|
-
typeName: "google.protobuf.Int64Value";
|
|
54
|
-
value: DescField & {
|
|
55
|
-
fieldKind: "scalar";
|
|
56
|
-
};
|
|
57
|
-
} | {
|
|
58
|
-
typeName: "google.protobuf.UInt64Value";
|
|
59
|
-
value: DescField & {
|
|
60
|
-
fieldKind: "scalar";
|
|
61
|
-
};
|
|
62
|
-
} | {
|
|
63
|
-
typeName: "google.protobuf.Int32Value";
|
|
64
|
-
value: DescField & {
|
|
65
|
-
fieldKind: "scalar";
|
|
66
|
-
};
|
|
67
|
-
} | {
|
|
68
|
-
typeName: "google.protobuf.UInt32Value";
|
|
69
|
-
value: DescField & {
|
|
70
|
-
fieldKind: "scalar";
|
|
71
|
-
};
|
|
72
|
-
} | {
|
|
73
|
-
typeName: "google.protobuf.BoolValue";
|
|
74
|
-
value: DescField & {
|
|
75
|
-
fieldKind: "scalar";
|
|
76
|
-
};
|
|
77
|
-
} | {
|
|
78
|
-
typeName: "google.protobuf.StringValue";
|
|
79
|
-
value: DescField & {
|
|
80
|
-
fieldKind: "scalar";
|
|
81
|
-
};
|
|
82
|
-
} | {
|
|
83
|
-
typeName: "google.protobuf.BytesValue";
|
|
84
|
-
value: DescField & {
|
|
85
|
-
fieldKind: "scalar";
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
|
-
* Reifies a given DescMessage into a more concrete object representing its
|
|
90
|
-
* respective well-known type. The returned object will contain properties
|
|
91
|
-
* representing the WKT's defined fields.
|
|
92
|
-
*
|
|
93
|
-
* Useful during code generation when immediate access to a particular field
|
|
94
|
-
* is needed without having to search the object's typename and DescField list.
|
|
95
|
-
*
|
|
96
|
-
* Returns undefined if the WKT cannot be completely constructed via the
|
|
97
|
-
* DescMessage.
|
|
98
|
-
*/
|
|
99
|
-
export declare function reifyWkt(message: DescMessage): DescWkt | undefined;
|
|
100
|
-
export {};
|
|
@@ -1,170 +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.reifyWkt = void 0;
|
|
17
|
-
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
|
-
/**
|
|
19
|
-
* Reifies a given DescMessage into a more concrete object representing its
|
|
20
|
-
* respective well-known type. The returned object will contain properties
|
|
21
|
-
* representing the WKT's defined fields.
|
|
22
|
-
*
|
|
23
|
-
* Useful during code generation when immediate access to a particular field
|
|
24
|
-
* is needed without having to search the object's typename and DescField list.
|
|
25
|
-
*
|
|
26
|
-
* Returns undefined if the WKT cannot be completely constructed via the
|
|
27
|
-
* DescMessage.
|
|
28
|
-
*/
|
|
29
|
-
function reifyWkt(message) {
|
|
30
|
-
switch (message.typeName) {
|
|
31
|
-
case "google.protobuf.Any": {
|
|
32
|
-
const typeUrl = message.fields.find((f) => f.number == 1 &&
|
|
33
|
-
f.fieldKind == "scalar" &&
|
|
34
|
-
f.scalar === protobuf_1.ScalarType.STRING);
|
|
35
|
-
const value = message.fields.find((f) => f.number == 2 &&
|
|
36
|
-
f.fieldKind == "scalar" &&
|
|
37
|
-
f.scalar === protobuf_1.ScalarType.BYTES);
|
|
38
|
-
if (typeUrl && value) {
|
|
39
|
-
return {
|
|
40
|
-
typeName: message.typeName,
|
|
41
|
-
typeUrl,
|
|
42
|
-
value,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
case "google.protobuf.Timestamp": {
|
|
48
|
-
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
49
|
-
f.fieldKind == "scalar" &&
|
|
50
|
-
f.scalar === protobuf_1.ScalarType.INT64);
|
|
51
|
-
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
52
|
-
f.fieldKind == "scalar" &&
|
|
53
|
-
f.scalar === protobuf_1.ScalarType.INT32);
|
|
54
|
-
if (seconds && nanos) {
|
|
55
|
-
return {
|
|
56
|
-
typeName: message.typeName,
|
|
57
|
-
seconds,
|
|
58
|
-
nanos,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
case "google.protobuf.Duration": {
|
|
64
|
-
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
65
|
-
f.fieldKind == "scalar" &&
|
|
66
|
-
f.scalar === protobuf_1.ScalarType.INT64);
|
|
67
|
-
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
68
|
-
f.fieldKind == "scalar" &&
|
|
69
|
-
f.scalar === protobuf_1.ScalarType.INT32);
|
|
70
|
-
if (seconds && nanos) {
|
|
71
|
-
return {
|
|
72
|
-
typeName: message.typeName,
|
|
73
|
-
seconds,
|
|
74
|
-
nanos,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
case "google.protobuf.Struct": {
|
|
80
|
-
const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
|
|
81
|
-
if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
|
|
82
|
-
fields.mapValue.kind !== "message" ||
|
|
83
|
-
fields.mapValue.message.typeName !== "google.protobuf.Value") {
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
return { typeName: message.typeName, fields };
|
|
87
|
-
}
|
|
88
|
-
case "google.protobuf.Value": {
|
|
89
|
-
const kind = message.oneofs.find((o) => o.name === "kind");
|
|
90
|
-
const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
|
|
91
|
-
if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
|
|
92
|
-
nullValue.enum.typeName !== "google.protobuf.NullValue") {
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
const numberValue = message.fields.find((f) => f.number == 2 &&
|
|
96
|
-
f.fieldKind == "scalar" &&
|
|
97
|
-
f.scalar === protobuf_1.ScalarType.DOUBLE &&
|
|
98
|
-
f.oneof === kind);
|
|
99
|
-
const stringValue = message.fields.find((f) => f.number == 3 &&
|
|
100
|
-
f.fieldKind == "scalar" &&
|
|
101
|
-
f.scalar === protobuf_1.ScalarType.STRING &&
|
|
102
|
-
f.oneof === kind);
|
|
103
|
-
const boolValue = message.fields.find((f) => f.number == 4 &&
|
|
104
|
-
f.fieldKind == "scalar" &&
|
|
105
|
-
f.scalar === protobuf_1.ScalarType.BOOL &&
|
|
106
|
-
f.oneof === kind);
|
|
107
|
-
const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
|
|
108
|
-
if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
|
|
109
|
-
structValue.message.typeName !== "google.protobuf.Struct") {
|
|
110
|
-
return undefined;
|
|
111
|
-
}
|
|
112
|
-
const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
|
|
113
|
-
if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
|
|
114
|
-
listValue.message.typeName !== "google.protobuf.ListValue") {
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
if (kind && numberValue && stringValue && boolValue) {
|
|
118
|
-
return {
|
|
119
|
-
typeName: message.typeName,
|
|
120
|
-
kind,
|
|
121
|
-
nullValue,
|
|
122
|
-
numberValue,
|
|
123
|
-
stringValue,
|
|
124
|
-
boolValue,
|
|
125
|
-
structValue,
|
|
126
|
-
listValue,
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
case "google.protobuf.ListValue": {
|
|
132
|
-
const values = message.fields.find((f) => f.number == 1 && f.repeated);
|
|
133
|
-
if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
|
|
134
|
-
values.message.typeName !== "google.protobuf.Value") {
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
return { typeName: message.typeName, values };
|
|
138
|
-
}
|
|
139
|
-
case "google.protobuf.FieldMask": {
|
|
140
|
-
const paths = message.fields.find((f) => f.number == 1 &&
|
|
141
|
-
f.fieldKind == "scalar" &&
|
|
142
|
-
f.scalar === protobuf_1.ScalarType.STRING &&
|
|
143
|
-
f.repeated);
|
|
144
|
-
if (paths) {
|
|
145
|
-
return { typeName: message.typeName, paths };
|
|
146
|
-
}
|
|
147
|
-
break;
|
|
148
|
-
}
|
|
149
|
-
case "google.protobuf.DoubleValue":
|
|
150
|
-
case "google.protobuf.FloatValue":
|
|
151
|
-
case "google.protobuf.Int64Value":
|
|
152
|
-
case "google.protobuf.UInt64Value":
|
|
153
|
-
case "google.protobuf.Int32Value":
|
|
154
|
-
case "google.protobuf.UInt32Value":
|
|
155
|
-
case "google.protobuf.BoolValue":
|
|
156
|
-
case "google.protobuf.StringValue":
|
|
157
|
-
case "google.protobuf.BytesValue": {
|
|
158
|
-
const value = message.fields.find((f) => f.number == 1 && f.name == "value");
|
|
159
|
-
if (!value) {
|
|
160
|
-
break;
|
|
161
|
-
}
|
|
162
|
-
if (value.fieldKind !== "scalar") {
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
return { typeName: message.typeName, value };
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return undefined;
|
|
169
|
-
}
|
|
170
|
-
exports.reifyWkt = reifyWkt;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { AnyDesc, MessageType } from "@bufbuild/protobuf";
|
|
2
|
-
import { Message, ScalarType } from "@bufbuild/protobuf";
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated Please use extensions instead.
|
|
5
|
-
*
|
|
6
|
-
* Returns the value of a custom option with a scalar type.
|
|
7
|
-
*
|
|
8
|
-
* If no option is found, returns undefined.
|
|
9
|
-
*/
|
|
10
|
-
export declare function findCustomScalarOption<T extends ScalarType>(desc: AnyDesc, extensionNumber: number, scalarType: T): ScalarValue<T> | undefined;
|
|
11
|
-
/**
|
|
12
|
-
* @deprecated Please use extensions instead.
|
|
13
|
-
*
|
|
14
|
-
* Returns the value of a custom message option for the given descriptor and
|
|
15
|
-
* extension number.
|
|
16
|
-
* The msgType param is then used to deserialize the message for returning to
|
|
17
|
-
* the caller.
|
|
18
|
-
*
|
|
19
|
-
* If no options are found, returns undefined.
|
|
20
|
-
*
|
|
21
|
-
* If the message option is unable to be read or deserialized, an error will
|
|
22
|
-
* be thrown.
|
|
23
|
-
*/
|
|
24
|
-
export declare function findCustomMessageOption<T extends Message<T>>(desc: AnyDesc, extensionNumber: number, msgType: MessageType<T>): T | undefined;
|
|
25
|
-
/**
|
|
26
|
-
* @deprecated Please use extensions instead.
|
|
27
|
-
*
|
|
28
|
-
* Returns the value of a custom enum option for the given descriptor and
|
|
29
|
-
* extension number.
|
|
30
|
-
*
|
|
31
|
-
* If no options are found, returns undefined.
|
|
32
|
-
*/
|
|
33
|
-
export declare function findCustomEnumOption(desc: AnyDesc, extensionNumber: number): number | undefined;
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Please use extensions instead.
|
|
36
|
-
*
|
|
37
|
-
* ScalarValue is a conditional type that pairs a ScalarType value with its concrete type.
|
|
38
|
-
*/
|
|
39
|
-
type ScalarValue<T> = T extends ScalarType.STRING ? string : T extends ScalarType.INT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.SINT32 ? number : T extends ScalarType.FIXED32 ? number : T extends ScalarType.SFIXED32 ? number : T extends ScalarType.FLOAT ? number : T extends ScalarType.DOUBLE ? number : T extends ScalarType.INT64 ? bigint | string : T extends ScalarType.SINT64 ? bigint | string : T extends ScalarType.SFIXED64 ? bigint | string : T extends ScalarType.UINT64 ? bigint | string : T extends ScalarType.FIXED64 ? bigint | string : T extends ScalarType.BOOL ? boolean : T extends ScalarType.BYTES ? Uint8Array : never;
|
|
40
|
-
export {};
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
import { proto3, BinaryReader, Message, ScalarType } from "@bufbuild/protobuf";
|
|
15
|
-
/**
|
|
16
|
-
* @deprecated Please use extensions instead.
|
|
17
|
-
*
|
|
18
|
-
* Returns the value of a custom option with a scalar type.
|
|
19
|
-
*
|
|
20
|
-
* If no option is found, returns undefined.
|
|
21
|
-
*/
|
|
22
|
-
export function findCustomScalarOption(desc, extensionNumber, scalarType) {
|
|
23
|
-
const reader = createBinaryReader(desc, extensionNumber);
|
|
24
|
-
if (reader) {
|
|
25
|
-
switch (scalarType) {
|
|
26
|
-
case ScalarType.INT32:
|
|
27
|
-
return reader.int32();
|
|
28
|
-
case ScalarType.UINT32:
|
|
29
|
-
return reader.uint32();
|
|
30
|
-
case ScalarType.SINT32:
|
|
31
|
-
return reader.sint32();
|
|
32
|
-
case ScalarType.FIXED32:
|
|
33
|
-
return reader.fixed32();
|
|
34
|
-
case ScalarType.SFIXED32:
|
|
35
|
-
return reader.sfixed32();
|
|
36
|
-
case ScalarType.FLOAT:
|
|
37
|
-
return reader.float();
|
|
38
|
-
case ScalarType.DOUBLE:
|
|
39
|
-
return reader.double();
|
|
40
|
-
case ScalarType.INT64:
|
|
41
|
-
return reader.int64();
|
|
42
|
-
case ScalarType.SINT64:
|
|
43
|
-
return reader.sint64();
|
|
44
|
-
case ScalarType.SFIXED64:
|
|
45
|
-
return reader.sfixed64();
|
|
46
|
-
case ScalarType.UINT64:
|
|
47
|
-
return reader.uint64();
|
|
48
|
-
case ScalarType.FIXED64:
|
|
49
|
-
return reader.fixed64();
|
|
50
|
-
case ScalarType.BOOL:
|
|
51
|
-
return reader.bool();
|
|
52
|
-
case ScalarType.BYTES:
|
|
53
|
-
return reader.bytes();
|
|
54
|
-
case ScalarType.STRING:
|
|
55
|
-
return reader.string();
|
|
56
|
-
default: {
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* @deprecated Please use extensions instead.
|
|
65
|
-
*
|
|
66
|
-
* Returns the value of a custom message option for the given descriptor and
|
|
67
|
-
* extension number.
|
|
68
|
-
* The msgType param is then used to deserialize the message for returning to
|
|
69
|
-
* the caller.
|
|
70
|
-
*
|
|
71
|
-
* If no options are found, returns undefined.
|
|
72
|
-
*
|
|
73
|
-
* If the message option is unable to be read or deserialized, an error will
|
|
74
|
-
* be thrown.
|
|
75
|
-
*/
|
|
76
|
-
export function findCustomMessageOption(desc, extensionNumber, msgType) {
|
|
77
|
-
const reader = createBinaryReader(desc, extensionNumber);
|
|
78
|
-
if (reader) {
|
|
79
|
-
try {
|
|
80
|
-
const data = reader.bytes();
|
|
81
|
-
return msgType.fromBinary(data);
|
|
82
|
-
}
|
|
83
|
-
catch (e) {
|
|
84
|
-
const innerMessage = e instanceof Error ? e.message : String(e);
|
|
85
|
-
throw new Error(`failed to access message option: ${innerMessage}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return undefined;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* @deprecated Please use extensions instead.
|
|
92
|
-
*
|
|
93
|
-
* Returns the value of a custom enum option for the given descriptor and
|
|
94
|
-
* extension number.
|
|
95
|
-
*
|
|
96
|
-
* If no options are found, returns undefined.
|
|
97
|
-
*/
|
|
98
|
-
export function findCustomEnumOption(desc, extensionNumber) {
|
|
99
|
-
return findCustomScalarOption(desc, extensionNumber, ScalarType.INT32);
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Returns a binary reader for the given descriptor and extension number.
|
|
103
|
-
*/
|
|
104
|
-
function createBinaryReader(desc, extensionNumber) {
|
|
105
|
-
const opt = desc.proto.options;
|
|
106
|
-
let reader = undefined;
|
|
107
|
-
if (opt !== undefined) {
|
|
108
|
-
const unknownFields = proto3.bin.listUnknownFields(opt);
|
|
109
|
-
const field = unknownFields.find((f) => f.no === extensionNumber);
|
|
110
|
-
if (field) {
|
|
111
|
-
reader = new BinaryReader(field.data);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return reader;
|
|
115
|
-
}
|