forked from protocolbuffers/protobuf-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal_public.js
More file actions
112 lines (104 loc) · 3.97 KB
/
internal_public.js
File metadata and controls
112 lines (104 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @fileoverview Public APIs exposed purely for use by generated code. Use of
* these APIs outside of that context is not supported and actively discouraged.
* @public
*
* DO NOT USE THIS OUTSIDE OF THIS PACKAGE.
*/
goog.module('jspb.internal.public_for_gencode');
goog.module.declareLegacyNamespace();
const asserts = goog.require('goog.asserts');
const { BinaryReader } = goog.require('jspb.binary.reader');
const { BinaryWriter } = goog.requireType('jspb.binary.writer');
const {Map: JspbMap} = goog.requireType('jspb.Map');
/**
* Write this Map field in wire format to a BinaryWriter, using the given
* field number.
* @param {?JspbMap<K,V>} map
* @param {number} fieldNumber
* @param {!BinaryWriter} writer
* @param {function(this:BinaryWriter,number,K_OR_NULL)} keyWriterFn
* The method on BinaryWriter that writes type K to the stream.
* @param {function(this:BinaryWriter,number,V,?=)|
* function(this:BinaryWriter,number,V,?)} valueWriterFn
* The method on BinaryWriter that writes type V to the stream. May be
* writeMessage, in which case the second callback arg form is used.
* @param {function(V,!BinaryWriter)=} valueWriterCallback
* The BinaryWriter serialization callback for type V, if V is a message
* type.
* @template K,V
* Use go/closure-ttl to create a `K|null` type for the keyWriterFn argument
* closure type inference will occasionally infer K based on the keyWriterFn
* argument instead of the map argument which will cause type errors when they
* don't match
* @template K_OR_NULL := union(K, 'null') =:
*/
function serializeMapToBinary(
map, fieldNumber, writer, keyWriterFn, valueWriterFn, valueWriterCallback) {
if (!map) {
return;
}
map.forEach((value, key) => {
writer.writeMessage(
fieldNumber, /* we need a non-null value to pass here */ map,
(ignored, w) => {
keyWriterFn.call(w, 1, key);
valueWriterFn.call(w, 2, value, valueWriterCallback);
});
});
}
/**
* Read one key/value message from the given BinaryReader. Compatible as the
* `reader` callback parameter to BinaryReader.readMessage, to be called
* when a key/value pair submessage is encountered. If the Key is undefined,
* we should default it to 0.
* @template K, V
* @param {!JspbMap<K,V>} map
* @param {!BinaryReader} reader
* @param {function(this:BinaryReader):K} keyReaderFn
* The method on BinaryReader that reads type K from the stream.
*
* @param {K} defaultKey
* The default value for the type of map keys. Accepting map entries with
* unset keys is required for maps to be backwards compatible with the
* repeated message representation described here: goo.gl/zuoLAC
*
* @param {function(this:BinaryReader):V|function(V,!BinaryReader)}
* valueReaderFn
* The method on BinaryReader that reads type V from the stream, or a
* callback for readMessage.
*
* @param {V} defaultValue
* The default value for the type of map values. Accepting map entries with
* unset values is required for maps to be backwards compatible with the
* repeated message representation described here: goo.gl/zuoLAC
*/
function deserializeMapFromBinary(
map, reader, keyReaderFn, defaultKey, valueReaderFn, defaultValue) {
reader.readMessage(map, (message, reader) => {
let key = defaultKey;
let value = defaultValue;
while (reader.nextField()) {
if (reader.isEndGroup()) {
break;
}
const field = reader.getFieldNumber();
if (field == 1) {
// Key.
key = keyReaderFn.call(reader);
} else if (field == 2) {
// Value.
if (map.valueCtor) {
reader.readMessage(value, valueReaderFn);
} else {
value = (/** @type {function(this:BinaryReader):?} */ (valueReaderFn))
.call(reader);
}
}
}
asserts.assert(key != undefined);
asserts.assert(value != undefined);
map.set(key, value);
});
}
exports = {deserializeMapFromBinary, serializeMapToBinary};