-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathkernel.js
4122 lines (3788 loc) · 137 KB
/
kernel.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview Kernel is a class to provide type-checked accessing
* (read/write bool/int32/string/...) on binary data.
*
* When creating the Kernel with the binary data, there is no deep
* decoding done (which requires full type information). The deep decoding is
* deferred until the first time accessing (when accessors can provide
* full type information).
*
* Because accessors can be statically analyzed and stripped, unlike eager
* binary decoding (which requires the full type information of all defined
* fields), Kernel will only need the full type information of used
* fields.
*/
goog.module('protobuf.runtime.Kernel');
const BinaryStorage = goog.require('protobuf.runtime.BinaryStorage');
const BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
const ByteString = goog.require('protobuf.ByteString');
const Int64 = goog.require('protobuf.Int64');
const InternalMessage = goog.require('protobuf.binary.InternalMessage');
const Storage = goog.require('protobuf.runtime.Storage');
const WireType = goog.require('protobuf.binary.WireType');
const Writer = goog.require('protobuf.binary.Writer');
const reader = goog.require('protobuf.binary.reader');
const {CHECK_TYPE, checkCriticalElementIndex, checkCriticalState, checkCriticalType, checkCriticalTypeBool, checkCriticalTypeBoolArray, checkCriticalTypeByteString, checkCriticalTypeByteStringArray, checkCriticalTypeDouble, checkCriticalTypeDoubleArray, checkCriticalTypeFloat, checkCriticalTypeFloatIterable, checkCriticalTypeMessageArray, checkCriticalTypeSignedInt32, checkCriticalTypeSignedInt32Array, checkCriticalTypeSignedInt64, checkCriticalTypeSignedInt64Array, checkCriticalTypeString, checkCriticalTypeStringArray, checkCriticalTypeUnsignedInt32, checkCriticalTypeUnsignedInt32Array, checkDefAndNotNull, checkElementIndex, checkFieldNumber, checkFunctionExists, checkState, checkTypeDouble, checkTypeFloat, checkTypeSignedInt32, checkTypeSignedInt64, checkTypeUnsignedInt32} = goog.require('protobuf.internal.checks');
const {Field, IndexEntry} = goog.require('protobuf.binary.field');
const {buildIndex} = goog.require('protobuf.binary.indexer');
const {createTag, get32BitVarintLength, getTagLength} = goog.require('protobuf.binary.tag');
/**
* Validates the index entry has the correct wire type.
* @param {!IndexEntry} indexEntry
* @param {!WireType} expected
*/
function validateWireType(indexEntry, expected) {
const wireType = Field.getWireType(indexEntry);
checkCriticalState(
wireType === expected,
`Expected wire type: ${expected} but found: ${wireType}`);
}
/**
* Checks if the object implements InternalMessage interface.
* @param {?} obj
* @return {!InternalMessage}
*/
function checkIsInternalMessage(obj) {
const message = /** @type {!InternalMessage} */ (obj);
checkFunctionExists(message.internalGetKernel);
return message;
}
/**
* Checks if the instanceCreator returns an instance that implements the
* InternalMessage interface.
* @param {function(!Kernel):T} instanceCreator
* @template T
*/
function checkInstanceCreator(instanceCreator) {
if (CHECK_TYPE) {
const emptyMessage = instanceCreator(Kernel.createEmpty());
checkFunctionExists(emptyMessage.internalGetKernel);
}
}
/**
* Reads the last entry of the index array using the given read function.
* This is used to implement parsing singular primitive fields.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {function(!BufferDecoder, number):T} readFunc
* @param {!WireType} wireType
* @return {T}
* @template T
*/
function readOptional(indexArray, bufferDecoder, readFunc, wireType) {
const index = indexArray.length - 1;
checkElementIndex(index, indexArray.length);
const indexEntry = indexArray[index];
validateWireType(indexEntry, wireType);
return readFunc(bufferDecoder, Field.getStartIndex(indexEntry));
}
/**
* Converts all entries of the index array to the template type using given read
* methods and return an Iterable containing those converted values.
* Primitive repeated fields may be encoded either packed or unpacked. Thus, two
* read methods are needed for those two cases.
* This is used to implement parsing repeated primitive fields.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {function(!BufferDecoder, number):T} singularReadFunc
* @param {function(!BufferDecoder, number):!Array<T>} packedReadFunc
* @param {!WireType} expectedWireType
* @return {!Array<T>}
* @template T
*/
function readRepeatedPrimitive(
indexArray, bufferDecoder, singularReadFunc, packedReadFunc,
expectedWireType) {
// Fast path when there is a single packed entry.
if (indexArray.length === 1 &&
Field.getWireType(indexArray[0]) === WireType.DELIMITED) {
return packedReadFunc(bufferDecoder, Field.getStartIndex(indexArray[0]));
}
let /** !Array<T> */ result = [];
for (const indexEntry of indexArray) {
const wireType = Field.getWireType(indexEntry);
const startIndex = Field.getStartIndex(indexEntry);
if (wireType === WireType.DELIMITED) {
result = result.concat(packedReadFunc(bufferDecoder, startIndex));
} else {
validateWireType(indexEntry, expectedWireType);
result.push(singularReadFunc(bufferDecoder, startIndex));
}
}
return result;
}
/**
* Converts all entries of the index array to the template type using the given
* read function and return an Array containing those converted values. This is
* used to implement parsing repeated non-primitive fields.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {function(!BufferDecoder, number):T} singularReadFunc
* @return {!Array<T>}
* @template T
*/
function readRepeatedNonPrimitive(indexArray, bufferDecoder, singularReadFunc) {
const result = new Array(indexArray.length);
for (let i = 0; i < indexArray.length; i++) {
validateWireType(indexArray[i], WireType.DELIMITED);
result[i] =
singularReadFunc(bufferDecoder, Field.getStartIndex(indexArray[i]));
}
return result;
}
/**
* Converts all entries of the index array to the template type using the given
* read function and return an Array containing those converted values. This is
* used to implement parsing repeated non-primitive fields.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {!Array<T>}
* @template T
*/
function readRepeatedGroup(
indexArray, bufferDecoder, fieldNumber, instanceCreator, pivot) {
const result = new Array(indexArray.length);
for (let i = 0; i < indexArray.length; i++) {
result[i] = doReadGroup(
bufferDecoder, indexArray[i], fieldNumber, instanceCreator, pivot);
}
return result;
}
/**
* Creates a new bytes array to contain all data of a submessage.
* When there are multiple entries, merge them together.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @return {!BufferDecoder}
*/
function mergeMessageArrays(indexArray, bufferDecoder) {
const dataArrays = indexArray.map(
indexEntry =>
reader.readDelimited(bufferDecoder, Field.getStartIndex(indexEntry)));
return BufferDecoder.merge(dataArrays);
}
/**
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {number=} pivot
* @return {!Kernel}
*/
function readAccessor(indexArray, bufferDecoder, pivot = undefined) {
checkState(indexArray.length > 0);
let accessorBuffer;
// Faster access for one member.
if (indexArray.length === 1) {
const indexEntry = indexArray[0];
validateWireType(indexEntry, WireType.DELIMITED);
accessorBuffer =
reader.readDelimited(bufferDecoder, Field.getStartIndex(indexEntry));
} else {
indexArray.forEach(indexEntry => {
validateWireType(indexEntry, WireType.DELIMITED);
});
accessorBuffer = mergeMessageArrays(indexArray, bufferDecoder);
}
return Kernel.fromBufferDecoder_(accessorBuffer, pivot);
}
/**
* Merges all index entries of the index array using the given read function.
* This is used to implement parsing singular message fields.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
*/
function readMessage(indexArray, bufferDecoder, instanceCreator, pivot) {
checkInstanceCreator(instanceCreator);
const accessor = readAccessor(indexArray, bufferDecoder, pivot);
return instanceCreator(accessor);
}
/**
* Merges all index entries of the index array using the given read function.
* This is used to implement parsing singular group fields.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
*/
function readGroup(
indexArray, bufferDecoder, fieldNumber, instanceCreator, pivot) {
checkInstanceCreator(instanceCreator);
checkState(indexArray.length > 0);
return doReadGroup(
bufferDecoder, indexArray[indexArray.length - 1], fieldNumber,
instanceCreator, pivot);
}
/**
* Merges all index entries of the index array using the given read function.
* This is used to implement parsing singular message fields.
* @param {!BufferDecoder} bufferDecoder
* @param {!IndexEntry} indexEntry
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
*/
function doReadGroup(
bufferDecoder, indexEntry, fieldNumber, instanceCreator, pivot) {
validateWireType(indexEntry, WireType.START_GROUP);
const fieldStartIndex = Field.getStartIndex(indexEntry);
const tag = createTag(WireType.START_GROUP, fieldNumber);
const groupTagLength = get32BitVarintLength(tag);
const groupLength = getTagLength(
bufferDecoder, fieldStartIndex, WireType.START_GROUP, fieldNumber);
const accessorBuffer = bufferDecoder.subBufferDecoder(
fieldStartIndex, groupLength - groupTagLength);
const kernel = Kernel.fromBufferDecoder_(accessorBuffer, pivot);
return instanceCreator(kernel);
}
/**
* @param {!Writer} writer
* @param {number} fieldNumber
* @param {?InternalMessage} value
*/
function writeMessage(writer, fieldNumber, value) {
writer.writeDelimited(
fieldNumber, checkDefAndNotNull(value).internalGetKernel().serialize());
}
/**
* @param {!Writer} writer
* @param {number} fieldNumber
* @param {?InternalMessage} value
*/
function writeGroup(writer, fieldNumber, value) {
const kernel = checkDefAndNotNull(value).internalGetKernel();
writer.writeStartGroup(fieldNumber);
kernel.serializeToWriter(writer);
writer.writeEndGroup(fieldNumber);
}
/**
* Writes the array of Messages into the writer for the given field number.
* @param {!Writer} writer
* @param {number} fieldNumber
* @param {!Iterable<!InternalMessage>} values
*/
function writeRepeatedMessage(writer, fieldNumber, values) {
for (const value of values) {
writeMessage(writer, fieldNumber, value);
}
}
/**
* Writes the array of Messages into the writer for the given field number.
* @param {!Writer} writer
* @param {number} fieldNumber
* @param {!Array<!InternalMessage>} values
*/
function writeRepeatedGroup(writer, fieldNumber, values) {
for (const value of values) {
writeGroup(writer, fieldNumber, value);
}
}
/**
* Array.from has a weird type definition in google3/javascript/externs/es6.js
* and wants the mapping function accept strings.
* @const {function((string|number)): number}
*/
const fround = /** @type {function((string|number)): number} */ (Math.fround);
/**
* Wraps an array and exposes it as an Iterable. This class is used to provide
* immutable access of the array to the caller.
* @implements {Iterable<T>}
* @template T
*/
class ArrayIterable {
/**
* @param {!Array<T>} array
*/
constructor(array) {
/** @private @const {!Array<T>} */
this.array_ = array;
}
/** @return {!Iterator<T>} */
[Symbol.iterator]() {
return this.array_[Symbol.iterator]();
}
}
/**
* Accesses protobuf fields on binary format data. Binary data is decoded lazily
* at the first access.
* @final
*/
class Kernel {
/**
* Create a Kernel for the given binary bytes.
* The bytes array is kept by the Kernel. DON'T MODIFY IT.
* @param {!ArrayBuffer} arrayBuffer Binary bytes.
* @param {number=} pivot Fields with a field number no greater than the pivot
* value will be stored in an array for fast access. Other fields will be
* stored in a map. A higher pivot value can improve runtime performance
* at the expense of requiring more memory. It's recommended to set the
* value to the max field number of the message unless the field numbers
* are too sparse. If the value is not set, a default value specified in
* storage.js will be used.
* @return {!Kernel}
*/
static fromArrayBuffer(arrayBuffer, pivot = undefined) {
const bufferDecoder = BufferDecoder.fromArrayBuffer(arrayBuffer);
return Kernel.fromBufferDecoder_(bufferDecoder, pivot);
}
/**
* Creates an empty Kernel.
* @param {number=} pivot Fields with a field number no greater than the pivot
* value will be stored in an array for fast access. Other fields will be
* stored in a map. A higher pivot value can improve runtime performance
* at the expense of requiring more memory. It's recommended to set the
* value to the max field number of the message unless the field numbers
* are too sparse. If the value is not set, a default value specified in
* storage.js will be used.
* @return {!Kernel}
*/
static createEmpty(pivot = undefined) {
return new Kernel(/* bufferDecoder= */ null, new BinaryStorage(pivot));
}
/**
* Create a Kernel for the given binary bytes.
* The bytes array is kept by the Kernel. DON'T MODIFY IT.
* @param {!BufferDecoder} bufferDecoder Binary bytes.
* @param {number|undefined} pivot
* @return {!Kernel}
* @private
*/
static fromBufferDecoder_(bufferDecoder, pivot) {
return new Kernel(bufferDecoder, buildIndex(bufferDecoder, pivot));
}
/**
* @param {?BufferDecoder} bufferDecoder Binary bytes. Accessor treats the
* bytes as immutable and will never attempt to write to it.
* @param {!Storage<!Field>} fields A map of field number to Field. The
* IndexEntry in each Field needs to be populated with the location of the
* field in the binary data.
* @private
*/
constructor(bufferDecoder, fields) {
/** @private @const {?BufferDecoder} */
this.bufferDecoder_ = bufferDecoder;
/** @private @const {!Storage<!Field>} */
this.fields_ = fields;
}
/**
* Creates a shallow copy of the accessor.
* @return {!Kernel}
*/
shallowCopy() {
return new Kernel(this.bufferDecoder_, this.fields_.shallowCopy());
}
/**
* See definition of the pivot parameter on the fromArrayBuffer() method.
* @return {number}
*/
getPivot() {
return this.fields_.getPivot();
}
/**
* Clears the field for the given field number.
* @param {number} fieldNumber
*/
clearField(fieldNumber) {
this.fields_.delete(fieldNumber);
}
/**
* Returns data for a field specified by the given field number. Also cache
* the data if it doesn't already exist in the cache. When no data is
* available, return the given default value.
* @param {number} fieldNumber
* @param {?T} defaultValue
* @param {function(!Array<!IndexEntry>, !BufferDecoder):T} readFunc
* @param {function(!Writer, number, T)=} encoder
* @return {T}
* @template T
* @private
*/
getFieldWithDefault_(
fieldNumber, defaultValue, readFunc, encoder = undefined) {
checkFieldNumber(fieldNumber);
const field = this.fields_.get(fieldNumber);
if (field === undefined) {
return defaultValue;
}
if (field.hasDecodedValue()) {
checkState(!encoder || !!field.getEncoder());
return field.getDecodedValue();
}
const parsed = readFunc(
checkDefAndNotNull(field.getIndexArray()),
checkDefAndNotNull(this.bufferDecoder_));
field.setCache(parsed, encoder);
return parsed;
}
/**
* Sets data for a singular field specified by the given field number.
* @param {number} fieldNumber
* @param {T} value
* @param {function(!Writer, number, T)} encoder
* @return {T}
* @template T
* @private
*/
setField_(fieldNumber, value, encoder) {
checkFieldNumber(fieldNumber);
this.fields_.set(fieldNumber, Field.fromDecodedValue(value, encoder));
}
/**
* Serializes internal contents to binary format bytes array to the
* given writer.
* @param {!Writer} writer
* @package
*/
serializeToWriter(writer) {
// If we use for...of here, jscompiler returns an array of both types for
// fieldNumber and field without specifying which type is for
// field, which prevents us to use fieldNumber. Thus, we use
// forEach here.
this.fields_.forEach((field, fieldNumber) => {
// If encoder doesn't exist, there is no need to encode the value
// because the data in the index is still valid.
if (field.getEncoder() !== undefined) {
const encoder = checkDefAndNotNull(field.getEncoder());
encoder(writer, fieldNumber, field.getDecodedValue());
return;
}
const indexArr = field.getIndexArray();
if (indexArr) {
for (const indexEntry of indexArr) {
writer.writeTag(fieldNumber, Field.getWireType(indexEntry));
writer.writeBufferDecoder(
checkDefAndNotNull(this.bufferDecoder_),
Field.getStartIndex(indexEntry), Field.getWireType(indexEntry),
fieldNumber);
}
}
});
}
/**
* Serializes internal contents to binary format bytes array.
* @return {!ArrayBuffer}
*/
serialize() {
const writer = new Writer();
this.serializeToWriter(writer);
return writer.getAndResetResultBuffer();
}
/**
* Returns whether data exists at the given field number.
* @param {number} fieldNumber
* @return {boolean}
*/
hasFieldNumber(fieldNumber) {
checkFieldNumber(fieldNumber);
const field = this.fields_.get(fieldNumber);
if (field === undefined) {
return false;
}
if (field.getIndexArray() !== null) {
return true;
}
if (Array.isArray(field.getDecodedValue())) {
// For repeated fields, existence is decided by number of elements.
return (/** !Array<?> */ (field.getDecodedValue())).length > 0;
}
return true;
}
/***************************************************************************
* OPTIONAL GETTER METHODS
***************************************************************************/
/**
* Returns data as boolean for the given field number.
* If no default is given, use false as the default.
* @param {number} fieldNumber
* @param {boolean=} defaultValue
* @return {boolean}
*/
getBoolWithDefault(fieldNumber, defaultValue = false) {
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) =>
readOptional(indexArray, bytes, reader.readBool, WireType.VARINT));
}
/**
* Returns data as a ByteString for the given field number.
* If no default is given, use false as the default.
* @param {number} fieldNumber
* @param {!ByteString=} defaultValue
* @return {!ByteString}
*/
getBytesWithDefault(fieldNumber, defaultValue = ByteString.EMPTY) {
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readBytes, WireType.DELIMITED));
}
/**
* Returns a double for the given field number.
* If no default is given uses zero as the default.
* @param {number} fieldNumber
* @param {number=} defaultValue
* @return {number}
*/
getDoubleWithDefault(fieldNumber, defaultValue = 0) {
checkTypeDouble(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readDouble, WireType.FIXED64));
}
/**
* Returns a fixed32 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {number=} defaultValue
* @return {number}
*/
getFixed32WithDefault(fieldNumber, defaultValue = 0) {
checkTypeUnsignedInt32(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readFixed32, WireType.FIXED32));
}
/**
* Returns a fixed64 for the given field number.
* Note: Since g.m.Long does not support unsigned int64 values we are going
* the Java route here for now and simply output the number as a signed int64.
* Users can get to individual bits by themselves.
* @param {number} fieldNumber
* @param {!Int64=} defaultValue
* @return {!Int64}
*/
getFixed64WithDefault(fieldNumber, defaultValue = Int64.getZero()) {
return this.getSfixed64WithDefault(fieldNumber, defaultValue);
}
/**
* Returns a float for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {number=} defaultValue
* @return {number}
*/
getFloatWithDefault(fieldNumber, defaultValue = 0) {
checkTypeFloat(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readFloat, WireType.FIXED32));
}
/**
* Returns a int32 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {number=} defaultValue
* @return {number}
*/
getInt32WithDefault(fieldNumber, defaultValue = 0) {
checkTypeSignedInt32(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) =>
readOptional(indexArray, bytes, reader.readInt32, WireType.VARINT));
}
/**
* Returns a int64 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {!Int64=} defaultValue
* @return {!Int64}
*/
getInt64WithDefault(fieldNumber, defaultValue = Int64.getZero()) {
checkTypeSignedInt64(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) =>
readOptional(indexArray, bytes, reader.readInt64, WireType.VARINT));
}
/**
* Returns a sfixed32 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {number=} defaultValue
* @return {number}
*/
getSfixed32WithDefault(fieldNumber, defaultValue = 0) {
checkTypeSignedInt32(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readSfixed32, WireType.FIXED32));
}
/**
* Returns a sfixed64 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {!Int64=} defaultValue
* @return {!Int64}
*/
getSfixed64WithDefault(fieldNumber, defaultValue = Int64.getZero()) {
checkTypeSignedInt64(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readSfixed64, WireType.FIXED64));
}
/**
* Returns a sint32 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {number=} defaultValue
* @return {number}
*/
getSint32WithDefault(fieldNumber, defaultValue = 0) {
checkTypeSignedInt32(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readSint32, WireType.VARINT));
}
/**
* Returns a sint64 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {!Int64=} defaultValue
* @return {!Int64}
*/
getSint64WithDefault(fieldNumber, defaultValue = Int64.getZero()) {
checkTypeSignedInt64(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readSint64, WireType.VARINT));
}
/**
* Returns a string for the given field number.
* If no default is given uses empty string as the default.
* @param {number} fieldNumber
* @param {string=} defaultValue
* @return {string}
*/
getStringWithDefault(fieldNumber, defaultValue = '') {
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readString, WireType.DELIMITED));
}
/**
* Returns a uint32 for the given field number.
* If no default is given zero as the default.
* @param {number} fieldNumber
* @param {number=} defaultValue
* @return {number}
*/
getUint32WithDefault(fieldNumber, defaultValue = 0) {
checkTypeUnsignedInt32(defaultValue);
return this.getFieldWithDefault_(
fieldNumber, defaultValue,
(indexArray, bytes) => readOptional(
indexArray, bytes, reader.readUint32, WireType.VARINT));
}
/**
* Returns a uint64 for the given field number.
* Note: Since g.m.Long does not support unsigned int64 values we are going
* the Java route here for now and simply output the number as a signed int64.
* Users can get to individual bits by themselves.
* @param {number} fieldNumber
* @param {!Int64=} defaultValue
* @return {!Int64}
*/
getUint64WithDefault(fieldNumber, defaultValue = Int64.getZero()) {
return this.getInt64WithDefault(fieldNumber, defaultValue);
}
/**
* Returns data as a mutable proto Message for the given field number.
* If no value has been set, return null.
* If hasFieldNumber(fieldNumber) == false before calling, it remains false.
*
* This method should not be used along with getMessage, since calling
* getMessageOrNull after getMessage will not register the encoder.
*
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {?T}
* @template T
*/
getMessageOrNull(fieldNumber, instanceCreator, pivot = undefined) {
return this.getFieldWithDefault_(
fieldNumber, null,
(indexArray, bytes) =>
readMessage(indexArray, bytes, instanceCreator, pivot),
writeMessage);
}
/**
* Returns data as a mutable proto Message for the given field number.
* If no value has been set, return null.
* If hasFieldNumber(fieldNumber) == false before calling, it remains false.
*
* This method should not be used along with getMessage, since calling
* getMessageOrNull after getMessage will not register the encoder.
*
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {?T}
* @template T
*/
getGroupOrNull(fieldNumber, instanceCreator, pivot = undefined) {
return this.getFieldWithDefault_(
fieldNumber, null,
(indexArray, bytes) =>
readGroup(indexArray, bytes, fieldNumber, instanceCreator, pivot),
writeGroup);
}
/**
* Returns data as a mutable proto Message for the given field number.
* If no value has been set previously, creates and attaches an instance.
* Postcondition: hasFieldNumber(fieldNumber) == true.
*
* This method should not be used along with getMessage, since calling
* getMessageAttach after getMessage will not register the encoder.
*
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
*/
getMessageAttach(fieldNumber, instanceCreator, pivot = undefined) {
checkInstanceCreator(instanceCreator);
let instance = this.getMessageOrNull(fieldNumber, instanceCreator, pivot);
if (!instance) {
instance = instanceCreator(Kernel.createEmpty());
this.setField_(fieldNumber, instance, writeMessage);
}
return instance;
}
/**
* Returns data as a mutable proto Message for the given field number.
* If no value has been set previously, creates and attaches an instance.
* Postcondition: hasFieldNumber(fieldNumber) == true.
*
* This method should not be used along with getMessage, since calling
* getMessageAttach after getMessage will not register the encoder.
*
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
*/
getGroupAttach(fieldNumber, instanceCreator, pivot = undefined) {
checkInstanceCreator(instanceCreator);
let instance = this.getGroupOrNull(fieldNumber, instanceCreator, pivot);
if (!instance) {
instance = instanceCreator(Kernel.createEmpty());
this.setField_(fieldNumber, instance, writeGroup);
}
return instance;
}
/**
* Returns data as a proto Message for the given field number.
* If no value has been set, return a default instance.
* This default instance is guaranteed to be the same instance, unless this
* field is cleared.
* Does not register the encoder, so changes made to the returned
* sub-message will not be included when serializing the parent message.
* Use getMessageAttach() if the resulting sub-message should be mutable.
*
* This method should not be used along with getMessageOrNull or
* getMessageAttach, since these methods register the encoder.
*
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
*/
getMessage(fieldNumber, instanceCreator, pivot = undefined) {
checkInstanceCreator(instanceCreator);
const message = this.getFieldWithDefault_(
fieldNumber, null,
(indexArray, bytes) =>
readMessage(indexArray, bytes, instanceCreator, pivot));
// Returns an empty message as the default value if the field doesn't exist.
// We don't pass the default value to getFieldWithDefault_ to reduce object
// allocation.
return message === null ? instanceCreator(Kernel.createEmpty()) : message;
}
/**
* Returns data as a proto Message for the given field number.
* If no value has been set, return a default instance.
* This default instance is guaranteed to be the same instance, unless this
* field is cleared.
* Does not register the encoder, so changes made to the returned
* sub-message will not be included when serializing the parent message.
* Use getMessageAttach() if the resulting sub-message should be mutable.
*
* This method should not be used along with getMessageOrNull or
* getMessageAttach, since these methods register the encoder.
*
* @param {number} fieldNumber
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
*/
getGroup(fieldNumber, instanceCreator, pivot = undefined) {
checkInstanceCreator(instanceCreator);
const message = this.getFieldWithDefault_(
fieldNumber, null,
(indexArray, bytes) =>
readGroup(indexArray, bytes, fieldNumber, instanceCreator, pivot));
// Returns an empty message as the default value if the field doesn't exist.
// We don't pass the default value to getFieldWithDefault_ to reduce object
// allocation.
return message === null ? instanceCreator(Kernel.createEmpty()) : message;
}
/**
* Returns the accessor for the given singular message, or returns null if
* it hasn't been set.
* @param {number} fieldNumber
* @param {number=} pivot
* @return {?Kernel}
*/
getMessageAccessorOrNull(fieldNumber, pivot = undefined) {
checkFieldNumber(fieldNumber);
const field = this.fields_.get(fieldNumber);
if (field === undefined) {
return null;
}
if (field.hasDecodedValue()) {
return checkIsInternalMessage(field.getDecodedValue())
.internalGetKernel();
} else {
return readAccessor(
checkDefAndNotNull(field.getIndexArray()),
checkDefAndNotNull(this.bufferDecoder_), pivot);
}
}
/***************************************************************************
* REPEATED GETTER METHODS
***************************************************************************/
/* Bool */
/**
* Returns an Array instance containing boolean values for the given field
* number.
* @param {number} fieldNumber
* @return {!Array<boolean>}
* @private
*/
getRepeatedBoolArray_(fieldNumber) {
return this.getFieldWithDefault_(
fieldNumber, /* defaultValue= */[],
(indexArray, bytes) => readRepeatedPrimitive(
indexArray, bytes, reader.readBool, reader.readPackedBool,
WireType.VARINT));
}
/**
* Returns the element at index for the given field number.
* @param {number} fieldNumber
* @param {number} index
* @return {boolean}
*/
getRepeatedBoolElement(fieldNumber, index) {
const array = this.getRepeatedBoolArray_(fieldNumber);
checkCriticalElementIndex(index, array.length);
return array[index];
}
/**
* Returns an Iterable instance containing boolean values for the given field
* number.
* @param {number} fieldNumber
* @return {!Iterable<boolean>}
*/
getRepeatedBoolIterable(fieldNumber) {
// Don't split this statement unless needed. JS compiler thinks
// getRepeatedBoolArray_ might have side effects and doesn't inline the
// call in the compiled code. See cl/293894484 for details.
return new ArrayIterable(this.getRepeatedBoolArray_(fieldNumber));
}
/**
* Returns the size of the repeated field.
* @param {number} fieldNumber
* @return {number}
*/
getRepeatedBoolSize(fieldNumber) {
return this.getRepeatedBoolArray_(fieldNumber).length;
}
/* Double */
/**
* Returns an Array instance containing double values for the given field
* number.
* @param {number} fieldNumber