Procházet zdrojové kódy

all: avoid accessing ExtensionField.Desc directly

CL/180538 adds methods to ExtensionFieldV1 to provide the illusion that
it only operates with protoreflect.ExtensionType. Use that instead
of touching the Desc field directly.

Change-Id: If6770920707ea3f277c6cfd7caf40c7ca3b0aa6f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/180577
Reviewed-by: Damien Neil <dneil@google.com>
Joe Tsai před 6 roky
rodič
revize
76c9e09470

+ 1 - 1
go.mod

@@ -2,4 +2,4 @@ module github.com/golang/protobuf
 
 go 1.9
 
-require google.golang.org/protobuf v0.0.0-20190522194032-21ade498bd69
+require google.golang.org/protobuf v0.0.0-20190605195314-89d49632e5cf

+ 3 - 1
go.sum

@@ -2,11 +2,13 @@ github.com/golang/protobuf v1.2.1-0.20190514181236-7800af189d76/go.mod h1:Zfz6qc
 github.com/golang/protobuf v1.2.1-0.20190515194842-7574ba03306e/go.mod h1:GjgUz9uwrRQmdPBBrFqiVbojAmlpy6ryM6DCzC+20rE=
 github.com/golang/protobuf v1.2.1-0.20190516201927-a2cd3ac1b343/go.mod h1:PScGDF2x230A126tLt9Ol9RjhXzbiPJrt/CogooD2mE=
 github.com/golang/protobuf v1.2.1-0.20190516215712-ae2eaafab405/go.mod h1:UmP8hhPKR5WWIjbT9v0JEVT+U0DBSjbW8KaZVeyFfRE=
+github.com/golang/protobuf v1.2.1-0.20190523175523-a1331f0b4ab4/go.mod h1:G+fNMoyvKWZDB7PCDHF+dXbH9OeE3+JoozCd9V7i66U=
 github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
 github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
 google.golang.org/protobuf v0.0.0-20190514172829-e89e6244e0e8/go.mod h1:791zQGC15vDqjpmPRn1uGPu5oHy/Jzw/Q1n5JsgIIcY=
 google.golang.org/protobuf v0.0.0-20190514231807-cdb777356907/go.mod h1:HeRLsKXv4+wE27dOIGwnqcOgq6a1O/GJ7mGhiEPnBrU=
 google.golang.org/protobuf v0.0.0-20190516201745-40b83d67fc75/go.mod h1:jf+u8AHuKtkib+0J4/bQXPNzCmT3V9a02hVzYKtatuw=
 google.golang.org/protobuf v0.0.0-20190516215540-a95b29fbf623/go.mod h1:cWWmz5lsCWIcqGLROrKq5Lu231IJw2PzqOZ8cgspbfY=
-google.golang.org/protobuf v0.0.0-20190522194032-21ade498bd69 h1:C4vak6RNv+6SZeonQPm8QpfDoEd2jwk0zmv/XjuhaXc=
 google.golang.org/protobuf v0.0.0-20190522194032-21ade498bd69/go.mod h1:cJytyYi/6qdwy/+gD49hmgHcwD7zhWxE/1KPEslaZ3M=
+google.golang.org/protobuf v0.0.0-20190605195314-89d49632e5cf h1:O9EJAZHrXZ8fKtT5e+sEc2u/izLtNaXiwHhthZEtsSs=
+google.golang.org/protobuf v0.0.0-20190605195314-89d49632e5cf/go.mod h1:Btug4TBaP5wNYcb2zGKDTS7WMcaPPLuqEAKfEAZWYbo=

+ 2 - 1
proto/clone.go

@@ -208,7 +208,8 @@ func mergeAny(out, in reflect.Value, viaPtr bool, prop *Properties) {
 
 func mergeExtension(out, in *extensionMap) {
 	in.Range(func(extNum protoreflect.FieldNumber, eIn Extension) bool {
-		eOut := Extension{Desc: eIn.Desc}
+		var eOut Extension
+		eOut.SetType(eIn.GetType())
 		if eIn.HasValue() {
 			v := reflect.New(reflect.TypeOf(eIn.GetValue())).Elem()
 			mergeAny(v, reflect.ValueOf(eIn.GetValue()), false, nil)

+ 5 - 4
proto/extensions.go

@@ -270,7 +270,7 @@ func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
 	e := epb.Get(protoreflect.FieldNumber(extension.Field))
 	if e.HasValue() {
 		// Already decoded. Check the descriptor, though.
-		if e.Desc != extension {
+		if protoimpl.X.ExtensionDescFromType(e.GetType()) != extension {
 			// This shouldn't happen. If it does, it means that
 			// GetExtension was called twice with two different
 			// descriptors with the same field number.
@@ -292,7 +292,7 @@ func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
 
 	// Remember the decoded version and drop the encoded version.
 	// That way it is safe to mutate what we return.
-	e.Desc = extension
+	e.SetType(protoimpl.X.ExtensionTypeFromDesc(extension))
 	e.SetEagerValue(extensionAsStorageType(v))
 	unrecognized.SetBytes(removeRawFields(unrecognized.Bytes(), fnum))
 	epb.Set(protoreflect.FieldNumber(extension.Field), e)
@@ -404,7 +404,7 @@ func ExtensionDescs(pb Message) ([]*ExtensionDesc, error) {
 	}
 	extensions := make([]*ExtensionDesc, 0, epb.Len())
 	epb.Range(func(extid protoreflect.FieldNumber, e Extension) bool {
-		desc := e.Desc
+		desc := protoimpl.X.ExtensionDescFromType(e.GetType())
 		if desc == nil {
 			desc = registeredExtensions[int32(extid)]
 			if desc == nil {
@@ -461,7 +461,8 @@ func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error
 		return fmt.Errorf("proto: SetExtension called with nil value of type %T", value)
 	}
 
-	x := Extension{Desc: extension}
+	var x Extension
+	x.SetType(protoimpl.X.ExtensionTypeFromDesc(extension))
 	x.SetEagerValue(extensionAsStorageType(value))
 	epb.Set(protoreflect.FieldNumber(extension.Field), x)
 	return nil

+ 17 - 16
proto/table_marshal.go

@@ -18,6 +18,7 @@ import (
 
 	"github.com/golang/protobuf/internal/wire"
 	"google.golang.org/protobuf/reflect/protoreflect"
+	"google.golang.org/protobuf/runtime/protoimpl"
 )
 
 // a sizer takes a pointer to a field and the size of its tag, computes the size of
@@ -2373,14 +2374,14 @@ func (u *marshalInfo) sizeExtensions(ext *XXX_InternalExtensions) int {
 
 	n := 0
 	m.Range(func(_ protoreflect.FieldNumber, e Extension) bool {
-		if e.Desc == nil || !e.HasValue() {
+		if !e.HasType() || !e.HasValue() {
 			return true // should never happen
 		}
 
 		// We don't skip extensions that have an encoded form set,
 		// because the extension value may have been mutated after
 		// the last time this function was called.
-		ei := u.getExtElemInfo(e.Desc)
+		ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 		v := e.GetValue()
 		p := toAddrPointer(&v, ei.isptr, ei.deref)
 		n += ei.sizer(p, ei.tagsize)
@@ -2403,7 +2404,7 @@ func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, de
 	// Don't bother sorting the keys.
 	if m.Len() <= 1 {
 		m.Range(func(_ protoreflect.FieldNumber, e Extension) bool {
-			if e.Desc == nil || !e.HasValue() {
+			if !e.HasType() || !e.HasValue() {
 				return true // should never happen
 			}
 
@@ -2411,7 +2412,7 @@ func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, de
 			// because the extension value may have been mutated after
 			// the last time this function was called.
 
-			ei := u.getExtElemInfo(e.Desc)
+			ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 			v := e.GetValue()
 			p := toAddrPointer(&v, ei.isptr, ei.deref)
 			b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
@@ -2435,7 +2436,7 @@ func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, de
 
 	for _, k := range keys {
 		e := m.Get(protoreflect.FieldNumber(k))
-		if e.Desc == nil || !e.HasValue() {
+		if !e.HasType() || !e.HasValue() {
 			continue // should never happen
 		}
 
@@ -2443,7 +2444,7 @@ func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, de
 		// because the extension value may have been mutated after
 		// the last time this function was called.
 
-		ei := u.getExtElemInfo(e.Desc)
+		ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 		v := e.GetValue()
 		p := toAddrPointer(&v, ei.isptr, ei.deref)
 		b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
@@ -2475,7 +2476,7 @@ func (u *marshalInfo) sizeMessageSet(ext *XXX_InternalExtensions, unk []byte) in
 		n += 2                          // start group, end group. tag = 1 (size=1)
 		n += SizeVarint(uint64(id)) + 1 // type_id, tag = 2 (size=1)
 
-		if e.Desc == nil || !e.HasValue() {
+		if !e.HasType() || !e.HasValue() {
 			return true // should never happen
 		}
 
@@ -2483,7 +2484,7 @@ func (u *marshalInfo) sizeMessageSet(ext *XXX_InternalExtensions, unk []byte) in
 		// because the extension value may have been mutated after
 		// the last time this function was called.
 
-		ei := u.getExtElemInfo(e.Desc)
+		ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 		v := e.GetValue()
 		p := toAddrPointer(&v, ei.isptr, ei.deref)
 		n += ei.sizer(p, 1) // message, tag = 3 (size=1)
@@ -2528,7 +2529,7 @@ func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, un
 			b = append(b, 2<<3|WireVarint)
 			b = appendVarint(b, uint64(id))
 
-			if e.Desc == nil || !e.HasValue() {
+			if !e.HasType() || !e.HasValue() {
 				return true // should never happen
 			}
 
@@ -2536,7 +2537,7 @@ func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, un
 			// because the extension value may have been mutated after
 			// the last time this function was called.
 
-			ei := u.getExtElemInfo(e.Desc)
+			ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 			v := e.GetValue()
 			p := toAddrPointer(&v, ei.isptr, ei.deref)
 			b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
@@ -2583,7 +2584,7 @@ func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, un
 		b = append(b, 2<<3|WireVarint)
 		b = appendVarint(b, uint64(id))
 
-		if e.Desc == nil || !e.HasValue() {
+		if !e.HasType() || !e.HasValue() {
 			continue // should never happen
 		}
 
@@ -2591,7 +2592,7 @@ func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, un
 		// because the extension value may have been mutated after
 		// the last time this function was called.
 
-		ei := u.getExtElemInfo(e.Desc)
+		ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 		v := e.GetValue()
 		p := toAddrPointer(&v, ei.isptr, ei.deref)
 		b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
@@ -2630,7 +2631,7 @@ func (u *marshalInfo) sizeV1Extensions(m map[int32]Extension) int {
 
 	n := 0
 	for _, e := range m {
-		if e.Desc == nil || !e.HasValue() {
+		if !e.HasType() || !e.HasValue() {
 			continue // should never happen
 		}
 
@@ -2638,7 +2639,7 @@ func (u *marshalInfo) sizeV1Extensions(m map[int32]Extension) int {
 		// because the extension value may have been mutated after
 		// the last time this function was called.
 
-		ei := u.getExtElemInfo(e.Desc)
+		ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 		v := e.GetValue()
 		p := toAddrPointer(&v, ei.isptr, ei.deref)
 		n += ei.sizer(p, ei.tagsize)
@@ -2663,7 +2664,7 @@ func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, determ
 	var nerr nonFatal
 	for _, k := range keys {
 		e := m[int32(k)]
-		if e.Desc == nil || !e.HasValue() {
+		if !e.HasType() || !e.HasValue() {
 			continue // should never happen
 		}
 
@@ -2671,7 +2672,7 @@ func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, determ
 		// because the extension value may have been mutated after
 		// the last time this function was called.
 
-		ei := u.getExtElemInfo(e.Desc)
+		ei := u.getExtElemInfo(protoimpl.X.ExtensionDescFromType(e.GetType()))
 		v := e.GetValue()
 		p := toAddrPointer(&v, ei.isptr, ei.deref)
 		b, err = ei.marshaler(b, p, ei.wiretag, deterministic)

+ 3 - 1
proto/table_unmarshal.go

@@ -17,6 +17,7 @@ import (
 	"unicode/utf8"
 
 	"github.com/golang/protobuf/internal/wire"
+	"google.golang.org/protobuf/runtime/protoimpl"
 )
 
 // Unmarshal is the entry point from the generated .pb.go files.
@@ -274,7 +275,8 @@ func unmarshalExtensions(mi Message, unrecognized *[]byte) error {
 		}
 
 		// Store the value into the extension field.
-		x := Extension{Desc: extDesc}
+		var x Extension
+		x.SetType(protoimpl.X.ExtensionTypeFromDesc(extDesc))
 		x.SetEagerValue(extensionAsStorageType(fieldVal.Interface()))
 		extFields.Set(fieldNum, x)
 	}

+ 6 - 1
protoc-gen-go/descriptor/descriptor.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/descriptor.proto
 

+ 6 - 1
protoc-gen-go/plugin/plugin.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/compiler/plugin.proto
 

+ 6 - 1
ptypes/any/any.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/any.proto
 

+ 6 - 1
ptypes/duration/duration.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/duration.proto
 

+ 6 - 1
ptypes/empty/empty.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/empty.proto
 

+ 6 - 1
ptypes/struct/struct.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/struct.proto
 

+ 6 - 1
ptypes/timestamp/timestamp.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/timestamp.proto
 

+ 6 - 1
ptypes/wrappers/wrappers.pb.go

@@ -11,7 +11,12 @@ import (
 	sync "sync"
 )
 
-const _ = protoimpl.EnforceVersion(protoimpl.Version - 0)
+const (
+	// Verify that runtime/protoimpl is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 0)
+	// Verify that this generated code is sufficiently up-to-date.
+	_ = protoimpl.EnforceVersion(0 - protoimpl.MinVersion)
+)
 
 // Symbols defined in public import of google/protobuf/wrappers.proto