2018-11-26 22:32:06 -08:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package legacy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2018-12-04 14:00:01 -08:00
|
|
|
"sync"
|
2018-11-26 22:32:06 -08:00
|
|
|
|
|
|
|
ptag "github.com/golang/protobuf/v2/internal/encoding/tag"
|
|
|
|
pimpl "github.com/golang/protobuf/v2/internal/impl"
|
2019-03-13 12:56:39 -07:00
|
|
|
ptype "github.com/golang/protobuf/v2/internal/prototype"
|
2018-12-05 10:35:35 -08:00
|
|
|
pfmt "github.com/golang/protobuf/v2/internal/typefmt"
|
2018-11-26 22:32:06 -08:00
|
|
|
pvalue "github.com/golang/protobuf/v2/internal/value"
|
|
|
|
pref "github.com/golang/protobuf/v2/reflect/protoreflect"
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
piface "github.com/golang/protobuf/v2/runtime/protoiface"
|
2018-11-26 22:32:06 -08:00
|
|
|
)
|
|
|
|
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
// extensionDescKey is a comparable version of protoiface.ExtensionDescV1
|
2018-12-04 14:00:01 -08:00
|
|
|
// suitable for use as a key in a map.
|
|
|
|
type extensionDescKey struct {
|
|
|
|
typeV2 pref.ExtensionType
|
|
|
|
extendedType reflect.Type
|
|
|
|
extensionType reflect.Type
|
|
|
|
field int32
|
|
|
|
name string
|
|
|
|
tag string
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
func extensionDescKeyOf(d *piface.ExtensionDescV1) extensionDescKey {
|
2018-12-04 14:00:01 -08:00
|
|
|
return extensionDescKey{
|
|
|
|
d.Type,
|
|
|
|
reflect.TypeOf(d.ExtendedType),
|
|
|
|
reflect.TypeOf(d.ExtensionType),
|
|
|
|
d.Field, d.Name, d.Tag, d.Filename,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
extensionTypeCache sync.Map // map[extensionDescKey]protoreflect.ExtensionType
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
extensionDescCache sync.Map // map[protoreflect.ExtensionType]*protoiface.ExtensionDescV1
|
2018-12-04 14:00:01 -08:00
|
|
|
)
|
|
|
|
|
2018-12-04 14:06:19 -08:00
|
|
|
// extensionDescFromType converts a v2 protoreflect.ExtensionType to a
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
// protoiface.ExtensionDescV1. The returned ExtensionDesc must not be mutated.
|
|
|
|
func extensionDescFromType(t pref.ExtensionType) *piface.ExtensionDescV1 {
|
2019-03-14 16:08:22 -07:00
|
|
|
// Fast-path: check whether an extension desc is already nested within.
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
if t, ok := t.(interface {
|
|
|
|
ProtoLegacyExtensionDesc() *piface.ExtensionDescV1
|
|
|
|
}); ok {
|
2019-03-14 16:08:22 -07:00
|
|
|
if d := t.ProtoLegacyExtensionDesc(); d != nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 14:00:01 -08:00
|
|
|
// Fast-path: check the cache for whether this ExtensionType has already
|
|
|
|
// been converted to a legacy descriptor.
|
|
|
|
if d, ok := extensionDescCache.Load(t); ok {
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
return d.(*piface.ExtensionDescV1)
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the parent type if possible.
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
var parent piface.MessageV1
|
2018-11-26 22:32:06 -08:00
|
|
|
if mt, ok := t.ExtendedType().(pref.MessageType); ok {
|
|
|
|
// Create a new parent message and unwrap it if possible.
|
2019-01-09 02:57:13 -08:00
|
|
|
mv := mt.New().Interface()
|
2018-11-26 22:32:06 -08:00
|
|
|
t := reflect.TypeOf(mv)
|
|
|
|
if mv, ok := mv.(pvalue.Unwrapper); ok {
|
|
|
|
t = reflect.TypeOf(mv.ProtoUnwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the message implements the legacy v1 Message interface.
|
|
|
|
mz := reflect.Zero(t).Interface()
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
if mz, ok := mz.(piface.MessageV1); ok {
|
2018-11-26 22:32:06 -08:00
|
|
|
parent = mz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the v1 extension type, which is unfortunately not the same as
|
|
|
|
// the v2 ExtensionType.GoType.
|
|
|
|
extType := t.GoType()
|
|
|
|
switch extType.Kind() {
|
|
|
|
case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
|
|
|
|
extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields
|
|
|
|
case reflect.Ptr:
|
|
|
|
if extType.Elem().Kind() == reflect.Slice {
|
|
|
|
extType = extType.Elem() // *[]T -> []T for repeated fields
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconstruct the legacy enum full name, which is an odd mixture of the
|
|
|
|
// proto package name with the Go type name.
|
|
|
|
var enumName string
|
|
|
|
if t.Kind() == pref.EnumKind {
|
|
|
|
// Derive Go type name.
|
|
|
|
// For legacy enums, unwrap the wrapper to get the underlying Go type.
|
|
|
|
et := t.EnumType().(pref.EnumType)
|
|
|
|
var ev interface{} = et.New(0)
|
|
|
|
if u, ok := ev.(pvalue.Unwrapper); ok {
|
|
|
|
ev = u.ProtoUnwrap()
|
|
|
|
}
|
|
|
|
enumName = reflect.TypeOf(ev).Name()
|
|
|
|
|
|
|
|
// Derive the proto package name.
|
|
|
|
// For legacy enums, obtain the proto package from the raw descriptor.
|
|
|
|
var protoPkg string
|
|
|
|
if fd := parentFileDescriptor(et); fd != nil {
|
|
|
|
protoPkg = string(fd.Package())
|
|
|
|
}
|
2018-12-04 14:06:19 -08:00
|
|
|
if ed, ok := ev.(enumV1); ok && protoPkg == "" {
|
2018-11-26 22:32:06 -08:00
|
|
|
b, _ := ed.EnumDescriptor()
|
2019-03-25 14:41:32 -07:00
|
|
|
protoPkg = LoadFileDesc(b).GetPackage()
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if protoPkg != "" {
|
|
|
|
enumName = protoPkg + "." + enumName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive the proto file that the extension was declared within.
|
|
|
|
var filename string
|
|
|
|
if fd := parentFileDescriptor(t); fd != nil {
|
|
|
|
filename = fd.Path()
|
|
|
|
}
|
|
|
|
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
// Construct and return a ExtensionDescV1.
|
|
|
|
d := &piface.ExtensionDescV1{
|
2018-11-26 22:32:06 -08:00
|
|
|
Type: t,
|
|
|
|
ExtendedType: parent,
|
|
|
|
ExtensionType: reflect.Zero(extType).Interface(),
|
|
|
|
Field: int32(t.Number()),
|
|
|
|
Name: string(t.FullName()),
|
|
|
|
Tag: ptag.Marshal(t, enumName),
|
|
|
|
Filename: filename,
|
|
|
|
}
|
2019-03-19 14:14:29 -07:00
|
|
|
if d, ok := extensionDescCache.LoadOrStore(t, d); ok {
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
return d.(*piface.ExtensionDescV1)
|
2019-03-19 14:14:29 -07:00
|
|
|
}
|
2018-12-04 14:00:01 -08:00
|
|
|
return d
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
// extensionTypeFromDesc converts a protoiface.ExtensionDescV1 to a
|
2018-12-04 14:00:01 -08:00
|
|
|
// v2 protoreflect.ExtensionType. The returned descriptor type takes ownership
|
|
|
|
// of the input extension desc. The input must not be mutated so long as the
|
|
|
|
// returned type is still in use.
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
func extensionTypeFromDesc(d *piface.ExtensionDescV1) pref.ExtensionType {
|
2018-12-04 14:00:01 -08:00
|
|
|
// Fast-path: check whether an extension type is already nested within.
|
2018-11-26 22:32:06 -08:00
|
|
|
if d.Type != nil {
|
2018-12-04 14:00:01 -08:00
|
|
|
return d.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-path: check the cache for whether this ExtensionType has already
|
|
|
|
// been converted from a legacy descriptor.
|
|
|
|
dk := extensionDescKeyOf(d)
|
|
|
|
if t, ok := extensionTypeCache.Load(dk); ok {
|
|
|
|
return t.(pref.ExtensionType)
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Derive basic field information from the struct tag.
|
|
|
|
t := reflect.TypeOf(d.ExtensionType)
|
|
|
|
isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
|
|
|
|
isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
|
|
|
|
if isOptional || isRepeated {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
f := ptag.Unmarshal(d.Tag, t)
|
|
|
|
|
|
|
|
// Construct a v2 ExtensionType.
|
|
|
|
conv := pvalue.NewLegacyConverter(t, f.Kind, Export{})
|
|
|
|
xd, err := ptype.NewExtension(&ptype.StandaloneExtension{
|
|
|
|
FullName: pref.FullName(d.Name),
|
|
|
|
Number: pref.FieldNumber(d.Field),
|
|
|
|
Cardinality: f.Cardinality,
|
|
|
|
Kind: f.Kind,
|
|
|
|
Default: f.Default,
|
|
|
|
Options: f.Options,
|
|
|
|
EnumType: conv.EnumType,
|
|
|
|
MessageType: conv.MessageType,
|
2018-12-20 12:56:42 -08:00
|
|
|
ExtendedType: pimpl.Export{}.MessageTypeOf(d.ExtendedType),
|
2018-11-26 22:32:06 -08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-20 12:56:42 -08:00
|
|
|
var zv interface{}
|
|
|
|
switch xd.Kind() {
|
|
|
|
case pref.EnumKind, pref.MessageKind, pref.GroupKind:
|
|
|
|
zv = reflect.Zero(t).Interface()
|
|
|
|
}
|
|
|
|
xt := pimpl.Export{}.ExtensionTypeOf(xd, zv)
|
2018-11-26 22:32:06 -08:00
|
|
|
|
2018-12-04 14:00:01 -08:00
|
|
|
// Cache the conversion for both directions.
|
2019-03-19 14:14:29 -07:00
|
|
|
extensionDescCache.LoadOrStore(xt, d)
|
|
|
|
if xt, ok := extensionTypeCache.LoadOrStore(dk, xt); ok {
|
|
|
|
return xt.(pref.ExtensionType)
|
|
|
|
}
|
2018-12-04 14:00:01 -08:00
|
|
|
return xt
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 14:06:19 -08:00
|
|
|
// extensionTypeOf returns a protoreflect.ExtensionType where the GoType
|
2018-11-26 22:32:06 -08:00
|
|
|
// is the underlying v1 Go type instead of the wrapper types used to present
|
|
|
|
// v1 Go types as if they satisfied the v2 API.
|
|
|
|
//
|
|
|
|
// This function is only valid if xd.Kind is an enum or message.
|
2018-12-04 14:06:19 -08:00
|
|
|
func extensionTypeOf(xd pref.ExtensionDescriptor, t reflect.Type) pref.ExtensionType {
|
2018-11-26 22:32:06 -08:00
|
|
|
// Step 1: Create an ExtensionType where GoType is the wrapper type.
|
|
|
|
conv := pvalue.NewLegacyConverter(t, xd.Kind(), Export{})
|
|
|
|
xt := ptype.GoExtension(xd, conv.EnumType, conv.MessageType)
|
|
|
|
|
|
|
|
// Step 2: Wrap ExtensionType such that GoType presents the legacy Go type.
|
2018-12-04 14:06:19 -08:00
|
|
|
xt2 := &extensionType{ExtensionType: xt}
|
2018-11-26 22:32:06 -08:00
|
|
|
if xd.Cardinality() != pref.Repeated {
|
|
|
|
xt2.typ = t
|
2019-01-09 03:23:55 -08:00
|
|
|
xt2.new = func() pref.Value {
|
|
|
|
return xt.New()
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
xt2.valueOf = func(v interface{}) pref.Value {
|
|
|
|
if reflect.TypeOf(v) != xt2.typ {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
|
|
|
|
}
|
|
|
|
if xd.Kind() == pref.EnumKind {
|
|
|
|
return xt.ValueOf(Export{}.EnumOf(v))
|
|
|
|
} else {
|
2019-03-11 13:45:14 -07:00
|
|
|
return xt.ValueOf(Export{}.MessageOf(v).Interface())
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xt2.interfaceOf = func(v pref.Value) interface{} {
|
|
|
|
return xt.InterfaceOf(v).(pvalue.Unwrapper).ProtoUnwrap()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xt2.typ = reflect.PtrTo(reflect.SliceOf(t))
|
2019-01-09 03:23:55 -08:00
|
|
|
xt2.new = func() pref.Value {
|
|
|
|
v := reflect.New(xt2.typ.Elem()).Interface()
|
|
|
|
return pref.ValueOf(pvalue.ListOf(v, conv))
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
xt2.valueOf = func(v interface{}) pref.Value {
|
|
|
|
if reflect.TypeOf(v) != xt2.typ {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
|
|
|
|
}
|
|
|
|
return pref.ValueOf(pvalue.ListOf(v, conv))
|
|
|
|
}
|
|
|
|
xt2.interfaceOf = func(pv pref.Value) interface{} {
|
|
|
|
v := pv.List().(pvalue.Unwrapper).ProtoUnwrap()
|
|
|
|
if reflect.TypeOf(v) != xt2.typ {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %T, want %v", v, xt2.typ))
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return xt2
|
|
|
|
}
|
|
|
|
|
2018-12-04 14:06:19 -08:00
|
|
|
type extensionType struct {
|
2018-11-26 22:32:06 -08:00
|
|
|
pref.ExtensionType
|
|
|
|
typ reflect.Type
|
2019-01-09 03:23:55 -08:00
|
|
|
new func() pref.Value
|
2018-11-26 22:32:06 -08:00
|
|
|
valueOf func(interface{}) pref.Value
|
|
|
|
interfaceOf func(pref.Value) interface{}
|
|
|
|
}
|
|
|
|
|
2018-12-04 14:06:19 -08:00
|
|
|
func (x *extensionType) GoType() reflect.Type { return x.typ }
|
2019-01-09 03:23:55 -08:00
|
|
|
func (x *extensionType) New() pref.Value { return x.new() }
|
2018-12-04 14:06:19 -08:00
|
|
|
func (x *extensionType) ValueOf(v interface{}) pref.Value { return x.valueOf(v) }
|
|
|
|
func (x *extensionType) InterfaceOf(v pref.Value) interface{} { return x.interfaceOf(v) }
|
2018-12-05 10:35:35 -08:00
|
|
|
func (x *extensionType) Format(s fmt.State, r rune) { pfmt.FormatDesc(s, r, x) }
|