convert_list.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. pref "google.golang.org/protobuf/reflect/protoreflect"
  9. )
  10. func newListConverter(t reflect.Type, fd pref.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
  21. c Converter
  22. }
  23. func (c *listConverter) PBValueOf(v reflect.Value) pref.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 pref.ValueOfList(&listReflect{pv, c.c})
  30. }
  31. func (c *listConverter) GoValueOf(v pref.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 pref.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.Type() == c.goType
  47. }
  48. func (c *listConverter) New() pref.Value {
  49. return pref.ValueOfList(&listReflect{reflect.New(c.goType), c.c})
  50. }
  51. func (c *listConverter) Zero() pref.Value {
  52. return pref.ValueOfList(&listReflect{reflect.Zero(reflect.PtrTo(c.goType)), c.c})
  53. }
  54. type listPtrConverter struct {
  55. goType reflect.Type
  56. c Converter
  57. }
  58. func (c *listPtrConverter) PBValueOf(v reflect.Value) pref.Value {
  59. if v.Type() != c.goType {
  60. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
  61. }
  62. return pref.ValueOfList(&listReflect{v, c.c})
  63. }
  64. func (c *listPtrConverter) GoValueOf(v pref.Value) reflect.Value {
  65. return v.List().(*listReflect).v
  66. }
  67. func (c *listPtrConverter) IsValidPB(v pref.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.Type() == c.goType
  76. }
  77. func (c *listPtrConverter) New() pref.Value {
  78. return c.PBValueOf(reflect.New(c.goType.Elem()))
  79. }
  80. func (c *listPtrConverter) Zero() pref.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) pref.Value {
  94. return ls.conv.PBValueOf(ls.v.Elem().Index(i))
  95. }
  96. func (ls *listReflect) Set(i int, v pref.Value) {
  97. ls.v.Elem().Index(i).Set(ls.conv.GoValueOf(v))
  98. }
  99. func (ls *listReflect) Append(v pref.Value) {
  100. ls.v.Elem().Set(reflect.Append(ls.v.Elem(), ls.conv.GoValueOf(v)))
  101. }
  102. func (ls *listReflect) Truncate(i int) {
  103. ls.v.Elem().Set(ls.v.Elem().Slice(0, i))
  104. }
  105. func (ls *listReflect) NewElement() pref.Value {
  106. return ls.conv.New()
  107. }
  108. func (ls *listReflect) protoUnwrap() interface{} {
  109. return ls.v.Interface()
  110. }