encode_gen.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/encoding/protowire"
  10. "google.golang.org/protobuf/internal/errors"
  11. "google.golang.org/protobuf/internal/strs"
  12. "google.golang.org/protobuf/reflect/protoreflect"
  13. )
  14. var wireTypes = map[protoreflect.Kind]protowire.Type{
  15. protoreflect.BoolKind: protowire.VarintType,
  16. protoreflect.EnumKind: protowire.VarintType,
  17. protoreflect.Int32Kind: protowire.VarintType,
  18. protoreflect.Sint32Kind: protowire.VarintType,
  19. protoreflect.Uint32Kind: protowire.VarintType,
  20. protoreflect.Int64Kind: protowire.VarintType,
  21. protoreflect.Sint64Kind: protowire.VarintType,
  22. protoreflect.Uint64Kind: protowire.VarintType,
  23. protoreflect.Sfixed32Kind: protowire.Fixed32Type,
  24. protoreflect.Fixed32Kind: protowire.Fixed32Type,
  25. protoreflect.FloatKind: protowire.Fixed32Type,
  26. protoreflect.Sfixed64Kind: protowire.Fixed64Type,
  27. protoreflect.Fixed64Kind: protowire.Fixed64Type,
  28. protoreflect.DoubleKind: protowire.Fixed64Type,
  29. protoreflect.StringKind: protowire.BytesType,
  30. protoreflect.BytesKind: protowire.BytesType,
  31. protoreflect.MessageKind: protowire.BytesType,
  32. protoreflect.GroupKind: protowire.StartGroupType,
  33. }
  34. func (o MarshalOptions) marshalSingular(b []byte, fd protoreflect.FieldDescriptor, v protoreflect.Value) ([]byte, error) {
  35. switch fd.Kind() {
  36. case protoreflect.BoolKind:
  37. b = protowire.AppendVarint(b, protowire.EncodeBool(v.Bool()))
  38. case protoreflect.EnumKind:
  39. b = protowire.AppendVarint(b, uint64(v.Enum()))
  40. case protoreflect.Int32Kind:
  41. b = protowire.AppendVarint(b, uint64(int32(v.Int())))
  42. case protoreflect.Sint32Kind:
  43. b = protowire.AppendVarint(b, protowire.EncodeZigZag(int64(int32(v.Int()))))
  44. case protoreflect.Uint32Kind:
  45. b = protowire.AppendVarint(b, uint64(uint32(v.Uint())))
  46. case protoreflect.Int64Kind:
  47. b = protowire.AppendVarint(b, uint64(v.Int()))
  48. case protoreflect.Sint64Kind:
  49. b = protowire.AppendVarint(b, protowire.EncodeZigZag(v.Int()))
  50. case protoreflect.Uint64Kind:
  51. b = protowire.AppendVarint(b, v.Uint())
  52. case protoreflect.Sfixed32Kind:
  53. b = protowire.AppendFixed32(b, uint32(v.Int()))
  54. case protoreflect.Fixed32Kind:
  55. b = protowire.AppendFixed32(b, uint32(v.Uint()))
  56. case protoreflect.FloatKind:
  57. b = protowire.AppendFixed32(b, math.Float32bits(float32(v.Float())))
  58. case protoreflect.Sfixed64Kind:
  59. b = protowire.AppendFixed64(b, uint64(v.Int()))
  60. case protoreflect.Fixed64Kind:
  61. b = protowire.AppendFixed64(b, v.Uint())
  62. case protoreflect.DoubleKind:
  63. b = protowire.AppendFixed64(b, math.Float64bits(v.Float()))
  64. case protoreflect.StringKind:
  65. if strs.EnforceUTF8(fd) && !utf8.ValidString(v.String()) {
  66. return b, errors.InvalidUTF8(string(fd.FullName()))
  67. }
  68. b = protowire.AppendString(b, v.String())
  69. case protoreflect.BytesKind:
  70. b = protowire.AppendBytes(b, v.Bytes())
  71. case protoreflect.MessageKind:
  72. var pos int
  73. var err error
  74. b, pos = appendSpeculativeLength(b)
  75. b, err = o.marshalMessage(b, v.Message())
  76. if err != nil {
  77. return b, err
  78. }
  79. b = finishSpeculativeLength(b, pos)
  80. case protoreflect.GroupKind:
  81. var err error
  82. b, err = o.marshalMessage(b, v.Message())
  83. if err != nil {
  84. return b, err
  85. }
  86. b = protowire.AppendVarint(b, protowire.EncodeTag(fd.Number(), protowire.EndGroupType))
  87. default:
  88. return b, errors.New("invalid kind %v", fd.Kind())
  89. }
  90. return b, nil
  91. }