internal: simplify ExtensionInfo initialization
This CL:
* Make the meaning of impl/ExtensionInfo.goType consistent. Before,
it was sometimes a T and other times a []T depending on the current
state of initialization. Change it so that it is the constructor's
responsibility to pass in a []T if it is repeated.
* Make internal/filetype responsible for constructing a []T for
repeated extension fields.
* Makes filedesc/Extension.Cardinality one of the eagerly initialized
pieces of information since it is useful to internal/filetype.
* Unify ExtensionInfo.desc and ExtensionInfo.tdesc.ExtensionField,
which held the same information.
* Remove the internal implementation for impl.X.ExtensionDescFromType
since we are dropping support for this from v1.
Change-Id: Ie95c4de66cd674c1d886da4f63b133b7d763c7ef
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/195777
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/extension.go b/internal/impl/extension.go
index 40074fc..a895763 100644
--- a/internal/impl/extension.go
+++ b/internal/impl/extension.go
@@ -26,21 +26,17 @@
// initialized. This is the starting state for an ExtensionInfo
// in legacy generated code.
//
- // extensionInfoDescInit: The desc and tdesc fields have been
- // set, but the descriptor is not otherwise initialized. Legacy
- // exported fields may or may not be set. This is the starting state
- // for an ExtensionInfo in new generated code. Calling the Descriptor
- // method will not trigger lazy initialization, although any other
- // method will.
+ // extensionInfoDescInit: The desc field is set, but other unexported fields
+ // may not be initialized. Legacy exported fields may or may not be set.
+ // This is the starting state for an ExtensionInfo in newly generated code.
//
// extensionInfoFullInit: The ExtensionInfo is fully initialized.
// This state is only entered after lazy initialization is complete.
init uint32
mu sync.Mutex
- desc pref.ExtensionDescriptor
- tdesc extensionTypeDescriptor
goType reflect.Type
+ desc extensionTypeDescriptor
conv Converter
// ExtendedType is a typed nil-pointer to the parent message type that
@@ -91,14 +87,8 @@
)
func InitExtensionInfo(xi *ExtensionInfo, xd pref.ExtensionDescriptor, goType reflect.Type) {
- if xi.desc != nil {
- return
- }
- xi.desc = xd
xi.goType = goType
-
- xi.tdesc.ExtensionDescriptor = xi.desc
- xi.tdesc.xi = xi
+ xi.desc = extensionTypeDescriptor{xd, xi}
xi.init = extensionInfoDescInit
}
@@ -125,14 +115,14 @@
return xi.goType
}
func (xi *ExtensionInfo) TypeDescriptor() pref.ExtensionTypeDescriptor {
- if atomic.LoadUint32(&xi.init) == extensionInfoUninitialized {
+ if atomic.LoadUint32(&xi.init) < extensionInfoDescInit {
xi.lazyInitSlow()
}
- return &xi.tdesc
+ return &xi.desc
}
func (xi *ExtensionInfo) lazyInit() Converter {
- if atomic.LoadUint32(&xi.init) != extensionInfoFullInit {
+ if atomic.LoadUint32(&xi.init) < extensionInfoFullInit {
xi.lazyInitSlow()
}
return xi.conv
@@ -147,19 +137,13 @@
}
defer atomic.StoreUint32(&xi.init, extensionInfoFullInit)
- if xi.desc == nil {
+ if xi.desc.ExtensionDescriptor == nil {
xi.initFromLegacy()
- } else if xi.desc.Cardinality() == pref.Repeated {
- // Cardinality is initialized lazily, so we defer consulting it until here.
- xi.goType = reflect.SliceOf(xi.goType)
}
- xi.conv = NewConverter(xi.goType, xi.desc)
- xi.tdesc.ExtensionDescriptor = xi.desc
- xi.tdesc.xi = xi
-
if xi.ExtensionType == nil {
xi.initToLegacy()
}
+ xi.conv = NewConverter(xi.goType, xi.desc)
}
type extensionTypeDescriptor struct {