Joe Tsai e1f8d50e17 reflect/protodesc: split descriptor related functionality from prototype
In order to generate descriptor.proto, the generated code would want to depend
on the prototype package to construct the reflection data structures.
However, this is a problem since descriptor itself is one of the dependencies
for prototype. To break this dependency, we do the following:
* Avoid using concrete *descriptorpb.XOptions messages in the public API, and
instead just use protoreflect.ProtoMessage. We do lose some type safety here
as a result.
* Use protobuf reflection to interpret the Options message.
* Split out NewFileFromDescriptorProto into a separate protodesc package since
constructing protobuf reflection from the descriptor proto obviously depends
on the descriptor protos themselves.

As part of this CL, we check in a pre-generated version of descriptor and plugin
that supports protobuf reflection natively and switchover all usages of those
protos to the new definitions. These files were generated by protoc-gen-go
from CL/150074, but hand-modified to remove dependencies on the v1 proto runtime.

Change-Id: I81e03c42eeab480b03764e2fcbe1aae0e058fc57
Reviewed-on: https://go-review.googlesource.com/c/152020
Reviewed-by: Damien Neil <dneil@google.com>
2018-12-05 00:38:30 +00:00

164 lines
4.8 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 legacy
import (
"fmt"
"math"
"reflect"
"sync"
pvalue "github.com/golang/protobuf/v2/internal/value"
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
ptype "github.com/golang/protobuf/v2/reflect/prototype"
descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
)
// wrapEnum wraps v as a protoreflect.ProtoEnum,
// where v must be a int32 kind and not implement the v2 API already.
func wrapEnum(v reflect.Value) pref.ProtoEnum {
et := loadEnumType(v.Type())
return et.New(pref.EnumNumber(v.Int()))
}
var enumTypeCache sync.Map // map[reflect.Type]protoreflect.EnumType
// loadEnumType dynamically loads a protoreflect.EnumType for t,
// where t must be an int32 kind and not implement the v2 API already.
func loadEnumType(t reflect.Type) pref.EnumType {
// Fast-path: check if a EnumType is cached for this concrete type.
if et, ok := enumTypeCache.Load(t); ok {
return et.(pref.EnumType)
}
// Slow-path: derive enum descriptor and initialize EnumType.
var m sync.Map // map[protoreflect.EnumNumber]proto.Enum
ed := loadEnumDesc(t)
et := ptype.GoEnum(ed, func(et pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
if e, ok := m.Load(n); ok {
return e.(pref.ProtoEnum)
}
e := &enumWrapper{num: n, pbTyp: et, goTyp: t}
m.Store(n, e)
return e
})
enumTypeCache.Store(t, et)
return et.(pref.EnumType)
}
type enumWrapper struct {
num pref.EnumNumber
pbTyp pref.EnumType
goTyp reflect.Type
}
func (e *enumWrapper) Number() pref.EnumNumber {
return e.num
}
func (e *enumWrapper) Type() pref.EnumType {
return e.pbTyp
}
func (e *enumWrapper) ProtoReflect() pref.Enum {
return e
}
func (e *enumWrapper) ProtoUnwrap() interface{} {
v := reflect.New(e.goTyp).Elem()
v.SetInt(int64(e.num))
return v.Interface()
}
var (
_ pref.Enum = (*enumWrapper)(nil)
_ pref.ProtoEnum = (*enumWrapper)(nil)
_ pvalue.Unwrapper = (*enumWrapper)(nil)
)
var enumDescCache sync.Map // map[reflect.Type]protoreflect.EnumDescriptor
var enumNumberType = reflect.TypeOf(pref.EnumNumber(0))
// loadEnumDesc returns an EnumDescriptor derived from the Go type,
// which must be an int32 kind and not implement the v2 API already.
func loadEnumDesc(t reflect.Type) pref.EnumDescriptor {
// Fast-path: check if an EnumDescriptor is cached for this concrete type.
if v, ok := enumDescCache.Load(t); ok {
return v.(pref.EnumDescriptor)
}
// Slow-path: initialize EnumDescriptor from the proto descriptor.
if t.Kind() != reflect.Int32 || t.PkgPath() == "" {
panic(fmt.Sprintf("got %v, want named int32 kind", t))
}
if t == enumNumberType {
panic(fmt.Sprintf("cannot be %v", t))
}
// Derive the enum descriptor from the raw descriptor proto.
e := new(ptype.StandaloneEnum)
ev := reflect.Zero(t).Interface()
if _, ok := ev.(pref.ProtoEnum); ok {
panic(fmt.Sprintf("%v already implements proto.Enum", t))
}
if ed, ok := ev.(enumV1); ok {
b, idxs := ed.EnumDescriptor()
fd := loadFileDesc(b)
// Derive syntax.
switch fd.GetSyntax() {
case "proto2", "":
e.Syntax = pref.Proto2
case "proto3":
e.Syntax = pref.Proto3
}
// Derive the full name and correct enum descriptor.
var ed *descriptorpb.EnumDescriptorProto
e.FullName = pref.FullName(fd.GetPackage())
if len(idxs) == 1 {
ed = fd.EnumType[idxs[0]]
e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
} else {
md := fd.MessageType[idxs[0]]
e.FullName = e.FullName.Append(pref.Name(md.GetName()))
for _, i := range idxs[1 : len(idxs)-1] {
md = md.NestedType[i]
e.FullName = e.FullName.Append(pref.Name(md.GetName()))
}
ed = md.EnumType[idxs[len(idxs)-1]]
e.FullName = e.FullName.Append(pref.Name(ed.GetName()))
}
// Derive the enum values.
for _, vd := range ed.GetValue() {
e.Values = append(e.Values, ptype.EnumValue{
Name: pref.Name(vd.GetName()),
Number: pref.EnumNumber(vd.GetNumber()),
})
}
} else {
// If the type does not implement enumV1, then there is no reliable
// way to derive the original protobuf type information.
// We are unable to use the global enum registry since it is
// unfortunately keyed by the full name, which we do not know.
// Furthermore, some generated enums register with a fork of
// golang/protobuf so the enum may not even be found in the registry.
//
// Instead, create a bogus enum descriptor to ensure that
// most operations continue to work. For example, textpb and jsonpb
// will be unable to parse a message with an enum value by name.
e.Syntax = pref.Proto2
e.FullName = deriveFullName(t)
e.Values = []ptype.EnumValue{{Name: "INVALID", Number: math.MinInt32}}
}
ed, err := ptype.NewEnum(e)
if err != nil {
panic(err)
}
enumDescCache.Store(t, ed)
return ed
}