internal/impl: move filetype.EnumInfo to impl

Change-Id: I5558d9d4618df80dd08969fb8af1bc16191176b5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/195620
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Joe Tsai 2019-09-16 09:57:27 -07:00 committed by Joe Tsai
parent fd4c605bfb
commit cbdefb1530
4 changed files with 32 additions and 26 deletions

View File

@ -90,15 +90,15 @@ type Builder struct {
// is appended to the end in reverse order.
DependencyIndexes []int32
// EnumInfos is a list of enum infos in "flattened ordering".
EnumInfos []pimpl.EnumInfo
// MessageInfos is a list of message infos in "flattened ordering".
// If provided, the GoType and PBType for each element is populated.
//
// Requirement: len(MessageInfos) == len(Build.Messages)
MessageInfos []pimpl.MessageInfo
// EnumInfos is a list of enum infos in "flattened ordering".
EnumInfos []EnumInfo
// ExtensionInfos is a list of extension infos in "flattened ordering".
// Each element is initialized and registered with the protoregistry package.
//
@ -144,9 +144,9 @@ func (tb Builder) Build() (out Out) {
}
if len(fbOut.Enums) > 0 {
for i := range fbOut.Enums {
tb.EnumInfos[i] = EnumInfo{
desc: &fbOut.Enums[i],
goType: reflect.TypeOf(enumGoTypes[i]),
tb.EnumInfos[i] = pimpl.EnumInfo{
GoReflectType: reflect.TypeOf(enumGoTypes[i]),
Desc: &fbOut.Enums[i],
}
// Register enum types.
if err := tb.TypeRegistry.Register(&tb.EnumInfos[i]); err != nil {
@ -293,20 +293,3 @@ func (r *resolverByIndex) FindMessageByIndex(i, j int32, es []fdesc.Enum, ms []f
return pimpl.Export{}.MessageDescriptorOf(r.goTypes[depIdx])
}
}
type EnumInfo struct {
desc pref.EnumDescriptor
goType reflect.Type
}
func (t *EnumInfo) New(n pref.EnumNumber) pref.Enum {
return reflect.ValueOf(n).Convert(t.goType).Interface().(pref.Enum)
}
func (t *EnumInfo) GoType() reflect.Type { return t.goType }
func (t *EnumInfo) Descriptor() pref.EnumDescriptor { return t.desc }
func messageMaker(t reflect.Type) func() pref.Message {
return func() pref.Message {
return reflect.New(t.Elem()).Interface().(pref.ProtoMessage).ProtoReflect()
}
}

22
internal/impl/enum.go Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2019 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 (
"reflect"
pref "google.golang.org/protobuf/reflect/protoreflect"
)
type EnumInfo struct {
GoReflectType reflect.Type // int32 kind
Desc pref.EnumDescriptor
}
func (t *EnumInfo) New(n pref.EnumNumber) pref.Enum {
return reflect.ValueOf(n).Convert(t.GoReflectType).Interface().(pref.Enum)
}
func (t *EnumInfo) GoType() reflect.Type { return t.GoReflectType }
func (t *EnumInfo) Descriptor() pref.EnumDescriptor { return t.Desc }

View File

@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package filetype_test
package impl_test
import (
"reflect"
"testing"
testpb "google.golang.org/protobuf/internal/testprotos/test"
pref "google.golang.org/protobuf/reflect/protoreflect"
testpb "google.golang.org/protobuf/internal/testprotos/test"
)
func TestEnum(t *testing.T) {

View File

@ -26,7 +26,7 @@ type (
TypeBuilder = filetype.Builder
// Types used by generated code to implement EnumType, MessageType, and ExtensionType.
EnumInfo = filetype.EnumInfo
EnumInfo = impl.EnumInfo
MessageInfo = impl.MessageInfo
ExtensionInfo = impl.ExtensionInfo