convert_map.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. type mapConverter struct {
  11. goType reflect.Type // map[K]V
  12. keyConv, valConv Converter
  13. }
  14. func newMapConverter(t reflect.Type, fd protoreflect.FieldDescriptor) *mapConverter {
  15. if t.Kind() != reflect.Map {
  16. panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
  17. }
  18. return &mapConverter{
  19. goType: t,
  20. keyConv: newSingularConverter(t.Key(), fd.MapKey()),
  21. valConv: newSingularConverter(t.Elem(), fd.MapValue()),
  22. }
  23. }
  24. func (c *mapConverter) PBValueOf(v reflect.Value) protoreflect.Value {
  25. if v.Type() != c.goType {
  26. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
  27. }
  28. return protoreflect.ValueOfMap(&mapReflect{v, c.keyConv, c.valConv})
  29. }
  30. func (c *mapConverter) GoValueOf(v protoreflect.Value) reflect.Value {
  31. return v.Map().(*mapReflect).v
  32. }
  33. func (c *mapConverter) IsValidPB(v protoreflect.Value) bool {
  34. mapv, ok := v.Interface().(*mapReflect)
  35. if !ok {
  36. return false
  37. }
  38. return mapv.v.Type() == c.goType
  39. }
  40. func (c *mapConverter) IsValidGo(v reflect.Value) bool {
  41. return v.IsValid() && v.Type() == c.goType
  42. }
  43. func (c *mapConverter) New() protoreflect.Value {
  44. return c.PBValueOf(reflect.MakeMap(c.goType))
  45. }
  46. func (c *mapConverter) Zero() protoreflect.Value {
  47. return c.PBValueOf(reflect.Zero(c.goType))
  48. }
  49. type mapReflect struct {
  50. v reflect.Value // map[K]V
  51. keyConv Converter
  52. valConv Converter
  53. }
  54. func (ms *mapReflect) Len() int {
  55. return ms.v.Len()
  56. }
  57. func (ms *mapReflect) Has(k protoreflect.MapKey) bool {
  58. rk := ms.keyConv.GoValueOf(k.Value())
  59. rv := ms.v.MapIndex(rk)
  60. return rv.IsValid()
  61. }
  62. func (ms *mapReflect) Get(k protoreflect.MapKey) protoreflect.Value {
  63. rk := ms.keyConv.GoValueOf(k.Value())
  64. rv := ms.v.MapIndex(rk)
  65. if !rv.IsValid() {
  66. return protoreflect.Value{}
  67. }
  68. return ms.valConv.PBValueOf(rv)
  69. }
  70. func (ms *mapReflect) Set(k protoreflect.MapKey, v protoreflect.Value) {
  71. rk := ms.keyConv.GoValueOf(k.Value())
  72. rv := ms.valConv.GoValueOf(v)
  73. ms.v.SetMapIndex(rk, rv)
  74. }
  75. func (ms *mapReflect) Clear(k protoreflect.MapKey) {
  76. rk := ms.keyConv.GoValueOf(k.Value())
  77. ms.v.SetMapIndex(rk, reflect.Value{})
  78. }
  79. func (ms *mapReflect) Mutable(k protoreflect.MapKey) protoreflect.Value {
  80. if _, ok := ms.valConv.(*messageConverter); !ok {
  81. panic("invalid Mutable on map with non-message value type")
  82. }
  83. v := ms.Get(k)
  84. if !v.IsValid() {
  85. v = ms.NewValue()
  86. ms.Set(k, v)
  87. }
  88. return v
  89. }
  90. func (ms *mapReflect) Range(f func(protoreflect.MapKey, protoreflect.Value) bool) {
  91. iter := mapRange(ms.v)
  92. for iter.Next() {
  93. k := ms.keyConv.PBValueOf(iter.Key()).MapKey()
  94. v := ms.valConv.PBValueOf(iter.Value())
  95. if !f(k, v) {
  96. return
  97. }
  98. }
  99. }
  100. func (ms *mapReflect) NewValue() protoreflect.Value {
  101. return ms.valConv.New()
  102. }
  103. func (ms *mapReflect) IsValid() bool {
  104. return !ms.v.IsNil()
  105. }
  106. func (ms *mapReflect) protoUnwrap() interface{} {
  107. return ms.v.Interface()
  108. }