encode_reflect.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build purego appengine
  5. package impl
  6. import (
  7. "google.golang.org/protobuf/internal/encoding/wire"
  8. )
  9. func sizeEnum(p pointer, tagsize int, _ marshalOptions) (size int) {
  10. v := p.v.Elem().Int()
  11. return tagsize + wire.SizeVarint(uint64(v))
  12. }
  13. func appendEnum(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  14. v := p.v.Elem().Int()
  15. b = wire.AppendVarint(b, wiretag)
  16. b = wire.AppendVarint(b, uint64(v))
  17. return b, nil
  18. }
  19. var coderEnum = pointerCoderFuncs{
  20. size: sizeEnum,
  21. marshal: appendEnum,
  22. }
  23. func sizeEnumNoZero(p pointer, tagsize int, opts marshalOptions) (size int) {
  24. if p.v.Elem().Int() == 0 {
  25. return 0
  26. }
  27. return sizeEnum(p, tagsize, opts)
  28. }
  29. func appendEnumNoZero(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  30. if p.v.Elem().Int() == 0 {
  31. return b, nil
  32. }
  33. return appendEnum(b, p, wiretag, opts)
  34. }
  35. var coderEnumNoZero = pointerCoderFuncs{
  36. size: sizeEnumNoZero,
  37. marshal: appendEnumNoZero,
  38. }
  39. func sizeEnumPtr(p pointer, tagsize int, opts marshalOptions) (size int) {
  40. return sizeEnum(pointer{p.v.Elem()}, tagsize, opts)
  41. }
  42. func appendEnumPtr(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  43. return appendEnum(b, pointer{p.v.Elem()}, wiretag, opts)
  44. }
  45. var coderEnumPtr = pointerCoderFuncs{
  46. size: sizeEnumPtr,
  47. marshal: appendEnumPtr,
  48. }
  49. func sizeEnumSlice(p pointer, tagsize int, opts marshalOptions) (size int) {
  50. return sizeEnumSliceReflect(p.v.Elem(), tagsize, opts)
  51. }
  52. func appendEnumSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  53. return appendEnumSliceReflect(b, p.v.Elem(), wiretag, opts)
  54. }
  55. var coderEnumSlice = pointerCoderFuncs{
  56. size: sizeEnumSlice,
  57. marshal: appendEnumSlice,
  58. }
  59. func sizeEnumPackedSlice(p pointer, tagsize int, _ marshalOptions) (size int) {
  60. s := p.v.Elem()
  61. slen := s.Len()
  62. if slen == 0 {
  63. return 0
  64. }
  65. n := 0
  66. for i := 0; i < slen; i++ {
  67. n += wire.SizeVarint(uint64(s.Index(i).Int()))
  68. }
  69. return tagsize + wire.SizeBytes(n)
  70. }
  71. func appendEnumPackedSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  72. s := p.v.Elem()
  73. slen := s.Len()
  74. if slen == 0 {
  75. return b, nil
  76. }
  77. b = wire.AppendVarint(b, wiretag)
  78. n := 0
  79. for i := 0; i < slen; i++ {
  80. n += wire.SizeVarint(uint64(s.Index(i).Int()))
  81. }
  82. b = wire.AppendVarint(b, uint64(n))
  83. for i := 0; i < slen; i++ {
  84. b = wire.AppendVarint(b, uint64(s.Index(i).Int()))
  85. }
  86. return b, nil
  87. }
  88. var coderEnumPackedSlice = pointerCoderFuncs{
  89. size: sizeEnumPackedSlice,
  90. marshal: appendEnumPackedSlice,
  91. }