convert.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // Package value provides functionality for wrapping Go values to implement
  5. // protoreflect values.
  6. package value
  7. import (
  8. "fmt"
  9. "reflect"
  10. pref "google.golang.org/protobuf/reflect/protoreflect"
  11. )
  12. // Unwrapper unwraps the value to the underlying value.
  13. // This is implemented by List and Map.
  14. type Unwrapper interface {
  15. ProtoUnwrap() interface{}
  16. }
  17. var (
  18. boolType = reflect.TypeOf(bool(false))
  19. int32Type = reflect.TypeOf(int32(0))
  20. int64Type = reflect.TypeOf(int64(0))
  21. uint32Type = reflect.TypeOf(uint32(0))
  22. uint64Type = reflect.TypeOf(uint64(0))
  23. float32Type = reflect.TypeOf(float32(0))
  24. float64Type = reflect.TypeOf(float64(0))
  25. stringType = reflect.TypeOf(string(""))
  26. bytesType = reflect.TypeOf([]byte(nil))
  27. enumIfaceV2 = reflect.TypeOf((*pref.Enum)(nil)).Elem()
  28. messageIfaceV2 = reflect.TypeOf((*pref.ProtoMessage)(nil)).Elem()
  29. byteType = reflect.TypeOf(byte(0))
  30. )
  31. // NewConverter matches a Go type with a protobuf kind and returns a Converter
  32. // that converts between the two. Enums must be a named int32 kind that
  33. // implements protoreflect.Enum, and messages must be pointer to a named
  34. // struct type that implements protoreflect.ProtoMessage.
  35. //
  36. // This matcher deliberately supports a wider range of Go types than what
  37. // protoc-gen-go historically generated to be able to automatically wrap some
  38. // v1 messages generated by other forks of protoc-gen-go.
  39. func NewConverter(t reflect.Type, k pref.Kind) Converter {
  40. switch k {
  41. case pref.BoolKind:
  42. if t.Kind() == reflect.Bool {
  43. return newScalarConverter(t, boolType)
  44. }
  45. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  46. if t.Kind() == reflect.Int32 {
  47. return newScalarConverter(t, int32Type)
  48. }
  49. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  50. if t.Kind() == reflect.Int64 {
  51. return newScalarConverter(t, int64Type)
  52. }
  53. case pref.Uint32Kind, pref.Fixed32Kind:
  54. if t.Kind() == reflect.Uint32 {
  55. return newScalarConverter(t, uint32Type)
  56. }
  57. case pref.Uint64Kind, pref.Fixed64Kind:
  58. if t.Kind() == reflect.Uint64 {
  59. return newScalarConverter(t, uint64Type)
  60. }
  61. case pref.FloatKind:
  62. if t.Kind() == reflect.Float32 {
  63. return newScalarConverter(t, float32Type)
  64. }
  65. case pref.DoubleKind:
  66. if t.Kind() == reflect.Float64 {
  67. return newScalarConverter(t, float64Type)
  68. }
  69. case pref.StringKind:
  70. if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
  71. return newScalarConverter(t, stringType)
  72. }
  73. case pref.BytesKind:
  74. if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
  75. return newScalarConverter(t, bytesType)
  76. }
  77. case pref.EnumKind:
  78. // Handle enums, which must be a named int32 type.
  79. if t.Implements(enumIfaceV2) && t.Kind() == reflect.Int32 {
  80. return Converter{
  81. PBValueOf: func(v reflect.Value) pref.Value {
  82. if v.Type() != t {
  83. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
  84. }
  85. return pref.ValueOf(pref.EnumNumber(v.Int()))
  86. },
  87. GoValueOf: func(v pref.Value) reflect.Value {
  88. return reflect.ValueOf(v.Enum()).Convert(t)
  89. },
  90. NewEnum: func(n pref.EnumNumber) pref.Enum {
  91. return reflect.ValueOf(n).Convert(t).Interface().(pref.Enum)
  92. },
  93. }
  94. }
  95. case pref.MessageKind, pref.GroupKind:
  96. // Handle v2 messages, which must satisfy the proto.Message interface.
  97. if t.Implements(messageIfaceV2) && t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct {
  98. return Converter{
  99. PBValueOf: func(v reflect.Value) pref.Value {
  100. if v.Type() != t {
  101. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), t))
  102. }
  103. return pref.ValueOf(v.Interface().(pref.ProtoMessage).ProtoReflect())
  104. },
  105. GoValueOf: func(v pref.Value) reflect.Value {
  106. rv := reflect.ValueOf(v.Message().Interface())
  107. if rv.Type() != t {
  108. panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), t))
  109. }
  110. return rv
  111. },
  112. NewMessage: func() pref.Message {
  113. return reflect.New(t.Elem()).Interface().(pref.ProtoMessage).ProtoReflect()
  114. },
  115. }
  116. }
  117. }
  118. panic(fmt.Sprintf("invalid Go type %v for protobuf kind %v", t, k))
  119. }
  120. func newScalarConverter(goType, pbType reflect.Type) Converter {
  121. return Converter{
  122. PBValueOf: func(v reflect.Value) pref.Value {
  123. if v.Type() != goType {
  124. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), goType))
  125. }
  126. if goType.Kind() == reflect.String && pbType.Kind() == reflect.Slice && v.Len() == 0 {
  127. return pref.ValueOf([]byte(nil)) // ensure empty string is []byte(nil)
  128. }
  129. return pref.ValueOf(v.Convert(pbType).Interface())
  130. },
  131. GoValueOf: func(v pref.Value) reflect.Value {
  132. rv := reflect.ValueOf(v.Interface())
  133. if rv.Type() != pbType {
  134. panic(fmt.Sprintf("invalid type: got %v, want %v", rv.Type(), pbType))
  135. }
  136. if pbType.Kind() == reflect.String && goType.Kind() == reflect.Slice && rv.Len() == 0 {
  137. return reflect.Zero(goType) // ensure empty string is []byte(nil)
  138. }
  139. return rv.Convert(goType)
  140. },
  141. }
  142. }
  143. // Converter provides functions for converting to/from Go reflect.Value types
  144. // and protobuf protoreflect.Value types.
  145. type Converter struct {
  146. PBValueOf func(reflect.Value) pref.Value
  147. GoValueOf func(pref.Value) reflect.Value
  148. NewEnum func(pref.EnumNumber) pref.Enum
  149. NewMessage func() pref.Message
  150. }