field.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @fileoverview Contains classes that hold data for a protobuf field.
  3. */
  4. goog.module('protobuf.binary.field');
  5. const WireType = goog.requireType('protobuf.binary.WireType');
  6. const Writer = goog.requireType('protobuf.binary.Writer');
  7. const {checkDefAndNotNull, checkState} = goog.require('protobuf.internal.checks');
  8. /**
  9. * Number of bits taken to represent a wire type.
  10. * @const {number}
  11. */
  12. const WIRE_TYPE_LENGTH_BITS = 3;
  13. /** @const {number} */
  14. const WIRE_TYPE_EXTRACTOR = (1 << WIRE_TYPE_LENGTH_BITS) - 1;
  15. /**
  16. * An IndexEntry consists of the wire type and the position of a field in the
  17. * binary data. The wire type and the position are encoded into a single number
  18. * to save memory, which can be decoded using Field.getWireType() and
  19. * Field.getStartIndex() methods.
  20. * @typedef {number}
  21. */
  22. let IndexEntry;
  23. /**
  24. * An entry containing the index into the binary data and/or the corresponding
  25. * cached JS object(s) for a field.
  26. * @template T
  27. * @final
  28. * @package
  29. */
  30. class Field {
  31. /**
  32. * Creates a field and inserts the wireType and position of the first
  33. * occurrence of a field.
  34. * @param {!WireType} wireType
  35. * @param {number} startIndex
  36. * @return {!Field}
  37. */
  38. static fromFirstIndexEntry(wireType, startIndex) {
  39. return new Field([Field.encodeIndexEntry(wireType, startIndex)]);
  40. }
  41. /**
  42. * @param {T} decodedValue The cached JS object decoded from the binary data.
  43. * @param {function(!Writer, number, T):void|undefined} encoder Write function
  44. * to encode the cache into binary bytes.
  45. * @return {!Field}
  46. * @template T
  47. */
  48. static fromDecodedValue(decodedValue, encoder) {
  49. return new Field(null, decodedValue, encoder);
  50. }
  51. /**
  52. * @param {!WireType} wireType
  53. * @param {number} startIndex
  54. * @return {!IndexEntry}
  55. */
  56. static encodeIndexEntry(wireType, startIndex) {
  57. return startIndex << WIRE_TYPE_LENGTH_BITS | wireType;
  58. }
  59. /**
  60. * @param {!IndexEntry} indexEntry
  61. * @return {!WireType}
  62. */
  63. static getWireType(indexEntry) {
  64. return /** @type {!WireType} */ (indexEntry & WIRE_TYPE_EXTRACTOR);
  65. }
  66. /**
  67. * @param {!IndexEntry} indexEntry
  68. * @return {number}
  69. */
  70. static getStartIndex(indexEntry) {
  71. return indexEntry >> WIRE_TYPE_LENGTH_BITS;
  72. }
  73. /**
  74. * @param {?Array<!IndexEntry>} indexArray
  75. * @param {T=} decodedValue
  76. * @param {function(!Writer, number, T):void=} encoder
  77. * @private
  78. */
  79. constructor(indexArray, decodedValue = undefined, encoder = undefined) {
  80. checkState(
  81. !!indexArray || decodedValue !== undefined,
  82. 'At least one of indexArray and decodedValue must be set');
  83. /** @private {?Array<!IndexEntry>} */
  84. this.indexArray_ = indexArray;
  85. /** @private {T|undefined} */
  86. this.decodedValue_ = decodedValue;
  87. // TODO: Consider storing an enum to represent encoder
  88. /** @private {function(!Writer, number, T)|undefined} */
  89. this.encoder_ = encoder;
  90. }
  91. /**
  92. * Adds a new IndexEntry.
  93. * @param {!WireType} wireType
  94. * @param {number} startIndex
  95. */
  96. addIndexEntry(wireType, startIndex) {
  97. checkDefAndNotNull(this.indexArray_)
  98. .push(Field.encodeIndexEntry(wireType, startIndex));
  99. }
  100. /**
  101. * Returns the array of IndexEntry.
  102. * @return {?Array<!IndexEntry>}
  103. */
  104. getIndexArray() {
  105. return this.indexArray_;
  106. }
  107. /**
  108. * Caches the decoded value and sets the write function to encode cache into
  109. * binary bytes.
  110. * @param {T} decodedValue
  111. * @param {function(!Writer, number, T):void|undefined} encoder
  112. */
  113. setCache(decodedValue, encoder) {
  114. this.decodedValue_ = decodedValue;
  115. this.encoder_ = encoder;
  116. this.maybeRemoveIndexArray_();
  117. }
  118. /**
  119. * If the decoded value has been set.
  120. * @return {boolean}
  121. */
  122. hasDecodedValue() {
  123. return this.decodedValue_ !== undefined;
  124. }
  125. /**
  126. * Returns the cached decoded value. The value needs to be set when this
  127. * method is called.
  128. * @return {T}
  129. */
  130. getDecodedValue() {
  131. // Makes sure that the decoded value in the cache has already been set. This
  132. // prevents callers from doing `if (field.getDecodedValue()) {...}` to check
  133. // if a value exist in the cache, because the check might return false even
  134. // if the cache has a valid value set (e.g. 0 or empty string).
  135. checkState(this.decodedValue_ !== undefined);
  136. return this.decodedValue_;
  137. }
  138. /**
  139. * Returns the write function to encode cache into binary bytes.
  140. * @return {function(!Writer, number, T)|undefined}
  141. */
  142. getEncoder() {
  143. return this.encoder_;
  144. }
  145. /**
  146. * Returns a copy of the field, containing the original index entries and a
  147. * shallow copy of the cache.
  148. * @return {!Field}
  149. */
  150. shallowCopy() {
  151. // Repeated fields are arrays in the cache.
  152. // We have to copy the array to make sure that modifications to a repeated
  153. // field (e.g. add) are not seen on a cloned accessor.
  154. const copiedCache = this.hasDecodedValue() ?
  155. (Array.isArray(this.getDecodedValue()) ? [...this.getDecodedValue()] :
  156. this.getDecodedValue()) :
  157. undefined;
  158. return new Field(this.getIndexArray(), copiedCache, this.getEncoder());
  159. }
  160. /**
  161. * @private
  162. */
  163. maybeRemoveIndexArray_() {
  164. checkState(
  165. this.encoder_ === undefined || this.decodedValue_ !== undefined,
  166. 'Encoder exists but decoded value doesn\'t');
  167. if (this.encoder_ !== undefined) {
  168. this.indexArray_ = null;
  169. }
  170. }
  171. }
  172. exports = {
  173. IndexEntry,
  174. Field,
  175. };