@bufbuild/protobuf 0.2.1 → 0.4.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.
- package/dist/cjs/codegen-info.js +1 -0
- package/dist/cjs/google/protobuf/any_pb.js +22 -1
- package/dist/cjs/google/protobuf/struct_pb.js +1 -1
- package/dist/cjs/google/protobuf/type_pb.js +1 -1
- package/dist/cjs/google/protobuf/wrappers_pb.js +9 -9
- package/dist/cjs/message.js +23 -3
- package/dist/cjs/private/binary-format-common.js +8 -2
- package/dist/cjs/private/field-wrapper.js +7 -15
- package/dist/cjs/private/json-format-common.js +10 -5
- package/dist/cjs/private/util-common.js +10 -20
- package/dist/esm/codegen-info.js +1 -0
- package/dist/esm/google/protobuf/any_pb.js +22 -1
- package/dist/esm/google/protobuf/struct_pb.js +1 -1
- package/dist/esm/google/protobuf/type_pb.js +1 -1
- package/dist/esm/google/protobuf/wrappers_pb.js +9 -9
- package/dist/esm/message.js +23 -3
- package/dist/esm/private/binary-format-common.js +9 -3
- package/dist/esm/private/field-wrapper.js +6 -13
- package/dist/esm/private/json-format-common.js +10 -5
- package/dist/esm/private/util-common.js +10 -20
- package/dist/types/binary-format.d.ts +1 -1
- package/dist/types/codegen-info.d.ts +1 -1
- package/dist/types/google/protobuf/any_pb.d.ts +3 -1
- package/dist/types/google/protobuf/wrappers_pb.d.ts +9 -9
- package/dist/types/message.d.ts +32 -6
- package/dist/types/private/field-wrapper.d.ts +5 -8
- package/dist/types/private/util.d.ts +1 -1
- package/package.json +1 -1
package/dist/cjs/codegen-info.js
CHANGED
|
@@ -44,6 +44,7 @@ exports.codegenInfo = {
|
|
|
44
44
|
ScalarType: { typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName },
|
|
45
45
|
MethodKind: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
|
|
46
46
|
MethodIdempotency: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
|
|
47
|
+
IMessageTypeRegistry: { typeOnly: true, privateImportPath: "./type-registry.js", publicImportPath: packageName },
|
|
47
48
|
},
|
|
48
49
|
wktSourceFiles: [
|
|
49
50
|
"google/protobuf/compiler/plugin.proto",
|
|
@@ -204,8 +204,29 @@ class Any extends message_js_1.Message {
|
|
|
204
204
|
target.fromBinary(this.value);
|
|
205
205
|
return true;
|
|
206
206
|
}
|
|
207
|
+
unpack(registry) {
|
|
208
|
+
if (this.typeUrl === "") {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
const messageType = registry.findMessage(this.typeUrlToName(this.typeUrl));
|
|
212
|
+
if (!messageType) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
return messageType.fromBinary(this.value);
|
|
216
|
+
}
|
|
207
217
|
is(type) {
|
|
208
|
-
|
|
218
|
+
if (this.typeUrl === '') {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
const name = this.typeUrlToName(this.typeUrl);
|
|
222
|
+
let typeName = '';
|
|
223
|
+
if (typeof type === 'string') {
|
|
224
|
+
typeName = type;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
typeName = type.typeName;
|
|
228
|
+
}
|
|
229
|
+
return name === typeName;
|
|
209
230
|
}
|
|
210
231
|
typeNameToUrl(name) {
|
|
211
232
|
return `type.googleapis.com/${name}`;
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.ListValue = exports.Value = exports.Struct = exports.NullValue = void 0;
|
|
17
|
-
// @generated by protoc-gen-es v0.
|
|
17
|
+
// @generated by protoc-gen-es v0.4.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
|
|
18
18
|
// @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
|
|
19
19
|
/* eslint-disable */
|
|
20
20
|
const proto3_js_1 = require("../../proto3.js");
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.Option = exports.EnumValue = exports.Enum = exports.Field_Cardinality = exports.Field_Kind = exports.Field = exports.Type = exports.Syntax = void 0;
|
|
17
|
-
// @generated by protoc-gen-es v0.
|
|
17
|
+
// @generated by protoc-gen-es v0.4.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
|
|
18
18
|
// @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
|
|
19
19
|
/* eslint-disable */
|
|
20
20
|
const proto3_js_1 = require("../../proto3.js");
|
|
@@ -73,7 +73,7 @@ DoubleValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
73
73
|
]);
|
|
74
74
|
DoubleValue.fieldWrapper = {
|
|
75
75
|
wrapField(value) {
|
|
76
|
-
return
|
|
76
|
+
return new DoubleValue({ value });
|
|
77
77
|
},
|
|
78
78
|
unwrapField(value) {
|
|
79
79
|
return value.value;
|
|
@@ -134,7 +134,7 @@ FloatValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
134
134
|
]);
|
|
135
135
|
FloatValue.fieldWrapper = {
|
|
136
136
|
wrapField(value) {
|
|
137
|
-
return
|
|
137
|
+
return new FloatValue({ value });
|
|
138
138
|
},
|
|
139
139
|
unwrapField(value) {
|
|
140
140
|
return value.value;
|
|
@@ -195,7 +195,7 @@ Int64Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
195
195
|
]);
|
|
196
196
|
Int64Value.fieldWrapper = {
|
|
197
197
|
wrapField(value) {
|
|
198
|
-
return
|
|
198
|
+
return new Int64Value({ value });
|
|
199
199
|
},
|
|
200
200
|
unwrapField(value) {
|
|
201
201
|
return value.value;
|
|
@@ -256,7 +256,7 @@ UInt64Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
256
256
|
]);
|
|
257
257
|
UInt64Value.fieldWrapper = {
|
|
258
258
|
wrapField(value) {
|
|
259
|
-
return
|
|
259
|
+
return new UInt64Value({ value });
|
|
260
260
|
},
|
|
261
261
|
unwrapField(value) {
|
|
262
262
|
return value.value;
|
|
@@ -317,7 +317,7 @@ Int32Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
317
317
|
]);
|
|
318
318
|
Int32Value.fieldWrapper = {
|
|
319
319
|
wrapField(value) {
|
|
320
|
-
return
|
|
320
|
+
return new Int32Value({ value });
|
|
321
321
|
},
|
|
322
322
|
unwrapField(value) {
|
|
323
323
|
return value.value;
|
|
@@ -378,7 +378,7 @@ UInt32Value.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
378
378
|
]);
|
|
379
379
|
UInt32Value.fieldWrapper = {
|
|
380
380
|
wrapField(value) {
|
|
381
|
-
return
|
|
381
|
+
return new UInt32Value({ value });
|
|
382
382
|
},
|
|
383
383
|
unwrapField(value) {
|
|
384
384
|
return value.value;
|
|
@@ -439,7 +439,7 @@ BoolValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
439
439
|
]);
|
|
440
440
|
BoolValue.fieldWrapper = {
|
|
441
441
|
wrapField(value) {
|
|
442
|
-
return
|
|
442
|
+
return new BoolValue({ value });
|
|
443
443
|
},
|
|
444
444
|
unwrapField(value) {
|
|
445
445
|
return value.value;
|
|
@@ -500,7 +500,7 @@ StringValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
500
500
|
]);
|
|
501
501
|
StringValue.fieldWrapper = {
|
|
502
502
|
wrapField(value) {
|
|
503
|
-
return
|
|
503
|
+
return new StringValue({ value });
|
|
504
504
|
},
|
|
505
505
|
unwrapField(value) {
|
|
506
506
|
return value.value;
|
|
@@ -561,7 +561,7 @@ BytesValue.fields = proto3_js_1.proto3.util.newFieldList(() => [
|
|
|
561
561
|
]);
|
|
562
562
|
BytesValue.fieldWrapper = {
|
|
563
563
|
wrapField(value) {
|
|
564
|
-
return
|
|
564
|
+
return new BytesValue({ value });
|
|
565
565
|
},
|
|
566
566
|
unwrapField(value) {
|
|
567
567
|
return value.value;
|
package/dist/cjs/message.js
CHANGED
|
@@ -32,7 +32,6 @@ class Message {
|
|
|
32
32
|
* Create a deep copy.
|
|
33
33
|
*/
|
|
34
34
|
clone() {
|
|
35
|
-
// return this.getType().runtime.util.clone(this);
|
|
36
35
|
return this.getType().runtime.util.clone(this);
|
|
37
36
|
}
|
|
38
37
|
/**
|
|
@@ -61,8 +60,14 @@ class Message {
|
|
|
61
60
|
* Parse a message from a JSON string.
|
|
62
61
|
*/
|
|
63
62
|
fromJsonString(jsonString, options) {
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
let json;
|
|
64
|
+
try {
|
|
65
|
+
json = JSON.parse(jsonString);
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
throw new Error(`cannot decode ${this.getType().typeName} from JSON: ${e instanceof Error ? e.message : String(e)}`);
|
|
69
|
+
}
|
|
70
|
+
return this.fromJson(json, options);
|
|
66
71
|
}
|
|
67
72
|
/**
|
|
68
73
|
* Serialize the message to binary data.
|
|
@@ -88,6 +93,21 @@ class Message {
|
|
|
88
93
|
const value = this.toJson(options);
|
|
89
94
|
return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
|
|
90
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Override for serialization behavior. This will be invoked when calling
|
|
98
|
+
* JSON.stringify on this message (i.e. JSON.stringify(msg)).
|
|
99
|
+
*
|
|
100
|
+
* Note that this will not serialize google.protobuf.Any with a packed
|
|
101
|
+
* message because the protobuf JSON format specifies that it needs to be
|
|
102
|
+
* unpacked, and this is only possible with a type registry to look up the
|
|
103
|
+
* message type. As a result, attempting to serialize a message with this
|
|
104
|
+
* type will throw an Error.
|
|
105
|
+
*/
|
|
106
|
+
toJSON() {
|
|
107
|
+
return this.toJson({
|
|
108
|
+
emitDefaultValues: true,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
91
111
|
/**
|
|
92
112
|
* Retrieve the MessageType of this message - a singleton that represents
|
|
93
113
|
* the protobuf message declaration and provides metadata for reflection-
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.writePacked = exports.writeScalar = exports.writeMessageField = exports.writeMapEntry = exports.makeBinaryFormatCommon = void 0;
|
|
17
17
|
const binary_encoding_js_1 = require("../binary-encoding.js");
|
|
18
|
+
const message_js_1 = require("../message.js");
|
|
18
19
|
const field_js_1 = require("../field.js");
|
|
19
20
|
const field_wrapper_js_1 = require("./field-wrapper.js");
|
|
20
21
|
const scalars_js_1 = require("./scalars.js");
|
|
@@ -114,11 +115,16 @@ function makeBinaryFormatCommon() {
|
|
|
114
115
|
target[localName].push(messageType.fromBinary(reader.bytes(), options));
|
|
115
116
|
}
|
|
116
117
|
else {
|
|
117
|
-
if (target[localName] instanceof
|
|
118
|
+
if (target[localName] instanceof message_js_1.Message) {
|
|
118
119
|
target[localName].fromBinary(reader.bytes(), options);
|
|
119
120
|
}
|
|
120
121
|
else {
|
|
121
|
-
target[localName] =
|
|
122
|
+
target[localName] = messageType.fromBinary(reader.bytes(), options);
|
|
123
|
+
if (messageType.fieldWrapper &&
|
|
124
|
+
!field.oneof &&
|
|
125
|
+
!field.repeated) {
|
|
126
|
+
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
127
|
+
}
|
|
122
128
|
}
|
|
123
129
|
}
|
|
124
130
|
break;
|
|
@@ -13,31 +13,23 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.getUnwrappedFieldType = exports.
|
|
16
|
+
exports.getUnwrappedFieldType = exports.wrapField = void 0;
|
|
17
|
+
const message_js_1 = require("../message.js");
|
|
17
18
|
const field_js_1 = require("../field.js");
|
|
18
19
|
/**
|
|
19
|
-
* Wrap
|
|
20
|
+
* Wrap a primitive message field value in its corresponding wrapper
|
|
21
|
+
* message. This function is idempotent.
|
|
20
22
|
*/
|
|
21
23
|
function wrapField(type, value) {
|
|
22
|
-
if (value instanceof type) {
|
|
24
|
+
if (value instanceof message_js_1.Message || !type.fieldWrapper) {
|
|
23
25
|
return value;
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
return type.fieldWrapper.wrapField(value);
|
|
27
|
-
}
|
|
28
|
-
throw new Error(`cannot unwrap field value, ${type.typeName} does not define a field wrapper`);
|
|
27
|
+
return type.fieldWrapper.wrapField(value);
|
|
29
28
|
}
|
|
30
29
|
exports.wrapField = wrapField;
|
|
31
|
-
/**
|
|
32
|
-
* Unwrap field values whose message type has a wrapper.
|
|
33
|
-
*/
|
|
34
|
-
function unwrapField(type, value) {
|
|
35
|
-
return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
|
|
36
|
-
}
|
|
37
|
-
exports.unwrapField = unwrapField;
|
|
38
30
|
/**
|
|
39
31
|
* If the given field uses one of the well-known wrapper types, return
|
|
40
|
-
* the
|
|
32
|
+
* the primitive type it wraps.
|
|
41
33
|
*/
|
|
42
34
|
function getUnwrappedFieldType(field) {
|
|
43
35
|
if (field.fieldKind !== "message") {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.makeJsonFormatCommon = void 0;
|
|
17
|
-
const
|
|
17
|
+
const message_js_1 = require("../message.js");
|
|
18
18
|
const field_js_1 = require("../field.js");
|
|
19
19
|
const assert_js_1 = require("./assert.js");
|
|
20
20
|
const proto_int64_js_1 = require("../proto-int64.js");
|
|
@@ -174,10 +174,15 @@ function makeJsonFormatCommon(makeWriteField) {
|
|
|
174
174
|
}
|
|
175
175
|
continue;
|
|
176
176
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
177
|
+
if (target[localName] instanceof message_js_1.Message) {
|
|
178
|
+
target[localName].fromJson(jsonValue, options);
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
target[localName] = messageType.fromJson(jsonValue, options);
|
|
182
|
+
if (messageType.fieldWrapper && !field.oneof) {
|
|
183
|
+
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
181
186
|
break;
|
|
182
187
|
case "enum":
|
|
183
188
|
const enumValue = readEnum(field.T, jsonValue, options.ignoreUnknownFields);
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.makeUtilCommon = void 0;
|
|
17
17
|
const enum_js_1 = require("./enum.js");
|
|
18
|
+
const message_js_1 = require("../message.js");
|
|
18
19
|
const field_js_1 = require("../field.js");
|
|
19
20
|
const scalars_js_1 = require("./scalars.js");
|
|
20
21
|
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument,no-case-declarations */
|
|
@@ -139,10 +140,7 @@ function makeUtilCommon() {
|
|
|
139
140
|
}
|
|
140
141
|
throw new Error(`oneof cannot contain ${s.kind}`);
|
|
141
142
|
case "map":
|
|
142
|
-
const keys = Object.keys(va);
|
|
143
|
-
if (keys.some((k) => vb[k] === undefined)) {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
143
|
+
const keys = Object.keys(va).concat(Object.keys(vb));
|
|
146
144
|
switch (m.V.kind) {
|
|
147
145
|
case "message":
|
|
148
146
|
const messageType = m.V.T;
|
|
@@ -192,21 +190,13 @@ function cloneSingularField(field, value) {
|
|
|
192
190
|
if (value === undefined) {
|
|
193
191
|
return value;
|
|
194
192
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
c.set(value);
|
|
203
|
-
return c;
|
|
204
|
-
}
|
|
205
|
-
return value;
|
|
206
|
-
case "message":
|
|
207
|
-
if (field.T.fieldWrapper) {
|
|
208
|
-
return field.T.fieldWrapper.unwrapField(field.T.fieldWrapper.wrapField(value).clone());
|
|
209
|
-
}
|
|
210
|
-
return value.clone();
|
|
193
|
+
if (value instanceof message_js_1.Message) {
|
|
194
|
+
return value.clone();
|
|
195
|
+
}
|
|
196
|
+
if (value instanceof Uint8Array) {
|
|
197
|
+
const c = new Uint8Array(value.byteLength);
|
|
198
|
+
c.set(value);
|
|
199
|
+
return c;
|
|
211
200
|
}
|
|
201
|
+
return value;
|
|
212
202
|
}
|
package/dist/esm/codegen-info.js
CHANGED
|
@@ -41,6 +41,7 @@ export const codegenInfo = {
|
|
|
41
41
|
ScalarType: { typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName },
|
|
42
42
|
MethodKind: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
|
|
43
43
|
MethodIdempotency: { typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName },
|
|
44
|
+
IMessageTypeRegistry: { typeOnly: true, privateImportPath: "./type-registry.js", publicImportPath: packageName },
|
|
44
45
|
},
|
|
45
46
|
wktSourceFiles: [
|
|
46
47
|
"google/protobuf/compiler/plugin.proto",
|
|
@@ -201,8 +201,29 @@ export class Any extends Message {
|
|
|
201
201
|
target.fromBinary(this.value);
|
|
202
202
|
return true;
|
|
203
203
|
}
|
|
204
|
+
unpack(registry) {
|
|
205
|
+
if (this.typeUrl === "") {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
const messageType = registry.findMessage(this.typeUrlToName(this.typeUrl));
|
|
209
|
+
if (!messageType) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
return messageType.fromBinary(this.value);
|
|
213
|
+
}
|
|
204
214
|
is(type) {
|
|
205
|
-
|
|
215
|
+
if (this.typeUrl === '') {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
const name = this.typeUrlToName(this.typeUrl);
|
|
219
|
+
let typeName = '';
|
|
220
|
+
if (typeof type === 'string') {
|
|
221
|
+
typeName = type;
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
typeName = type.typeName;
|
|
225
|
+
}
|
|
226
|
+
return name === typeName;
|
|
206
227
|
}
|
|
207
228
|
typeNameToUrl(name) {
|
|
208
229
|
return `type.googleapis.com/${name}`;
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
// @generated by protoc-gen-es v0.
|
|
14
|
+
// @generated by protoc-gen-es v0.4.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
|
|
15
15
|
// @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
|
|
16
16
|
/* eslint-disable */
|
|
17
17
|
import { proto3 } from "../../proto3.js";
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
// @generated by protoc-gen-es v0.
|
|
14
|
+
// @generated by protoc-gen-es v0.4.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
|
|
15
15
|
// @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
|
|
16
16
|
/* eslint-disable */
|
|
17
17
|
import { proto3 } from "../../proto3.js";
|
|
@@ -69,7 +69,7 @@ DoubleValue.fields = proto3.util.newFieldList(() => [
|
|
|
69
69
|
]);
|
|
70
70
|
DoubleValue.fieldWrapper = {
|
|
71
71
|
wrapField(value) {
|
|
72
|
-
return
|
|
72
|
+
return new DoubleValue({ value });
|
|
73
73
|
},
|
|
74
74
|
unwrapField(value) {
|
|
75
75
|
return value.value;
|
|
@@ -129,7 +129,7 @@ FloatValue.fields = proto3.util.newFieldList(() => [
|
|
|
129
129
|
]);
|
|
130
130
|
FloatValue.fieldWrapper = {
|
|
131
131
|
wrapField(value) {
|
|
132
|
-
return
|
|
132
|
+
return new FloatValue({ value });
|
|
133
133
|
},
|
|
134
134
|
unwrapField(value) {
|
|
135
135
|
return value.value;
|
|
@@ -189,7 +189,7 @@ Int64Value.fields = proto3.util.newFieldList(() => [
|
|
|
189
189
|
]);
|
|
190
190
|
Int64Value.fieldWrapper = {
|
|
191
191
|
wrapField(value) {
|
|
192
|
-
return
|
|
192
|
+
return new Int64Value({ value });
|
|
193
193
|
},
|
|
194
194
|
unwrapField(value) {
|
|
195
195
|
return value.value;
|
|
@@ -249,7 +249,7 @@ UInt64Value.fields = proto3.util.newFieldList(() => [
|
|
|
249
249
|
]);
|
|
250
250
|
UInt64Value.fieldWrapper = {
|
|
251
251
|
wrapField(value) {
|
|
252
|
-
return
|
|
252
|
+
return new UInt64Value({ value });
|
|
253
253
|
},
|
|
254
254
|
unwrapField(value) {
|
|
255
255
|
return value.value;
|
|
@@ -309,7 +309,7 @@ Int32Value.fields = proto3.util.newFieldList(() => [
|
|
|
309
309
|
]);
|
|
310
310
|
Int32Value.fieldWrapper = {
|
|
311
311
|
wrapField(value) {
|
|
312
|
-
return
|
|
312
|
+
return new Int32Value({ value });
|
|
313
313
|
},
|
|
314
314
|
unwrapField(value) {
|
|
315
315
|
return value.value;
|
|
@@ -369,7 +369,7 @@ UInt32Value.fields = proto3.util.newFieldList(() => [
|
|
|
369
369
|
]);
|
|
370
370
|
UInt32Value.fieldWrapper = {
|
|
371
371
|
wrapField(value) {
|
|
372
|
-
return
|
|
372
|
+
return new UInt32Value({ value });
|
|
373
373
|
},
|
|
374
374
|
unwrapField(value) {
|
|
375
375
|
return value.value;
|
|
@@ -429,7 +429,7 @@ BoolValue.fields = proto3.util.newFieldList(() => [
|
|
|
429
429
|
]);
|
|
430
430
|
BoolValue.fieldWrapper = {
|
|
431
431
|
wrapField(value) {
|
|
432
|
-
return
|
|
432
|
+
return new BoolValue({ value });
|
|
433
433
|
},
|
|
434
434
|
unwrapField(value) {
|
|
435
435
|
return value.value;
|
|
@@ -489,7 +489,7 @@ StringValue.fields = proto3.util.newFieldList(() => [
|
|
|
489
489
|
]);
|
|
490
490
|
StringValue.fieldWrapper = {
|
|
491
491
|
wrapField(value) {
|
|
492
|
-
return
|
|
492
|
+
return new StringValue({ value });
|
|
493
493
|
},
|
|
494
494
|
unwrapField(value) {
|
|
495
495
|
return value.value;
|
|
@@ -549,7 +549,7 @@ BytesValue.fields = proto3.util.newFieldList(() => [
|
|
|
549
549
|
]);
|
|
550
550
|
BytesValue.fieldWrapper = {
|
|
551
551
|
wrapField(value) {
|
|
552
|
-
return
|
|
552
|
+
return new BytesValue({ value });
|
|
553
553
|
},
|
|
554
554
|
unwrapField(value) {
|
|
555
555
|
return value.value;
|
package/dist/esm/message.js
CHANGED
|
@@ -29,7 +29,6 @@ export class Message {
|
|
|
29
29
|
* Create a deep copy.
|
|
30
30
|
*/
|
|
31
31
|
clone() {
|
|
32
|
-
// return this.getType().runtime.util.clone(this);
|
|
33
32
|
return this.getType().runtime.util.clone(this);
|
|
34
33
|
}
|
|
35
34
|
/**
|
|
@@ -58,8 +57,14 @@ export class Message {
|
|
|
58
57
|
* Parse a message from a JSON string.
|
|
59
58
|
*/
|
|
60
59
|
fromJsonString(jsonString, options) {
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
let json;
|
|
61
|
+
try {
|
|
62
|
+
json = JSON.parse(jsonString);
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
throw new Error(`cannot decode ${this.getType().typeName} from JSON: ${e instanceof Error ? e.message : String(e)}`);
|
|
66
|
+
}
|
|
67
|
+
return this.fromJson(json, options);
|
|
63
68
|
}
|
|
64
69
|
/**
|
|
65
70
|
* Serialize the message to binary data.
|
|
@@ -85,6 +90,21 @@ export class Message {
|
|
|
85
90
|
const value = this.toJson(options);
|
|
86
91
|
return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
|
|
87
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Override for serialization behavior. This will be invoked when calling
|
|
95
|
+
* JSON.stringify on this message (i.e. JSON.stringify(msg)).
|
|
96
|
+
*
|
|
97
|
+
* Note that this will not serialize google.protobuf.Any with a packed
|
|
98
|
+
* message because the protobuf JSON format specifies that it needs to be
|
|
99
|
+
* unpacked, and this is only possible with a type registry to look up the
|
|
100
|
+
* message type. As a result, attempting to serialize a message with this
|
|
101
|
+
* type will throw an Error.
|
|
102
|
+
*/
|
|
103
|
+
toJSON() {
|
|
104
|
+
return this.toJson({
|
|
105
|
+
emitDefaultValues: true,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
88
108
|
/**
|
|
89
109
|
* Retrieve the MessageType of this message - a singleton that represents
|
|
90
110
|
* the protobuf message declaration and provides metadata for reflection-
|
|
@@ -12,8 +12,9 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { BinaryReader, BinaryWriter, WireType, } from "../binary-encoding.js";
|
|
15
|
+
import { Message } from "../message.js";
|
|
15
16
|
import { ScalarType } from "../field.js";
|
|
16
|
-
import {
|
|
17
|
+
import { wrapField } from "./field-wrapper.js";
|
|
17
18
|
import { scalarDefaultValue, scalarTypeInfo } from "./scalars.js";
|
|
18
19
|
import { assert } from "./assert.js";
|
|
19
20
|
/* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unnecessary-condition, no-case-declarations, prefer-const */
|
|
@@ -111,11 +112,16 @@ export function makeBinaryFormatCommon() {
|
|
|
111
112
|
target[localName].push(messageType.fromBinary(reader.bytes(), options));
|
|
112
113
|
}
|
|
113
114
|
else {
|
|
114
|
-
if (target[localName] instanceof
|
|
115
|
+
if (target[localName] instanceof Message) {
|
|
115
116
|
target[localName].fromBinary(reader.bytes(), options);
|
|
116
117
|
}
|
|
117
118
|
else {
|
|
118
|
-
target[localName] =
|
|
119
|
+
target[localName] = messageType.fromBinary(reader.bytes(), options);
|
|
120
|
+
if (messageType.fieldWrapper &&
|
|
121
|
+
!field.oneof &&
|
|
122
|
+
!field.repeated) {
|
|
123
|
+
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
124
|
+
}
|
|
119
125
|
}
|
|
120
126
|
}
|
|
121
127
|
break;
|
|
@@ -11,28 +11,21 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
+
import { Message } from "../message.js";
|
|
14
15
|
import { ScalarType } from "../field.js";
|
|
15
16
|
/**
|
|
16
|
-
* Wrap
|
|
17
|
+
* Wrap a primitive message field value in its corresponding wrapper
|
|
18
|
+
* message. This function is idempotent.
|
|
17
19
|
*/
|
|
18
20
|
export function wrapField(type, value) {
|
|
19
|
-
if (value instanceof type) {
|
|
21
|
+
if (value instanceof Message || !type.fieldWrapper) {
|
|
20
22
|
return value;
|
|
21
23
|
}
|
|
22
|
-
|
|
23
|
-
return type.fieldWrapper.wrapField(value);
|
|
24
|
-
}
|
|
25
|
-
throw new Error(`cannot unwrap field value, ${type.typeName} does not define a field wrapper`);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Unwrap field values whose message type has a wrapper.
|
|
29
|
-
*/
|
|
30
|
-
export function unwrapField(type, value) {
|
|
31
|
-
return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
|
|
24
|
+
return type.fieldWrapper.wrapField(value);
|
|
32
25
|
}
|
|
33
26
|
/**
|
|
34
27
|
* If the given field uses one of the well-known wrapper types, return
|
|
35
|
-
* the
|
|
28
|
+
* the primitive type it wraps.
|
|
36
29
|
*/
|
|
37
30
|
export function getUnwrappedFieldType(field) {
|
|
38
31
|
if (field.fieldKind !== "message") {
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import {
|
|
14
|
+
import { Message } from "../message.js";
|
|
15
15
|
import { ScalarType } from "../field.js";
|
|
16
16
|
import { assert, assertFloat32, assertInt32, assertUInt32 } from "./assert.js";
|
|
17
17
|
import { protoInt64 } from "../proto-int64.js";
|
|
@@ -171,10 +171,15 @@ export function makeJsonFormatCommon(makeWriteField) {
|
|
|
171
171
|
}
|
|
172
172
|
continue;
|
|
173
173
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
174
|
+
if (target[localName] instanceof Message) {
|
|
175
|
+
target[localName].fromJson(jsonValue, options);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
target[localName] = messageType.fromJson(jsonValue, options);
|
|
179
|
+
if (messageType.fieldWrapper && !field.oneof) {
|
|
180
|
+
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
178
183
|
break;
|
|
179
184
|
case "enum":
|
|
180
185
|
const enumValue = readEnum(field.T, jsonValue, options.ignoreUnknownFields);
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { setEnumType } from "./enum.js";
|
|
15
|
+
import { Message, } from "../message.js";
|
|
15
16
|
import { ScalarType } from "../field.js";
|
|
16
17
|
import { scalarEquals } from "./scalars.js";
|
|
17
18
|
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument,no-case-declarations */
|
|
@@ -136,10 +137,7 @@ export function makeUtilCommon() {
|
|
|
136
137
|
}
|
|
137
138
|
throw new Error(`oneof cannot contain ${s.kind}`);
|
|
138
139
|
case "map":
|
|
139
|
-
const keys = Object.keys(va);
|
|
140
|
-
if (keys.some((k) => vb[k] === undefined)) {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
140
|
+
const keys = Object.keys(va).concat(Object.keys(vb));
|
|
143
141
|
switch (m.V.kind) {
|
|
144
142
|
case "message":
|
|
145
143
|
const messageType = m.V.T;
|
|
@@ -188,21 +186,13 @@ function cloneSingularField(field, value) {
|
|
|
188
186
|
if (value === undefined) {
|
|
189
187
|
return value;
|
|
190
188
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
c.set(value);
|
|
199
|
-
return c;
|
|
200
|
-
}
|
|
201
|
-
return value;
|
|
202
|
-
case "message":
|
|
203
|
-
if (field.T.fieldWrapper) {
|
|
204
|
-
return field.T.fieldWrapper.unwrapField(field.T.fieldWrapper.wrapField(value).clone());
|
|
205
|
-
}
|
|
206
|
-
return value.clone();
|
|
189
|
+
if (value instanceof Message) {
|
|
190
|
+
return value.clone();
|
|
191
|
+
}
|
|
192
|
+
if (value instanceof Uint8Array) {
|
|
193
|
+
const c = new Uint8Array(value.byteLength);
|
|
194
|
+
c.set(value);
|
|
195
|
+
return c;
|
|
207
196
|
}
|
|
197
|
+
return value;
|
|
208
198
|
}
|
|
@@ -17,7 +17,7 @@ export interface BinaryFormat {
|
|
|
17
17
|
/**
|
|
18
18
|
* Parse a message from binary data, merging fields.
|
|
19
19
|
*/
|
|
20
|
-
readMessage
|
|
20
|
+
readMessage(message: Message, reader: IBinaryReader, length: number, options: BinaryReadOptions): void;
|
|
21
21
|
/**
|
|
22
22
|
* Serialize a message to binary data.
|
|
23
23
|
*/
|
|
@@ -11,7 +11,7 @@ interface CodegenInfo {
|
|
|
11
11
|
readonly scalarDefaultValue: typeof scalarDefaultValue;
|
|
12
12
|
readonly reifyWkt: typeof reifyWkt;
|
|
13
13
|
}
|
|
14
|
-
declare type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoInt64" | "ScalarType" | "MethodKind" | "MethodIdempotency";
|
|
14
|
+
declare type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoInt64" | "ScalarType" | "MethodKind" | "MethodIdempotency" | "IMessageTypeRegistry";
|
|
15
15
|
declare type RuntimeSymbolInfo = {
|
|
16
16
|
typeOnly: boolean;
|
|
17
17
|
publicImportPath: string;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PartialMessage, PlainMessage } from "../../message.js";
|
|
2
2
|
import { Message } from "../../message.js";
|
|
3
3
|
import type { JsonReadOptions, JsonValue, JsonWriteOptions } from "../../json-format.js";
|
|
4
|
+
import type { IMessageTypeRegistry } from "../../type-registry.js";
|
|
4
5
|
import type { MessageType } from "../../message-type.js";
|
|
5
6
|
import type { FieldList } from "../../field-list.js";
|
|
6
7
|
import type { BinaryReadOptions } from "../../binary-format.js";
|
|
@@ -136,7 +137,8 @@ export declare class Any extends Message<Any> {
|
|
|
136
137
|
fromJson(json: JsonValue, options?: Partial<JsonReadOptions>): this;
|
|
137
138
|
packFrom(message: Message): void;
|
|
138
139
|
unpackTo(target: Message): boolean;
|
|
139
|
-
|
|
140
|
+
unpack(registry: IMessageTypeRegistry): Message | undefined;
|
|
141
|
+
is(type: MessageType | string): boolean;
|
|
140
142
|
private typeNameToUrl;
|
|
141
143
|
private typeUrlToName;
|
|
142
144
|
static readonly runtime: import("../../private/proto-runtime.js").ProtoRuntime;
|
|
@@ -24,7 +24,7 @@ export declare class DoubleValue extends Message<DoubleValue> {
|
|
|
24
24
|
static readonly typeName = "google.protobuf.DoubleValue";
|
|
25
25
|
static readonly fields: FieldList;
|
|
26
26
|
static readonly fieldWrapper: {
|
|
27
|
-
wrapField(value: number
|
|
27
|
+
wrapField(value: number): DoubleValue;
|
|
28
28
|
unwrapField(value: DoubleValue): number;
|
|
29
29
|
};
|
|
30
30
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): DoubleValue;
|
|
@@ -53,7 +53,7 @@ export declare class FloatValue extends Message<FloatValue> {
|
|
|
53
53
|
static readonly typeName = "google.protobuf.FloatValue";
|
|
54
54
|
static readonly fields: FieldList;
|
|
55
55
|
static readonly fieldWrapper: {
|
|
56
|
-
wrapField(value: number
|
|
56
|
+
wrapField(value: number): FloatValue;
|
|
57
57
|
unwrapField(value: FloatValue): number;
|
|
58
58
|
};
|
|
59
59
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): FloatValue;
|
|
@@ -82,7 +82,7 @@ export declare class Int64Value extends Message<Int64Value> {
|
|
|
82
82
|
static readonly typeName = "google.protobuf.Int64Value";
|
|
83
83
|
static readonly fields: FieldList;
|
|
84
84
|
static readonly fieldWrapper: {
|
|
85
|
-
wrapField(value: bigint
|
|
85
|
+
wrapField(value: bigint): Int64Value;
|
|
86
86
|
unwrapField(value: Int64Value): bigint;
|
|
87
87
|
};
|
|
88
88
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Int64Value;
|
|
@@ -111,7 +111,7 @@ export declare class UInt64Value extends Message<UInt64Value> {
|
|
|
111
111
|
static readonly typeName = "google.protobuf.UInt64Value";
|
|
112
112
|
static readonly fields: FieldList;
|
|
113
113
|
static readonly fieldWrapper: {
|
|
114
|
-
wrapField(value: bigint
|
|
114
|
+
wrapField(value: bigint): UInt64Value;
|
|
115
115
|
unwrapField(value: UInt64Value): bigint;
|
|
116
116
|
};
|
|
117
117
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): UInt64Value;
|
|
@@ -140,7 +140,7 @@ export declare class Int32Value extends Message<Int32Value> {
|
|
|
140
140
|
static readonly typeName = "google.protobuf.Int32Value";
|
|
141
141
|
static readonly fields: FieldList;
|
|
142
142
|
static readonly fieldWrapper: {
|
|
143
|
-
wrapField(value: number
|
|
143
|
+
wrapField(value: number): Int32Value;
|
|
144
144
|
unwrapField(value: Int32Value): number;
|
|
145
145
|
};
|
|
146
146
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Int32Value;
|
|
@@ -169,7 +169,7 @@ export declare class UInt32Value extends Message<UInt32Value> {
|
|
|
169
169
|
static readonly typeName = "google.protobuf.UInt32Value";
|
|
170
170
|
static readonly fields: FieldList;
|
|
171
171
|
static readonly fieldWrapper: {
|
|
172
|
-
wrapField(value: number
|
|
172
|
+
wrapField(value: number): UInt32Value;
|
|
173
173
|
unwrapField(value: UInt32Value): number;
|
|
174
174
|
};
|
|
175
175
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): UInt32Value;
|
|
@@ -198,7 +198,7 @@ export declare class BoolValue extends Message<BoolValue> {
|
|
|
198
198
|
static readonly typeName = "google.protobuf.BoolValue";
|
|
199
199
|
static readonly fields: FieldList;
|
|
200
200
|
static readonly fieldWrapper: {
|
|
201
|
-
wrapField(value: boolean
|
|
201
|
+
wrapField(value: boolean): BoolValue;
|
|
202
202
|
unwrapField(value: BoolValue): boolean;
|
|
203
203
|
};
|
|
204
204
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): BoolValue;
|
|
@@ -227,7 +227,7 @@ export declare class StringValue extends Message<StringValue> {
|
|
|
227
227
|
static readonly typeName = "google.protobuf.StringValue";
|
|
228
228
|
static readonly fields: FieldList;
|
|
229
229
|
static readonly fieldWrapper: {
|
|
230
|
-
wrapField(value: string
|
|
230
|
+
wrapField(value: string): StringValue;
|
|
231
231
|
unwrapField(value: StringValue): string;
|
|
232
232
|
};
|
|
233
233
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): StringValue;
|
|
@@ -256,7 +256,7 @@ export declare class BytesValue extends Message<BytesValue> {
|
|
|
256
256
|
static readonly typeName = "google.protobuf.BytesValue";
|
|
257
257
|
static readonly fields: FieldList;
|
|
258
258
|
static readonly fieldWrapper: {
|
|
259
|
-
wrapField(value: Uint8Array
|
|
259
|
+
wrapField(value: Uint8Array): BytesValue;
|
|
260
260
|
unwrapField(value: BytesValue): Uint8Array;
|
|
261
261
|
};
|
|
262
262
|
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): BytesValue;
|
package/dist/types/message.d.ts
CHANGED
|
@@ -56,6 +56,17 @@ export declare class Message<T extends Message<T> = AnyMessage> {
|
|
|
56
56
|
* Serialize the message to a JSON string.
|
|
57
57
|
*/
|
|
58
58
|
toJsonString(options?: Partial<JsonWriteStringOptions>): string;
|
|
59
|
+
/**
|
|
60
|
+
* Override for serialization behavior. This will be invoked when calling
|
|
61
|
+
* JSON.stringify on this message (i.e. JSON.stringify(msg)).
|
|
62
|
+
*
|
|
63
|
+
* Note that this will not serialize google.protobuf.Any with a packed
|
|
64
|
+
* message because the protobuf JSON format specifies that it needs to be
|
|
65
|
+
* unpacked, and this is only possible with a type registry to look up the
|
|
66
|
+
* message type. As a result, attempting to serialize a message with this
|
|
67
|
+
* type will throw an Error.
|
|
68
|
+
*/
|
|
69
|
+
toJSON(): JsonValue;
|
|
59
70
|
/**
|
|
60
71
|
* Retrieve the MessageType of this message - a singleton that represents
|
|
61
72
|
* the protobuf message declaration and provides metadata for reflection-
|
|
@@ -65,23 +76,38 @@ export declare class Message<T extends Message<T> = AnyMessage> {
|
|
|
65
76
|
}
|
|
66
77
|
/**
|
|
67
78
|
* PlainMessage<T> strips all methods from a message, leaving only fields
|
|
68
|
-
* and oneof groups.
|
|
79
|
+
* and oneof groups. It is recursive, meaning it applies this same logic to all
|
|
80
|
+
* nested message fields as well.
|
|
69
81
|
*/
|
|
70
|
-
export declare type PlainMessage<T extends Message
|
|
71
|
-
[P in keyof T as T[P] extends Function ? never : P]: T[P]
|
|
82
|
+
export declare type PlainMessage<T extends Message<T>> = {
|
|
83
|
+
[P in keyof T as T[P] extends Function ? never : P]: PlainField<T[P]>;
|
|
72
84
|
};
|
|
85
|
+
declare type PlainField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PlainField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PlainField<U>> : F extends Message<infer U> ? PlainMessage<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
|
|
86
|
+
case: C;
|
|
87
|
+
value: PlainField<V>;
|
|
88
|
+
} : F extends {
|
|
89
|
+
case: string | undefined;
|
|
90
|
+
value?: unknown;
|
|
91
|
+
} ? F : F extends {
|
|
92
|
+
[key: string | number]: Message<infer U>;
|
|
93
|
+
} ? {
|
|
94
|
+
[key: string | number]: PlainField<U>;
|
|
95
|
+
} : F;
|
|
73
96
|
/**
|
|
74
97
|
* PartialMessage<T> constructs a type from a message. The resulting type
|
|
75
98
|
* only contains the protobuf field members of the message, and all of them
|
|
76
99
|
* are optional.
|
|
77
100
|
*
|
|
101
|
+
* Note that the optionality of the fields is the only difference between
|
|
102
|
+
* PartialMessage and PlainMessage.
|
|
103
|
+
*
|
|
78
104
|
* PartialMessage is similar to the built-in type Partial<T>, but recursive,
|
|
79
105
|
* and respects `oneof` groups.
|
|
80
106
|
*/
|
|
81
|
-
export declare type PartialMessage<T extends Message
|
|
107
|
+
export declare type PartialMessage<T extends Message<T>> = {
|
|
82
108
|
[P in keyof T as T[P] extends Function ? never : P]?: PartialField<T[P]>;
|
|
83
109
|
};
|
|
84
|
-
declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PartialField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : F extends Message ? PartialMessage<
|
|
110
|
+
declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PartialField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : F extends Message<infer U> ? PartialMessage<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
|
|
85
111
|
case: C;
|
|
86
112
|
value: PartialMessage<V>;
|
|
87
113
|
} : F extends {
|
|
@@ -92,7 +118,7 @@ declare type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean |
|
|
|
92
118
|
} ? {
|
|
93
119
|
[key: string | number]: PartialMessage<U>;
|
|
94
120
|
} : F;
|
|
95
|
-
declare type OneofSelectedMessage<K extends string, M extends Message
|
|
121
|
+
declare type OneofSelectedMessage<K extends string, M extends Message<M>> = {
|
|
96
122
|
case: K;
|
|
97
123
|
value: M;
|
|
98
124
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Message } from "../message.js";
|
|
2
2
|
import type { MessageType } from "../message-type.js";
|
|
3
3
|
import type { DescField } from "../descriptor-set.js";
|
|
4
4
|
import { ScalarType } from "../field.js";
|
|
@@ -6,7 +6,7 @@ import { ScalarType } from "../field.js";
|
|
|
6
6
|
* A field wrapper unwraps a message to a primitive value that is more
|
|
7
7
|
* ergonomic for use as a message field.
|
|
8
8
|
*
|
|
9
|
-
* Note that this feature exists for google/protobuf/
|
|
9
|
+
* Note that this feature exists for google/protobuf/wrappers.proto
|
|
10
10
|
* and cannot be used to arbitrarily modify types in generated code.
|
|
11
11
|
*/
|
|
12
12
|
export interface FieldWrapper<T extends Message<T> = any, U = any> {
|
|
@@ -14,15 +14,12 @@ export interface FieldWrapper<T extends Message<T> = any, U = any> {
|
|
|
14
14
|
unwrapField(value: T): U;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* Wrap
|
|
17
|
+
* Wrap a primitive message field value in its corresponding wrapper
|
|
18
|
+
* message. This function is idempotent.
|
|
18
19
|
*/
|
|
19
20
|
export declare function wrapField<T extends Message<T>>(type: MessageType<T>, value: any): T;
|
|
20
|
-
/**
|
|
21
|
-
* Unwrap field values whose message type has a wrapper.
|
|
22
|
-
*/
|
|
23
|
-
export declare function unwrapField<T extends Message<T>>(type: MessageType<T>, value: T): any;
|
|
24
21
|
/**
|
|
25
22
|
* If the given field uses one of the well-known wrapper types, return
|
|
26
|
-
* the
|
|
23
|
+
* the primitive type it wraps.
|
|
27
24
|
*/
|
|
28
25
|
export declare function getUnwrappedFieldType(field: DescField): ScalarType | undefined;
|
|
@@ -30,7 +30,7 @@ export interface Util {
|
|
|
30
30
|
* Compares two messages of the same type recursively.
|
|
31
31
|
* Will also return true if both messages are `undefined` or `null`.
|
|
32
32
|
*/
|
|
33
|
-
equals<T extends Message<T>>(type: MessageType
|
|
33
|
+
equals<T extends Message<T>>(type: MessageType<T>, a: T | PlainMessage<T> | undefined | null, b: T | PlainMessage<T> | undefined | null): boolean;
|
|
34
34
|
/**
|
|
35
35
|
* Create a deep copy.
|
|
36
36
|
*/
|
package/package.json
CHANGED