mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-06 00:55:51 +00:00
f0c01e459b
Implement support for extension fields for messages that use the v1 data structures for extensions. The legacyExtensionFields type wraps a v1 map to implement the v2 protoreflect.KnownFields interface. Working on this change revealed a bug in the dynamic construction of message types for protobuf messages that had cyclic dependencies (e.g., message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo). In such a situation, a deadlock occurs because initialization code depends on the very initialization code that is currently running. To break these cycles, we make some systematic changes listed in the following paragraphs. Generally speaking, we separate the logic for construction and wrapping, where constuction does not recursively rely on dependencies, while wrapping may recursively inspect dependencies. Promote the MessageType.MessageOf method as a standalone MessageOf function that dynamically finds the proper *MessageType to use. We make it such that MessageType only supports two forms of messages types: * Those that fully implement the v2 API. * Those that do not implement the v2 API at all. This removes support for the hybrid form that was exploited by message_test.go In impl/message_test.go, switch each message to look more like how future generated messages will look like. This is done in reaction to the fact that MessageType.MessageOf no longer exists. In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying reflect.Value is addressable reference value, not a pointer value. In value/convert.go, split the logic apart so that obtaining a v2 type and wrapping a type as v2 are distinct operations. Wrapping requires further initialization than simply creating the initial message type, and calling it during initial construction would lead to a deadlock. In protoreflect/go_type.go, we switch back to a lazy initialization of GoType to avoid a deadlock since the user-provided fn may rely on the fact that prototype.GoMessage returned. Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420 Reviewed-on: https://go-review.googlesource.com/c/148826 Reviewed-by: Herbie Ong <herbie@google.com>
88 lines
2.2 KiB
Go
88 lines
2.2 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 MapOf(p interface{}, kc, kv Converter) pref.Map {
|
|
// TODO: Validate that p is a *map[K]V?
|
|
rv := reflect.ValueOf(p).Elem()
|
|
return mapReflect{rv, kc, kv}
|
|
}
|
|
|
|
type mapReflect struct {
|
|
v reflect.Value // addressable map[K]V
|
|
keyConv Converter
|
|
valConv Converter
|
|
}
|
|
|
|
func (ms mapReflect) Len() int {
|
|
return ms.v.Len()
|
|
}
|
|
func (ms mapReflect) Has(k pref.MapKey) bool {
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
|
rv := ms.v.MapIndex(rk)
|
|
return rv.IsValid()
|
|
}
|
|
func (ms mapReflect) Get(k pref.MapKey) pref.Value {
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
|
rv := ms.v.MapIndex(rk)
|
|
if !rv.IsValid() {
|
|
return pref.Value{}
|
|
}
|
|
return ms.valConv.PBValueOf(rv)
|
|
}
|
|
func (ms mapReflect) Set(k pref.MapKey, v pref.Value) {
|
|
if ms.v.IsNil() {
|
|
ms.v.Set(reflect.MakeMap(ms.v.Type()))
|
|
}
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
|
rv := ms.valConv.GoValueOf(v)
|
|
ms.v.SetMapIndex(rk, rv)
|
|
}
|
|
func (ms mapReflect) Clear(k pref.MapKey) {
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
|
ms.v.SetMapIndex(rk, reflect.Value{})
|
|
}
|
|
func (ms mapReflect) Mutable(k pref.MapKey) pref.Mutable {
|
|
// Mutable is only valid for messages and panics for other kinds.
|
|
if ms.v.IsNil() {
|
|
ms.v.Set(reflect.MakeMap(ms.v.Type()))
|
|
}
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
|
rv := ms.v.MapIndex(rk)
|
|
if !rv.IsValid() || rv.IsNil() {
|
|
// TODO: Is checking for nil proper behavior for custom messages?
|
|
pv := pref.ValueOf(ms.valConv.MessageType.New().ProtoReflect())
|
|
rv = ms.valConv.GoValueOf(pv)
|
|
ms.v.SetMapIndex(rk, rv)
|
|
}
|
|
return rv.Interface().(pref.Message)
|
|
}
|
|
func (ms mapReflect) Range(f func(pref.MapKey, pref.Value) bool) {
|
|
for _, k := range ms.v.MapKeys() {
|
|
if v := ms.v.MapIndex(k); v.IsValid() {
|
|
pk := ms.keyConv.PBValueOf(k).MapKey()
|
|
pv := ms.valConv.PBValueOf(v)
|
|
if !f(pk, pv) {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
func (ms mapReflect) Unwrap() interface{} {
|
|
return ms.v.Addr().Interface()
|
|
}
|
|
func (ms mapReflect) ProtoMutable() {}
|
|
|
|
var (
|
|
_ pref.Map = mapReflect{}
|
|
_ Unwrapper = mapReflect{}
|
|
)
|