Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -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 | |
| 10 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 11 | ) |
| 12 | |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 13 | func ListOf(p interface{}, c Converter) interface { |
| 14 | pref.List |
| 15 | Unwrapper |
| 16 | } { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 17 | // TODO: Validate that p is a *[]T? |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 18 | rv := reflect.ValueOf(p) |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 19 | return listReflect{rv, c} |
| 20 | } |
| 21 | |
| 22 | type listReflect struct { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 23 | v reflect.Value // *[]T |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 24 | conv Converter |
| 25 | } |
| 26 | |
| 27 | func (ls listReflect) Len() int { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 28 | if ls.v.IsNil() { |
| 29 | return 0 |
| 30 | } |
| 31 | return ls.v.Elem().Len() |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 32 | } |
| 33 | func (ls listReflect) Get(i int) pref.Value { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 34 | return ls.conv.PBValueOf(ls.v.Elem().Index(i)) |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 35 | } |
| 36 | func (ls listReflect) Set(i int, v pref.Value) { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 37 | ls.v.Elem().Index(i).Set(ls.conv.GoValueOf(v)) |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 38 | } |
| 39 | func (ls listReflect) Append(v pref.Value) { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 40 | ls.v.Elem().Set(reflect.Append(ls.v.Elem(), ls.conv.GoValueOf(v))) |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 41 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 42 | func (ls listReflect) Truncate(i int) { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 43 | ls.v.Elem().Set(ls.v.Elem().Slice(0, i)) |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 44 | } |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 45 | func (ls listReflect) NewMessage() pref.Message { |
Damien Neil | 97e7f57 | 2018-12-07 14:28:33 -0800 | [diff] [blame] | 46 | return ls.conv.MessageType.New() |
| 47 | } |
Joe Tsai | ba0ef9a | 2018-11-29 14:54:05 -0800 | [diff] [blame] | 48 | func (ls listReflect) ProtoUnwrap() interface{} { |
Joe Tsai | 6cf80c4 | 2018-12-01 04:57:09 -0800 | [diff] [blame] | 49 | return ls.v.Interface() |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame] | 50 | } |