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