@aptre/protobuf-es-lite 0.2.10 → 0.2.12
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/bin/protoc-gen-es-lite +2 -2
- package/dist/assert.js +4 -11
- package/dist/binary-encoding.js +69 -42
- package/dist/binary.js +87 -105
- package/dist/codegen-info.d.ts +0 -1
- package/dist/codegen-info.js +12 -16
- package/dist/create-descriptor-set.js +114 -118
- package/dist/descriptor-set.js +1 -2
- package/dist/enum.js +2 -7
- package/dist/feature-set.d.ts +1 -1
- package/dist/feature-set.js +18 -22
- package/dist/field-wrapper.js +12 -17
- package/dist/field.js +35 -34
- package/dist/google/index.js +9 -41
- package/dist/google/protobuf/any.pb.d.ts +2 -2
- package/dist/google/protobuf/any.pb.js +3 -6
- package/dist/google/protobuf/api.pb.d.ts +2 -2
- package/dist/google/protobuf/api.pb.js +56 -21
- package/dist/google/protobuf/compiler/plugin.pb.d.ts +2 -2
- package/dist/google/protobuf/compiler/plugin.pb.js +15 -18
- package/dist/google/protobuf/descriptor.pb.d.ts +19 -19
- package/dist/google/protobuf/descriptor.pb.js +831 -223
- package/dist/google/protobuf/duration.pb.d.ts +2 -2
- package/dist/google/protobuf/duration.pb.js +3 -6
- package/dist/google/protobuf/empty.pb.d.ts +2 -2
- package/dist/google/protobuf/empty.pb.js +3 -6
- package/dist/google/protobuf/source_context.pb.d.ts +2 -2
- package/dist/google/protobuf/source_context.pb.js +3 -6
- package/dist/google/protobuf/struct.pb.d.ts +3 -3
- package/dist/google/protobuf/struct.pb.js +31 -18
- package/dist/google/protobuf/timestamp.pb.d.ts +2 -2
- package/dist/google/protobuf/timestamp.pb.js +3 -6
- package/dist/google/protobuf/type.pb.d.ts +5 -5
- package/dist/google/protobuf/type.pb.js +74 -36
- package/dist/google/protobuf/wrappers.pb.d.ts +2 -2
- package/dist/google/protobuf/wrappers.pb.js +11 -14
- package/dist/index.js +12 -60
- package/dist/is-message.js +9 -16
- package/dist/json.d.ts +0 -9
- package/dist/json.js +72 -102
- package/dist/message.d.ts +3 -3
- package/dist/message.js +50 -57
- package/dist/names.js +12 -23
- package/dist/partial.js +15 -24
- package/dist/proto-base64.js +1 -4
- package/dist/proto-double.js +1 -4
- package/dist/proto-int64.js +9 -12
- package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.js +4 -7
- package/dist/protoc-gen-es-lite/typescript.js +27 -36
- package/dist/protoplugin/create-es-plugin.js +14 -18
- package/dist/protoplugin/ecmascript/file-preamble.js +5 -9
- package/dist/protoplugin/ecmascript/generated-file.js +16 -20
- package/dist/protoplugin/ecmascript/import-path.js +13 -20
- package/dist/protoplugin/ecmascript/import-symbol.js +1 -5
- package/dist/protoplugin/ecmascript/index.js +2 -7
- package/dist/protoplugin/ecmascript/jsdoc.js +1 -5
- package/dist/protoplugin/ecmascript/opaque-printables.js +1 -2
- package/dist/protoplugin/ecmascript/parameter.js +10 -14
- package/dist/protoplugin/ecmascript/reify-wkt.js +12 -16
- package/dist/protoplugin/ecmascript/runtime-imports.js +5 -9
- package/dist/protoplugin/ecmascript/schema.js +16 -20
- package/dist/protoplugin/ecmascript/target.js +1 -2
- package/dist/protoplugin/ecmascript/transpile.js +15 -42
- package/dist/protoplugin/error.js +4 -10
- package/dist/protoplugin/index.js +2 -7
- package/dist/protoplugin/plugin.js +1 -2
- package/dist/protoplugin/run-node.js +6 -10
- package/dist/scalar.js +9 -15
- package/dist/service-type.js +4 -7
- package/dist/text-format.js +25 -30
- package/dist/unknown.js +7 -12
- package/dist/util.d.ts +1 -1
- package/dist/util.js +49 -55
- package/dist/varint.js +7 -17
- package/example/example.pb.ts +24 -18
- package/package.json +10 -7
- package/tsconfig.base.json +3 -3
- package/dist/type-registry.d.ts +0 -43
- package/dist/type-registry.js +0 -15
package/dist/is-message.js
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isCompleteField = exports.isCompleteMessage = exports.isField = exports.isMessage = void 0;
|
|
4
|
-
const field_js_1 = require("./field.js");
|
|
1
|
+
import { resolveMessageType } from "./field.js";
|
|
5
2
|
/**
|
|
6
3
|
* Check whether the given partial is a valid message.
|
|
7
4
|
*/
|
|
8
|
-
function isMessage(arg, fields) {
|
|
5
|
+
export function isMessage(arg, fields) {
|
|
9
6
|
if (arg == null || typeof arg !== "object") {
|
|
10
7
|
return false;
|
|
11
8
|
}
|
|
@@ -24,11 +21,10 @@ function isMessage(arg, fields) {
|
|
|
24
21
|
}
|
|
25
22
|
});
|
|
26
23
|
}
|
|
27
|
-
exports.isMessage = isMessage;
|
|
28
24
|
/**
|
|
29
25
|
* Check whether the given partial field is a valid field value.
|
|
30
26
|
*/
|
|
31
|
-
function isField(value, field) {
|
|
27
|
+
export function isField(value, field) {
|
|
32
28
|
if (value == null) {
|
|
33
29
|
return false;
|
|
34
30
|
}
|
|
@@ -46,7 +42,7 @@ function isField(value, field) {
|
|
|
46
42
|
case "scalar":
|
|
47
43
|
return true;
|
|
48
44
|
case "message":
|
|
49
|
-
const messageType =
|
|
45
|
+
const messageType = resolveMessageType(field.T);
|
|
50
46
|
return isMessage(value, messageType.fields.list());
|
|
51
47
|
case "enum":
|
|
52
48
|
return typeof value === "number";
|
|
@@ -59,7 +55,7 @@ function isField(value, field) {
|
|
|
59
55
|
case "enum":
|
|
60
56
|
return typeof val === "number";
|
|
61
57
|
case "message":
|
|
62
|
-
const messageType =
|
|
58
|
+
const messageType = resolveMessageType(field.V.T);
|
|
63
59
|
return isMessage(val, messageType.fields.list());
|
|
64
60
|
default:
|
|
65
61
|
return valueKind;
|
|
@@ -69,11 +65,10 @@ function isField(value, field) {
|
|
|
69
65
|
return fieldKind;
|
|
70
66
|
}
|
|
71
67
|
}
|
|
72
|
-
exports.isField = isField;
|
|
73
68
|
/**
|
|
74
69
|
* Check whether the given partial has all fields present recursively.
|
|
75
70
|
*/
|
|
76
|
-
function isCompleteMessage(arg, fields) {
|
|
71
|
+
export function isCompleteMessage(arg, fields) {
|
|
77
72
|
// arg is CompleteMessage<T> {
|
|
78
73
|
if (arg == null || typeof arg !== "object") {
|
|
79
74
|
return false;
|
|
@@ -89,11 +84,10 @@ function isCompleteMessage(arg, fields) {
|
|
|
89
84
|
}
|
|
90
85
|
});
|
|
91
86
|
}
|
|
92
|
-
exports.isCompleteMessage = isCompleteMessage;
|
|
93
87
|
/**
|
|
94
88
|
* Check whether the given partial field has a full value present recursively.
|
|
95
89
|
*/
|
|
96
|
-
function isCompleteField(value, field) {
|
|
90
|
+
export function isCompleteField(value, field) {
|
|
97
91
|
// value is CompleteField<F> {
|
|
98
92
|
if (field.oneof) {
|
|
99
93
|
// For oneof fields, only one field should be set
|
|
@@ -109,7 +103,7 @@ function isCompleteField(value, field) {
|
|
|
109
103
|
case "scalar":
|
|
110
104
|
return true;
|
|
111
105
|
case "message":
|
|
112
|
-
const messageType =
|
|
106
|
+
const messageType = resolveMessageType(field.T);
|
|
113
107
|
return isCompleteMessage(value, messageType.fields.list());
|
|
114
108
|
case "enum":
|
|
115
109
|
return typeof value === "number";
|
|
@@ -122,7 +116,7 @@ function isCompleteField(value, field) {
|
|
|
122
116
|
case "enum":
|
|
123
117
|
return typeof val === "number";
|
|
124
118
|
case "message":
|
|
125
|
-
const messageType =
|
|
119
|
+
const messageType = resolveMessageType(field.V.T);
|
|
126
120
|
return isCompleteMessage(val, messageType.fields.list());
|
|
127
121
|
default:
|
|
128
122
|
return valueKind;
|
|
@@ -132,4 +126,3 @@ function isCompleteField(value, field) {
|
|
|
132
126
|
return fieldKind;
|
|
133
127
|
}
|
|
134
128
|
}
|
|
135
|
-
exports.isCompleteField = isCompleteField;
|
package/dist/json.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { FieldInfo, FieldList } from "./field.js";
|
|
2
|
-
import { IMessageTypeRegistry } from "./type-registry.js";
|
|
3
2
|
/**
|
|
4
3
|
* Options for parsing JSON data.
|
|
5
4
|
*/
|
|
@@ -10,10 +9,6 @@ export interface JsonReadOptions {
|
|
|
10
9
|
* unrecognized enum string representations.
|
|
11
10
|
*/
|
|
12
11
|
ignoreUnknownFields: boolean;
|
|
13
|
-
/**
|
|
14
|
-
* This option is required to read `google.protobuf.Any`
|
|
15
|
-
*/
|
|
16
|
-
typeRegistry?: IMessageTypeRegistry;
|
|
17
12
|
}
|
|
18
13
|
/**
|
|
19
14
|
* Options for serializing to JSON.
|
|
@@ -40,10 +35,6 @@ export interface JsonWriteOptions {
|
|
|
40
35
|
* field name.
|
|
41
36
|
*/
|
|
42
37
|
useProtoFieldName: boolean;
|
|
43
|
-
/**
|
|
44
|
-
* This option is required to write `google.protobuf.Any`
|
|
45
|
-
*/
|
|
46
|
-
typeRegistry?: IMessageTypeRegistry;
|
|
47
38
|
}
|
|
48
39
|
/**
|
|
49
40
|
* Options for serializing to JSON.
|
package/dist/json.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const field_wrapper_js_1 = require("./field-wrapper.js");
|
|
8
|
-
const proto_int64_js_1 = require("./proto-int64.js");
|
|
9
|
-
const proto_base64_js_1 = require("./proto-base64.js");
|
|
1
|
+
import { isFieldSet, resolveMessageType, } from "./field.js";
|
|
2
|
+
import { assert, assertFloat32, assertInt32, assertUInt32 } from "./assert.js";
|
|
3
|
+
import { LongType, ScalarType, scalarZeroValue, } from "./scalar.js";
|
|
4
|
+
import { wrapField } from "./field-wrapper.js";
|
|
5
|
+
import { protoInt64 } from "./proto-int64.js";
|
|
6
|
+
import { protoBase64 } from "./proto-base64.js";
|
|
10
7
|
// Default options for parsing JSON.
|
|
11
8
|
const jsonReadDefaults = {
|
|
12
9
|
ignoreUnknownFields: false,
|
|
@@ -18,14 +15,12 @@ const jsonWriteDefaults = {
|
|
|
18
15
|
useProtoFieldName: false,
|
|
19
16
|
prettySpaces: 0,
|
|
20
17
|
};
|
|
21
|
-
function makeReadOptions(options) {
|
|
18
|
+
export function makeReadOptions(options) {
|
|
22
19
|
return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
function makeWriteOptions(options) {
|
|
21
|
+
export function makeWriteOptions(options) {
|
|
26
22
|
return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
|
|
27
23
|
}
|
|
28
|
-
exports.makeWriteOptions = makeWriteOptions;
|
|
29
24
|
function debugJsonValue(json) {
|
|
30
25
|
if (json === null) {
|
|
31
26
|
return "null";
|
|
@@ -39,7 +34,7 @@ function debugJsonValue(json) {
|
|
|
39
34
|
return String(json);
|
|
40
35
|
}
|
|
41
36
|
}
|
|
42
|
-
function readMessage(fields, typeName, json, options, message) {
|
|
37
|
+
export function readMessage(fields, typeName, json, options, message) {
|
|
43
38
|
if (json == null || Array.isArray(json) || typeof json != "object") {
|
|
44
39
|
throw new Error(`cannot decode message ${typeName} from JSON: ${debugJsonValue(json)}`);
|
|
45
40
|
}
|
|
@@ -69,13 +64,12 @@ function readMessage(fields, typeName, json, options, message) {
|
|
|
69
64
|
}
|
|
70
65
|
return message;
|
|
71
66
|
}
|
|
72
|
-
|
|
73
|
-
function writeMessage(message, fields, options) {
|
|
67
|
+
export function writeMessage(message, fields, options) {
|
|
74
68
|
const json = {};
|
|
75
69
|
let field;
|
|
76
70
|
try {
|
|
77
71
|
for (field of fields.byNumber()) {
|
|
78
|
-
if (!
|
|
72
|
+
if (!isFieldSet(field, message)) {
|
|
79
73
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
80
74
|
if (field.req) {
|
|
81
75
|
throw `required field not set`;
|
|
@@ -96,28 +90,6 @@ function writeMessage(message, fields, options) {
|
|
|
96
90
|
jsonValue;
|
|
97
91
|
}
|
|
98
92
|
}
|
|
99
|
-
// TODO: handle extensions
|
|
100
|
-
/*
|
|
101
|
-
const registry = options.typeRegistry;
|
|
102
|
-
if (registry?.findExtensionFor) {
|
|
103
|
-
for (const uf of listUnknownFields(message)) {
|
|
104
|
-
const ext = registry.findExtensionFor(typeName, uf.no);
|
|
105
|
-
if (ext && hasExtension(message, ext)) {
|
|
106
|
-
// We pass on the options as BinaryReadOptions, so that users can bring their own
|
|
107
|
-
// binary reader factory if necessary.
|
|
108
|
-
const value = getExtension(
|
|
109
|
-
message,
|
|
110
|
-
ext,
|
|
111
|
-
options as Partial<BinaryReadOptions>,
|
|
112
|
-
);
|
|
113
|
-
const jsonValue = writeField(ext.field, value, options);
|
|
114
|
-
if (jsonValue !== undefined) {
|
|
115
|
-
json[ext.field.jsonName] = jsonValue;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
*/
|
|
121
93
|
}
|
|
122
94
|
catch (e) {
|
|
123
95
|
const m = field ?
|
|
@@ -128,13 +100,12 @@ function writeMessage(message, fields, options) {
|
|
|
128
100
|
}
|
|
129
101
|
return json;
|
|
130
102
|
}
|
|
131
|
-
exports.writeMessage = writeMessage;
|
|
132
103
|
// Read a JSON value for a field.
|
|
133
104
|
// The "parentType" argument is only used to provide context in errors.
|
|
134
105
|
function readField(target, jsonValue, field, options) {
|
|
135
106
|
let localName = field.localName;
|
|
136
107
|
if (field.repeated) {
|
|
137
|
-
|
|
108
|
+
assert(field.kind != "map");
|
|
138
109
|
if (jsonValue === null) {
|
|
139
110
|
return;
|
|
140
111
|
}
|
|
@@ -151,7 +122,7 @@ function readField(target, jsonValue, field, options) {
|
|
|
151
122
|
}
|
|
152
123
|
switch (field.kind) {
|
|
153
124
|
case "message":
|
|
154
|
-
const messageType =
|
|
125
|
+
const messageType = resolveMessageType(field.T);
|
|
155
126
|
targetArray.push(messageType.fromJson(jsonItem, options));
|
|
156
127
|
break;
|
|
157
128
|
case "enum":
|
|
@@ -203,7 +174,7 @@ function readField(target, jsonValue, field, options) {
|
|
|
203
174
|
}
|
|
204
175
|
switch (field.V.kind) {
|
|
205
176
|
case "message":
|
|
206
|
-
const messageType =
|
|
177
|
+
const messageType = resolveMessageType(field.V.T);
|
|
207
178
|
targetMap[key] = messageType.fromJson(jsonMapValue, options);
|
|
208
179
|
break;
|
|
209
180
|
case "enum":
|
|
@@ -214,7 +185,7 @@ function readField(target, jsonValue, field, options) {
|
|
|
214
185
|
break;
|
|
215
186
|
case "scalar":
|
|
216
187
|
try {
|
|
217
|
-
targetMap[key] = readScalar(field.V.T, jsonMapValue,
|
|
188
|
+
targetMap[key] = readScalar(field.V.T, jsonMapValue, LongType.BIGINT, true);
|
|
218
189
|
}
|
|
219
190
|
catch (e) {
|
|
220
191
|
let m = `cannot decode map value for field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
|
|
@@ -234,7 +205,7 @@ function readField(target, jsonValue, field, options) {
|
|
|
234
205
|
}
|
|
235
206
|
switch (field.kind) {
|
|
236
207
|
case "message":
|
|
237
|
-
const messageType =
|
|
208
|
+
const messageType = resolveMessageType(field.T);
|
|
238
209
|
if (jsonValue === null &&
|
|
239
210
|
messageType.typeName != "google.protobuf.Value") {
|
|
240
211
|
return;
|
|
@@ -312,7 +283,7 @@ function readEnum(type, json, ignoreUnknownFields, nullAsZeroValue) {
|
|
|
312
283
|
function readScalar(type, json, longType, nullAsZeroValue) {
|
|
313
284
|
if (json === null) {
|
|
314
285
|
if (nullAsZeroValue) {
|
|
315
|
-
return
|
|
286
|
+
return scalarZeroValue(type, longType);
|
|
316
287
|
}
|
|
317
288
|
return tokenNull;
|
|
318
289
|
}
|
|
@@ -321,8 +292,8 @@ function readScalar(type, json, longType, nullAsZeroValue) {
|
|
|
321
292
|
switch (type) {
|
|
322
293
|
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
|
|
323
294
|
// Either numbers or strings are accepted. Exponent notation is also accepted.
|
|
324
|
-
case
|
|
325
|
-
case
|
|
295
|
+
case ScalarType.DOUBLE:
|
|
296
|
+
case ScalarType.FLOAT:
|
|
326
297
|
if (json === "NaN")
|
|
327
298
|
return Number.NaN;
|
|
328
299
|
if (json === "Infinity")
|
|
@@ -349,15 +320,15 @@ function readScalar(type, json, longType, nullAsZeroValue) {
|
|
|
349
320
|
// infinity and -infinity are handled by string representation above, so this is an error
|
|
350
321
|
break;
|
|
351
322
|
}
|
|
352
|
-
if (type ==
|
|
353
|
-
|
|
323
|
+
if (type == ScalarType.FLOAT)
|
|
324
|
+
assertFloat32(float);
|
|
354
325
|
return float;
|
|
355
326
|
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
|
|
356
|
-
case
|
|
357
|
-
case
|
|
358
|
-
case
|
|
359
|
-
case
|
|
360
|
-
case
|
|
327
|
+
case ScalarType.INT32:
|
|
328
|
+
case ScalarType.FIXED32:
|
|
329
|
+
case ScalarType.SFIXED32:
|
|
330
|
+
case ScalarType.SINT32:
|
|
331
|
+
case ScalarType.UINT32:
|
|
361
332
|
let int32;
|
|
362
333
|
if (typeof json == "number")
|
|
363
334
|
int32 = json;
|
|
@@ -367,34 +338,34 @@ function readScalar(type, json, longType, nullAsZeroValue) {
|
|
|
367
338
|
}
|
|
368
339
|
if (int32 === undefined)
|
|
369
340
|
break;
|
|
370
|
-
if (type ==
|
|
371
|
-
|
|
341
|
+
if (type == ScalarType.UINT32 || type == ScalarType.FIXED32)
|
|
342
|
+
assertUInt32(int32);
|
|
372
343
|
else
|
|
373
|
-
|
|
344
|
+
assertInt32(int32);
|
|
374
345
|
return int32;
|
|
375
346
|
// int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
|
|
376
|
-
case
|
|
377
|
-
case
|
|
378
|
-
case
|
|
347
|
+
case ScalarType.INT64:
|
|
348
|
+
case ScalarType.SFIXED64:
|
|
349
|
+
case ScalarType.SINT64:
|
|
379
350
|
if (typeof json != "number" && typeof json != "string")
|
|
380
351
|
break;
|
|
381
|
-
const long =
|
|
352
|
+
const long = protoInt64.parse(json);
|
|
382
353
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
383
354
|
return longType ? long.toString() : long;
|
|
384
|
-
case
|
|
385
|
-
case
|
|
355
|
+
case ScalarType.FIXED64:
|
|
356
|
+
case ScalarType.UINT64:
|
|
386
357
|
if (typeof json != "number" && typeof json != "string")
|
|
387
358
|
break;
|
|
388
|
-
const uLong =
|
|
359
|
+
const uLong = protoInt64.uParse(json);
|
|
389
360
|
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
390
361
|
return longType ? uLong.toString() : uLong;
|
|
391
362
|
// bool:
|
|
392
|
-
case
|
|
363
|
+
case ScalarType.BOOL:
|
|
393
364
|
if (typeof json !== "boolean")
|
|
394
365
|
break;
|
|
395
366
|
return json;
|
|
396
367
|
// string:
|
|
397
|
-
case
|
|
368
|
+
case ScalarType.STRING:
|
|
398
369
|
if (typeof json !== "string") {
|
|
399
370
|
break;
|
|
400
371
|
}
|
|
@@ -409,17 +380,17 @@ function readScalar(type, json, longType, nullAsZeroValue) {
|
|
|
409
380
|
return json;
|
|
410
381
|
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
411
382
|
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
412
|
-
case
|
|
383
|
+
case ScalarType.BYTES:
|
|
413
384
|
if (json === "")
|
|
414
385
|
return new Uint8Array(0);
|
|
415
386
|
if (typeof json !== "string")
|
|
416
387
|
break;
|
|
417
|
-
return
|
|
388
|
+
return protoBase64.dec(json);
|
|
418
389
|
}
|
|
419
390
|
throw new Error();
|
|
420
391
|
}
|
|
421
392
|
function readMapKey(type, json) {
|
|
422
|
-
if (type ===
|
|
393
|
+
if (type === ScalarType.BOOL) {
|
|
423
394
|
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
424
395
|
switch (json) {
|
|
425
396
|
case "true":
|
|
@@ -430,12 +401,12 @@ function readMapKey(type, json) {
|
|
|
430
401
|
break;
|
|
431
402
|
}
|
|
432
403
|
}
|
|
433
|
-
return readScalar(type, json,
|
|
404
|
+
return readScalar(type, json, LongType.BIGINT, true).toString();
|
|
434
405
|
}
|
|
435
406
|
/**
|
|
436
407
|
* Resets the field, so that isFieldSet() will return false.
|
|
437
408
|
*/
|
|
438
|
-
function clearField(field, target) {
|
|
409
|
+
export function clearField(field, target) {
|
|
439
410
|
const localName = field.localName;
|
|
440
411
|
const implicitPresence = !field.opt && !field.req;
|
|
441
412
|
if (field.repeated) {
|
|
@@ -454,7 +425,7 @@ function clearField(field, target) {
|
|
|
454
425
|
break;
|
|
455
426
|
case "scalar":
|
|
456
427
|
target[localName] =
|
|
457
|
-
implicitPresence ?
|
|
428
|
+
implicitPresence ? scalarZeroValue(field.T, field.L) : undefined;
|
|
458
429
|
break;
|
|
459
430
|
case "message":
|
|
460
431
|
target[localName] = undefined;
|
|
@@ -462,7 +433,6 @@ function clearField(field, target) {
|
|
|
462
433
|
}
|
|
463
434
|
}
|
|
464
435
|
}
|
|
465
|
-
exports.clearField = clearField;
|
|
466
436
|
// Decide whether an unset field should be emitted with JSON write option `emitDefaultValues`
|
|
467
437
|
function canEmitFieldDefaultValue(field) {
|
|
468
438
|
if (field.repeated || field.kind == "map") {
|
|
@@ -486,7 +456,7 @@ function canEmitFieldDefaultValue(field) {
|
|
|
486
456
|
}
|
|
487
457
|
function writeField(field, value, options) {
|
|
488
458
|
if (field.kind == "map") {
|
|
489
|
-
|
|
459
|
+
assert(typeof value == "object" && value != null);
|
|
490
460
|
const jsonObj = {};
|
|
491
461
|
const entries = Object.entries(value);
|
|
492
462
|
switch (field.V.kind) {
|
|
@@ -498,7 +468,7 @@ function writeField(field, value, options) {
|
|
|
498
468
|
case "message":
|
|
499
469
|
for (const [entryKey, entryValue] of entries) {
|
|
500
470
|
// JSON standard allows only (double quoted) string as property key
|
|
501
|
-
const messageType =
|
|
471
|
+
const messageType = resolveMessageType(field.V.T);
|
|
502
472
|
jsonObj[entryKey.toString()] = messageType.toJson(entryValue, options);
|
|
503
473
|
}
|
|
504
474
|
break;
|
|
@@ -515,7 +485,7 @@ function writeField(field, value, options) {
|
|
|
515
485
|
: undefined;
|
|
516
486
|
}
|
|
517
487
|
if (field.repeated) {
|
|
518
|
-
|
|
488
|
+
assert(Array.isArray(value));
|
|
519
489
|
const jsonArr = [];
|
|
520
490
|
switch (field.kind) {
|
|
521
491
|
case "scalar":
|
|
@@ -544,26 +514,26 @@ function writeField(field, value, options) {
|
|
|
544
514
|
case "enum":
|
|
545
515
|
return writeEnum(field.T, value, options.enumAsInteger);
|
|
546
516
|
case "message":
|
|
547
|
-
const messageType =
|
|
548
|
-
return messageType.toJson(
|
|
517
|
+
const messageType = resolveMessageType(field.T);
|
|
518
|
+
return messageType.toJson(wrapField(messageType.fieldWrapper, value));
|
|
549
519
|
}
|
|
550
520
|
}
|
|
551
521
|
function writeScalar(type, value) {
|
|
552
522
|
switch (type) {
|
|
553
523
|
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
|
|
554
|
-
case
|
|
555
|
-
case
|
|
556
|
-
case
|
|
557
|
-
case
|
|
558
|
-
case
|
|
559
|
-
|
|
524
|
+
case ScalarType.INT32:
|
|
525
|
+
case ScalarType.SFIXED32:
|
|
526
|
+
case ScalarType.SINT32:
|
|
527
|
+
case ScalarType.FIXED32:
|
|
528
|
+
case ScalarType.UINT32:
|
|
529
|
+
assert(typeof value == "number");
|
|
560
530
|
return value;
|
|
561
531
|
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
|
|
562
532
|
// Either numbers or strings are accepted. Exponent notation is also accepted.
|
|
563
|
-
case
|
|
533
|
+
case ScalarType.FLOAT:
|
|
564
534
|
// assertFloat32(value);
|
|
565
|
-
case
|
|
566
|
-
|
|
535
|
+
case ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
|
|
536
|
+
assert(typeof value == "number");
|
|
567
537
|
if (Number.isNaN(value))
|
|
568
538
|
return "NaN";
|
|
569
539
|
if (value === Number.POSITIVE_INFINITY)
|
|
@@ -572,32 +542,32 @@ function writeScalar(type, value) {
|
|
|
572
542
|
return "-Infinity";
|
|
573
543
|
return value;
|
|
574
544
|
// string:
|
|
575
|
-
case
|
|
576
|
-
|
|
545
|
+
case ScalarType.STRING:
|
|
546
|
+
assert(typeof value == "string");
|
|
577
547
|
return value;
|
|
578
548
|
// bool:
|
|
579
|
-
case
|
|
580
|
-
|
|
549
|
+
case ScalarType.BOOL:
|
|
550
|
+
assert(typeof value == "boolean");
|
|
581
551
|
return value;
|
|
582
552
|
// JSON value will be a decimal string. Either numbers or strings are accepted.
|
|
583
|
-
case
|
|
584
|
-
case
|
|
585
|
-
case
|
|
586
|
-
case
|
|
587
|
-
case
|
|
588
|
-
|
|
553
|
+
case ScalarType.UINT64:
|
|
554
|
+
case ScalarType.FIXED64:
|
|
555
|
+
case ScalarType.INT64:
|
|
556
|
+
case ScalarType.SFIXED64:
|
|
557
|
+
case ScalarType.SINT64:
|
|
558
|
+
assert(typeof value == "bigint" ||
|
|
589
559
|
typeof value == "string" ||
|
|
590
560
|
typeof value == "number");
|
|
591
561
|
return value.toString();
|
|
592
562
|
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
593
563
|
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
594
|
-
case
|
|
595
|
-
|
|
596
|
-
return
|
|
564
|
+
case ScalarType.BYTES:
|
|
565
|
+
assert(value instanceof Uint8Array);
|
|
566
|
+
return protoBase64.enc(value);
|
|
597
567
|
}
|
|
598
568
|
}
|
|
599
569
|
function writeEnum(type, value, enumAsInteger) {
|
|
600
|
-
|
|
570
|
+
assert(typeof value == "number");
|
|
601
571
|
if (type.typeName == "google.protobuf.NullValue") {
|
|
602
572
|
return null;
|
|
603
573
|
}
|
package/dist/message.d.ts
CHANGED
|
@@ -55,15 +55,15 @@ export interface MessageType<T extends Message<T>> {
|
|
|
55
55
|
* If a message field is already present, it will be merged with the
|
|
56
56
|
* new data.
|
|
57
57
|
*/
|
|
58
|
-
fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>):
|
|
58
|
+
fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): T;
|
|
59
59
|
/**
|
|
60
60
|
* Parse a message from a JSON value.
|
|
61
61
|
*/
|
|
62
|
-
fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>):
|
|
62
|
+
fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): T;
|
|
63
63
|
/**
|
|
64
64
|
* Parse a message from a JSON string.
|
|
65
65
|
*/
|
|
66
|
-
fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>):
|
|
66
|
+
fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): T;
|
|
67
67
|
/**
|
|
68
68
|
* Returns true if the given arguments have equal field values, recursively.
|
|
69
69
|
* Will also return true if both messages are `undefined` or `null`.
|