mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-27 12:35:21 +00:00
all: rename ExtensionType Descriptor method to TypeDescriptor (1/2)
Descriptor methods generally return a Descriptor with no Go type information. ExtensionType's Descriptor is an exception, returning an ExtensionTypeDescriptor containing both the proto descriptor and a reference back to the ExtensionType. The pure descriptor is accessed by xt.Descriptor().Descriptor(). Rename ExtensionType's Descriptor method to TypeDescriptor to make it clear that it behaves a bit differently. Change 1/2: Add the TypeDescriptor method and deprecate Descriptor. Change-Id: I1806095044d35a474d60f94d2a28bdf528f12238 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/192139 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
parent
95758c0892
commit
79bfdbe45b
@ -183,7 +183,7 @@ func (o UnmarshalOptions) unmarshalFields(m pref.Message, skipTypeURL bool) erro
|
||||
return errors.New("unable to resolve [%v]: %v", extName, err)
|
||||
}
|
||||
if extType != nil {
|
||||
fd = extType.Descriptor()
|
||||
fd = extType.TypeDescriptor()
|
||||
}
|
||||
} else {
|
||||
// The name can either be the JSON name or the proto field name.
|
||||
|
@ -127,7 +127,7 @@ func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message)
|
||||
return errors.New("unable to resolve [%v]: %v", extName, err)
|
||||
}
|
||||
if xt != nil {
|
||||
fd = xt.Descriptor()
|
||||
fd = xt.TypeDescriptor()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ func FindMessageSetExtension(r preg.ExtensionTypeResolver, s pref.FullName) (pre
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !IsMessageSetExtension(xt.Descriptor()) {
|
||||
if !IsMessageSetExtension(xt.TypeDescriptor()) {
|
||||
return nil, preg.NotFound
|
||||
}
|
||||
return xt, nil
|
||||
|
@ -29,7 +29,7 @@ func (mi *MessageInfo) extensionFieldInfo(xt pref.ExtensionType) *extensionField
|
||||
return e
|
||||
}
|
||||
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
var wiretag uint64
|
||||
if !xd.IsPacked() {
|
||||
wiretag = wire.EncodeTag(xd.Number(), wireTypes[xd.Kind()])
|
||||
|
@ -119,6 +119,10 @@ func (xi *ExtensionInfo) GoType() reflect.Type {
|
||||
return xi.goType
|
||||
}
|
||||
func (xi *ExtensionInfo) Descriptor() pref.ExtensionTypeDescriptor {
|
||||
// TODO: Remove.
|
||||
return xi.TypeDescriptor()
|
||||
}
|
||||
func (xi *ExtensionInfo) TypeDescriptor() pref.ExtensionTypeDescriptor {
|
||||
if atomic.LoadUint32(&xi.init) == extensionInfoUninitialized {
|
||||
xi.lazyInitSlow()
|
||||
}
|
||||
|
@ -33,11 +33,11 @@ func legacyExtensionDescFromType(xt pref.ExtensionType) *ExtensionInfo {
|
||||
}
|
||||
|
||||
tt := xt.GoType()
|
||||
if xt.Descriptor().Cardinality() == pref.Repeated {
|
||||
if xt.TypeDescriptor().Cardinality() == pref.Repeated {
|
||||
tt = tt.Elem().Elem()
|
||||
}
|
||||
xi := &ExtensionInfo{}
|
||||
InitExtensionInfo(xi, xt.Descriptor().Descriptor(), tt)
|
||||
InitExtensionInfo(xi, xt.TypeDescriptor().Descriptor(), tt)
|
||||
xi.lazyInit() // populate legacy fields
|
||||
|
||||
if xi, ok := legacyExtensionInfoCache.LoadOrStore(xt, xi); ok {
|
||||
|
@ -358,7 +358,7 @@ func TestLegacyExtensions(t *testing.T) {
|
||||
}
|
||||
for i, xt := range extensionTypes {
|
||||
var got interface{}
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
if !(xd.IsList() || xd.IsMap() || xd.Message() != nil) {
|
||||
got = xt.InterfaceOf(m.Get(xd))
|
||||
}
|
||||
@ -370,7 +370,7 @@ func TestLegacyExtensions(t *testing.T) {
|
||||
|
||||
// All fields should be unpopulated.
|
||||
for _, xt := range extensionTypes {
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
if m.Has(xd) {
|
||||
t.Errorf("Message.Has(%d) = true, want false", xd.Number())
|
||||
}
|
||||
@ -404,11 +404,11 @@ func TestLegacyExtensions(t *testing.T) {
|
||||
19: &[]*EnumMessages{m2b},
|
||||
}
|
||||
for i, xt := range extensionTypes {
|
||||
m.Set(xt.Descriptor(), xt.ValueOf(setValues[i]))
|
||||
m.Set(xt.TypeDescriptor(), xt.ValueOf(setValues[i]))
|
||||
}
|
||||
for i, xt := range extensionTypes[len(extensionTypes)/2:] {
|
||||
v := extensionTypes[i].ValueOf(setValues[i])
|
||||
m.Get(xt.Descriptor()).List().Append(v)
|
||||
m.Get(xt.TypeDescriptor()).List().Append(v)
|
||||
}
|
||||
|
||||
// Get the values and check for equality.
|
||||
@ -435,7 +435,7 @@ func TestLegacyExtensions(t *testing.T) {
|
||||
19: &[]*EnumMessages{m2b, m2a},
|
||||
}
|
||||
for i, xt := range extensionTypes {
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
got := xt.InterfaceOf(m.Get(xd))
|
||||
want := getValues[i]
|
||||
if diff := cmp.Diff(want, got, opts); diff != "" {
|
||||
@ -445,15 +445,15 @@ func TestLegacyExtensions(t *testing.T) {
|
||||
|
||||
// Clear all singular fields and truncate all repeated fields.
|
||||
for _, xt := range extensionTypes[:len(extensionTypes)/2] {
|
||||
m.Clear(xt.Descriptor())
|
||||
m.Clear(xt.TypeDescriptor())
|
||||
}
|
||||
for _, xt := range extensionTypes[len(extensionTypes)/2:] {
|
||||
m.Get(xt.Descriptor()).List().Truncate(0)
|
||||
m.Get(xt.TypeDescriptor()).List().Truncate(0)
|
||||
}
|
||||
|
||||
// Clear all repeated fields.
|
||||
for _, xt := range extensionTypes[len(extensionTypes)/2:] {
|
||||
m.Clear(xt.Descriptor())
|
||||
m.Clear(xt.TypeDescriptor())
|
||||
}
|
||||
}
|
||||
|
||||
@ -517,7 +517,7 @@ func TestExtensionConvert(t *testing.T) {
|
||||
}),
|
||||
cmp.Transformer("", func(xt pref.ExtensionType) map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"Descriptor": xt.Descriptor(),
|
||||
"Descriptor": xt.TypeDescriptor(),
|
||||
"GoType": xt.GoType(),
|
||||
}
|
||||
}),
|
||||
|
@ -119,7 +119,7 @@ func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
|
||||
if m != nil {
|
||||
for _, x := range *m {
|
||||
xt := x.GetType()
|
||||
if !f(xt.Descriptor(), xt.ValueOf(x.GetValue())) {
|
||||
if !f(xt.TypeDescriptor(), xt.ValueOf(x.GetValue())) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -127,15 +127,15 @@ func (m *extensionMap) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
|
||||
}
|
||||
func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
|
||||
if m != nil {
|
||||
_, ok = (*m)[int32(xt.Descriptor().Number())]
|
||||
_, ok = (*m)[int32(xt.TypeDescriptor().Number())]
|
||||
}
|
||||
return ok
|
||||
}
|
||||
func (m *extensionMap) Clear(xt pref.ExtensionType) {
|
||||
delete(*m, int32(xt.Descriptor().Number()))
|
||||
delete(*m, int32(xt.TypeDescriptor().Number()))
|
||||
}
|
||||
func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
if m != nil {
|
||||
if x, ok := (*m)[int32(xd.Number())]; ok {
|
||||
return xt.ValueOf(x.GetValue())
|
||||
@ -150,10 +150,10 @@ func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
|
||||
var x ExtensionField
|
||||
x.SetType(xt)
|
||||
x.SetEagerValue(xt.InterfaceOf(v))
|
||||
(*m)[int32(xt.Descriptor().Number())] = x
|
||||
(*m)[int32(xt.TypeDescriptor().Number())] = x
|
||||
}
|
||||
func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
if xd.Kind() != pref.MessageKind && xd.Kind() != pref.GroupKind && !xd.IsList() && !xd.IsMap() {
|
||||
panic("invalid Mutable on field with non-composite type")
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message)
|
||||
return err
|
||||
}
|
||||
if extType != nil {
|
||||
fd = extType.Descriptor()
|
||||
fd = extType.TypeDescriptor()
|
||||
}
|
||||
}
|
||||
var err error
|
||||
|
@ -10,13 +10,13 @@ import (
|
||||
|
||||
// HasExtension reports whether an extension field is populated.
|
||||
func HasExtension(m Message, ext protoreflect.ExtensionType) bool {
|
||||
return m.ProtoReflect().Has(ext.Descriptor())
|
||||
return m.ProtoReflect().Has(ext.TypeDescriptor())
|
||||
}
|
||||
|
||||
// ClearExtension clears an extension field such that subsequent
|
||||
// HasExtension calls return false.
|
||||
func ClearExtension(m Message, ext protoreflect.ExtensionType) {
|
||||
m.ProtoReflect().Clear(ext.Descriptor())
|
||||
m.ProtoReflect().Clear(ext.TypeDescriptor())
|
||||
}
|
||||
|
||||
// GetExtension retrieves the value for an extension field.
|
||||
@ -24,10 +24,10 @@ func ClearExtension(m Message, ext protoreflect.ExtensionType) {
|
||||
// If the field is unpopulated, it returns the default value for
|
||||
// scalars and an immutable, empty value for lists, maps, or messages.
|
||||
func GetExtension(m Message, ext protoreflect.ExtensionType) interface{} {
|
||||
return ext.InterfaceOf(m.ProtoReflect().Get(ext.Descriptor()))
|
||||
return ext.InterfaceOf(m.ProtoReflect().Get(ext.TypeDescriptor()))
|
||||
}
|
||||
|
||||
// SetExtension stores the value of an extension field.
|
||||
func SetExtension(m Message, ext protoreflect.ExtensionType, value interface{}) {
|
||||
m.ProtoReflect().Set(ext.Descriptor(), ext.ValueOf(value))
|
||||
m.ProtoReflect().Set(ext.TypeDescriptor(), ext.ValueOf(value))
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func TestExtensionFuncs(t *testing.T) {
|
||||
value: true,
|
||||
},
|
||||
} {
|
||||
desc := fmt.Sprintf("Extension %v, value %v", test.ext.Descriptor().FullName(), test.value)
|
||||
desc := fmt.Sprintf("Extension %v, value %v", test.ext.TypeDescriptor().FullName(), test.value)
|
||||
if proto.HasExtension(test.message, test.ext) {
|
||||
t.Errorf("%v:\nbefore setting extension HasExtension(...) = true, want false", desc)
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ func unmarshalMessageSet(b []byte, m protoreflect.Message, o UnmarshalOptions) e
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
if err := o.unmarshalMessage(v, m.Mutable(xd).Message()); err != nil {
|
||||
// Contents cannot be unmarshaled.
|
||||
return err
|
||||
|
@ -73,13 +73,15 @@
|
||||
// ProtoMessage, which can convert to/from a Message.
|
||||
//
|
||||
//
|
||||
// ┌── Descriptor() ──┐ ┌───── implements ─────┐
|
||||
// ┌── Descriptor() ──┐ ┌──── Descriptor() ────┐
|
||||
// │ V │ V
|
||||
// ╔═══════════════╗ ╔═════════════════════════╗ ╔═════════════════════╗
|
||||
// ║ ExtensionType ║ ║ ExtensionTypeDescriptor ║ ║ ExtensionDescriptor ║
|
||||
// ╚═══════════════╝ ╚═════════════════════════╝ ╚═════════════════════╝
|
||||
// Λ │ Λ │
|
||||
// └───── Type() ─────┘ └─── may implement ────┘
|
||||
// Λ │ │ Λ │ Λ
|
||||
// └───── Type() ─────┘ │ └─── may implement ────┘ │
|
||||
// │ │
|
||||
// └────── implements ────────┘
|
||||
//
|
||||
// • An ExtensionType describes a concrete Go implementation of an extension.
|
||||
// It has an ExtensionTypeDescriptor and can convert to/from
|
||||
|
@ -461,7 +461,12 @@ type ExtensionType interface {
|
||||
// t.GoType() == reflect.TypeOf(t.InterfaceOf(t.New()))
|
||||
GoType() reflect.Type
|
||||
|
||||
// Descriptor returns the extension descriptor.
|
||||
// TypeDescriptor returns the extension type descriptor.
|
||||
TypeDescriptor() ExtensionTypeDescriptor
|
||||
|
||||
// Descriptor is deprecated.
|
||||
//
|
||||
// TODO: remove.
|
||||
Descriptor() ExtensionTypeDescriptor
|
||||
|
||||
// TODO: What to do with nil?
|
||||
|
@ -434,7 +434,7 @@ typeLoop:
|
||||
case protoreflect.MessageType:
|
||||
desc = t.Descriptor()
|
||||
case protoreflect.ExtensionType:
|
||||
desc = t.Descriptor()
|
||||
desc = t.TypeDescriptor()
|
||||
default:
|
||||
panic(fmt.Sprintf("invalid type: %T", t))
|
||||
}
|
||||
@ -453,7 +453,7 @@ typeLoop:
|
||||
|
||||
// Check for conflicts in extensionsByMessage.
|
||||
if xt, _ := typ.(protoreflect.ExtensionType); xt != nil {
|
||||
xd := xt.Descriptor()
|
||||
xd := xt.TypeDescriptor()
|
||||
field := xd.Number()
|
||||
message := xd.ContainingMessage().FullName()
|
||||
if prev := r.extensionsByMessage[message][field]; prev != nil {
|
||||
@ -660,7 +660,7 @@ func goPackage(v interface{}) string {
|
||||
case protoreflect.MessageType:
|
||||
v = d.Descriptor()
|
||||
case protoreflect.ExtensionType:
|
||||
v = d.Descriptor()
|
||||
v = d.TypeDescriptor()
|
||||
}
|
||||
if d, ok := v.(protoreflect.Descriptor); ok {
|
||||
v = d.ParentFile()
|
||||
|
@ -540,7 +540,7 @@ func TestTypes(t *testing.T) {
|
||||
case pref.MessageType:
|
||||
return t.Descriptor().FullName()
|
||||
case pref.ExtensionType:
|
||||
return t.Descriptor().FullName()
|
||||
return t.TypeDescriptor().FullName()
|
||||
default:
|
||||
panic("invalid type")
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ func TestMessage(t testing.TB, m proto.Message, opts MessageOptions) {
|
||||
})
|
||||
}
|
||||
for _, xt := range opts.ExtensionTypes {
|
||||
testField(t, m1, xt.Descriptor())
|
||||
testField(t, m1, xt.TypeDescriptor())
|
||||
}
|
||||
for i := 0; i < md.Oneofs().Len(); i++ {
|
||||
testOneof(t, m1, md.Oneofs().Get(i))
|
||||
|
Loading…
x
Reference in New Issue
Block a user