reflect/protoreflect: change Options back to an interface{}

Introduce a protoreflect.OptionsMessage interface type to represent
options messages. The protoreflect package can't deal with concrete
options types, to avoid dependency cycles; OptionsMessage serves both
as documentation and a single point to define the constraints we apply
to options.

Change the constraints on options from ProtoMessage to interface{} to
permit use of option message types which only implement the v1
proto.Message interface.

This still leaves a requirement in the internal/legacy package that
options implement protoreflect.ProtoMessage, since that package now uses
the v2 Unmarshal.

Change-Id: I547518ab2c3b90c3911ef641b05b169d50a4b33a
Reviewed-on: https://go-review.googlesource.com/c/154877
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
Damien Neil 2018-12-18 12:45:07 -08:00
parent 7bf88d1377
commit c05538c6a8
7 changed files with 85 additions and 73 deletions

View File

@ -100,11 +100,15 @@ type Descriptor interface {
//
// This method returns a typed nil-pointer if no options are present.
// The caller must import the descriptor package to use this.
Options() ProtoMessage
Options() OptionsMessage
doNotImplement
}
// An OptionsMessage is a google.protobuf.XXXOptions message, defined here
// as an interface value to avoid a dependency cycle.
type OptionsMessage interface{}
// FileDescriptor describes the types in a complete proto file and
// corresponds with the google.protobuf.FileDescriptorProto message.
//
@ -211,7 +215,7 @@ type MessageDescriptor interface {
// The caller must not modify the returned message.
//
// This method may return a nil interface value if no options are present.
ExtensionRangeOptions(i int) ProtoMessage
ExtensionRangeOptions(i int) OptionsMessage
// Messages is a list of nested message declarations.
Messages() MessageDescriptors

View File

@ -42,7 +42,7 @@ type placeholderFile struct {
placeholderName
}
func (t placeholderFile) Options() pref.ProtoMessage { return optionTypes.File }
func (t placeholderFile) Options() pref.OptionsMessage { return optionTypes.File }
func (t placeholderFile) Path() string { return t.path }
func (t placeholderFile) Package() pref.FullName { return t.FullName() }
func (t placeholderFile) Imports() pref.FileImports { return &emptyFiles }
@ -58,26 +58,26 @@ type placeholderMessage struct {
placeholderName
}
func (t placeholderMessage) Options() pref.ProtoMessage { return optionTypes.Message }
func (t placeholderMessage) IsMapEntry() bool { return false }
func (t placeholderMessage) Fields() pref.FieldDescriptors { return &emptyFields }
func (t placeholderMessage) Oneofs() pref.OneofDescriptors { return &emptyOneofs }
func (t placeholderMessage) ReservedNames() pref.Names { return &emptyNames }
func (t placeholderMessage) ReservedRanges() pref.FieldRanges { return &emptyFieldRanges }
func (t placeholderMessage) RequiredNumbers() pref.FieldNumbers { return &emptyNumbers }
func (t placeholderMessage) ExtensionRanges() pref.FieldRanges { return &emptyFieldRanges }
func (t placeholderMessage) ExtensionRangeOptions(int) pref.ProtoMessage { panic("out of bounds") }
func (t placeholderMessage) Enums() pref.EnumDescriptors { return &emptyEnums }
func (t placeholderMessage) Messages() pref.MessageDescriptors { return &emptyMessages }
func (t placeholderMessage) Extensions() pref.ExtensionDescriptors { return &emptyExtensions }
func (t placeholderMessage) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, t) }
func (t placeholderMessage) ProtoType(pref.MessageDescriptor) {}
func (t placeholderMessage) Options() pref.OptionsMessage { return optionTypes.Message }
func (t placeholderMessage) IsMapEntry() bool { return false }
func (t placeholderMessage) Fields() pref.FieldDescriptors { return &emptyFields }
func (t placeholderMessage) Oneofs() pref.OneofDescriptors { return &emptyOneofs }
func (t placeholderMessage) ReservedNames() pref.Names { return &emptyNames }
func (t placeholderMessage) ReservedRanges() pref.FieldRanges { return &emptyFieldRanges }
func (t placeholderMessage) RequiredNumbers() pref.FieldNumbers { return &emptyNumbers }
func (t placeholderMessage) ExtensionRanges() pref.FieldRanges { return &emptyFieldRanges }
func (t placeholderMessage) ExtensionRangeOptions(int) pref.OptionsMessage { panic("out of bounds") }
func (t placeholderMessage) Enums() pref.EnumDescriptors { return &emptyEnums }
func (t placeholderMessage) Messages() pref.MessageDescriptors { return &emptyMessages }
func (t placeholderMessage) Extensions() pref.ExtensionDescriptors { return &emptyExtensions }
func (t placeholderMessage) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, t) }
func (t placeholderMessage) ProtoType(pref.MessageDescriptor) {}
type placeholderEnum struct {
placeholderName
}
func (t placeholderEnum) Options() pref.ProtoMessage { return optionTypes.Enum }
func (t placeholderEnum) Options() pref.OptionsMessage { return optionTypes.Enum }
func (t placeholderEnum) Values() pref.EnumValueDescriptors { return &emptyEnumValues }
func (t placeholderEnum) ReservedNames() pref.Names { return &emptyNames }
func (t placeholderEnum) ReservedRanges() pref.EnumRanges { return &emptyEnumRanges }

View File

@ -44,7 +44,7 @@ type File struct {
Path string
Package protoreflect.FullName
Imports []protoreflect.FileImport
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
Enums []Enum
Messages []Message
@ -108,8 +108,8 @@ type Message struct {
ReservedNames []protoreflect.Name
ReservedRanges [][2]protoreflect.FieldNumber
ExtensionRanges [][2]protoreflect.FieldNumber
ExtensionRangeOptions []protoreflect.ProtoMessage
Options protoreflect.ProtoMessage
ExtensionRangeOptions []protoreflect.OptionsMessage
Options protoreflect.OptionsMessage
IsMapEntry bool
Enums []Enum
@ -138,7 +138,7 @@ type Field struct {
OneofName protoreflect.Name
MessageType protoreflect.MessageDescriptor
EnumType protoreflect.EnumDescriptor
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
IsPacked OptionalBool
IsWeak bool
@ -148,7 +148,7 @@ type Field struct {
// Oneof is a constructor for protoreflect.OneofDescriptor.
type Oneof struct {
Name protoreflect.Name
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
*oneofMeta
}
@ -163,7 +163,7 @@ type Extension struct {
MessageType protoreflect.MessageDescriptor
EnumType protoreflect.EnumDescriptor
ExtendedType protoreflect.MessageDescriptor
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
IsPacked OptionalBool
*extensionMeta
@ -175,7 +175,7 @@ type Enum struct {
Values []EnumValue
ReservedNames []protoreflect.Name
ReservedRanges [][2]protoreflect.EnumNumber
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
*enumMeta
}
@ -192,7 +192,7 @@ func (e *Enum) Reference() protoreflect.EnumDescriptor {
type EnumValue struct {
Name protoreflect.Name
Number protoreflect.EnumNumber
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
*enumValueMeta
}
@ -201,7 +201,7 @@ type EnumValue struct {
type Service struct {
Name protoreflect.Name
Methods []Method
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
*serviceMeta
}
@ -213,7 +213,7 @@ type Method struct {
OutputType protoreflect.MessageDescriptor
IsStreamingClient bool
IsStreamingServer bool
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
*methodMeta
}

View File

@ -48,7 +48,7 @@ type fileMeta struct {
type fileDesc struct{ f *File }
// altOptions returns m as is if it is non-nil. Otherwise, it returns alt.
func altOptions(m, alt pref.ProtoMessage) pref.ProtoMessage {
func altOptions(m, alt pref.OptionsMessage) pref.OptionsMessage {
if m != nil {
return m
}
@ -68,7 +68,7 @@ func (t fileDesc) Syntax() pref.Syntax { return t.f
func (t fileDesc) Name() pref.Name { return t.f.Package.Name() }
func (t fileDesc) FullName() pref.FullName { return t.f.Package }
func (t fileDesc) IsPlaceholder() bool { return false }
func (t fileDesc) Options() pref.ProtoMessage { return altOptions(t.f.Options, optionTypes.File) }
func (t fileDesc) Options() pref.OptionsMessage { return altOptions(t.f.Options, optionTypes.File) }
func (t fileDesc) Path() string { return t.f.Path }
func (t fileDesc) Package() pref.FullName { return t.f.Package }
func (t fileDesc) Imports() pref.FileImports { return (*fileImports)(&t.f.Imports) }
@ -168,13 +168,15 @@ type messageMeta struct {
}
type messageDesc struct{ m *Message }
func (t messageDesc) Parent() (pref.Descriptor, bool) { return t.m.parent, true }
func (t messageDesc) Index() int { return t.m.index }
func (t messageDesc) Syntax() pref.Syntax { return t.m.syntax }
func (t messageDesc) Name() pref.Name { return t.m.Name }
func (t messageDesc) FullName() pref.FullName { return t.m.fullName }
func (t messageDesc) IsPlaceholder() bool { return false }
func (t messageDesc) Options() pref.ProtoMessage { return altOptions(t.m.Options, optionTypes.Message) }
func (t messageDesc) Parent() (pref.Descriptor, bool) { return t.m.parent, true }
func (t messageDesc) Index() int { return t.m.index }
func (t messageDesc) Syntax() pref.Syntax { return t.m.syntax }
func (t messageDesc) Name() pref.Name { return t.m.Name }
func (t messageDesc) FullName() pref.FullName { return t.m.fullName }
func (t messageDesc) IsPlaceholder() bool { return false }
func (t messageDesc) Options() pref.OptionsMessage {
return altOptions(t.m.Options, optionTypes.Message)
}
func (t messageDesc) IsMapEntry() bool { return t.m.IsMapEntry }
func (t messageDesc) Fields() pref.FieldDescriptors { return t.m.fs.lazyInit(t, t.m.Fields) }
func (t messageDesc) Oneofs() pref.OneofDescriptors { return t.m.os.lazyInit(t, t.m.Oneofs) }
@ -182,7 +184,7 @@ func (t messageDesc) ReservedNames() pref.Names { return (*names)(&t.m.
func (t messageDesc) ReservedRanges() pref.FieldRanges { return (*fieldRanges)(&t.m.ReservedRanges) }
func (t messageDesc) RequiredNumbers() pref.FieldNumbers { return t.m.ns.lazyInit(t.m.Fields) }
func (t messageDesc) ExtensionRanges() pref.FieldRanges { return (*fieldRanges)(&t.m.ExtensionRanges) }
func (t messageDesc) ExtensionRangeOptions(i int) pref.ProtoMessage {
func (t messageDesc) ExtensionRangeOptions(i int) pref.OptionsMessage {
return extensionRangeOptions(i, len(t.m.ExtensionRanges), t.m.ExtensionRangeOptions)
}
func (t messageDesc) Enums() pref.EnumDescriptors { return t.m.es.lazyInit(t, t.m.Enums) }
@ -192,11 +194,11 @@ func (t messageDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s,
func (t messageDesc) ProtoType(pref.MessageDescriptor) {}
func (t messageDesc) ProtoInternal(pragma.DoNotImplement) {}
func extensionRangeOptions(i, n int, ms []pref.ProtoMessage) pref.ProtoMessage {
func extensionRangeOptions(i, n int, ms []pref.OptionsMessage) pref.OptionsMessage {
if i < 0 || i >= n {
panic("out of bounds")
}
var m pref.ProtoMessage
var m pref.OptionsMessage
if i < len(ms) {
m = ms[i]
}
@ -223,7 +225,7 @@ func (t fieldDesc) Syntax() pref.Syntax { return t.f.syntax }
func (t fieldDesc) Name() pref.Name { return t.f.Name }
func (t fieldDesc) FullName() pref.FullName { return t.f.fullName }
func (t fieldDesc) IsPlaceholder() bool { return false }
func (t fieldDesc) Options() pref.ProtoMessage { return altOptions(t.f.Options, optionTypes.Field) }
func (t fieldDesc) Options() pref.OptionsMessage { return altOptions(t.f.Options, optionTypes.Field) }
func (t fieldDesc) Number() pref.FieldNumber { return t.f.Number }
func (t fieldDesc) Cardinality() pref.Cardinality { return t.f.Cardinality }
func (t fieldDesc) Kind() pref.Kind { return t.f.Kind }
@ -324,7 +326,7 @@ func (t oneofDesc) Syntax() pref.Syntax { return t.o.syntax }
func (t oneofDesc) Name() pref.Name { return t.o.Name }
func (t oneofDesc) FullName() pref.FullName { return t.o.fullName }
func (t oneofDesc) IsPlaceholder() bool { return false }
func (t oneofDesc) Options() pref.ProtoMessage { return altOptions(t.o.Options, optionTypes.Oneof) }
func (t oneofDesc) Options() pref.OptionsMessage { return altOptions(t.o.Options, optionTypes.Oneof) }
func (t oneofDesc) Fields() pref.FieldDescriptors { return t.o.fs.lazyInit(t) }
func (t oneofDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, t) }
func (t oneofDesc) ProtoType(pref.OneofDescriptor) {}
@ -346,12 +348,14 @@ func (t extensionDesc) Index() int { return t.x.index }
func (t extensionDesc) Name() pref.Name { return t.x.Name }
func (t extensionDesc) FullName() pref.FullName { return t.x.fullName }
func (t extensionDesc) IsPlaceholder() bool { return false }
func (t extensionDesc) Options() pref.ProtoMessage { return altOptions(t.x.Options, optionTypes.Field) }
func (t extensionDesc) Number() pref.FieldNumber { return t.x.Number }
func (t extensionDesc) Cardinality() pref.Cardinality { return t.x.Cardinality }
func (t extensionDesc) Kind() pref.Kind { return t.x.Kind }
func (t extensionDesc) HasJSONName() bool { return false }
func (t extensionDesc) JSONName() string { return "" }
func (t extensionDesc) Options() pref.OptionsMessage {
return altOptions(t.x.Options, optionTypes.Field)
}
func (t extensionDesc) Number() pref.FieldNumber { return t.x.Number }
func (t extensionDesc) Cardinality() pref.Cardinality { return t.x.Cardinality }
func (t extensionDesc) Kind() pref.Kind { return t.x.Kind }
func (t extensionDesc) HasJSONName() bool { return false }
func (t extensionDesc) JSONName() string { return "" }
func (t extensionDesc) IsPacked() bool {
// Extensions always use proto2 defaults for packing.
return isPacked(t.x.IsPacked, pref.Proto2, t.x.Cardinality, t.x.Kind)
@ -386,7 +390,7 @@ func (t enumDesc) Syntax() pref.Syntax { return t.e.syntax }
func (t enumDesc) Name() pref.Name { return t.e.Name }
func (t enumDesc) FullName() pref.FullName { return t.e.fullName }
func (t enumDesc) IsPlaceholder() bool { return false }
func (t enumDesc) Options() pref.ProtoMessage { return altOptions(t.e.Options, optionTypes.Enum) }
func (t enumDesc) Options() pref.OptionsMessage { return altOptions(t.e.Options, optionTypes.Enum) }
func (t enumDesc) Values() pref.EnumValueDescriptors { return t.e.vs.lazyInit(t, t.e.Values) }
func (t enumDesc) ReservedNames() pref.Names { return (*names)(&t.e.ReservedNames) }
func (t enumDesc) ReservedRanges() pref.EnumRanges { return (*enumRanges)(&t.e.ReservedRanges) }
@ -405,7 +409,7 @@ func (t enumValueDesc) Syntax() pref.Syntax { return t.v.syntax }
func (t enumValueDesc) Name() pref.Name { return t.v.Name }
func (t enumValueDesc) FullName() pref.FullName { return t.v.fullName }
func (t enumValueDesc) IsPlaceholder() bool { return false }
func (t enumValueDesc) Options() pref.ProtoMessage {
func (t enumValueDesc) Options() pref.OptionsMessage {
return altOptions(t.v.Options, optionTypes.EnumValue)
}
func (t enumValueDesc) Number() pref.EnumNumber { return t.v.Number }
@ -420,13 +424,15 @@ type serviceMeta struct {
}
type serviceDesc struct{ s *Service }
func (t serviceDesc) Parent() (pref.Descriptor, bool) { return t.s.parent, true }
func (t serviceDesc) Index() int { return t.s.index }
func (t serviceDesc) Syntax() pref.Syntax { return t.s.syntax }
func (t serviceDesc) Name() pref.Name { return t.s.Name }
func (t serviceDesc) FullName() pref.FullName { return t.s.fullName }
func (t serviceDesc) IsPlaceholder() bool { return false }
func (t serviceDesc) Options() pref.ProtoMessage { return altOptions(t.s.Options, optionTypes.Service) }
func (t serviceDesc) Parent() (pref.Descriptor, bool) { return t.s.parent, true }
func (t serviceDesc) Index() int { return t.s.index }
func (t serviceDesc) Syntax() pref.Syntax { return t.s.syntax }
func (t serviceDesc) Name() pref.Name { return t.s.Name }
func (t serviceDesc) FullName() pref.FullName { return t.s.fullName }
func (t serviceDesc) IsPlaceholder() bool { return false }
func (t serviceDesc) Options() pref.OptionsMessage {
return altOptions(t.s.Options, optionTypes.Service)
}
func (t serviceDesc) Methods() pref.MethodDescriptors { return t.s.ms.lazyInit(t, t.s.Methods) }
func (t serviceDesc) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, t) }
func (t serviceDesc) ProtoType(pref.ServiceDescriptor) {}
@ -446,7 +452,7 @@ func (t methodDesc) Syntax() pref.Syntax { return t.m.syntax }
func (t methodDesc) Name() pref.Name { return t.m.Name }
func (t methodDesc) FullName() pref.FullName { return t.m.fullName }
func (t methodDesc) IsPlaceholder() bool { return false }
func (t methodDesc) Options() pref.ProtoMessage { return altOptions(t.m.Options, optionTypes.Method) }
func (t methodDesc) Options() pref.OptionsMessage { return altOptions(t.m.Options, optionTypes.Method) }
func (t methodDesc) InputType() pref.MessageDescriptor { return t.m.mit.lazyInit(t, &t.m.InputType) }
func (t methodDesc) OutputType() pref.MessageDescriptor { return t.m.mot.lazyInit(t, &t.m.OutputType) }
func (t methodDesc) IsStreamingClient() bool { return t.m.IsStreamingClient }

View File

@ -22,8 +22,8 @@ type StandaloneMessage struct {
ReservedNames []protoreflect.Name
ReservedRanges [][2]protoreflect.FieldNumber
ExtensionRanges [][2]protoreflect.FieldNumber
ExtensionRangeOptions []protoreflect.ProtoMessage
Options protoreflect.ProtoMessage
ExtensionRangeOptions []protoreflect.OptionsMessage
Options protoreflect.OptionsMessage
IsMapEntry bool
fields fieldsMeta
@ -90,7 +90,7 @@ type StandaloneEnum struct {
Values []EnumValue
ReservedNames []protoreflect.Name
ReservedRanges [][2]protoreflect.EnumNumber
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
vals enumValuesMeta
}
@ -117,7 +117,7 @@ type StandaloneExtension struct {
MessageType protoreflect.MessageDescriptor
EnumType protoreflect.EnumDescriptor
ExtendedType protoreflect.MessageDescriptor
Options protoreflect.ProtoMessage
Options protoreflect.OptionsMessage
IsPacked OptionalBool
dv defaultValue

View File

@ -20,7 +20,7 @@ func (t standaloneMessage) Syntax() pref.Syntax { return t.m.Syntax
func (t standaloneMessage) Name() pref.Name { return t.m.FullName.Name() }
func (t standaloneMessage) FullName() pref.FullName { return t.m.FullName }
func (t standaloneMessage) IsPlaceholder() bool { return false }
func (t standaloneMessage) Options() pref.ProtoMessage {
func (t standaloneMessage) Options() pref.OptionsMessage {
return altOptions(t.m.Options, optionTypes.Message)
}
func (t standaloneMessage) IsMapEntry() bool { return t.m.IsMapEntry }
@ -34,7 +34,7 @@ func (t standaloneMessage) RequiredNumbers() pref.FieldNumbers { return t.m.nums
func (t standaloneMessage) ExtensionRanges() pref.FieldRanges {
return (*fieldRanges)(&t.m.ExtensionRanges)
}
func (t standaloneMessage) ExtensionRangeOptions(i int) pref.ProtoMessage {
func (t standaloneMessage) ExtensionRangeOptions(i int) pref.OptionsMessage {
return extensionRangeOptions(i, len(t.m.ExtensionRanges), t.m.ExtensionRangeOptions)
}
func (t standaloneMessage) Enums() pref.EnumDescriptors { return &emptyEnums }
@ -46,13 +46,15 @@ func (t standaloneMessage) ProtoInternal(pragma.DoNotImplement) {}
type standaloneEnum struct{ e *StandaloneEnum }
func (t standaloneEnum) Parent() (pref.Descriptor, bool) { return nil, false }
func (t standaloneEnum) Index() int { return 0 }
func (t standaloneEnum) Syntax() pref.Syntax { return t.e.Syntax }
func (t standaloneEnum) Name() pref.Name { return t.e.FullName.Name() }
func (t standaloneEnum) FullName() pref.FullName { return t.e.FullName }
func (t standaloneEnum) IsPlaceholder() bool { return false }
func (t standaloneEnum) Options() pref.ProtoMessage { return altOptions(t.e.Options, optionTypes.Enum) }
func (t standaloneEnum) Parent() (pref.Descriptor, bool) { return nil, false }
func (t standaloneEnum) Index() int { return 0 }
func (t standaloneEnum) Syntax() pref.Syntax { return t.e.Syntax }
func (t standaloneEnum) Name() pref.Name { return t.e.FullName.Name() }
func (t standaloneEnum) FullName() pref.FullName { return t.e.FullName }
func (t standaloneEnum) IsPlaceholder() bool { return false }
func (t standaloneEnum) Options() pref.OptionsMessage {
return altOptions(t.e.Options, optionTypes.Enum)
}
func (t standaloneEnum) Values() pref.EnumValueDescriptors { return t.e.vals.lazyInit(t, t.e.Values) }
func (t standaloneEnum) ReservedNames() pref.Names { return (*names)(&t.e.ReservedNames) }
func (t standaloneEnum) ReservedRanges() pref.EnumRanges { return (*enumRanges)(&t.e.ReservedRanges) }
@ -68,7 +70,7 @@ func (t standaloneExtension) Syntax() pref.Syntax { return pref.Prot
func (t standaloneExtension) Name() pref.Name { return t.x.FullName.Name() }
func (t standaloneExtension) FullName() pref.FullName { return t.x.FullName }
func (t standaloneExtension) IsPlaceholder() bool { return false }
func (t standaloneExtension) Options() pref.ProtoMessage {
func (t standaloneExtension) Options() pref.OptionsMessage {
return altOptions(t.x.Options, optionTypes.Field)
}
func (t standaloneExtension) Number() pref.FieldNumber { return t.x.Number }

View File

@ -114,7 +114,7 @@ func TestFile(t *testing.T) {
ReservedNames: []pref.Name{"fizz", "buzz"},
ReservedRanges: [][2]pref.FieldNumber{{100, 200}, {300, 301}},
ExtensionRanges: [][2]pref.FieldNumber{{1000, 2000}, {3000, 3001}},
ExtensionRangeOptions: []pref.ProtoMessage{
ExtensionRangeOptions: []pref.OptionsMessage{
0: (*descriptorpb.ExtensionRangeOptions)(nil),
1: new(descriptorpb.ExtensionRangeOptions),
},