generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfastpb_impl.go
727 lines (654 loc) · 20 KB
/
fastpb_impl.go
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
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fastpb
import (
"math"
"unicode/utf8"
"github.com/bytedance/gopkg/lang/span"
"google.golang.org/protobuf/encoding/protowire"
)
// Impl implements Protocol.
var Impl impl
// When encoding length-prefixed fields, we speculatively set aside some number of bytes
// for the length, encode the data, and then encode the length (shifting the data if necessary
// to make room).
const speculativeLength = 1
var (
_ Protocol = impl{}
spanCache = span.NewSpanCache(1024 * 1024) // 1MB
spanCacheEnable bool = false
)
// SetSpanCache enable/disable binary protocol bytes/string allocator
func SetSpanCache(enable bool) {
spanCacheEnable = enable
}
type impl struct{}
// WriteMessage implements TLV(tag, length, value) and V(value).
func (b impl) WriteMessage(buf []byte, number int32, writer Writer) (n int) {
// V
if number == SkipTagNumber {
return writer.FastWrite(buf)
}
// TLV
size := writer.Size()
n += AppendTag(buf, protowire.Number(number), protowire.BytesType)
n += AppendVarint(buf[n:], uint64(size))
n += writer.FastWrite(buf[n:])
return n
}
// WriteListPacked implements TLV(tag, length, value).
func (b impl) WriteListPacked(buf []byte, number int32, length int, single Marshal) (n int) {
n += AppendTag(buf, protowire.Number(number), protowire.BytesType)
buf = buf[n:]
prefix := speculativeLength
offset := prefix
for i := 0; i < length; i++ {
offset += single(buf[offset:], SkipTagNumber, int32(i))
}
mlen := offset - prefix
msiz := protowire.SizeVarint(uint64(mlen))
if msiz != speculativeLength {
copy(buf[msiz:], buf[prefix:prefix+mlen])
}
AppendVarint(buf[:msiz], uint64(mlen))
n += msiz + mlen
return n
}
// WriteMapEntry implements TLV(tag, length, value).
func (b impl) WriteMapEntry(buf []byte, number int32, entry Marshal) (n int) {
n += AppendTag(buf, protowire.Number(number), protowire.BytesType)
buf = buf[n:]
prefix := speculativeLength
mlen := entry(buf[prefix:], MapEntry_Key_FieldNumber, MapEntry_Value_FieldNumber)
msiz := protowire.SizeVarint(uint64(mlen))
if msiz != speculativeLength {
copy(buf[msiz:], buf[prefix:prefix+mlen])
}
AppendVarint(buf[:msiz], uint64(mlen))
n += msiz + mlen
return n
}
// WriteBool implements TV(tag, value) and V(value).
func (b impl) WriteBool(buf []byte, number int32, value bool) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.VarintType)
}
n += AppendVarint(buf[n:], protowire.EncodeBool(value))
return n
}
// WriteInt32 implements TV(tag, value) and V(value).
func (b impl) WriteInt32(buf []byte, number, value int32) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.VarintType)
}
n += AppendVarint(buf[n:], uint64(value))
return n
}
// WriteInt64 implements TV(tag, value) and V(value).
func (b impl) WriteInt64(buf []byte, number int32, value int64) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.VarintType)
}
n += AppendVarint(buf[n:], uint64(value))
return n
}
// WriteUint32 implements TV(tag, value) and V(value).
func (b impl) WriteUint32(buf []byte, number int32, value uint32) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.VarintType)
}
n += AppendVarint(buf[n:], uint64(value))
return n
}
// WriteUint64 implements TV(tag, value) and V(value).
func (b impl) WriteUint64(buf []byte, number int32, value uint64) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.VarintType)
}
n += AppendVarint(buf[n:], value)
return n
}
// WriteSint32 implements TV(tag, value) and V(value).
func (b impl) WriteSint32(buf []byte, number, value int32) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.VarintType)
}
n += AppendVarint(buf[n:], protowire.EncodeZigZag(int64(value)))
return n
}
// WriteSint64 implements TV(tag, value) and V(value).
func (b impl) WriteSint64(buf []byte, number int32, value int64) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.VarintType)
}
n += AppendVarint(buf[n:], protowire.EncodeZigZag(value))
return n
}
// WriteFloat implements TV(tag, value) and V(value).
func (b impl) WriteFloat(buf []byte, number int32, value float32) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.Fixed32Type)
}
n += AppendFixed32(buf[n:], math.Float32bits(value))
return n
}
// WriteDouble implements TV(tag, value) and V(value).
func (b impl) WriteDouble(buf []byte, number int32, value float64) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.Fixed64Type)
}
n += AppendFixed64(buf[n:], math.Float64bits(value))
return n
}
// WriteFixed32 implements TV(tag, value) and V(value).
func (b impl) WriteFixed32(buf []byte, number int32, value uint32) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.Fixed32Type)
}
n += AppendFixed32(buf[n:], value)
return n
}
// WriteFixed64 implements TV(tag, value) and V(value).
func (b impl) WriteFixed64(buf []byte, number int32, value uint64) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.Fixed64Type)
}
n += AppendFixed64(buf[n:], value)
return n
}
// WriteSfixed32 implements TV(tag, value) and V(value).
func (b impl) WriteSfixed32(buf []byte, number, value int32) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.Fixed32Type)
}
n += AppendFixed32(buf[n:], uint32(value))
return n
}
// WriteSfixed64 implements TV(tag, value) and V(value).
func (b impl) WriteSfixed64(buf []byte, number int32, value int64) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.Fixed64Type)
}
n += AppendFixed64(buf[n:], uint64(value))
return n
}
// WriteString implements TLV(tag, length, value) and LV(length, value).
func (b impl) WriteString(buf []byte, number int32, value string) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.BytesType)
}
// only support proto3
if EnforceUTF8() && !utf8.ValidString(value) {
panic(errInvalidUTF8)
}
n += AppendString(buf[n:], value)
return n
}
// WriteBytes implements TLV(tag, length, value) and LV(length, value).
func (b impl) WriteBytes(buf []byte, number int32, value []byte) (n int) {
if number != SkipTagNumber {
n += AppendTag(buf[n:], protowire.Number(number), protowire.BytesType)
}
n += AppendBytes(buf[n:], value)
return n
}
// ReadBool implements TV(tag, value) and V(value).
func (b impl) ReadBool(buf []byte, _type int8) (value bool, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.VarintType {
return value, 0, errUnknown
}
v, n := ConsumeVarint(buf)
if n < 0 {
return value, 0, errDecode
}
return protowire.DecodeBool(v), n, nil
}
// ReadInt32 implements TV(tag, value) and V(value).
func (b impl) ReadInt32(buf []byte, _type int8) (value int32, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.VarintType {
return value, 0, errUnknown
}
v, n := ConsumeVarint(buf)
if n < 0 {
return value, 0, errDecode
}
return int32(v), n, nil
}
// ReadInt64 implements TV(tag, value) and V(value).
func (b impl) ReadInt64(buf []byte, _type int8) (value int64, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.VarintType {
return value, 0, errUnknown
}
v, n := ConsumeVarint(buf)
if n < 0 {
return value, 0, errDecode
}
return int64(v), n, nil
}
// ReadUint32 implements TV(tag, value) and V(value).
func (b impl) ReadUint32(buf []byte, _type int8) (value uint32, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.VarintType {
return value, 0, errUnknown
}
v, n := ConsumeVarint(buf)
if n < 0 {
return value, 0, errDecode
}
return uint32(v), n, nil
}
// ReadUint64 implements TV(tag, value) and V(value).
func (b impl) ReadUint64(buf []byte, _type int8) (value uint64, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.VarintType {
return value, 0, errUnknown
}
v, n := ConsumeVarint(buf)
if n < 0 {
return value, 0, errDecode
}
return v, n, nil
}
// ReadSint32 implements TV(tag, value) and V(value).
func (b impl) ReadSint32(buf []byte, _type int8) (value int32, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.VarintType {
return value, 0, errUnknown
}
v, n := ConsumeVarint(buf)
if n < 0 {
return value, 0, errDecode
}
return int32(protowire.DecodeZigZag(v & math.MaxUint32)), n, nil
}
// ReadSint64 implements TV(tag, value) and V(value).
func (b impl) ReadSint64(buf []byte, _type int8) (value int64, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.VarintType {
return value, 0, errUnknown
}
v, n := ConsumeVarint(buf)
if n < 0 {
return value, 0, errDecode
}
return protowire.DecodeZigZag(v), n, nil
}
// ReadFloat implements TV(tag, value) and V(value).
func (b impl) ReadFloat(buf []byte, _type int8) (value float32, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.Fixed32Type {
return value, 0, errUnknown
}
v, n := protowire.ConsumeFixed32(buf)
if n < 0 {
return value, 0, errDecode
}
return math.Float32frombits(uint32(v)), n, nil
}
// ReadDouble implements TV(tag, value) and V(value).
func (b impl) ReadDouble(buf []byte, _type int8) (value float64, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.Fixed64Type {
return value, 0, errUnknown
}
v, n := protowire.ConsumeFixed64(buf)
if n < 0 {
return value, 0, errDecode
}
return math.Float64frombits(v), n, nil
}
// ReadFixed32 implements TV(tag, value) and V(value).
func (b impl) ReadFixed32(buf []byte, _type int8) (value uint32, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.Fixed32Type {
return value, 0, errUnknown
}
v, n := protowire.ConsumeFixed32(buf)
if n < 0 {
return value, 0, errDecode
}
return uint32(v), n, nil
}
// ReadFixed64 implements TV(tag, value) and V(value).
func (b impl) ReadFixed64(buf []byte, _type int8) (value uint64, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.Fixed64Type {
return value, 0, errUnknown
}
v, n := protowire.ConsumeFixed64(buf)
if n < 0 {
return value, 0, errDecode
}
return v, n, nil
}
// ReadSfixed32 implements TV(tag, value) and V(value).
func (b impl) ReadSfixed32(buf []byte, _type int8) (value int32, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.Fixed32Type {
return value, 0, errUnknown
}
v, n := protowire.ConsumeFixed32(buf)
if n < 0 {
return value, 0, errDecode
}
return int32(v), n, nil
}
// ReadSfixed64 implements TV(tag, value) and V(value).
func (b impl) ReadSfixed64(buf []byte, _type int8) (value int64, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.Fixed64Type {
return value, 0, errUnknown
}
v, n := protowire.ConsumeFixed64(buf)
if n < 0 {
return value, 0, errDecode
}
return int64(v), n, nil
}
// ReadString implements TLV(tag, length, value) and V(length, value).
func (b impl) ReadString(buf []byte, _type int8) (value string, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != protowire.BytesType {
return value, 0, errUnknown
}
v, n := ConsumeBytes(buf)
if n < 0 {
return value, 0, errDecode
}
// only support proto3
if EnforceUTF8() && !utf8.Valid(v) {
return value, 0, errInvalidUTF8
}
if spanCacheEnable {
value = SliceByteToString(spanCache.Copy(v))
} else {
value = string(v)
}
return value, n, nil
}
// ReadBytes .
func (b impl) ReadBytes(buf []byte, _type int8) (value []byte, n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != protowire.BytesType {
return value, 0, errUnknown
}
v, n := ConsumeBytes(buf)
if n < 0 {
return value, 0, errDecode
}
if spanCacheEnable {
value = spanCache.Copy(v)
} else {
value = make([]byte, len(v))
copy(value, v)
}
return value, n, nil
}
// ReadList .
func (b impl) ReadList(buf []byte, _type int8, single Unmarshal) (n int, err error) {
wtyp := protowire.Type(_type)
if wtyp == protowire.BytesType {
var framed []byte
framed, n = ConsumeBytes(buf)
if n < 0 {
return 0, errDecode
}
for len(framed) > 0 {
off, err := single(framed, SkipTypeCheck)
if err != nil {
return 0, err
}
framed = framed[off:]
}
return n, nil
}
n, err = single(buf, _type)
return n, err
}
// ReadMapEntry .
func (b impl) ReadMapEntry(buf []byte, _type int8, umk, umv Unmarshal) (int, error) {
offset := 0
wtyp := protowire.Type(_type)
if wtyp != protowire.BytesType {
return 0, errUnknown
}
bs, n := ConsumeBytes(buf)
if n < 0 {
return 0, errDecode
}
offset = n
// Map entries are represented as a two-element message with fields
// containing the key and value.
for len(bs) > 0 {
num, wtyp, n := ConsumeTag(bs)
if n < 0 {
return 0, errDecode
}
if num > protowire.MaxValidNumber {
return 0, errDecode
}
bs = bs[n:]
err := errUnknown
switch num {
case MapEntry_Key_FieldNumber:
n, err = umk(bs, int8(wtyp))
case MapEntry_Value_FieldNumber:
n, err = umv(bs, int8(wtyp))
}
if err == errUnknown {
n, err = b.Skip(buf, int8(wtyp), int32(num))
}
if err != nil && err != errUnknown {
return 0, err
}
bs = bs[n:]
}
return offset, nil
}
// ReadMessage .
func (b impl) ReadMessage(buf []byte, _type int8, reader Reader) (n int, err error) {
wtyp := protowire.Type(_type)
if wtyp != SkipTypeCheck && wtyp != protowire.BytesType {
return 0, errUnknown
}
// framed
if wtyp == protowire.BytesType {
buf, n = ConsumeBytes(buf)
if n < 0 {
return 0, errDecode
}
}
offset := 0
for offset < len(buf) {
// Parse the tag (field number and wire type).
num, wtyp, l := ConsumeTag(buf[offset:])
offset += l
if l < 0 {
return offset, errDecode
}
if num > protowire.MaxValidNumber {
return offset, errDecode
}
l, err = reader.FastRead(buf[offset:], int8(wtyp), int32(num))
if err != nil {
return offset, err
}
offset += l
}
// check if framed
if n == 0 {
n = offset
}
return n, nil
}
// Skip .
func (b impl) Skip(buf []byte, _type int8, number int32) (n int, err error) {
n = protowire.ConsumeFieldValue(protowire.Number(number), protowire.Type(_type), buf)
if n < 0 {
return 0, errDecode
}
return n, nil
}
// SizeBool .
func (b impl) SizeBool(number int32, value bool) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.VarintType))
}
n += protowire.SizeVarint(protowire.EncodeBool(value))
return n
}
// SizeInt32 .
func (b impl) SizeInt32(number, value int32) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.VarintType))
}
n += protowire.SizeVarint(uint64(value))
return n
}
// SizeInt64 .
func (b impl) SizeInt64(number int32, value int64) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.VarintType))
}
n += protowire.SizeVarint(uint64(value))
return n
}
// SizeUint32 .
func (b impl) SizeUint32(number int32, value uint32) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.VarintType))
}
n += protowire.SizeVarint(uint64(value))
return n
}
// SizeUint64 .
func (b impl) SizeUint64(number int32, value uint64) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.VarintType))
}
n += protowire.SizeVarint(value)
return n
}
// SizeSint32 .
func (b impl) SizeSint32(number, value int32) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.VarintType))
}
n += protowire.SizeVarint(protowire.EncodeZigZag(int64(value)))
return n
}
// SizeSint64 .
func (b impl) SizeSint64(number int32, value int64) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.VarintType))
}
n += protowire.SizeVarint(protowire.EncodeZigZag(value))
return n
}
// SizeFloat .
func (b impl) SizeFloat(number int32, value float32) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.Fixed32Type))
}
n += protowire.SizeFixed32()
return n
}
// SizeDouble .
func (b impl) SizeDouble(number int32, value float64) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.Fixed64Type))
}
n += protowire.SizeFixed64()
return n
}
// SizeFixed32 .
func (b impl) SizeFixed32(number int32, value uint32) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.Fixed32Type))
}
n += protowire.SizeFixed32()
return n
}
// SizeFixed64 .
func (b impl) SizeFixed64(number int32, value uint64) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.Fixed64Type))
}
n += protowire.SizeFixed64()
return n
}
// SizeSfixed32 .
func (b impl) SizeSfixed32(number, value int32) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.Fixed32Type))
}
n += protowire.SizeFixed32()
return n
}
// SizeSfixed64 .
func (b impl) SizeSfixed64(number int32, value int64) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.Fixed64Type))
}
n += protowire.SizeFixed64()
return n
}
// SizeString .
func (b impl) SizeString(number int32, value string) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.BytesType))
}
n += protowire.SizeBytes(len(value))
return n
}
// SizeBytes .
func (b impl) SizeBytes(number int32, value []byte) (n int) {
if number != SkipTagNumber {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.BytesType))
}
n += protowire.SizeBytes(len(value))
return n
}
// SizeMessage .
func (b impl) SizeMessage(number int32, sizer Sizer) (n int) {
if number == SkipTagNumber {
return sizer.Size()
}
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.BytesType))
n += protowire.SizeBytes(sizer.Size())
return n
}
// SizeListPacked .
func (b impl) SizeListPacked(number int32, length int, single EntrySize) (n int) {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.BytesType))
mlen := 0
for i := 0; i < length; i++ {
// append mlen
mlen += single(SkipTagNumber, int32(i))
}
n += protowire.SizeBytes(mlen)
return n
}
// SizeMapEntry .
func (b impl) SizeMapEntry(number int32, entry EntrySize) (n int) {
n += protowire.SizeVarint(protowire.EncodeTag(protowire.Number(number), protowire.BytesType))
mlen := entry(MapEntry_Key_FieldNumber, MapEntry_Value_FieldNumber)
n += protowire.SizeBytes(mlen)
return n
}