@bufbuild/protoc-gen-es 0.0.8 → 0.1.0

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,159 @@
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.matchWkt = void 0;
17
+ const protobuf_1 = require("@bufbuild/protobuf");
18
+ function matchWkt(message) {
19
+ switch (message.typeName) {
20
+ case "google.protobuf.Any": {
21
+ const typeUrl = message.fields.find((f) => f.number == 1 &&
22
+ f.kind == "scalar_field" &&
23
+ f.scalar === protobuf_1.ScalarType.STRING);
24
+ const value = message.fields.find((f) => f.number == 2 &&
25
+ f.kind == "scalar_field" &&
26
+ f.scalar === protobuf_1.ScalarType.BYTES);
27
+ if (typeUrl && value) {
28
+ return {
29
+ typeName: message.typeName,
30
+ typeUrl,
31
+ value,
32
+ };
33
+ }
34
+ break;
35
+ }
36
+ case "google.protobuf.Timestamp": {
37
+ const seconds = message.fields.find((f) => f.number == 1 &&
38
+ f.kind == "scalar_field" &&
39
+ f.scalar === protobuf_1.ScalarType.INT64);
40
+ const nanos = message.fields.find((f) => f.number == 2 &&
41
+ f.kind == "scalar_field" &&
42
+ f.scalar === protobuf_1.ScalarType.INT32);
43
+ if (seconds && nanos) {
44
+ return {
45
+ typeName: message.typeName,
46
+ seconds,
47
+ nanos,
48
+ };
49
+ }
50
+ break;
51
+ }
52
+ case "google.protobuf.Duration": {
53
+ const seconds = message.fields.find((f) => f.number == 1 &&
54
+ f.kind == "scalar_field" &&
55
+ f.scalar === protobuf_1.ScalarType.INT64);
56
+ const nanos = message.fields.find((f) => f.number == 2 &&
57
+ f.kind == "scalar_field" &&
58
+ f.scalar === protobuf_1.ScalarType.INT32);
59
+ if (seconds && nanos) {
60
+ return {
61
+ typeName: message.typeName,
62
+ seconds,
63
+ nanos,
64
+ };
65
+ }
66
+ break;
67
+ }
68
+ case "google.protobuf.Struct": {
69
+ const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
70
+ if ((fields === null || fields === void 0 ? void 0 : fields.kind) !== "map_field" ||
71
+ fields.mapValue.kind !== "message" ||
72
+ fields.mapValue.message.typeName !== "google.protobuf.Value") {
73
+ break;
74
+ }
75
+ return { typeName: message.typeName, fields };
76
+ }
77
+ case "google.protobuf.Value": {
78
+ const kind = message.oneofs.find((o) => o.name === "kind");
79
+ const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
80
+ if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.kind) !== "enum_field" ||
81
+ nullValue.enum.typeName !== "google.protobuf.NullValue") {
82
+ return undefined;
83
+ }
84
+ const numberValue = message.fields.find((f) => f.number == 2 &&
85
+ f.kind == "scalar_field" &&
86
+ f.scalar === protobuf_1.ScalarType.DOUBLE &&
87
+ f.oneof === kind);
88
+ const stringValue = message.fields.find((f) => f.number == 3 &&
89
+ f.kind == "scalar_field" &&
90
+ f.scalar === protobuf_1.ScalarType.STRING &&
91
+ f.oneof === kind);
92
+ const boolValue = message.fields.find((f) => f.number == 4 &&
93
+ f.kind == "scalar_field" &&
94
+ f.scalar === protobuf_1.ScalarType.BOOL &&
95
+ f.oneof === kind);
96
+ const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
97
+ if ((structValue === null || structValue === void 0 ? void 0 : structValue.kind) !== "message_field" ||
98
+ structValue.message.typeName !== "google.protobuf.Struct") {
99
+ return undefined;
100
+ }
101
+ const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
102
+ if ((listValue === null || listValue === void 0 ? void 0 : listValue.kind) !== "message_field" ||
103
+ listValue.message.typeName !== "google.protobuf.ListValue") {
104
+ return undefined;
105
+ }
106
+ if (kind && numberValue && stringValue && boolValue) {
107
+ return {
108
+ typeName: message.typeName,
109
+ kind,
110
+ nullValue,
111
+ numberValue,
112
+ stringValue,
113
+ boolValue,
114
+ structValue,
115
+ listValue,
116
+ };
117
+ }
118
+ break;
119
+ }
120
+ case "google.protobuf.ListValue": {
121
+ const values = message.fields.find((f) => f.number == 1 && f.repeated);
122
+ if ((values === null || values === void 0 ? void 0 : values.kind) != "message_field" ||
123
+ values.message.typeName !== "google.protobuf.Value") {
124
+ break;
125
+ }
126
+ return { typeName: message.typeName, values };
127
+ }
128
+ case "google.protobuf.FieldMask": {
129
+ const paths = message.fields.find((f) => f.number == 1 &&
130
+ f.kind == "scalar_field" &&
131
+ f.scalar === protobuf_1.ScalarType.STRING &&
132
+ f.repeated);
133
+ if (paths) {
134
+ return { typeName: message.typeName, paths };
135
+ }
136
+ break;
137
+ }
138
+ case "google.protobuf.DoubleValue":
139
+ case "google.protobuf.FloatValue":
140
+ case "google.protobuf.Int64Value":
141
+ case "google.protobuf.UInt64Value":
142
+ case "google.protobuf.Int32Value":
143
+ case "google.protobuf.UInt32Value":
144
+ case "google.protobuf.BoolValue":
145
+ case "google.protobuf.StringValue":
146
+ case "google.protobuf.BytesValue": {
147
+ const value = message.fields.find((f) => f.number == 1 && f.name == "value");
148
+ if (!value) {
149
+ break;
150
+ }
151
+ if (value.kind !== "scalar_field") {
152
+ break;
153
+ }
154
+ return { typeName: message.typeName, value };
155
+ }
156
+ }
157
+ return undefined;
158
+ }
159
+ exports.matchWkt = matchWkt;
@@ -0,0 +1,40 @@
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.protocGenEs = void 0;
17
+ const protoplugin_1 = require("@bufbuild/protoplugin");
18
+ const typescript_js_1 = require("./typescript.js");
19
+ const javascript_js_1 = require("./javascript.js");
20
+ const declaration_1 = require("./declaration");
21
+ const package_json_1 = require("../package.json");
22
+ exports.protocGenEs = (0, protoplugin_1.createEcmaScriptPlugin)({
23
+ name: "protoc-gen-es",
24
+ version: `v${String(package_json_1.version)}`,
25
+ }, (schema) => {
26
+ const targets = [typescript_js_1.typescript, javascript_js_1.javascript, declaration_1.declaration].filter((gen) => schema.targets.includes(gen.target));
27
+ for (const target of targets) {
28
+ for (const file of schema.files) {
29
+ const f = schema.generateFile(file.name + target.extension);
30
+ f.preamble(file);
31
+ for (const enumeration of file.enums) {
32
+ target.generateEnum(schema, f, enumeration);
33
+ }
34
+ for (const message of file.messages) {
35
+ target.generateMessage(schema, f, message);
36
+ }
37
+ // We do not generate anything for services, and we do not support extensions at this time
38
+ }
39
+ }
40
+ });