convert_map.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. type mapConverter struct {
  11. goType reflect.Type
  12. keyConv, valConv Converter
  13. }
  14. func newMapConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
  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) pref.Value {
  25. if v.Type() != c.goType {
  26. panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
  27. }
  28. return pref.ValueOfMap(&mapReflect{v, c.keyConv, c.valConv})
  29. }
  30. func (c *mapConverter) GoValueOf(v pref.Value) reflect.Value {
  31. return v.Map().(*mapReflect).v
  32. }
  33. func (c *mapConverter) IsValidPB(v pref.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.Type() == c.goType
  42. }
  43. func (c *mapConverter) New() pref.Value {
  44. return c.PBValueOf(reflect.MakeMap(c.goType))
  45. }
  46. func (c *mapConverter) Zero() pref.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 pref.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 pref.MapKey) pref.Value {
  63. rk := ms.keyConv.GoValueOf(k.Value())
  64. rv := ms.v.MapIndex(rk)
  65. if !rv.IsValid() {
  66. return pref.Value{}
  67. }
  68. return ms.valConv.PBValueOf(rv)
  69. }
  70. func (ms *mapReflect) Set(k pref.MapKey, v pref.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 pref.MapKey) {
  76. rk := ms.keyConv.GoValueOf(k.Value())
  77. ms.v.SetMapIndex(rk, reflect.Value{})
  78. }
  79. func (ms *mapReflect) Range(f func(pref.MapKey, pref.Value) bool) {
  80. iter := mapRange(ms.v)
  81. for iter.Next() {
  82. k := ms.keyConv.PBValueOf(iter.Key()).MapKey()
  83. v := ms.valConv.PBValueOf(iter.Value())
  84. if !f(k, v) {
  85. return
  86. }
  87. }
  88. }
  89. func (ms *mapReflect) NewValue() pref.Value {
  90. return ms.valConv.New()
  91. }
  92. func (ms *mapReflect) protoUnwrap() interface{} {
  93. return ms.v.Interface()
  94. }