codec_reflect.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. return sizeEnumSliceReflect(p.v.Elem(), tagsize, opts)
  75. }
  76. func appendEnumSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  77. return appendEnumSliceReflect(b, p.v.Elem(), wiretag, opts)
  78. }
  79. func consumeEnumSlice(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (n int, err error) {
  80. return consumeEnumSliceReflect(b, p.v, wtyp, opts)
  81. }
  82. var coderEnumSlice = pointerCoderFuncs{
  83. size: sizeEnumSlice,
  84. marshal: appendEnumSlice,
  85. unmarshal: consumeEnumSlice,
  86. }
  87. func sizeEnumPackedSlice(p pointer, tagsize int, opts marshalOptions) (size int) {
  88. return sizeEnumPackedSliceReflect(p.v.Elem(), tagsize, opts)
  89. }
  90. func appendEnumPackedSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  91. return appendEnumPackedSliceReflect(b, p.v.Elem(), wiretag, opts)
  92. }
  93. var coderEnumPackedSlice = pointerCoderFuncs{
  94. size: sizeEnumPackedSlice,
  95. marshal: appendEnumPackedSlice,
  96. unmarshal: consumeEnumSlice,
  97. }