encode_gen.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2018 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. // Code generated by generate-types. DO NOT EDIT.
  5. package proto
  6. import (
  7. "math"
  8. "github.com/golang/protobuf/v2/internal/encoding/wire"
  9. "github.com/golang/protobuf/v2/internal/errors"
  10. "github.com/golang/protobuf/v2/reflect/protoreflect"
  11. )
  12. var wireTypes = map[protoreflect.Kind]wire.Type{
  13. protoreflect.BoolKind: wire.VarintType,
  14. protoreflect.EnumKind: wire.VarintType,
  15. protoreflect.Int32Kind: wire.VarintType,
  16. protoreflect.Sint32Kind: wire.VarintType,
  17. protoreflect.Uint32Kind: wire.VarintType,
  18. protoreflect.Int64Kind: wire.VarintType,
  19. protoreflect.Sint64Kind: wire.VarintType,
  20. protoreflect.Uint64Kind: wire.VarintType,
  21. protoreflect.Sfixed32Kind: wire.Fixed32Type,
  22. protoreflect.Fixed32Kind: wire.Fixed32Type,
  23. protoreflect.FloatKind: wire.Fixed32Type,
  24. protoreflect.Sfixed64Kind: wire.Fixed64Type,
  25. protoreflect.Fixed64Kind: wire.Fixed64Type,
  26. protoreflect.DoubleKind: wire.Fixed64Type,
  27. protoreflect.StringKind: wire.BytesType,
  28. protoreflect.BytesKind: wire.BytesType,
  29. protoreflect.MessageKind: wire.BytesType,
  30. protoreflect.GroupKind: wire.StartGroupType,
  31. }
  32. func (o MarshalOptions) marshalSingular(b []byte, num wire.Number, kind protoreflect.Kind, v protoreflect.Value) ([]byte, error) {
  33. var nerr errors.NonFatal
  34. switch kind {
  35. case protoreflect.BoolKind:
  36. b = wire.AppendVarint(b, wire.EncodeBool(v.Bool()))
  37. case protoreflect.EnumKind:
  38. b = wire.AppendVarint(b, uint64(v.Enum()))
  39. case protoreflect.Int32Kind:
  40. b = wire.AppendVarint(b, uint64(int32(v.Int())))
  41. case protoreflect.Sint32Kind:
  42. b = wire.AppendVarint(b, wire.EncodeZigZag(int64(int32(v.Int()))))
  43. case protoreflect.Uint32Kind:
  44. b = wire.AppendVarint(b, uint64(uint32(v.Uint())))
  45. case protoreflect.Int64Kind:
  46. b = wire.AppendVarint(b, uint64(v.Int()))
  47. case protoreflect.Sint64Kind:
  48. b = wire.AppendVarint(b, wire.EncodeZigZag(v.Int()))
  49. case protoreflect.Uint64Kind:
  50. b = wire.AppendVarint(b, v.Uint())
  51. case protoreflect.Sfixed32Kind:
  52. b = wire.AppendFixed32(b, uint32(v.Int()))
  53. case protoreflect.Fixed32Kind:
  54. b = wire.AppendFixed32(b, uint32(v.Uint()))
  55. case protoreflect.FloatKind:
  56. b = wire.AppendFixed32(b, math.Float32bits(float32(v.Float())))
  57. case protoreflect.Sfixed64Kind:
  58. b = wire.AppendFixed64(b, uint64(v.Int()))
  59. case protoreflect.Fixed64Kind:
  60. b = wire.AppendFixed64(b, v.Uint())
  61. case protoreflect.DoubleKind:
  62. b = wire.AppendFixed64(b, math.Float64bits(v.Float()))
  63. case protoreflect.StringKind:
  64. b = wire.AppendBytes(b, []byte(v.String()))
  65. case protoreflect.BytesKind:
  66. b = wire.AppendBytes(b, v.Bytes())
  67. case protoreflect.MessageKind:
  68. var pos int
  69. var err error
  70. b, pos = appendSpeculativeLength(b)
  71. b, err = o.marshalMessage(b, v.Message())
  72. if !nerr.Merge(err) {
  73. return b, err
  74. }
  75. b = finishSpeculativeLength(b, pos)
  76. case protoreflect.GroupKind:
  77. var err error
  78. b, err = o.marshalMessage(b, v.Message())
  79. if !nerr.Merge(err) {
  80. return b, err
  81. }
  82. b = wire.AppendVarint(b, wire.EncodeTag(num, wire.EndGroupType))
  83. default:
  84. return b, errors.New("invalid kind %v", kind)
  85. }
  86. return b, nerr.E
  87. }