mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-04-18 14:42:40 +00:00
Clearly specify that Get on an unpopulated field: * returns the default value for scalars * returns a mutable (but empty) List for repeated fields * returns a mutable (but empty) Map for map fields * returns an invalid value for message fields The difference in semantics between List+Maps and Messages is because protobuf semantics provide no distinction between an unpopulated and empty list or map. On the other hand, there is a semantic difference between an unpopulated message and an empty message. Default values for scalars is trivial to implement with FieldDescriptor.Default. A mutable, but empty List and Map is easy to implement for known fields since known fields are generated as a slice or map field in a struct. Since struct fields are addressable, the implementation can just return a reference to the slice or map. Repeated, extension fields are a little more tricky since extension fields are implemented under the hood as a map[FieldNumber]Extension. Rather than allocating an empty list in KnownFields.Get upon first retrieval (which presents a race), delegate the work to ExtensionFieldTypes.Register, which must occur before any Get operation. Register is not a concurrent-safe operation, so that is an excellent time to initilize empty lists. The implementation of extensions will need to be careful that Clear on a repeated field simply truncates it zero instead of deleting the object. For unpopulated messages, we return an invalid value, instead of the prior behavior of returning a typed nil-pointer to the Go type for the message. The approach is problematic because it assumes that 1) all messages are always implemented on a pointer reciever 2) a typed nil-pointer is an appropriate "read-only, but empty" message These assumptions are not true of all message types (e.g., dynamic messages). Change-Id: Ie96e6744c890308d9de738b6cf01d3b19e7e7c6a Reviewed-on: https://go-review.googlesource.com/c/150319 Reviewed-by: Damien Neil <dneil@google.com>
58 lines
1.5 KiB
Go
58 lines
1.5 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 "github.com/golang/protobuf/v2/reflect/protoreflect"
|
|
)
|
|
|
|
func ListOf(p interface{}, c Converter) pref.List {
|
|
// TODO: Validate that p is a *[]T?
|
|
rv := reflect.ValueOf(p).Elem()
|
|
return listReflect{rv, c}
|
|
}
|
|
|
|
type listReflect struct {
|
|
v reflect.Value // addressable []T
|
|
conv Converter
|
|
}
|
|
|
|
func (ls listReflect) Len() int {
|
|
return ls.v.Len()
|
|
}
|
|
func (ls listReflect) Get(i int) pref.Value {
|
|
return ls.conv.PBValueOf(ls.v.Index(i))
|
|
}
|
|
func (ls listReflect) Set(i int, v pref.Value) {
|
|
ls.v.Index(i).Set(ls.conv.GoValueOf(v))
|
|
}
|
|
func (ls listReflect) Append(v pref.Value) {
|
|
ls.v.Set(reflect.Append(ls.v, ls.conv.GoValueOf(v)))
|
|
}
|
|
func (ls listReflect) Mutable(i int) pref.Mutable {
|
|
// Mutable is only valid for messages and panics for other kinds.
|
|
return ls.conv.PBValueOf(ls.v.Index(i)).Message()
|
|
}
|
|
func (ls listReflect) MutableAppend() pref.Mutable {
|
|
// MutableAppend is only valid for messages and panics for other kinds.
|
|
pv := pref.ValueOf(ls.conv.MessageType.New().ProtoReflect())
|
|
ls.v.Set(reflect.Append(ls.v, ls.conv.GoValueOf(pv)))
|
|
return pv.Message()
|
|
}
|
|
func (ls listReflect) Truncate(i int) {
|
|
ls.v.Set(ls.v.Slice(0, i))
|
|
}
|
|
func (ls listReflect) Unwrap() interface{} {
|
|
return ls.v.Addr().Interface()
|
|
}
|
|
func (ls listReflect) ProtoMutable() {}
|
|
|
|
var (
|
|
_ pref.List = listReflect{}
|
|
_ Unwrapper = listReflect{}
|
|
)
|