@bufbuild/protoc-gen-es 0.2.0 → 0.2.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.2.0"
23
+ "@bufbuild/protoplugin": "0.2.1"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.2.0"
26
+ "@bufbuild/protobuf": "0.2.1"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -15,7 +15,6 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateDts = void 0;
17
17
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
18
- const match_wkt_js_1 = require("./match-wkt.js");
19
18
  function generateDts(schema) {
20
19
  for (const file of schema.files) {
21
20
  const f = schema.generateFile(file.name + "_pb.d.ts");
@@ -119,7 +118,7 @@ function generateField(schema, f, field) {
119
118
  }
120
119
  // prettier-ignore
121
120
  function generateWktMethods(schema, f, message) {
122
- const ref = (0, match_wkt_js_1.matchWkt)(message);
121
+ const ref = (0, ecmascript_1.reifyWkt)(message);
123
122
  if (ref === undefined) {
124
123
  return;
125
124
  }
@@ -160,7 +159,7 @@ function generateWktMethods(schema, f, message) {
160
159
  }
161
160
  // prettier-ignore
162
161
  function generateWktStaticMethods(schema, f, message) {
163
- const ref = (0, match_wkt_js_1.matchWkt)(message);
162
+ const ref = (0, ecmascript_1.reifyWkt)(message);
164
163
  if (ref === undefined) {
165
164
  return;
166
165
  }
@@ -16,7 +16,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateFieldInfo = exports.generateJs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
- const match_wkt_js_1 = require("./match-wkt.js");
20
19
  function generateJs(schema) {
21
20
  for (const file of schema.files) {
22
21
  const f = schema.generateFile(file.name + "_pb.js");
@@ -148,7 +147,7 @@ exports.generateFieldInfo = generateFieldInfo;
148
147
  // prettier-ignore
149
148
  function generateWktMethods(schema, f, message) {
150
149
  var _a;
151
- const ref = (0, match_wkt_js_1.matchWkt)(message);
150
+ const ref = (0, ecmascript_1.reifyWkt)(message);
152
151
  if (ref === undefined) {
153
152
  return;
154
153
  }
@@ -178,6 +177,9 @@ function generateWktMethods(schema, f, message) {
178
177
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
179
178
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
180
179
  f.print(" }");
180
+ f.print(` if (Object.keys(json).length == 0) {`);
181
+ f.print(` return this;`);
182
+ f.print(` }`);
181
183
  f.print(` const typeUrl = json["@type"];`);
182
184
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
183
185
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -500,7 +502,7 @@ function generateWktMethods(schema, f, message) {
500
502
  }
501
503
  // prettier-ignore
502
504
  function generateWktStaticMethods(schema, f, message) {
503
- const ref = (0, match_wkt_js_1.matchWkt)(message);
505
+ const ref = (0, ecmascript_1.reifyWkt)(message);
504
506
  if (ref === undefined) {
505
507
  return;
506
508
  }
@@ -16,7 +16,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.generateTs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
- const match_wkt_js_1 = require("./match-wkt.js");
20
19
  const javascript_js_1 = require("./javascript.js");
21
20
  const ecmascript_2 = require("@bufbuild/protoplugin/ecmascript");
22
21
  function generateTs(schema) {
@@ -149,7 +148,7 @@ function generateField(schema, f, field) {
149
148
  // prettier-ignore
150
149
  function generateWktMethods(schema, f, message) {
151
150
  var _a;
152
- const ref = (0, match_wkt_js_1.matchWkt)(message);
151
+ const ref = (0, ecmascript_1.reifyWkt)(message);
153
152
  if (ref === undefined) {
154
153
  return;
155
154
  }
@@ -179,6 +178,9 @@ function generateWktMethods(schema, f, message) {
179
178
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
180
179
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
181
180
  f.print(" }");
181
+ f.print(` if (Object.keys(json).length == 0) {`);
182
+ f.print(` return this;`);
183
+ f.print(` }`);
182
184
  f.print(` const typeUrl = json["@type"];`);
183
185
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
184
186
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -501,7 +503,7 @@ function generateWktMethods(schema, f, message) {
501
503
  }
502
504
  // prettier-ignore
503
505
  function generateWktStaticMethods(schema, f, message) {
504
- const ref = (0, match_wkt_js_1.matchWkt)(message);
506
+ const ref = (0, ecmascript_1.reifyWkt)(message);
505
507
  if (ref === undefined) {
506
508
  return;
507
509
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.2.0"
23
+ "@bufbuild/protoplugin": "0.2.1"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.2.0"
26
+ "@bufbuild/protobuf": "0.2.1"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -28,8 +28,8 @@ import {
28
28
  literalString,
29
29
  localName,
30
30
  makeJsDoc,
31
+ reifyWkt,
31
32
  } from "@bufbuild/protoplugin/ecmascript";
32
- import { matchWkt } from "./match-wkt.js";
33
33
 
34
34
  export function generateDts(schema: Schema) {
35
35
  for (const file of schema.files) {
@@ -145,7 +145,7 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
145
145
 
146
146
  // prettier-ignore
147
147
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
148
- const ref = matchWkt(message);
148
+ const ref = reifyWkt(message);
149
149
  if (ref === undefined) {
150
150
  return;
151
151
  }
@@ -190,7 +190,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
190
190
 
191
191
  // prettier-ignore
192
192
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
193
- const ref = matchWkt(message);
193
+ const ref = reifyWkt(message);
194
194
  if (ref === undefined) {
195
195
  return;
196
196
  }
package/src/javascript.ts CHANGED
@@ -24,8 +24,8 @@ import {
24
24
  literalString,
25
25
  localName,
26
26
  makeJsDoc,
27
+ reifyWkt,
27
28
  } from "@bufbuild/protoplugin/ecmascript";
28
- import { matchWkt } from "./match-wkt.js";
29
29
 
30
30
  export function generateJs(schema: Schema) {
31
31
  for (const file of schema.files) {
@@ -157,7 +157,7 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
157
157
 
158
158
  // prettier-ignore
159
159
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
160
- const ref = matchWkt(message);
160
+ const ref = reifyWkt(message);
161
161
  if (ref === undefined) {
162
162
  return;
163
163
  }
@@ -190,6 +190,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
190
190
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
191
191
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
192
192
  f.print(" }");
193
+ f.print(` if (Object.keys(json).length == 0) {`);
194
+ f.print(` return this;`);
195
+ f.print(` }`);
193
196
  f.print(` const typeUrl = json["@type"];`);
194
197
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
195
198
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -513,7 +516,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
513
516
 
514
517
  // prettier-ignore
515
518
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
516
- const ref = matchWkt(message);
519
+ const ref = reifyWkt(message);
517
520
  if (ref === undefined) {
518
521
  return;
519
522
  }
package/src/typescript.ts CHANGED
@@ -29,8 +29,8 @@ import {
29
29
  getFieldIntrinsicDefaultValue,
30
30
  getFieldTyping,
31
31
  makeJsDoc,
32
+ reifyWkt,
32
33
  } from "@bufbuild/protoplugin/ecmascript";
33
- import { matchWkt } from "./match-wkt.js";
34
34
  import { generateFieldInfo } from "./javascript.js";
35
35
  import { literalString } from "@bufbuild/protoplugin/ecmascript";
36
36
 
@@ -175,7 +175,7 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
175
175
 
176
176
  // prettier-ignore
177
177
  function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
178
- const ref = matchWkt(message);
178
+ const ref = reifyWkt(message);
179
179
  if (ref === undefined) {
180
180
  return;
181
181
  }
@@ -214,6 +214,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
214
214
  f.print(` if (json === null || Array.isArray(json) || typeof json != "object") {`);
215
215
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);');
216
216
  f.print(" }");
217
+ f.print(` if (Object.keys(json).length == 0) {`);
218
+ f.print(` return this;`);
219
+ f.print(` }`);
217
220
  f.print(` const typeUrl = json["@type"];`);
218
221
  f.print(` if (typeof typeUrl != "string" || typeUrl == "") {`);
219
222
  f.print(" throw new Error(`cannot decode message ", message.typeName, ' from JSON: "@type" is empty`);');
@@ -537,7 +540,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
537
540
 
538
541
  // prettier-ignore
539
542
  function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: DescMessage) {
540
- const ref = matchWkt(message);
543
+ const ref = reifyWkt(message);
541
544
  if (ref === undefined) {
542
545
  return;
543
546
  }
@@ -1,159 +0,0 @@
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.fieldKind == "scalar" &&
23
- f.scalar === protobuf_1.ScalarType.STRING);
24
- const value = message.fields.find((f) => f.number == 2 &&
25
- f.fieldKind == "scalar" &&
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.fieldKind == "scalar" &&
39
- f.scalar === protobuf_1.ScalarType.INT64);
40
- const nanos = message.fields.find((f) => f.number == 2 &&
41
- f.fieldKind == "scalar" &&
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.fieldKind == "scalar" &&
55
- f.scalar === protobuf_1.ScalarType.INT64);
56
- const nanos = message.fields.find((f) => f.number == 2 &&
57
- f.fieldKind == "scalar" &&
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.fieldKind) !== "map" ||
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.fieldKind) !== "enum" ||
81
- nullValue.enum.typeName !== "google.protobuf.NullValue") {
82
- return undefined;
83
- }
84
- const numberValue = message.fields.find((f) => f.number == 2 &&
85
- f.fieldKind == "scalar" &&
86
- f.scalar === protobuf_1.ScalarType.DOUBLE &&
87
- f.oneof === kind);
88
- const stringValue = message.fields.find((f) => f.number == 3 &&
89
- f.fieldKind == "scalar" &&
90
- f.scalar === protobuf_1.ScalarType.STRING &&
91
- f.oneof === kind);
92
- const boolValue = message.fields.find((f) => f.number == 4 &&
93
- f.fieldKind == "scalar" &&
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.fieldKind) !== "message" ||
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.fieldKind) !== "message" ||
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.fieldKind) != "message" ||
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.fieldKind == "scalar" &&
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.fieldKind !== "scalar") {
152
- break;
153
- }
154
- return { typeName: message.typeName, value };
155
- }
156
- }
157
- return undefined;
158
- }
159
- exports.matchWkt = matchWkt;
package/src/match-wkt.ts DELETED
@@ -1,285 +0,0 @@
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 & { fieldKind: "map" };
41
- }
42
- | {
43
- typeName: "google.protobuf.Value";
44
- kind: DescOneof;
45
- nullValue: DescField & { fieldKind: "enum" };
46
- numberValue: DescField;
47
- stringValue: DescField;
48
- boolValue: DescField;
49
- structValue: DescField & { fieldKind: "message" };
50
- listValue: DescField & { fieldKind: "message" };
51
- }
52
- | {
53
- typeName: "google.protobuf.ListValue";
54
- values: DescField & { fieldKind: "message" };
55
- }
56
- | {
57
- typeName: "google.protobuf.FieldMask";
58
- paths: DescField;
59
- }
60
- | {
61
- typeName: "google.protobuf.DoubleValue";
62
- value: DescField & { fieldKind: "scalar" };
63
- }
64
- | {
65
- typeName: "google.protobuf.FloatValue";
66
- value: DescField & { fieldKind: "scalar" };
67
- }
68
- | {
69
- typeName: "google.protobuf.Int64Value";
70
- value: DescField & { fieldKind: "scalar" };
71
- }
72
- | {
73
- typeName: "google.protobuf.UInt64Value";
74
- value: DescField & { fieldKind: "scalar" };
75
- }
76
- | {
77
- typeName: "google.protobuf.Int32Value";
78
- value: DescField & { fieldKind: "scalar" };
79
- }
80
- | {
81
- typeName: "google.protobuf.UInt32Value";
82
- value: DescField & { fieldKind: "scalar" };
83
- }
84
- | {
85
- typeName: "google.protobuf.BoolValue";
86
- value: DescField & { fieldKind: "scalar" };
87
- }
88
- | {
89
- typeName: "google.protobuf.StringValue";
90
- value: DescField & { fieldKind: "scalar" };
91
- }
92
- | {
93
- typeName: "google.protobuf.BytesValue";
94
- value: DescField & { fieldKind: "scalar" };
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.fieldKind == "scalar" &&
104
- f.scalar === ScalarType.STRING
105
- );
106
- const value = message.fields.find(
107
- (f) =>
108
- f.number == 2 &&
109
- f.fieldKind == "scalar" &&
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.fieldKind == "scalar" &&
126
- f.scalar === ScalarType.INT64
127
- );
128
- const nanos = message.fields.find(
129
- (f) =>
130
- f.number == 2 &&
131
- f.fieldKind == "scalar" &&
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.fieldKind == "scalar" &&
148
- f.scalar === ScalarType.INT64
149
- );
150
- const nanos = message.fields.find(
151
- (f) =>
152
- f.number == 2 &&
153
- f.fieldKind == "scalar" &&
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?.fieldKind !== "map" ||
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?.fieldKind !== "enum" ||
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.fieldKind == "scalar" &&
191
- f.scalar === ScalarType.DOUBLE &&
192
- f.oneof === kind
193
- );
194
- const stringValue = message.fields.find(
195
- (f) =>
196
- f.number == 3 &&
197
- f.fieldKind == "scalar" &&
198
- f.scalar === ScalarType.STRING &&
199
- f.oneof === kind
200
- );
201
- const boolValue = message.fields.find(
202
- (f) =>
203
- f.number == 4 &&
204
- f.fieldKind == "scalar" &&
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?.fieldKind !== "message" ||
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?.fieldKind !== "message" ||
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?.fieldKind != "message" ||
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.fieldKind == "scalar" &&
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.fieldKind !== "scalar") {
279
- break;
280
- }
281
- return { typeName: message.typeName, value };
282
- }
283
- }
284
- return undefined;
285
- }