@bufbuild/protoc-gen-es 0.0.6 → 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.
@@ -0,0 +1,285 @@
1
+ // Copyright 2021-2022 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
+
15
+ import {
16
+ DescField,
17
+ DescMessage,
18
+ DescOneof,
19
+ ScalarType,
20
+ } from "@bufbuild/protobuf";
21
+
22
+ type DescWkt =
23
+ | {
24
+ typeName: "google.protobuf.Any";
25
+ typeUrl: DescField;
26
+ value: DescField;
27
+ }
28
+ | {
29
+ typeName: "google.protobuf.Timestamp";
30
+ seconds: DescField;
31
+ nanos: DescField;
32
+ }
33
+ | {
34
+ typeName: "google.protobuf.Duration";
35
+ seconds: DescField;
36
+ nanos: DescField;
37
+ }
38
+ | {
39
+ typeName: "google.protobuf.Struct";
40
+ fields: DescField & { kind: "map_field" };
41
+ }
42
+ | {
43
+ typeName: "google.protobuf.Value";
44
+ kind: DescOneof;
45
+ nullValue: DescField & { kind: "enum_field" };
46
+ numberValue: DescField;
47
+ stringValue: DescField;
48
+ boolValue: DescField;
49
+ structValue: DescField & { kind: "message_field" };
50
+ listValue: DescField & { kind: "message_field" };
51
+ }
52
+ | {
53
+ typeName: "google.protobuf.ListValue";
54
+ values: DescField & { kind: "message_field" };
55
+ }
56
+ | {
57
+ typeName: "google.protobuf.FieldMask";
58
+ paths: DescField;
59
+ }
60
+ | {
61
+ typeName: "google.protobuf.DoubleValue";
62
+ value: DescField & { kind: "scalar_field" };
63
+ }
64
+ | {
65
+ typeName: "google.protobuf.FloatValue";
66
+ value: DescField & { kind: "scalar_field" };
67
+ }
68
+ | {
69
+ typeName: "google.protobuf.Int64Value";
70
+ value: DescField & { kind: "scalar_field" };
71
+ }
72
+ | {
73
+ typeName: "google.protobuf.UInt64Value";
74
+ value: DescField & { kind: "scalar_field" };
75
+ }
76
+ | {
77
+ typeName: "google.protobuf.Int32Value";
78
+ value: DescField & { kind: "scalar_field" };
79
+ }
80
+ | {
81
+ typeName: "google.protobuf.UInt32Value";
82
+ value: DescField & { kind: "scalar_field" };
83
+ }
84
+ | {
85
+ typeName: "google.protobuf.BoolValue";
86
+ value: DescField & { kind: "scalar_field" };
87
+ }
88
+ | {
89
+ typeName: "google.protobuf.StringValue";
90
+ value: DescField & { kind: "scalar_field" };
91
+ }
92
+ | {
93
+ typeName: "google.protobuf.BytesValue";
94
+ value: DescField & { kind: "scalar_field" };
95
+ };
96
+
97
+ export function matchWkt(message: DescMessage): DescWkt | undefined {
98
+ switch (message.typeName) {
99
+ case "google.protobuf.Any": {
100
+ const typeUrl = message.fields.find(
101
+ (f) =>
102
+ f.number == 1 &&
103
+ f.kind == "scalar_field" &&
104
+ f.scalar === ScalarType.STRING
105
+ );
106
+ const value = message.fields.find(
107
+ (f) =>
108
+ f.number == 2 &&
109
+ f.kind == "scalar_field" &&
110
+ f.scalar === ScalarType.BYTES
111
+ );
112
+ if (typeUrl && value) {
113
+ return {
114
+ typeName: message.typeName,
115
+ typeUrl,
116
+ value,
117
+ };
118
+ }
119
+ break;
120
+ }
121
+ case "google.protobuf.Timestamp": {
122
+ const seconds = message.fields.find(
123
+ (f) =>
124
+ f.number == 1 &&
125
+ f.kind == "scalar_field" &&
126
+ f.scalar === ScalarType.INT64
127
+ );
128
+ const nanos = message.fields.find(
129
+ (f) =>
130
+ f.number == 2 &&
131
+ f.kind == "scalar_field" &&
132
+ f.scalar === ScalarType.INT32
133
+ );
134
+ if (seconds && nanos) {
135
+ return {
136
+ typeName: message.typeName,
137
+ seconds,
138
+ nanos,
139
+ };
140
+ }
141
+ break;
142
+ }
143
+ case "google.protobuf.Duration": {
144
+ const seconds = message.fields.find(
145
+ (f) =>
146
+ f.number == 1 &&
147
+ f.kind == "scalar_field" &&
148
+ f.scalar === ScalarType.INT64
149
+ );
150
+ const nanos = message.fields.find(
151
+ (f) =>
152
+ f.number == 2 &&
153
+ f.kind == "scalar_field" &&
154
+ f.scalar === ScalarType.INT32
155
+ );
156
+ if (seconds && nanos) {
157
+ return {
158
+ typeName: message.typeName,
159
+ seconds,
160
+ nanos,
161
+ };
162
+ }
163
+ break;
164
+ }
165
+ case "google.protobuf.Struct": {
166
+ const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
167
+ if (
168
+ fields?.kind !== "map_field" ||
169
+ fields.mapValue.kind !== "message" ||
170
+ fields.mapValue.message.typeName !== "google.protobuf.Value"
171
+ ) {
172
+ break;
173
+ }
174
+ return { typeName: message.typeName, fields };
175
+ }
176
+ case "google.protobuf.Value": {
177
+ const kind = message.oneofs.find((o) => o.name === "kind");
178
+ const nullValue = message.fields.find(
179
+ (f) => f.number == 1 && f.oneof === kind
180
+ );
181
+ if (
182
+ nullValue?.kind !== "enum_field" ||
183
+ nullValue.enum.typeName !== "google.protobuf.NullValue"
184
+ ) {
185
+ return undefined;
186
+ }
187
+ const numberValue = message.fields.find(
188
+ (f) =>
189
+ f.number == 2 &&
190
+ f.kind == "scalar_field" &&
191
+ f.scalar === ScalarType.DOUBLE &&
192
+ f.oneof === kind
193
+ );
194
+ const stringValue = message.fields.find(
195
+ (f) =>
196
+ f.number == 3 &&
197
+ f.kind == "scalar_field" &&
198
+ f.scalar === ScalarType.STRING &&
199
+ f.oneof === kind
200
+ );
201
+ const boolValue = message.fields.find(
202
+ (f) =>
203
+ f.number == 4 &&
204
+ f.kind == "scalar_field" &&
205
+ f.scalar === ScalarType.BOOL &&
206
+ f.oneof === kind
207
+ );
208
+ const structValue = message.fields.find(
209
+ (f) => f.number == 5 && f.oneof === kind
210
+ );
211
+ if (
212
+ structValue?.kind !== "message_field" ||
213
+ structValue.message.typeName !== "google.protobuf.Struct"
214
+ ) {
215
+ return undefined;
216
+ }
217
+ const listValue = message.fields.find(
218
+ (f) => f.number == 6 && f.oneof === kind
219
+ );
220
+ if (
221
+ listValue?.kind !== "message_field" ||
222
+ listValue.message.typeName !== "google.protobuf.ListValue"
223
+ ) {
224
+ return undefined;
225
+ }
226
+ if (kind && numberValue && stringValue && boolValue) {
227
+ return {
228
+ typeName: message.typeName,
229
+ kind,
230
+ nullValue,
231
+ numberValue,
232
+ stringValue,
233
+ boolValue,
234
+ structValue,
235
+ listValue,
236
+ };
237
+ }
238
+ break;
239
+ }
240
+ case "google.protobuf.ListValue": {
241
+ const values = message.fields.find((f) => f.number == 1 && f.repeated);
242
+ if (
243
+ values?.kind != "message_field" ||
244
+ values.message.typeName !== "google.protobuf.Value"
245
+ ) {
246
+ break;
247
+ }
248
+ return { typeName: message.typeName, values };
249
+ }
250
+ case "google.protobuf.FieldMask": {
251
+ const paths = message.fields.find(
252
+ (f) =>
253
+ f.number == 1 &&
254
+ f.kind == "scalar_field" &&
255
+ f.scalar === ScalarType.STRING &&
256
+ f.repeated
257
+ );
258
+ if (paths) {
259
+ return { typeName: message.typeName, paths };
260
+ }
261
+ break;
262
+ }
263
+ case "google.protobuf.DoubleValue":
264
+ case "google.protobuf.FloatValue":
265
+ case "google.protobuf.Int64Value":
266
+ case "google.protobuf.UInt64Value":
267
+ case "google.protobuf.Int32Value":
268
+ case "google.protobuf.UInt32Value":
269
+ case "google.protobuf.BoolValue":
270
+ case "google.protobuf.StringValue":
271
+ case "google.protobuf.BytesValue": {
272
+ const value = message.fields.find(
273
+ (f) => f.number == 1 && f.name == "value"
274
+ );
275
+ if (!value) {
276
+ break;
277
+ }
278
+ if (value.kind !== "scalar_field") {
279
+ break;
280
+ }
281
+ return { typeName: message.typeName, value };
282
+ }
283
+ }
284
+ return undefined;
285
+ }
@@ -0,0 +1,44 @@
1
+ // Copyright 2021-2022 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
+
15
+ import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
16
+ import { typescript } from "./typescript.js";
17
+ import { javascript } from "./javascript.js";
18
+ import { declaration } from "./declaration";
19
+ import { version } from "../package.json";
20
+
21
+ export const protocGenEs = createEcmaScriptPlugin(
22
+ {
23
+ name: "protoc-gen-es",
24
+ version: `v${String(version)}`,
25
+ },
26
+ (schema) => {
27
+ const targets = [typescript, javascript, declaration].filter((gen) =>
28
+ schema.targets.includes(gen.target)
29
+ );
30
+ for (const target of targets) {
31
+ for (const file of schema.files) {
32
+ const f = schema.generateFile(file.name + target.extension);
33
+ f.preamble(file);
34
+ for (const enumeration of file.enums) {
35
+ target.generateEnum(schema, f, enumeration);
36
+ }
37
+ for (const message of file.messages) {
38
+ target.generateMessage(schema, f, message);
39
+ }
40
+ // We do not generate anything for services, and we do not support extensions at this time
41
+ }
42
+ }
43
+ }
44
+ );