mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-29 09:32:38 +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>
331 lines
10 KiB
Go
331 lines
10 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 impl
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
"unicode"
|
|
|
|
protoV1 "github.com/golang/protobuf/proto"
|
|
descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
ptag "github.com/golang/protobuf/v2/internal/encoding/tag"
|
|
pvalue "github.com/golang/protobuf/v2/internal/value"
|
|
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
|
|
ptype "github.com/golang/protobuf/v2/reflect/prototype"
|
|
)
|
|
|
|
// legacyWrapMessage wraps v as a protoreflect.ProtoMessage,
|
|
// where v must be a *struct kind and not implement the v2 API already.
|
|
func legacyWrapMessage(v reflect.Value) pref.ProtoMessage {
|
|
mt := legacyLoadMessageType(v.Type())
|
|
return (*legacyMessageWrapper)(mt.dataTypeOf(v.Interface()))
|
|
}
|
|
|
|
var messageTypeCache sync.Map // map[reflect.Type]*MessageType
|
|
|
|
// legacyLoadMessageType dynamically loads a *MessageType for t,
|
|
// where t must be a *struct kind and not implement the v2 API already.
|
|
func legacyLoadMessageType(t reflect.Type) *MessageType {
|
|
// Fast-path: check if a MessageType is cached for this concrete type.
|
|
if mt, ok := messageTypeCache.Load(t); ok {
|
|
return mt.(*MessageType)
|
|
}
|
|
|
|
// Slow-path: derive message descriptor and initialize MessageType.
|
|
md := legacyLoadMessageDesc(t)
|
|
mt := new(MessageType)
|
|
mt.Type = ptype.GoMessage(md, func(pref.MessageType) pref.ProtoMessage {
|
|
p := reflect.New(t.Elem()).Interface()
|
|
return (*legacyMessageWrapper)(mt.dataTypeOf(p))
|
|
})
|
|
messageTypeCache.Store(t, mt)
|
|
return mt
|
|
}
|
|
|
|
type legacyMessageWrapper messageDataType
|
|
|
|
func (m *legacyMessageWrapper) Type() pref.MessageType {
|
|
return m.mi.Type
|
|
}
|
|
func (m *legacyMessageWrapper) KnownFields() pref.KnownFields {
|
|
return (*knownFields)(m)
|
|
}
|
|
func (m *legacyMessageWrapper) UnknownFields() pref.UnknownFields {
|
|
return m.mi.unknownFields((*messageDataType)(m))
|
|
}
|
|
func (m *legacyMessageWrapper) Unwrap() interface{} {
|
|
return m.p.asType(m.mi.goType.Elem()).Interface()
|
|
}
|
|
func (m *legacyMessageWrapper) Interface() pref.ProtoMessage {
|
|
return m
|
|
}
|
|
func (m *legacyMessageWrapper) ProtoReflect() pref.Message {
|
|
return m
|
|
}
|
|
func (m *legacyMessageWrapper) ProtoMutable() {}
|
|
|
|
var (
|
|
_ pref.Message = (*legacyMessageWrapper)(nil)
|
|
_ pref.ProtoMessage = (*legacyMessageWrapper)(nil)
|
|
_ pvalue.Unwrapper = (*legacyMessageWrapper)(nil)
|
|
)
|
|
|
|
var messageDescCache sync.Map // map[reflect.Type]protoreflect.MessageDescriptor
|
|
|
|
// legacyLoadMessageDesc returns an MessageDescriptor derived from the Go type,
|
|
// which must be a *struct kind and not implement the v2 API already.
|
|
func legacyLoadMessageDesc(t reflect.Type) pref.MessageDescriptor {
|
|
return messageDescSet{}.Load(t)
|
|
}
|
|
|
|
type messageDescSet struct {
|
|
visited map[reflect.Type]*ptype.StandaloneMessage
|
|
descs []*ptype.StandaloneMessage
|
|
types []reflect.Type
|
|
}
|
|
|
|
func (ms messageDescSet) Load(t reflect.Type) pref.MessageDescriptor {
|
|
// Fast-path: check if a MessageDescriptor is cached for this concrete type.
|
|
if mi, ok := messageDescCache.Load(t); ok {
|
|
return mi.(pref.MessageDescriptor)
|
|
}
|
|
|
|
// Slow-path: initialize MessageDescriptor from the Go type.
|
|
|
|
// Processing t recursively populates descs and types with all sub-messages.
|
|
// The descriptor for the first type is guaranteed to be at the front.
|
|
ms.processMessage(t)
|
|
|
|
// Within a proto file it is possible for cyclic dependencies to exist
|
|
// between multiple message types. When these cases arise, the set of
|
|
// message descriptors must be created together.
|
|
mds, err := ptype.NewMessages(ms.descs)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for i, md := range mds {
|
|
// Protobuf semantics represents map entries under-the-hood as
|
|
// pseudo-messages (has a descriptor, but no generated Go type).
|
|
// Avoid caching these fake messages.
|
|
if t := ms.types[i]; t.Kind() != reflect.Map {
|
|
messageDescCache.Store(t, md)
|
|
}
|
|
}
|
|
return mds[0]
|
|
}
|
|
|
|
func (ms *messageDescSet) processMessage(t reflect.Type) pref.MessageDescriptor {
|
|
// Fast-path: Obtain a placeholder if the message is already processed.
|
|
if m, ok := ms.visited[t]; ok {
|
|
return ptype.PlaceholderMessage(m.FullName)
|
|
}
|
|
|
|
// Slow-path: Walk over the struct fields to derive the message descriptor.
|
|
if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Struct || t.Elem().PkgPath() == "" {
|
|
panic(fmt.Sprintf("got %v, want named *struct kind", t))
|
|
}
|
|
|
|
// Derive name and syntax from the raw descriptor.
|
|
m := new(ptype.StandaloneMessage)
|
|
mv := reflect.New(t.Elem()).Interface()
|
|
if _, ok := mv.(pref.ProtoMessage); ok {
|
|
panic(fmt.Sprintf("%v already implements proto.Message", t))
|
|
}
|
|
if md, ok := mv.(legacyMessage); ok {
|
|
b, idxs := md.Descriptor()
|
|
fd := legacyLoadFileDesc(b)
|
|
|
|
// Derive syntax.
|
|
switch fd.GetSyntax() {
|
|
case "proto2", "":
|
|
m.Syntax = pref.Proto2
|
|
case "proto3":
|
|
m.Syntax = pref.Proto3
|
|
}
|
|
|
|
// Derive full name.
|
|
md := fd.MessageType[idxs[0]]
|
|
m.FullName = pref.FullName(fd.GetPackage()).Append(pref.Name(md.GetName()))
|
|
for _, i := range idxs[1:] {
|
|
md = md.NestedType[i]
|
|
m.FullName = m.FullName.Append(pref.Name(md.GetName()))
|
|
}
|
|
} else {
|
|
// If the type does not implement legacyMessage, then the only way to
|
|
// obtain the full name is through the registry. However, this is
|
|
// unreliable as some generated messages register with a fork of
|
|
// golang/protobuf, so the registry may not have this information.
|
|
m.FullName = deriveFullName(t.Elem())
|
|
m.Syntax = pref.Proto2
|
|
|
|
// Try to determine if the message is using proto3 by checking scalars.
|
|
for i := 0; i < t.Elem().NumField(); i++ {
|
|
f := t.Elem().Field(i)
|
|
if tag := f.Tag.Get("protobuf"); tag != "" {
|
|
switch f.Type.Kind() {
|
|
case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
|
|
m.Syntax = pref.Proto3
|
|
}
|
|
for _, s := range strings.Split(tag, ",") {
|
|
if s == "proto3" {
|
|
m.Syntax = pref.Proto3
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ms.visit(m, t)
|
|
|
|
// Obtain a list of oneof wrapper types.
|
|
var oneofWrappers []reflect.Type
|
|
if fn, ok := t.MethodByName("XXX_OneofFuncs"); ok {
|
|
vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[3]
|
|
for _, v := range vs.Interface().([]interface{}) {
|
|
oneofWrappers = append(oneofWrappers, reflect.TypeOf(v))
|
|
}
|
|
}
|
|
|
|
// Obtain a list of the extension ranges.
|
|
if fn, ok := t.MethodByName("ExtensionRangeArray"); ok {
|
|
vs := fn.Func.Call([]reflect.Value{reflect.Zero(fn.Type.In(0))})[0]
|
|
for i := 0; i < vs.Len(); i++ {
|
|
v := vs.Index(i)
|
|
m.ExtensionRanges = append(m.ExtensionRanges, [2]pref.FieldNumber{
|
|
pref.FieldNumber(v.FieldByName("Start").Int()),
|
|
pref.FieldNumber(v.FieldByName("End").Int() + 1),
|
|
})
|
|
}
|
|
}
|
|
|
|
// Derive the message fields by inspecting the struct fields.
|
|
for i := 0; i < t.Elem().NumField(); i++ {
|
|
f := t.Elem().Field(i)
|
|
if tag := f.Tag.Get("protobuf"); tag != "" {
|
|
tagKey := f.Tag.Get("protobuf_key")
|
|
tagVal := f.Tag.Get("protobuf_val")
|
|
m.Fields = append(m.Fields, ms.parseField(tag, tagKey, tagVal, f.Type, m))
|
|
}
|
|
if tag := f.Tag.Get("protobuf_oneof"); tag != "" {
|
|
name := pref.Name(tag)
|
|
m.Oneofs = append(m.Oneofs, ptype.Oneof{Name: name})
|
|
for _, t := range oneofWrappers {
|
|
if t.Implements(f.Type) {
|
|
f := t.Elem().Field(0)
|
|
if tag := f.Tag.Get("protobuf"); tag != "" {
|
|
ft := ms.parseField(tag, "", "", f.Type, m)
|
|
ft.OneofName = name
|
|
m.Fields = append(m.Fields, ft)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return ptype.PlaceholderMessage(m.FullName)
|
|
}
|
|
|
|
func (ms *messageDescSet) parseField(tag, tagKey, tagVal string, goType reflect.Type, parent *ptype.StandaloneMessage) ptype.Field {
|
|
t := goType
|
|
isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
|
|
isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
|
|
if isOptional || isRepeated {
|
|
t = t.Elem()
|
|
}
|
|
f := ptag.Unmarshal(tag, t)
|
|
|
|
// Populate EnumType and MessageType.
|
|
if f.EnumType == nil && f.Kind == pref.EnumKind {
|
|
if ev, ok := reflect.Zero(t).Interface().(pref.ProtoEnum); ok {
|
|
f.EnumType = ev.ProtoReflect().Type()
|
|
} else {
|
|
f.EnumType = legacyLoadEnumDesc(t)
|
|
}
|
|
}
|
|
if f.MessageType == nil && (f.Kind == pref.MessageKind || f.Kind == pref.GroupKind) {
|
|
if mv, ok := reflect.Zero(t).Interface().(pref.ProtoMessage); ok {
|
|
f.MessageType = mv.ProtoReflect().Type()
|
|
} else if t.Kind() == reflect.Map {
|
|
m := &ptype.StandaloneMessage{
|
|
Syntax: parent.Syntax,
|
|
FullName: parent.FullName.Append(mapEntryName(f.Name)),
|
|
Options: &descriptorV1.MessageOptions{MapEntry: protoV1.Bool(true)},
|
|
Fields: []ptype.Field{
|
|
ms.parseField(tagKey, "", "", t.Key(), nil),
|
|
ms.parseField(tagVal, "", "", t.Elem(), nil),
|
|
},
|
|
}
|
|
ms.visit(m, t)
|
|
f.MessageType = ptype.PlaceholderMessage(m.FullName)
|
|
} else if mv, ok := messageDescCache.Load(t); ok {
|
|
f.MessageType = mv.(pref.MessageDescriptor)
|
|
} else {
|
|
f.MessageType = ms.processMessage(t)
|
|
}
|
|
}
|
|
return f
|
|
}
|
|
|
|
func (ms *messageDescSet) visit(m *ptype.StandaloneMessage, t reflect.Type) {
|
|
if ms.visited == nil {
|
|
ms.visited = make(map[reflect.Type]*ptype.StandaloneMessage)
|
|
}
|
|
if t.Kind() != reflect.Map {
|
|
ms.visited[t] = m
|
|
}
|
|
ms.descs = append(ms.descs, m)
|
|
ms.types = append(ms.types, t)
|
|
}
|
|
|
|
// deriveFullName derives a fully qualified protobuf name for the given Go type
|
|
// The provided name is not guaranteed to be stable nor universally unique.
|
|
// It should be sufficiently unique within a program.
|
|
func deriveFullName(t reflect.Type) pref.FullName {
|
|
sanitize := func(r rune) rune {
|
|
switch {
|
|
case r == '/':
|
|
return '.'
|
|
case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z', '0' <= r && r <= '9':
|
|
return r
|
|
default:
|
|
return '_'
|
|
}
|
|
}
|
|
prefix := strings.Map(sanitize, t.PkgPath())
|
|
suffix := strings.Map(sanitize, t.Name())
|
|
if suffix == "" {
|
|
suffix = fmt.Sprintf("UnknownX%X", reflect.ValueOf(t).Pointer())
|
|
}
|
|
|
|
ss := append(strings.Split(prefix, "."), suffix)
|
|
for i, s := range ss {
|
|
if s == "" || ('0' <= s[0] && s[0] <= '9') {
|
|
ss[i] = "x" + s
|
|
}
|
|
}
|
|
return pref.FullName(strings.Join(ss, "."))
|
|
}
|
|
|
|
// mapEntryName derives the message name for a map field of a given name.
|
|
// This is identical to MapEntryName from parser.cc in the protoc source.
|
|
func mapEntryName(s pref.Name) pref.Name {
|
|
var b []byte
|
|
nextUpper := true
|
|
for i := 0; i < len(s); i++ {
|
|
if c := s[i]; c == '_' {
|
|
nextUpper = true
|
|
} else {
|
|
if nextUpper {
|
|
c = byte(unicode.ToUpper(rune(c)))
|
|
nextUpper = false
|
|
}
|
|
b = append(b, c)
|
|
}
|
|
}
|
|
return pref.Name(append(b, "Entry"...))
|
|
}
|