Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [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 value |
| 6 | |
| 7 | import ( |
| 8 | "reflect" |
| 9 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 10 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 11 | ) |
| 12 | |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 13 | func MapOf(p interface{}, kc, kv Converter) interface { |
| 14 | pref.Map |
| 15 | Unwrapper |
| 16 | } { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 17 | // TODO: Validate that p is a *map[K]V? |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 18 | rv := reflect.ValueOf(p) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 19 | return mapReflect{rv, kc, kv} |
| 20 | } |
| 21 | |
| 22 | type mapReflect struct { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 23 | v reflect.Value // *map[K]V |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 24 | keyConv Converter |
| 25 | valConv Converter |
| 26 | } |
| 27 | |
| 28 | func (ms mapReflect) Len() int { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 29 | if ms.v.IsNil() { |
| 30 | return 0 |
| 31 | } |
| 32 | return ms.v.Elem().Len() |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 33 | } |
| 34 | func (ms mapReflect) Has(k pref.MapKey) bool { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 35 | if ms.v.IsNil() { |
| 36 | return false |
| 37 | } |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 38 | rk := ms.keyConv.GoValueOf(k.Value()) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 39 | rv := ms.v.Elem().MapIndex(rk) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 40 | return rv.IsValid() |
| 41 | } |
| 42 | func (ms mapReflect) Get(k pref.MapKey) pref.Value { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 43 | if ms.v.IsNil() { |
| 44 | return pref.Value{} |
| 45 | } |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 46 | rk := ms.keyConv.GoValueOf(k.Value()) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 47 | rv := ms.v.Elem().MapIndex(rk) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 48 | if !rv.IsValid() { |
| 49 | return pref.Value{} |
| 50 | } |
| 51 | return ms.valConv.PBValueOf(rv) |
| 52 | } |
| 53 | func (ms mapReflect) Set(k pref.MapKey, v pref.Value) { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 54 | if ms.v.Elem().IsNil() { |
| 55 | ms.v.Elem().Set(reflect.MakeMap(ms.v.Elem().Type())) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 56 | } |
| 57 | rk := ms.keyConv.GoValueOf(k.Value()) |
| 58 | rv := ms.valConv.GoValueOf(v) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 59 | ms.v.Elem().SetMapIndex(rk, rv) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 60 | } |
| 61 | func (ms mapReflect) Clear(k pref.MapKey) { |
| 62 | rk := ms.keyConv.GoValueOf(k.Value()) |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 63 | ms.v.Elem().SetMapIndex(rk, reflect.Value{}) |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 64 | } |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 65 | func (ms mapReflect) Range(f func(pref.MapKey, pref.Value) bool) { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 66 | if ms.v.IsNil() { |
| 67 | return |
| 68 | } |
| 69 | for _, k := range ms.v.Elem().MapKeys() { |
| 70 | if v := ms.v.Elem().MapIndex(k); v.IsValid() { |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 71 | pk := ms.keyConv.PBValueOf(k).MapKey() |
| 72 | pv := ms.valConv.PBValueOf(v) |
| 73 | if !f(pk, pv) { |
| 74 | return |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 79 | func (ms mapReflect) NewMessage() pref.Message { |
Joe Tsai | b2f66be | 2019-05-22 00:42:45 -0400 | [diff] [blame^] | 80 | return ms.valConv.NewMessage() |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 81 | } |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 82 | func (ms mapReflect) ProtoUnwrap() interface{} { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 83 | return ms.v.Interface() |
Joe Tsai | 88bc5a7 | 2018-11-05 11:42:22 -0800 | [diff] [blame] | 84 | } |