vendor/google.golang.org/protobuf/internal/impl/legacy_extension.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
    10 	"google.golang.org/protobuf/internal/descopts"
    10 	"google.golang.org/protobuf/internal/descopts"
    11 	"google.golang.org/protobuf/internal/encoding/messageset"
    11 	"google.golang.org/protobuf/internal/encoding/messageset"
    12 	ptag "google.golang.org/protobuf/internal/encoding/tag"
    12 	ptag "google.golang.org/protobuf/internal/encoding/tag"
    13 	"google.golang.org/protobuf/internal/filedesc"
    13 	"google.golang.org/protobuf/internal/filedesc"
    14 	"google.golang.org/protobuf/internal/pragma"
    14 	"google.golang.org/protobuf/internal/pragma"
    15 	pref "google.golang.org/protobuf/reflect/protoreflect"
    15 	"google.golang.org/protobuf/reflect/protoreflect"
    16 	preg "google.golang.org/protobuf/reflect/protoregistry"
    16 	"google.golang.org/protobuf/reflect/protoregistry"
    17 	piface "google.golang.org/protobuf/runtime/protoiface"
    17 	"google.golang.org/protobuf/runtime/protoiface"
    18 )
    18 )
    19 
    19 
    20 func (xi *ExtensionInfo) initToLegacy() {
    20 func (xi *ExtensionInfo) initToLegacy() {
    21 	xd := xi.desc
    21 	xd := xi.desc
    22 	var parent piface.MessageV1
    22 	var parent protoiface.MessageV1
    23 	messageName := xd.ContainingMessage().FullName()
    23 	messageName := xd.ContainingMessage().FullName()
    24 	if mt, _ := preg.GlobalTypes.FindMessageByName(messageName); mt != nil {
    24 	if mt, _ := protoregistry.GlobalTypes.FindMessageByName(messageName); mt != nil {
    25 		// Create a new parent message and unwrap it if possible.
    25 		// Create a new parent message and unwrap it if possible.
    26 		mv := mt.New().Interface()
    26 		mv := mt.New().Interface()
    27 		t := reflect.TypeOf(mv)
    27 		t := reflect.TypeOf(mv)
    28 		if mv, ok := mv.(unwrapper); ok {
    28 		if mv, ok := mv.(unwrapper); ok {
    29 			t = reflect.TypeOf(mv.protoUnwrap())
    29 			t = reflect.TypeOf(mv.protoUnwrap())
    30 		}
    30 		}
    31 
    31 
    32 		// Check whether the message implements the legacy v1 Message interface.
    32 		// Check whether the message implements the legacy v1 Message interface.
    33 		mz := reflect.Zero(t).Interface()
    33 		mz := reflect.Zero(t).Interface()
    34 		if mz, ok := mz.(piface.MessageV1); ok {
    34 		if mz, ok := mz.(protoiface.MessageV1); ok {
    35 			parent = mz
    35 			parent = mz
    36 		}
    36 		}
    37 	}
    37 	}
    38 
    38 
    39 	// Determine the v1 extension type, which is unfortunately not the same as
    39 	// Determine the v1 extension type, which is unfortunately not the same as
    44 		extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields
    44 		extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields
    45 	}
    45 	}
    46 
    46 
    47 	// Reconstruct the legacy enum full name.
    47 	// Reconstruct the legacy enum full name.
    48 	var enumName string
    48 	var enumName string
    49 	if xd.Kind() == pref.EnumKind {
    49 	if xd.Kind() == protoreflect.EnumKind {
    50 		enumName = legacyEnumName(xd.Enum())
    50 		enumName = legacyEnumName(xd.Enum())
    51 	}
    51 	}
    52 
    52 
    53 	// Derive the proto file that the extension was declared within.
    53 	// Derive the proto file that the extension was declared within.
    54 	var filename string
    54 	var filename string
    75 func (xi *ExtensionInfo) initFromLegacy() {
    75 func (xi *ExtensionInfo) initFromLegacy() {
    76 	// The v1 API returns "type incomplete" descriptors where only the
    76 	// The v1 API returns "type incomplete" descriptors where only the
    77 	// field number is specified. In such a case, use a placeholder.
    77 	// field number is specified. In such a case, use a placeholder.
    78 	if xi.ExtendedType == nil || xi.ExtensionType == nil {
    78 	if xi.ExtendedType == nil || xi.ExtensionType == nil {
    79 		xd := placeholderExtension{
    79 		xd := placeholderExtension{
    80 			name:   pref.FullName(xi.Name),
    80 			name:   protoreflect.FullName(xi.Name),
    81 			number: pref.FieldNumber(xi.Field),
    81 			number: protoreflect.FieldNumber(xi.Field),
    82 		}
    82 		}
    83 		xi.desc = extensionTypeDescriptor{xd, xi}
    83 		xi.desc = extensionTypeDescriptor{xd, xi}
    84 		return
    84 		return
    85 	}
    85 	}
    86 
    86 
    87 	// Resolve enum or message dependencies.
    87 	// Resolve enum or message dependencies.
    88 	var ed pref.EnumDescriptor
    88 	var ed protoreflect.EnumDescriptor
    89 	var md pref.MessageDescriptor
    89 	var md protoreflect.MessageDescriptor
    90 	t := reflect.TypeOf(xi.ExtensionType)
    90 	t := reflect.TypeOf(xi.ExtensionType)
    91 	isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
    91 	isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
    92 	isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
    92 	isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
    93 	if isOptional || isRepeated {
    93 	if isOptional || isRepeated {
    94 		t = t.Elem()
    94 		t = t.Elem()
    95 	}
    95 	}
    96 	switch v := reflect.Zero(t).Interface().(type) {
    96 	switch v := reflect.Zero(t).Interface().(type) {
    97 	case pref.Enum:
    97 	case protoreflect.Enum:
    98 		ed = v.Descriptor()
    98 		ed = v.Descriptor()
    99 	case enumV1:
    99 	case enumV1:
   100 		ed = LegacyLoadEnumDesc(t)
   100 		ed = LegacyLoadEnumDesc(t)
   101 	case pref.ProtoMessage:
   101 	case protoreflect.ProtoMessage:
   102 		md = v.ProtoReflect().Descriptor()
   102 		md = v.ProtoReflect().Descriptor()
   103 	case messageV1:
   103 	case messageV1:
   104 		md = LegacyLoadMessageDesc(t)
   104 		md = LegacyLoadMessageDesc(t)
   105 	}
   105 	}
   106 
   106 
   107 	// Derive basic field information from the struct tag.
   107 	// Derive basic field information from the struct tag.
   108 	var evs pref.EnumValueDescriptors
   108 	var evs protoreflect.EnumValueDescriptors
   109 	if ed != nil {
   109 	if ed != nil {
   110 		evs = ed.Values()
   110 		evs = ed.Values()
   111 	}
   111 	}
   112 	fd := ptag.Unmarshal(xi.Tag, t, evs).(*filedesc.Field)
   112 	fd := ptag.Unmarshal(xi.Tag, t, evs).(*filedesc.Field)
   113 
   113 
   114 	// Construct a v2 ExtensionType.
   114 	// Construct a v2 ExtensionType.
   115 	xd := &filedesc.Extension{L2: new(filedesc.ExtensionL2)}
   115 	xd := &filedesc.Extension{L2: new(filedesc.ExtensionL2)}
   116 	xd.L0.ParentFile = filedesc.SurrogateProto2
   116 	xd.L0.ParentFile = filedesc.SurrogateProto2
   117 	xd.L0.FullName = pref.FullName(xi.Name)
   117 	xd.L0.FullName = protoreflect.FullName(xi.Name)
   118 	xd.L1.Number = pref.FieldNumber(xi.Field)
   118 	xd.L1.Number = protoreflect.FieldNumber(xi.Field)
   119 	xd.L1.Cardinality = fd.L1.Cardinality
   119 	xd.L1.Cardinality = fd.L1.Cardinality
   120 	xd.L1.Kind = fd.L1.Kind
   120 	xd.L1.Kind = fd.L1.Kind
   121 	xd.L2.IsPacked = fd.L1.IsPacked
   121 	xd.L2.IsPacked = fd.L1.IsPacked
   122 	xd.L2.Default = fd.L1.Default
   122 	xd.L2.Default = fd.L1.Default
   123 	xd.L1.Extendee = Export{}.MessageDescriptorOf(xi.ExtendedType)
   123 	xd.L1.Extendee = Export{}.MessageDescriptorOf(xi.ExtendedType)
   136 	xi.goType = tt
   136 	xi.goType = tt
   137 	xi.desc = extensionTypeDescriptor{xd, xi}
   137 	xi.desc = extensionTypeDescriptor{xd, xi}
   138 }
   138 }
   139 
   139 
   140 type placeholderExtension struct {
   140 type placeholderExtension struct {
   141 	name   pref.FullName
   141 	name   protoreflect.FullName
   142 	number pref.FieldNumber
   142 	number protoreflect.FieldNumber
   143 }
   143 }
   144 
   144 
   145 func (x placeholderExtension) ParentFile() pref.FileDescriptor            { return nil }
   145 func (x placeholderExtension) ParentFile() protoreflect.FileDescriptor            { return nil }
   146 func (x placeholderExtension) Parent() pref.Descriptor                    { return nil }
   146 func (x placeholderExtension) Parent() protoreflect.Descriptor                    { return nil }
   147 func (x placeholderExtension) Index() int                                 { return 0 }
   147 func (x placeholderExtension) Index() int                                         { return 0 }
   148 func (x placeholderExtension) Syntax() pref.Syntax                        { return 0 }
   148 func (x placeholderExtension) Syntax() protoreflect.Syntax                        { return 0 }
   149 func (x placeholderExtension) Name() pref.Name                            { return x.name.Name() }
   149 func (x placeholderExtension) Name() protoreflect.Name                            { return x.name.Name() }
   150 func (x placeholderExtension) FullName() pref.FullName                    { return x.name }
   150 func (x placeholderExtension) FullName() protoreflect.FullName                    { return x.name }
   151 func (x placeholderExtension) IsPlaceholder() bool                        { return true }
   151 func (x placeholderExtension) IsPlaceholder() bool                                { return true }
   152 func (x placeholderExtension) Options() pref.ProtoMessage                 { return descopts.Field }
   152 func (x placeholderExtension) Options() protoreflect.ProtoMessage                 { return descopts.Field }
   153 func (x placeholderExtension) Number() pref.FieldNumber                   { return x.number }
   153 func (x placeholderExtension) Number() protoreflect.FieldNumber                   { return x.number }
   154 func (x placeholderExtension) Cardinality() pref.Cardinality              { return 0 }
   154 func (x placeholderExtension) Cardinality() protoreflect.Cardinality              { return 0 }
   155 func (x placeholderExtension) Kind() pref.Kind                            { return 0 }
   155 func (x placeholderExtension) Kind() protoreflect.Kind                            { return 0 }
   156 func (x placeholderExtension) HasJSONName() bool                          { return false }
   156 func (x placeholderExtension) HasJSONName() bool                                  { return false }
   157 func (x placeholderExtension) JSONName() string                           { return "[" + string(x.name) + "]" }
   157 func (x placeholderExtension) JSONName() string                                   { return "[" + string(x.name) + "]" }
   158 func (x placeholderExtension) TextName() string                           { return "[" + string(x.name) + "]" }
   158 func (x placeholderExtension) TextName() string                                   { return "[" + string(x.name) + "]" }
   159 func (x placeholderExtension) HasPresence() bool                          { return false }
   159 func (x placeholderExtension) HasPresence() bool                                  { return false }
   160 func (x placeholderExtension) HasOptionalKeyword() bool                   { return false }
   160 func (x placeholderExtension) HasOptionalKeyword() bool                           { return false }
   161 func (x placeholderExtension) IsExtension() bool                          { return true }
   161 func (x placeholderExtension) IsExtension() bool                                  { return true }
   162 func (x placeholderExtension) IsWeak() bool                               { return false }
   162 func (x placeholderExtension) IsWeak() bool                                       { return false }
   163 func (x placeholderExtension) IsPacked() bool                             { return false }
   163 func (x placeholderExtension) IsPacked() bool                                     { return false }
   164 func (x placeholderExtension) IsList() bool                               { return false }
   164 func (x placeholderExtension) IsList() bool                                       { return false }
   165 func (x placeholderExtension) IsMap() bool                                { return false }
   165 func (x placeholderExtension) IsMap() bool                                        { return false }
   166 func (x placeholderExtension) MapKey() pref.FieldDescriptor               { return nil }
   166 func (x placeholderExtension) MapKey() protoreflect.FieldDescriptor               { return nil }
   167 func (x placeholderExtension) MapValue() pref.FieldDescriptor             { return nil }
   167 func (x placeholderExtension) MapValue() protoreflect.FieldDescriptor             { return nil }
   168 func (x placeholderExtension) HasDefault() bool                           { return false }
   168 func (x placeholderExtension) HasDefault() bool                                   { return false }
   169 func (x placeholderExtension) Default() pref.Value                        { return pref.Value{} }
   169 func (x placeholderExtension) Default() protoreflect.Value                        { return protoreflect.Value{} }
   170 func (x placeholderExtension) DefaultEnumValue() pref.EnumValueDescriptor { return nil }
   170 func (x placeholderExtension) DefaultEnumValue() protoreflect.EnumValueDescriptor { return nil }
   171 func (x placeholderExtension) ContainingOneof() pref.OneofDescriptor      { return nil }
   171 func (x placeholderExtension) ContainingOneof() protoreflect.OneofDescriptor      { return nil }
   172 func (x placeholderExtension) ContainingMessage() pref.MessageDescriptor  { return nil }
   172 func (x placeholderExtension) ContainingMessage() protoreflect.MessageDescriptor  { return nil }
   173 func (x placeholderExtension) Enum() pref.EnumDescriptor                  { return nil }
   173 func (x placeholderExtension) Enum() protoreflect.EnumDescriptor                  { return nil }
   174 func (x placeholderExtension) Message() pref.MessageDescriptor            { return nil }
   174 func (x placeholderExtension) Message() protoreflect.MessageDescriptor            { return nil }
   175 func (x placeholderExtension) ProtoType(pref.FieldDescriptor)             { return }
   175 func (x placeholderExtension) ProtoType(protoreflect.FieldDescriptor)             { return }
   176 func (x placeholderExtension) ProtoInternal(pragma.DoNotImplement)        { return }
   176 func (x placeholderExtension) ProtoInternal(pragma.DoNotImplement)                { return }