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