You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

142 lines
3.6 KiB

  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 impl
  5. import (
  6. "fmt"
  7. "reflect"
  8. "google.golang.org/protobuf/reflect/protoreflect"
  9. )
  10. func newListConverter(t reflect.Type, fd protoreflect.FieldDescriptor) Converter {
  11. switch {
  12. case t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Slice:
  13. return &listPtrConverter{t, newSingularConverter(t.Elem().Elem(), fd)}
  14. case t.Kind() == reflect.Slice:
  15. return &listConverter{t, newSingularConverter(t.Elem(), fd)}
  16. }
  17. panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
  18. }
  19. type listConverter struct {
  20. goType reflect.Type // []T
  21. c Converter
  22. }
  23. func (c *listConverter) PBValueOf(v reflect.Value) protoreflect.Value {
  24. if v.Type() != c.goType {
  25. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
  26. }
  27. pv := reflect.New(c.goType)
  28. pv.Elem().Set(v)
  29. return protoreflect.ValueOfList(&listReflect{pv, c.c})
  30. }
  31. func (c *listConverter) GoValueOf(v protoreflect.Value) reflect.Value {
  32. rv := v.List().(*listReflect).v
  33. if rv.IsNil() {
  34. return reflect.Zero(c.goType)
  35. }
  36. return rv.Elem()
  37. }
  38. func (c *listConverter) IsValidPB(v protoreflect.Value) bool {
  39. list, ok := v.Interface().(*listReflect)
  40. if !ok {
  41. return false
  42. }
  43. return list.v.Type().Elem() == c.goType
  44. }
  45. func (c *listConverter) IsValidGo(v reflect.Value) bool {
  46. return v.IsValid() && v.Type() == c.goType
  47. }
  48. func (c *listConverter) New() protoreflect.Value {
  49. return protoreflect.ValueOfList(&listReflect{reflect.New(c.goType), c.c})
  50. }
  51. func (c *listConverter) Zero() protoreflect.Value {
  52. return protoreflect.ValueOfList(&listReflect{reflect.Zero(reflect.PtrTo(c.goType)), c.c})
  53. }
  54. type listPtrConverter struct {
  55. goType reflect.Type // *[]T
  56. c Converter
  57. }
  58. func (c *listPtrConverter) PBValueOf(v reflect.Value) protoreflect.Value {
  59. if v.Type() != c.goType {
  60. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
  61. }
  62. return protoreflect.ValueOfList(&listReflect{v, c.c})
  63. }
  64. func (c *listPtrConverter) GoValueOf(v protoreflect.Value) reflect.Value {
  65. return v.List().(*listReflect).v
  66. }
  67. func (c *listPtrConverter) IsValidPB(v protoreflect.Value) bool {
  68. list, ok := v.Interface().(*listReflect)
  69. if !ok {
  70. return false
  71. }
  72. return list.v.Type() == c.goType
  73. }
  74. func (c *listPtrConverter) IsValidGo(v reflect.Value) bool {
  75. return v.IsValid() && v.Type() == c.goType
  76. }
  77. func (c *listPtrConverter) New() protoreflect.Value {
  78. return c.PBValueOf(reflect.New(c.goType.Elem()))
  79. }
  80. func (c *listPtrConverter) Zero() protoreflect.Value {
  81. return c.PBValueOf(reflect.Zero(c.goType))
  82. }
  83. type listReflect struct {
  84. v reflect.Value // *[]T
  85. conv Converter
  86. }
  87. func (ls *listReflect) Len() int {
  88. if ls.v.IsNil() {
  89. return 0
  90. }
  91. return ls.v.Elem().Len()
  92. }
  93. func (ls *listReflect) Get(i int) protoreflect.Value {
  94. return ls.conv.PBValueOf(ls.v.Elem().Index(i))
  95. }
  96. func (ls *listReflect) Set(i int, v protoreflect.Value) {
  97. ls.v.Elem().Index(i).Set(ls.conv.GoValueOf(v))
  98. }
  99. func (ls *listReflect) Append(v protoreflect.Value) {
  100. ls.v.Elem().Set(reflect.Append(ls.v.Elem(), ls.conv.GoValueOf(v)))
  101. }
  102. func (ls *listReflect) AppendMutable() protoreflect.Value {
  103. if _, ok := ls.conv.(*messageConverter); !ok {
  104. panic("invalid AppendMutable on list with non-message type")
  105. }
  106. v := ls.NewElement()
  107. ls.Append(v)
  108. return v
  109. }
  110. func (ls *listReflect) Truncate(i int) {
  111. ls.v.Elem().Set(ls.v.Elem().Slice(0, i))
  112. }
  113. func (ls *listReflect) NewElement() protoreflect.Value {
  114. return ls.conv.New()
  115. }
  116. func (ls *listReflect) IsValid() bool {
  117. return !ls.v.IsNil()
  118. }
  119. func (ls *listReflect) protoUnwrap() interface{} {
  120. return ls.v.Interface()
  121. }