vendor/google.golang.org/protobuf/internal/impl/extension.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
     7 import (
     7 import (
     8 	"reflect"
     8 	"reflect"
     9 	"sync"
     9 	"sync"
    10 	"sync/atomic"
    10 	"sync/atomic"
    11 
    11 
    12 	pref "google.golang.org/protobuf/reflect/protoreflect"
    12 	"google.golang.org/protobuf/reflect/protoreflect"
    13 	piface "google.golang.org/protobuf/runtime/protoiface"
    13 	"google.golang.org/protobuf/runtime/protoiface"
    14 )
    14 )
    15 
    15 
    16 // ExtensionInfo implements ExtensionType.
    16 // ExtensionInfo implements ExtensionType.
    17 //
    17 //
    18 // This type contains a number of exported fields for legacy compatibility.
    18 // This type contains a number of exported fields for legacy compatibility.
    43 	// ExtendedType is a typed nil-pointer to the parent message type that
    43 	// ExtendedType is a typed nil-pointer to the parent message type that
    44 	// is being extended. It is possible for this to be unpopulated in v2
    44 	// is being extended. It is possible for this to be unpopulated in v2
    45 	// since the message may no longer implement the MessageV1 interface.
    45 	// since the message may no longer implement the MessageV1 interface.
    46 	//
    46 	//
    47 	// Deprecated: Use the ExtendedType method instead.
    47 	// Deprecated: Use the ExtendedType method instead.
    48 	ExtendedType piface.MessageV1
    48 	ExtendedType protoiface.MessageV1
    49 
    49 
    50 	// ExtensionType is the zero value of the extension type.
    50 	// ExtensionType is the zero value of the extension type.
    51 	//
    51 	//
    52 	// For historical reasons, reflect.TypeOf(ExtensionType) and the
    52 	// For historical reasons, reflect.TypeOf(ExtensionType) and the
    53 	// type returned by InterfaceOf may not be identical.
    53 	// type returned by InterfaceOf may not be identical.
    81 	extensionInfoUninitialized = 0
    81 	extensionInfoUninitialized = 0
    82 	extensionInfoDescInit      = 1
    82 	extensionInfoDescInit      = 1
    83 	extensionInfoFullInit      = 2
    83 	extensionInfoFullInit      = 2
    84 )
    84 )
    85 
    85 
    86 func InitExtensionInfo(xi *ExtensionInfo, xd pref.ExtensionDescriptor, goType reflect.Type) {
    86 func InitExtensionInfo(xi *ExtensionInfo, xd protoreflect.ExtensionDescriptor, goType reflect.Type) {
    87 	xi.goType = goType
    87 	xi.goType = goType
    88 	xi.desc = extensionTypeDescriptor{xd, xi}
    88 	xi.desc = extensionTypeDescriptor{xd, xi}
    89 	xi.init = extensionInfoDescInit
    89 	xi.init = extensionInfoDescInit
    90 }
    90 }
    91 
    91 
    92 func (xi *ExtensionInfo) New() pref.Value {
    92 func (xi *ExtensionInfo) New() protoreflect.Value {
    93 	return xi.lazyInit().New()
    93 	return xi.lazyInit().New()
    94 }
    94 }
    95 func (xi *ExtensionInfo) Zero() pref.Value {
    95 func (xi *ExtensionInfo) Zero() protoreflect.Value {
    96 	return xi.lazyInit().Zero()
    96 	return xi.lazyInit().Zero()
    97 }
    97 }
    98 func (xi *ExtensionInfo) ValueOf(v interface{}) pref.Value {
    98 func (xi *ExtensionInfo) ValueOf(v interface{}) protoreflect.Value {
    99 	return xi.lazyInit().PBValueOf(reflect.ValueOf(v))
    99 	return xi.lazyInit().PBValueOf(reflect.ValueOf(v))
   100 }
   100 }
   101 func (xi *ExtensionInfo) InterfaceOf(v pref.Value) interface{} {
   101 func (xi *ExtensionInfo) InterfaceOf(v protoreflect.Value) interface{} {
   102 	return xi.lazyInit().GoValueOf(v).Interface()
   102 	return xi.lazyInit().GoValueOf(v).Interface()
   103 }
   103 }
   104 func (xi *ExtensionInfo) IsValidValue(v pref.Value) bool {
   104 func (xi *ExtensionInfo) IsValidValue(v protoreflect.Value) bool {
   105 	return xi.lazyInit().IsValidPB(v)
   105 	return xi.lazyInit().IsValidPB(v)
   106 }
   106 }
   107 func (xi *ExtensionInfo) IsValidInterface(v interface{}) bool {
   107 func (xi *ExtensionInfo) IsValidInterface(v interface{}) bool {
   108 	return xi.lazyInit().IsValidGo(reflect.ValueOf(v))
   108 	return xi.lazyInit().IsValidGo(reflect.ValueOf(v))
   109 }
   109 }
   110 func (xi *ExtensionInfo) TypeDescriptor() pref.ExtensionTypeDescriptor {
   110 func (xi *ExtensionInfo) TypeDescriptor() protoreflect.ExtensionTypeDescriptor {
   111 	if atomic.LoadUint32(&xi.init) < extensionInfoDescInit {
   111 	if atomic.LoadUint32(&xi.init) < extensionInfoDescInit {
   112 		xi.lazyInitSlow()
   112 		xi.lazyInitSlow()
   113 	}
   113 	}
   114 	return &xi.desc
   114 	return &xi.desc
   115 }
   115 }
   142 		xi.info.validation = newValidationInfo(xi.desc.ExtensionDescriptor, xi.goType)
   142 		xi.info.validation = newValidationInfo(xi.desc.ExtensionDescriptor, xi.goType)
   143 	}
   143 	}
   144 }
   144 }
   145 
   145 
   146 type extensionTypeDescriptor struct {
   146 type extensionTypeDescriptor struct {
   147 	pref.ExtensionDescriptor
   147 	protoreflect.ExtensionDescriptor
   148 	xi *ExtensionInfo
   148 	xi *ExtensionInfo
   149 }
   149 }
   150 
   150 
   151 func (xtd *extensionTypeDescriptor) Type() pref.ExtensionType {
   151 func (xtd *extensionTypeDescriptor) Type() protoreflect.ExtensionType {
   152 	return xtd.xi
   152 	return xtd.xi
   153 }
   153 }
   154 func (xtd *extensionTypeDescriptor) Descriptor() pref.ExtensionDescriptor {
   154 func (xtd *extensionTypeDescriptor) Descriptor() protoreflect.ExtensionDescriptor {
   155 	return xtd.ExtensionDescriptor
   155 	return xtd.ExtensionDescriptor
   156 }
   156 }