Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 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 | |
| 11 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 12 | ) |
| 13 | |
| 14 | type mapConverter struct { |
| 15 | goType reflect.Type |
| 16 | keyConv, valConv Converter |
| 17 | } |
| 18 | |
| 19 | func newMapConverter(t reflect.Type, fd pref.FieldDescriptor) Converter { |
| 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 | |
| 30 | func (c *mapConverter) PBValueOf(v reflect.Value) pref.Value { |
| 31 | if v.Type() != c.goType { |
| 32 | panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType)) |
| 33 | } |
| 34 | return pref.ValueOf(&mapReflect{v, c.keyConv, c.valConv}) |
| 35 | } |
| 36 | |
| 37 | func (c *mapConverter) GoValueOf(v pref.Value) reflect.Value { |
| 38 | return v.Map().(*mapReflect).v |
| 39 | } |
| 40 | |
| 41 | func (c *mapConverter) New() pref.Value { |
| 42 | return c.PBValueOf(reflect.MakeMap(c.goType)) |
| 43 | } |
| 44 | |
| 45 | type mapReflect struct { |
| 46 | v reflect.Value // map[K]V |
| 47 | keyConv Converter |
| 48 | valConv Converter |
| 49 | } |
| 50 | |
| 51 | func (ms *mapReflect) Len() int { |
| 52 | return ms.v.Len() |
| 53 | } |
| 54 | func (ms *mapReflect) Has(k pref.MapKey) bool { |
| 55 | rk := ms.keyConv.GoValueOf(k.Value()) |
| 56 | rv := ms.v.MapIndex(rk) |
| 57 | return rv.IsValid() |
| 58 | } |
| 59 | func (ms *mapReflect) Get(k pref.MapKey) pref.Value { |
| 60 | rk := ms.keyConv.GoValueOf(k.Value()) |
| 61 | rv := ms.v.MapIndex(rk) |
| 62 | if !rv.IsValid() { |
| 63 | return pref.Value{} |
| 64 | } |
| 65 | return ms.valConv.PBValueOf(rv) |
| 66 | } |
| 67 | func (ms *mapReflect) Set(k pref.MapKey, v pref.Value) { |
| 68 | rk := ms.keyConv.GoValueOf(k.Value()) |
| 69 | rv := ms.valConv.GoValueOf(v) |
| 70 | ms.v.SetMapIndex(rk, rv) |
| 71 | } |
| 72 | func (ms *mapReflect) Clear(k pref.MapKey) { |
| 73 | rk := ms.keyConv.GoValueOf(k.Value()) |
| 74 | ms.v.SetMapIndex(rk, reflect.Value{}) |
| 75 | } |
| 76 | func (ms *mapReflect) Range(f func(pref.MapKey, pref.Value) bool) { |
| 77 | for _, k := range ms.v.MapKeys() { |
| 78 | if v := ms.v.MapIndex(k); v.IsValid() { |
| 79 | pk := ms.keyConv.PBValueOf(k).MapKey() |
| 80 | pv := ms.valConv.PBValueOf(v) |
| 81 | if !f(pk, pv) { |
| 82 | return |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | func (ms *mapReflect) NewMessage() pref.Message { |
Damien Neil | f527451 | 2019-08-05 10:48:38 -0700 | [diff] [blame^] | 88 | return ms.NewValue().Message() |
| 89 | } |
| 90 | func (ms *mapReflect) NewValue() pref.Value { |
| 91 | return ms.valConv.New() |
Damien Neil | 954bd92 | 2019-07-17 16:52:10 -0700 | [diff] [blame] | 92 | } |
| 93 | func (ms *mapReflect) ProtoUnwrap() interface{} { |
| 94 | return ms.v.Interface() |
| 95 | } |