codec_reflect.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. "reflect"
  8. "google.golang.org/protobuf/internal/encoding/wire"
  9. )
  10. func sizeEnum(p pointer, tagsize int, _ marshalOptions) (size int) {
  11. v := p.v.Elem().Int()
  12. return tagsize + wire.SizeVarint(uint64(v))
  13. }
  14. func appendEnum(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  15. v := p.v.Elem().Int()
  16. b = wire.AppendVarint(b, wiretag)
  17. b = wire.AppendVarint(b, uint64(v))
  18. return b, nil
  19. }
  20. func consumeEnum(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (n int, err error) {
  21. if wtyp != wire.VarintType {
  22. return 0, errUnknown
  23. }
  24. v, n := wire.ConsumeVarint(b)
  25. if n < 0 {
  26. return 0, wire.ParseError(n)
  27. }
  28. p.v.Elem().SetInt(int64(v))
  29. return n, nil
  30. }
  31. var coderEnum = pointerCoderFuncs{
  32. size: sizeEnum,
  33. marshal: appendEnum,
  34. unmarshal: consumeEnum,
  35. }
  36. func sizeEnumNoZero(p pointer, tagsize int, opts marshalOptions) (size int) {
  37. if p.v.Elem().Int() == 0 {
  38. return 0
  39. }
  40. return sizeEnum(p, tagsize, opts)
  41. }
  42. func appendEnumNoZero(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  43. if p.v.Elem().Int() == 0 {
  44. return b, nil
  45. }
  46. return appendEnum(b, p, wiretag, opts)
  47. }
  48. var coderEnumNoZero = pointerCoderFuncs{
  49. size: sizeEnumNoZero,
  50. marshal: appendEnumNoZero,
  51. unmarshal: consumeEnum,
  52. }
  53. func sizeEnumPtr(p pointer, tagsize int, opts marshalOptions) (size int) {
  54. return sizeEnum(pointer{p.v.Elem()}, tagsize, opts)
  55. }
  56. func appendEnumPtr(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  57. return appendEnum(b, pointer{p.v.Elem()}, wiretag, opts)
  58. }
  59. func consumeEnumPtr(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (n int, err error) {
  60. if wtyp != wire.VarintType {
  61. return 0, errUnknown
  62. }
  63. if p.v.Elem().IsNil() {
  64. p.v.Elem().Set(reflect.New(p.v.Elem().Type().Elem()))
  65. }
  66. return consumeEnum(b, pointer{p.v.Elem()}, wtyp, opts)
  67. }
  68. var coderEnumPtr = pointerCoderFuncs{
  69. size: sizeEnumPtr,
  70. marshal: appendEnumPtr,
  71. unmarshal: consumeEnumPtr,
  72. }
  73. func sizeEnumSlice(p pointer, tagsize int, opts marshalOptions) (size int) {
  74. s := p.v.Elem()
  75. for i, llen := 0, s.Len(); i < llen; i++ {
  76. size += wire.SizeVarint(uint64(s.Index(i).Int())) + tagsize
  77. }
  78. return size
  79. }
  80. func appendEnumSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  81. s := p.v.Elem()
  82. for i, llen := 0, s.Len(); i < llen; i++ {
  83. b = wire.AppendVarint(b, wiretag)
  84. b = wire.AppendVarint(b, uint64(s.Index(i).Int()))
  85. }
  86. return b, nil
  87. }
  88. func consumeEnumSlice(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (n int, err error) {
  89. s := p.v.Elem()
  90. if wtyp == wire.BytesType {
  91. b, n = wire.ConsumeBytes(b)
  92. if n < 0 {
  93. return 0, wire.ParseError(n)
  94. }
  95. for len(b) > 0 {
  96. v, n := wire.ConsumeVarint(b)
  97. if n < 0 {
  98. return 0, wire.ParseError(n)
  99. }
  100. rv := reflect.New(s.Type().Elem()).Elem()
  101. rv.SetInt(int64(v))
  102. s.Set(reflect.Append(s, rv))
  103. b = b[n:]
  104. }
  105. return n, nil
  106. }
  107. if wtyp != wire.VarintType {
  108. return 0, errUnknown
  109. }
  110. v, n := wire.ConsumeVarint(b)
  111. if n < 0 {
  112. return 0, wire.ParseError(n)
  113. }
  114. rv := reflect.New(s.Type().Elem()).Elem()
  115. rv.SetInt(int64(v))
  116. s.Set(reflect.Append(s, rv))
  117. return n, nil
  118. }
  119. var coderEnumSlice = pointerCoderFuncs{
  120. size: sizeEnumSlice,
  121. marshal: appendEnumSlice,
  122. unmarshal: consumeEnumSlice,
  123. }
  124. func sizeEnumPackedSlice(p pointer, tagsize int, opts marshalOptions) (size int) {
  125. s := p.v.Elem()
  126. llen := s.Len()
  127. if llen == 0 {
  128. return 0
  129. }
  130. n := 0
  131. for i := 0; i < llen; i++ {
  132. n += wire.SizeVarint(uint64(s.Index(i).Int()))
  133. }
  134. return tagsize + wire.SizeBytes(n)
  135. }
  136. func appendEnumPackedSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  137. s := p.v.Elem()
  138. llen := s.Len()
  139. if llen == 0 {
  140. return b, nil
  141. }
  142. b = wire.AppendVarint(b, wiretag)
  143. n := 0
  144. for i := 0; i < llen; i++ {
  145. n += wire.SizeVarint(uint64(s.Index(i).Int()))
  146. }
  147. b = wire.AppendVarint(b, uint64(n))
  148. for i := 0; i < llen; i++ {
  149. b = wire.AppendVarint(b, uint64(s.Index(i).Int()))
  150. }
  151. return b, nil
  152. }
  153. var coderEnumPackedSlice = pointerCoderFuncs{
  154. size: sizeEnumPackedSlice,
  155. marshal: appendEnumPackedSlice,
  156. unmarshal: consumeEnumSlice,
  157. }