@bufbuild/protoplugin 1.8.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/dist/cjs/create-es-plugin.d.ts +8 -12
- package/dist/cjs/create-es-plugin.js +15 -13
- package/dist/cjs/ecmascript/file-preamble.js +8 -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 +23 -31
- package/dist/cjs/ecmascript/names.d.ts +4 -0
- package/dist/cjs/ecmascript/names.js +119 -0
- package/dist/cjs/ecmascript/parameter.d.ts +3 -3
- package/dist/cjs/ecmascript/parameter.js +2 -2
- 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 +10 -9
- package/dist/cjs/ecmascript/schema.js +64 -20
- package/dist/cjs/ecmascript/transpile.js +1 -1
- package/dist/cjs/index.d.ts +2 -6
- 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 +18 -0
- package/dist/cjs/source-code-info.js +259 -0
- package/dist/esm/create-es-plugin.d.ts +8 -12
- package/dist/esm/create-es-plugin.js +15 -13
- package/dist/esm/ecmascript/file-preamble.js +5 -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 +20 -29
- package/dist/esm/ecmascript/names.d.ts +4 -0
- package/dist/esm/ecmascript/names.js +113 -0
- package/dist/esm/ecmascript/parameter.d.ts +3 -3
- package/dist/esm/ecmascript/parameter.js +2 -2
- 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 +10 -9
- package/dist/esm/ecmascript/schema.js +66 -22
- package/dist/esm/ecmascript/transpile.js +1 -1
- package/dist/esm/index.d.ts +2 -6
- 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 +18 -0
- package/dist/esm/source-code-info.js +252 -0
- package/package.json +4 -4
- 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 -26
- package/dist/cjs/ecmascript/legacy-gencommon.js +0 -237
- package/dist/cjs/ecmascript/opaque-printables.d.ts +0 -26
- 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 -26
- package/dist/esm/ecmascript/legacy-gencommon.js +0 -230
- package/dist/esm/ecmascript/opaque-printables.d.ts +0 -26
- 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,26 +0,0 @@
|
|
|
1
|
-
import type { DescExtension } from "@bufbuild/protobuf";
|
|
2
|
-
import { DescField } from "@bufbuild/protobuf";
|
|
3
|
-
import type { GeneratedFile, Printable } from "./generated-file.js";
|
|
4
|
-
import type { ImportSymbol } from "./import-symbol.js";
|
|
5
|
-
/**
|
|
6
|
-
* @deprecated Please use GeneratedFile.string() instead
|
|
7
|
-
*/
|
|
8
|
-
export declare function literalString(value: string): string;
|
|
9
|
-
/**
|
|
10
|
-
* @deprecated
|
|
11
|
-
*/
|
|
12
|
-
export declare function getFieldTyping(field: DescField | DescExtension, file: GeneratedFile): {
|
|
13
|
-
typing: Printable;
|
|
14
|
-
optional: boolean;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated
|
|
18
|
-
*/
|
|
19
|
-
export declare function getFieldExplicitDefaultValue(field: DescField | DescExtension, protoInt64Symbol: ImportSymbol): Printable | undefined;
|
|
20
|
-
/**
|
|
21
|
-
* @deprecated
|
|
22
|
-
*/
|
|
23
|
-
export declare function getFieldIntrinsicDefaultValue(field: DescField): {
|
|
24
|
-
defaultValue: Printable | undefined;
|
|
25
|
-
typingInferrable: boolean;
|
|
26
|
-
};
|
|
@@ -1,230 +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 { codegenInfo, LongType, ScalarType, } from "@bufbuild/protobuf";
|
|
15
|
-
const { localName, getUnwrappedFieldType, scalarZeroValue } = codegenInfo;
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated Please use GeneratedFile.string() instead
|
|
18
|
-
*/
|
|
19
|
-
export function literalString(value) {
|
|
20
|
-
return ('"' +
|
|
21
|
-
value
|
|
22
|
-
.split("\\")
|
|
23
|
-
.join("\\\\")
|
|
24
|
-
.split('"')
|
|
25
|
-
.join('\\"')
|
|
26
|
-
.split("\r")
|
|
27
|
-
.join("\\r")
|
|
28
|
-
.split("\n")
|
|
29
|
-
.join("\\n") +
|
|
30
|
-
'"');
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* @deprecated
|
|
34
|
-
*/
|
|
35
|
-
export function getFieldTyping(field, file) {
|
|
36
|
-
const typing = [];
|
|
37
|
-
let optional = false;
|
|
38
|
-
switch (field.fieldKind) {
|
|
39
|
-
case "scalar":
|
|
40
|
-
typing.push(scalarTypeScriptType(field.scalar, field.longType));
|
|
41
|
-
optional = field.optional;
|
|
42
|
-
break;
|
|
43
|
-
case "message": {
|
|
44
|
-
const baseType = getUnwrappedFieldType(field);
|
|
45
|
-
if (baseType !== undefined) {
|
|
46
|
-
typing.push(scalarTypeScriptType(baseType, LongType.BIGINT));
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
typing.push(file.import(field.message).toTypeOnly());
|
|
50
|
-
}
|
|
51
|
-
optional = true;
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
case "enum":
|
|
55
|
-
typing.push(file.import(field.enum).toTypeOnly());
|
|
56
|
-
optional = field.optional;
|
|
57
|
-
break;
|
|
58
|
-
case "map": {
|
|
59
|
-
let keyType;
|
|
60
|
-
switch (field.mapKey) {
|
|
61
|
-
case ScalarType.INT32:
|
|
62
|
-
case ScalarType.FIXED32:
|
|
63
|
-
case ScalarType.UINT32:
|
|
64
|
-
case ScalarType.SFIXED32:
|
|
65
|
-
case ScalarType.SINT32:
|
|
66
|
-
keyType = "number";
|
|
67
|
-
break;
|
|
68
|
-
default:
|
|
69
|
-
keyType = "string";
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
let valueType;
|
|
73
|
-
switch (field.mapValue.kind) {
|
|
74
|
-
case "scalar":
|
|
75
|
-
valueType = scalarTypeScriptType(field.mapValue.scalar, LongType.BIGINT);
|
|
76
|
-
break;
|
|
77
|
-
case "message":
|
|
78
|
-
valueType = file.import(field.mapValue.message).toTypeOnly();
|
|
79
|
-
break;
|
|
80
|
-
case "enum":
|
|
81
|
-
valueType = file.import(field.mapValue.enum).toTypeOnly();
|
|
82
|
-
break;
|
|
83
|
-
}
|
|
84
|
-
typing.push("{ [key: ", keyType, "]: ", valueType, " }");
|
|
85
|
-
optional = false;
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (field.repeated) {
|
|
90
|
-
typing.push("[]");
|
|
91
|
-
optional = false;
|
|
92
|
-
}
|
|
93
|
-
return { typing, optional };
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* @deprecated
|
|
97
|
-
*/
|
|
98
|
-
export function getFieldExplicitDefaultValue(field, protoInt64Symbol) {
|
|
99
|
-
switch (field.fieldKind) {
|
|
100
|
-
case "enum": {
|
|
101
|
-
const value = field.enum.values.find((v) => v.number === field.getDefaultValue());
|
|
102
|
-
if (value !== undefined) {
|
|
103
|
-
return [value.parent, ".", localName(value)];
|
|
104
|
-
}
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
case "scalar": {
|
|
108
|
-
const defaultValue = field.getDefaultValue();
|
|
109
|
-
if (defaultValue === undefined) {
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
switch (field.scalar) {
|
|
113
|
-
case ScalarType.FLOAT:
|
|
114
|
-
case ScalarType.DOUBLE: {
|
|
115
|
-
return defaultValue;
|
|
116
|
-
}
|
|
117
|
-
case ScalarType.INT64:
|
|
118
|
-
case ScalarType.SINT64:
|
|
119
|
-
case ScalarType.SFIXED64:
|
|
120
|
-
return [protoInt64Symbol, `.parse("${defaultValue.toString()}")`];
|
|
121
|
-
case ScalarType.UINT64:
|
|
122
|
-
case ScalarType.FIXED64:
|
|
123
|
-
return [protoInt64Symbol, `.uParse("${defaultValue.toString()}")`];
|
|
124
|
-
case ScalarType.INT32:
|
|
125
|
-
case ScalarType.FIXED32:
|
|
126
|
-
case ScalarType.UINT32:
|
|
127
|
-
case ScalarType.SFIXED32:
|
|
128
|
-
case ScalarType.SINT32:
|
|
129
|
-
return defaultValue;
|
|
130
|
-
case ScalarType.BOOL: {
|
|
131
|
-
return defaultValue;
|
|
132
|
-
}
|
|
133
|
-
case ScalarType.STRING: {
|
|
134
|
-
if (typeof defaultValue == "string") {
|
|
135
|
-
return literalString(defaultValue);
|
|
136
|
-
}
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
case ScalarType.BYTES: {
|
|
140
|
-
if (defaultValue instanceof Uint8Array) {
|
|
141
|
-
return defaultValue;
|
|
142
|
-
}
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
default:
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
return undefined;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* @deprecated
|
|
155
|
-
*/
|
|
156
|
-
export function getFieldIntrinsicDefaultValue(field) {
|
|
157
|
-
if (field.repeated) {
|
|
158
|
-
return {
|
|
159
|
-
defaultValue: "[]",
|
|
160
|
-
typingInferrable: false,
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
if (field.fieldKind == "map") {
|
|
164
|
-
return {
|
|
165
|
-
defaultValue: "{}",
|
|
166
|
-
typingInferrable: false,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
let defaultValue = undefined;
|
|
170
|
-
let typingInferrable = false;
|
|
171
|
-
if (field.parent.file.syntax == "proto3") {
|
|
172
|
-
switch (field.fieldKind) {
|
|
173
|
-
case "enum": {
|
|
174
|
-
if (!field.optional) {
|
|
175
|
-
const zeroValue = field.enum.values.find((v) => v.number === 0);
|
|
176
|
-
if (zeroValue === undefined) {
|
|
177
|
-
throw new Error("invalid proto3 enum: missing 0 value");
|
|
178
|
-
}
|
|
179
|
-
defaultValue = [field.enum, ".", localName(zeroValue)];
|
|
180
|
-
typingInferrable = true;
|
|
181
|
-
}
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
184
|
-
case "scalar":
|
|
185
|
-
if (!field.optional) {
|
|
186
|
-
typingInferrable = true;
|
|
187
|
-
if (field.scalar === ScalarType.STRING) {
|
|
188
|
-
defaultValue = literalString("");
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
defaultValue = scalarZeroValue(field.scalar, field.longType);
|
|
192
|
-
if (typeof defaultValue === "string") {
|
|
193
|
-
defaultValue = literalString(defaultValue);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
break;
|
|
198
|
-
default:
|
|
199
|
-
break;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return {
|
|
203
|
-
defaultValue,
|
|
204
|
-
typingInferrable,
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* @deprecated
|
|
209
|
-
*/
|
|
210
|
-
function scalarTypeScriptType(type, longType) {
|
|
211
|
-
switch (type) {
|
|
212
|
-
case ScalarType.STRING:
|
|
213
|
-
return "string";
|
|
214
|
-
case ScalarType.BOOL:
|
|
215
|
-
return "boolean";
|
|
216
|
-
case ScalarType.UINT64:
|
|
217
|
-
case ScalarType.SFIXED64:
|
|
218
|
-
case ScalarType.FIXED64:
|
|
219
|
-
case ScalarType.SINT64:
|
|
220
|
-
case ScalarType.INT64:
|
|
221
|
-
if (longType === LongType.STRING) {
|
|
222
|
-
return "string";
|
|
223
|
-
}
|
|
224
|
-
return "bigint";
|
|
225
|
-
case ScalarType.BYTES:
|
|
226
|
-
return "Uint8Array";
|
|
227
|
-
default:
|
|
228
|
-
return "number";
|
|
229
|
-
}
|
|
230
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { DescEnum, DescExtension, DescMessage, LongType, ScalarType } from "@bufbuild/protobuf";
|
|
2
|
-
export type LiteralProtoInt64 = {
|
|
3
|
-
readonly kind: "es_proto_int64";
|
|
4
|
-
type: ScalarType.INT64 | ScalarType.SINT64 | ScalarType.SFIXED64 | ScalarType.UINT64 | ScalarType.FIXED64;
|
|
5
|
-
longType: LongType;
|
|
6
|
-
value: bigint | string;
|
|
7
|
-
};
|
|
8
|
-
export type LiteralString = {
|
|
9
|
-
readonly kind: "es_string";
|
|
10
|
-
value: string;
|
|
11
|
-
};
|
|
12
|
-
export type RefDescMessage = {
|
|
13
|
-
readonly kind: "es_ref_message";
|
|
14
|
-
type: DescMessage;
|
|
15
|
-
typeOnly: boolean;
|
|
16
|
-
};
|
|
17
|
-
export type RefDescEnum = {
|
|
18
|
-
readonly kind: "es_ref_enum";
|
|
19
|
-
type: DescEnum;
|
|
20
|
-
typeOnly: boolean;
|
|
21
|
-
};
|
|
22
|
-
export type ExportDeclaration = {
|
|
23
|
-
readonly kind: "es_export_decl";
|
|
24
|
-
declaration: string;
|
|
25
|
-
name: string | DescMessage | DescEnum | DescExtension;
|
|
26
|
-
};
|
|
@@ -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,166 +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 { ScalarType } from "@bufbuild/protobuf";
|
|
15
|
-
/**
|
|
16
|
-
* Reifies a given DescMessage into a more concrete object representing its
|
|
17
|
-
* respective well-known type. The returned object will contain properties
|
|
18
|
-
* representing the WKT's defined fields.
|
|
19
|
-
*
|
|
20
|
-
* Useful during code generation when immediate access to a particular field
|
|
21
|
-
* is needed without having to search the object's typename and DescField list.
|
|
22
|
-
*
|
|
23
|
-
* Returns undefined if the WKT cannot be completely constructed via the
|
|
24
|
-
* DescMessage.
|
|
25
|
-
*/
|
|
26
|
-
export function reifyWkt(message) {
|
|
27
|
-
switch (message.typeName) {
|
|
28
|
-
case "google.protobuf.Any": {
|
|
29
|
-
const typeUrl = message.fields.find((f) => f.number == 1 &&
|
|
30
|
-
f.fieldKind == "scalar" &&
|
|
31
|
-
f.scalar === ScalarType.STRING);
|
|
32
|
-
const value = message.fields.find((f) => f.number == 2 &&
|
|
33
|
-
f.fieldKind == "scalar" &&
|
|
34
|
-
f.scalar === ScalarType.BYTES);
|
|
35
|
-
if (typeUrl && value) {
|
|
36
|
-
return {
|
|
37
|
-
typeName: message.typeName,
|
|
38
|
-
typeUrl,
|
|
39
|
-
value,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
case "google.protobuf.Timestamp": {
|
|
45
|
-
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
46
|
-
f.fieldKind == "scalar" &&
|
|
47
|
-
f.scalar === ScalarType.INT64);
|
|
48
|
-
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
49
|
-
f.fieldKind == "scalar" &&
|
|
50
|
-
f.scalar === ScalarType.INT32);
|
|
51
|
-
if (seconds && nanos) {
|
|
52
|
-
return {
|
|
53
|
-
typeName: message.typeName,
|
|
54
|
-
seconds,
|
|
55
|
-
nanos,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
case "google.protobuf.Duration": {
|
|
61
|
-
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
62
|
-
f.fieldKind == "scalar" &&
|
|
63
|
-
f.scalar === ScalarType.INT64);
|
|
64
|
-
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
65
|
-
f.fieldKind == "scalar" &&
|
|
66
|
-
f.scalar === ScalarType.INT32);
|
|
67
|
-
if (seconds && nanos) {
|
|
68
|
-
return {
|
|
69
|
-
typeName: message.typeName,
|
|
70
|
-
seconds,
|
|
71
|
-
nanos,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
case "google.protobuf.Struct": {
|
|
77
|
-
const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
|
|
78
|
-
if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
|
|
79
|
-
fields.mapValue.kind !== "message" ||
|
|
80
|
-
fields.mapValue.message.typeName !== "google.protobuf.Value") {
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
return { typeName: message.typeName, fields };
|
|
84
|
-
}
|
|
85
|
-
case "google.protobuf.Value": {
|
|
86
|
-
const kind = message.oneofs.find((o) => o.name === "kind");
|
|
87
|
-
const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
|
|
88
|
-
if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
|
|
89
|
-
nullValue.enum.typeName !== "google.protobuf.NullValue") {
|
|
90
|
-
return undefined;
|
|
91
|
-
}
|
|
92
|
-
const numberValue = message.fields.find((f) => f.number == 2 &&
|
|
93
|
-
f.fieldKind == "scalar" &&
|
|
94
|
-
f.scalar === ScalarType.DOUBLE &&
|
|
95
|
-
f.oneof === kind);
|
|
96
|
-
const stringValue = message.fields.find((f) => f.number == 3 &&
|
|
97
|
-
f.fieldKind == "scalar" &&
|
|
98
|
-
f.scalar === ScalarType.STRING &&
|
|
99
|
-
f.oneof === kind);
|
|
100
|
-
const boolValue = message.fields.find((f) => f.number == 4 &&
|
|
101
|
-
f.fieldKind == "scalar" &&
|
|
102
|
-
f.scalar === ScalarType.BOOL &&
|
|
103
|
-
f.oneof === kind);
|
|
104
|
-
const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
|
|
105
|
-
if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
|
|
106
|
-
structValue.message.typeName !== "google.protobuf.Struct") {
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
|
|
110
|
-
if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
|
|
111
|
-
listValue.message.typeName !== "google.protobuf.ListValue") {
|
|
112
|
-
return undefined;
|
|
113
|
-
}
|
|
114
|
-
if (kind && numberValue && stringValue && boolValue) {
|
|
115
|
-
return {
|
|
116
|
-
typeName: message.typeName,
|
|
117
|
-
kind,
|
|
118
|
-
nullValue,
|
|
119
|
-
numberValue,
|
|
120
|
-
stringValue,
|
|
121
|
-
boolValue,
|
|
122
|
-
structValue,
|
|
123
|
-
listValue,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
case "google.protobuf.ListValue": {
|
|
129
|
-
const values = message.fields.find((f) => f.number == 1 && f.repeated);
|
|
130
|
-
if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
|
|
131
|
-
values.message.typeName !== "google.protobuf.Value") {
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
return { typeName: message.typeName, values };
|
|
135
|
-
}
|
|
136
|
-
case "google.protobuf.FieldMask": {
|
|
137
|
-
const paths = message.fields.find((f) => f.number == 1 &&
|
|
138
|
-
f.fieldKind == "scalar" &&
|
|
139
|
-
f.scalar === ScalarType.STRING &&
|
|
140
|
-
f.repeated);
|
|
141
|
-
if (paths) {
|
|
142
|
-
return { typeName: message.typeName, paths };
|
|
143
|
-
}
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
case "google.protobuf.DoubleValue":
|
|
147
|
-
case "google.protobuf.FloatValue":
|
|
148
|
-
case "google.protobuf.Int64Value":
|
|
149
|
-
case "google.protobuf.UInt64Value":
|
|
150
|
-
case "google.protobuf.Int32Value":
|
|
151
|
-
case "google.protobuf.UInt32Value":
|
|
152
|
-
case "google.protobuf.BoolValue":
|
|
153
|
-
case "google.protobuf.StringValue":
|
|
154
|
-
case "google.protobuf.BytesValue": {
|
|
155
|
-
const value = message.fields.find((f) => f.number == 1 && f.name == "value");
|
|
156
|
-
if (!value) {
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
if (value.fieldKind !== "scalar") {
|
|
160
|
-
break;
|
|
161
|
-
}
|
|
162
|
-
return { typeName: message.typeName, value };
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return undefined;
|
|
166
|
-
}
|
|
File without changes
|