mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-22 03:40:55 +00:00
Temporarily remove go.mod, since we can't generate an accurate one until the corresponding v1 change is submitted. Change-Id: I1e1ad97f2b455e33f61ffaeb8676289795e47e72 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177000 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package value
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
func ListOf(p interface{}, c Converter) interface {
|
|
pref.List
|
|
Unwrapper
|
|
} {
|
|
// TODO: Validate that p is a *[]T?
|
|
rv := reflect.ValueOf(p)
|
|
return listReflect{rv, c}
|
|
}
|
|
|
|
type listReflect struct {
|
|
v reflect.Value // *[]T
|
|
conv Converter
|
|
}
|
|
|
|
func (ls listReflect) Len() int {
|
|
if ls.v.IsNil() {
|
|
return 0
|
|
}
|
|
return ls.v.Elem().Len()
|
|
}
|
|
func (ls listReflect) Get(i int) pref.Value {
|
|
return ls.conv.PBValueOf(ls.v.Elem().Index(i))
|
|
}
|
|
func (ls listReflect) Set(i int, v pref.Value) {
|
|
ls.v.Elem().Index(i).Set(ls.conv.GoValueOf(v))
|
|
}
|
|
func (ls listReflect) Append(v pref.Value) {
|
|
ls.v.Elem().Set(reflect.Append(ls.v.Elem(), ls.conv.GoValueOf(v)))
|
|
}
|
|
func (ls listReflect) Truncate(i int) {
|
|
ls.v.Elem().Set(ls.v.Elem().Slice(0, i))
|
|
}
|
|
func (ls listReflect) NewMessage() pref.Message {
|
|
return ls.conv.MessageType.New()
|
|
}
|
|
func (ls listReflect) ProtoUnwrap() interface{} {
|
|
return ls.v.Interface()
|
|
}
|