encode_gen.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "unicode/utf8"
  9. "google.golang.org/protobuf/internal/encoding/wire"
  10. "google.golang.org/protobuf/internal/errors"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. )
  13. var wireTypes = map[protoreflect.Kind]wire.Type{
  14. protoreflect.BoolKind: wire.VarintType,
  15. protoreflect.EnumKind: wire.VarintType,
  16. protoreflect.Int32Kind: wire.VarintType,
  17. protoreflect.Sint32Kind: wire.VarintType,
  18. protoreflect.Uint32Kind: wire.VarintType,
  19. protoreflect.Int64Kind: wire.VarintType,
  20. protoreflect.Sint64Kind: wire.VarintType,
  21. protoreflect.Uint64Kind: wire.VarintType,
  22. protoreflect.Sfixed32Kind: wire.Fixed32Type,
  23. protoreflect.Fixed32Kind: wire.Fixed32Type,
  24. protoreflect.FloatKind: wire.Fixed32Type,
  25. protoreflect.Sfixed64Kind: wire.Fixed64Type,
  26. protoreflect.Fixed64Kind: wire.Fixed64Type,
  27. protoreflect.DoubleKind: wire.Fixed64Type,
  28. protoreflect.StringKind: wire.BytesType,
  29. protoreflect.BytesKind: wire.BytesType,
  30. protoreflect.MessageKind: wire.BytesType,
  31. protoreflect.GroupKind: wire.StartGroupType,
  32. }
  33. func (o MarshalOptions) marshalSingular(b []byte, fd protoreflect.FieldDescriptor, v protoreflect.Value) ([]byte, error) {
  34. switch fd.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. if fd.Syntax() == protoreflect.Proto3 && !utf8.ValidString(v.String()) {
  65. return b, errors.InvalidUTF8(string(fd.FullName()))
  66. }
  67. b = wire.AppendString(b, v.String())
  68. case protoreflect.BytesKind:
  69. b = wire.AppendBytes(b, v.Bytes())
  70. case protoreflect.MessageKind:
  71. var pos int
  72. var err error
  73. b, pos = appendSpeculativeLength(b)
  74. b, err = o.marshalMessage(b, v.Message())
  75. if err != nil {
  76. return b, err
  77. }
  78. b = finishSpeculativeLength(b, pos)
  79. case protoreflect.GroupKind:
  80. var err error
  81. b, err = o.marshalMessage(b, v.Message())
  82. if err != nil {
  83. return b, err
  84. }
  85. b = wire.AppendVarint(b, wire.EncodeTag(fd.Number(), wire.EndGroupType))
  86. default:
  87. return b, errors.New("invalid kind %v", fd.Kind())
  88. }
  89. return b, nil
  90. }