|
|
@@ -52,14 +52,99 @@ type ExtensionRange struct {
|
|
|
Start, End int32 // both inclusive
|
|
|
}
|
|
|
|
|
|
-// extendableProto is an interface implemented by any protocol buffer that may be extended.
|
|
|
+// extendableProto is an interface implemented by any protocol buffer generated by the current
|
|
|
+// proto compiler that may be extended.
|
|
|
type extendableProto interface {
|
|
|
+ Message
|
|
|
+ ExtensionRangeArray() []ExtensionRange
|
|
|
+ extensionsWrite() map[int32]Extension
|
|
|
+ extensionsRead() (map[int32]Extension, sync.Locker)
|
|
|
+}
|
|
|
+
|
|
|
+// extendableProtoV1 is an interface implemented by a protocol buffer generated by the previous
|
|
|
+// version of the proto compiler that may be extended.
|
|
|
+type extendableProtoV1 interface {
|
|
|
Message
|
|
|
ExtensionRangeArray() []ExtensionRange
|
|
|
ExtensionMap() map[int32]Extension
|
|
|
}
|
|
|
|
|
|
+// extensionAdapter is a wrapper around extendableProtoV1 that implements extendableProto.
|
|
|
+type extensionAdapter struct {
|
|
|
+ extendableProtoV1
|
|
|
+}
|
|
|
+
|
|
|
+func (e extensionAdapter) extensionsWrite() map[int32]Extension {
|
|
|
+ return e.ExtensionMap()
|
|
|
+}
|
|
|
+
|
|
|
+func (e extensionAdapter) extensionsRead() (map[int32]Extension, sync.Locker) {
|
|
|
+ return e.ExtensionMap(), notLocker{}
|
|
|
+}
|
|
|
+
|
|
|
+// notLocker is a sync.Locker whose Lock and Unlock methods are nops.
|
|
|
+type notLocker struct{}
|
|
|
+
|
|
|
+func (n notLocker) Lock() {}
|
|
|
+func (n notLocker) Unlock() {}
|
|
|
+
|
|
|
+// extendable returns the extendableProto interface for the given generated proto message.
|
|
|
+// If the proto message has the old extension format, it returns a wrapper that implements
|
|
|
+// the extendableProto interface.
|
|
|
+func extendable(p interface{}) (extendableProto, bool) {
|
|
|
+ if ep, ok := p.(extendableProto); ok {
|
|
|
+ return ep, ok
|
|
|
+ }
|
|
|
+ if ep, ok := p.(extendableProtoV1); ok {
|
|
|
+ return extensionAdapter{ep}, ok
|
|
|
+ }
|
|
|
+ return nil, false
|
|
|
+}
|
|
|
+
|
|
|
+// XXX_InternalExtensions is an internal representation of proto extensions.
|
|
|
+//
|
|
|
+// Each generated message struct type embeds an anonymous XXX_InternalExtensions field,
|
|
|
+// thus gaining the unexported 'extensions' method, which can be called only from the proto package.
|
|
|
+//
|
|
|
+// The methods of XXX_InternalExtensions are not concurrency safe in general,
|
|
|
+// but calls to logically read-only methods such as has and get may be executed concurrently.
|
|
|
+type XXX_InternalExtensions struct {
|
|
|
+ // The struct must be indirect so that if a user inadvertently copies a
|
|
|
+ // generated message and its embedded XXX_InternalExtensions, they
|
|
|
+ // avoid the mayhem of a copied mutex.
|
|
|
+ //
|
|
|
+ // The mutex serializes all logically read-only operations to p.extensionMap.
|
|
|
+ // It is up to the client to ensure that write operations to p.extensionMap are
|
|
|
+ // mutually exclusive with other accesses.
|
|
|
+ p *struct {
|
|
|
+ mu sync.Mutex
|
|
|
+ extensionMap map[int32]Extension
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// extensionsWrite returns the extension map, creating it on first use.
|
|
|
+func (e *XXX_InternalExtensions) extensionsWrite() map[int32]Extension {
|
|
|
+ if e.p == nil {
|
|
|
+ e.p = new(struct {
|
|
|
+ mu sync.Mutex
|
|
|
+ extensionMap map[int32]Extension
|
|
|
+ })
|
|
|
+ e.p.extensionMap = make(map[int32]Extension)
|
|
|
+ }
|
|
|
+ return e.p.extensionMap
|
|
|
+}
|
|
|
+
|
|
|
+// extensionsRead returns the extensions map for read-only use. It may be nil.
|
|
|
+// The caller must hold the returned mutex's lock when accessing Elements within the map.
|
|
|
+func (e *XXX_InternalExtensions) extensionsRead() (map[int32]Extension, sync.Locker) {
|
|
|
+ if e.p == nil {
|
|
|
+ return nil, nil
|
|
|
+ }
|
|
|
+ return e.p.extensionMap, &e.p.mu
|
|
|
+}
|
|
|
+
|
|
|
var extendableProtoType = reflect.TypeOf((*extendableProto)(nil)).Elem()
|
|
|
+var extendableProtoV1Type = reflect.TypeOf((*extendableProtoV1)(nil)).Elem()
|
|
|
|
|
|
// ExtensionDesc represents an extension specification.
|
|
|
// Used in generated code from the protocol compiler.
|
|
|
@@ -93,11 +178,12 @@ type Extension struct {
|
|
|
|
|
|
// SetRawExtension is for testing only.
|
|
|
func SetRawExtension(base Message, id int32, b []byte) {
|
|
|
- epb, ok := base.(extendableProto)
|
|
|
+ epb, ok := extendable(base)
|
|
|
if !ok {
|
|
|
return
|
|
|
}
|
|
|
- epb.ExtensionMap()[id] = Extension{enc: b}
|
|
|
+ extmap := epb.extensionsWrite()
|
|
|
+ extmap[id] = Extension{enc: b}
|
|
|
}
|
|
|
|
|
|
// isExtensionField returns true iff the given field number is in an extension range.
|
|
|
@@ -112,8 +198,12 @@ func isExtensionField(pb extendableProto, field int32) bool {
|
|
|
|
|
|
// checkExtensionTypes checks that the given extension is valid for pb.
|
|
|
func checkExtensionTypes(pb extendableProto, extension *ExtensionDesc) error {
|
|
|
+ var pbi interface{} = pb
|
|
|
// Check the extended type.
|
|
|
- if a, b := reflect.TypeOf(pb), reflect.TypeOf(extension.ExtendedType); a != b {
|
|
|
+ if ea, ok := pbi.(extensionAdapter); ok {
|
|
|
+ pbi = ea.extendableProtoV1
|
|
|
+ }
|
|
|
+ if a, b := reflect.TypeOf(pbi), reflect.TypeOf(extension.ExtendedType); a != b {
|
|
|
return errors.New("proto: bad extended type; " + b.String() + " does not extend " + a.String())
|
|
|
}
|
|
|
// Check the range.
|
|
|
@@ -159,8 +249,19 @@ func extensionProperties(ed *ExtensionDesc) *Properties {
|
|
|
return prop
|
|
|
}
|
|
|
|
|
|
-// encodeExtensionMap encodes any unmarshaled (unencoded) extensions in m.
|
|
|
-func encodeExtensionMap(m map[int32]Extension) error {
|
|
|
+// encode encodes any unmarshaled (unencoded) extensions in e.
|
|
|
+func encodeExtensions(e *XXX_InternalExtensions) error {
|
|
|
+ m, mu := e.extensionsRead()
|
|
|
+ if m == nil {
|
|
|
+ return nil // fast path
|
|
|
+ }
|
|
|
+ mu.Lock()
|
|
|
+ defer mu.Unlock()
|
|
|
+ return encodeExtensionsMap(m)
|
|
|
+}
|
|
|
+
|
|
|
+// encode encodes any unmarshaled (unencoded) extensions in e.
|
|
|
+func encodeExtensionsMap(m map[int32]Extension) error {
|
|
|
for k, e := range m {
|
|
|
if e.value == nil || e.desc == nil {
|
|
|
// Extension is only in its encoded form.
|
|
|
@@ -188,7 +289,17 @@ func encodeExtensionMap(m map[int32]Extension) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func sizeExtensionMap(m map[int32]Extension) (n int) {
|
|
|
+func extensionsSize(e *XXX_InternalExtensions) (n int) {
|
|
|
+ m, mu := e.extensionsRead()
|
|
|
+ if m == nil {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ mu.Lock()
|
|
|
+ defer mu.Unlock()
|
|
|
+ return extensionsMapSize(m)
|
|
|
+}
|
|
|
+
|
|
|
+func extensionsMapSize(m map[int32]Extension) (n int) {
|
|
|
for _, e := range m {
|
|
|
if e.value == nil || e.desc == nil {
|
|
|
// Extension is only in its encoded form.
|
|
|
@@ -215,28 +326,35 @@ func sizeExtensionMap(m map[int32]Extension) (n int) {
|
|
|
// HasExtension returns whether the given extension is present in pb.
|
|
|
func HasExtension(pb Message, extension *ExtensionDesc) bool {
|
|
|
// TODO: Check types, field numbers, etc.?
|
|
|
- epb, ok := pb.(extendableProto)
|
|
|
+ epb, ok := extendable(pb)
|
|
|
if !ok {
|
|
|
return false
|
|
|
}
|
|
|
- _, ok = epb.ExtensionMap()[extension.Field]
|
|
|
+ extmap, mu := epb.extensionsRead()
|
|
|
+ if extmap == nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ mu.Lock()
|
|
|
+ _, ok = extmap[extension.Field]
|
|
|
+ mu.Unlock()
|
|
|
return ok
|
|
|
}
|
|
|
|
|
|
// ClearExtension removes the given extension from pb.
|
|
|
func ClearExtension(pb Message, extension *ExtensionDesc) {
|
|
|
- epb, ok := pb.(extendableProto)
|
|
|
+ epb, ok := extendable(pb)
|
|
|
if !ok {
|
|
|
return
|
|
|
}
|
|
|
// TODO: Check types, field numbers, etc.?
|
|
|
- delete(epb.ExtensionMap(), extension.Field)
|
|
|
+ extmap := epb.extensionsWrite()
|
|
|
+ delete(extmap, extension.Field)
|
|
|
}
|
|
|
|
|
|
// GetExtension parses and returns the given extension of pb.
|
|
|
// If the extension is not present and has no default value it returns ErrMissingExtension.
|
|
|
func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
|
|
|
- epb, ok := pb.(extendableProto)
|
|
|
+ epb, ok := extendable(pb)
|
|
|
if !ok {
|
|
|
return nil, errors.New("proto: not an extendable proto")
|
|
|
}
|
|
|
@@ -245,7 +363,12 @@ func GetExtension(pb Message, extension *ExtensionDesc) (interface{}, error) {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- emap := epb.ExtensionMap()
|
|
|
+ emap, mu := epb.extensionsRead()
|
|
|
+ if emap == nil {
|
|
|
+ return defaultExtensionValue(extension)
|
|
|
+ }
|
|
|
+ mu.Lock()
|
|
|
+ defer mu.Unlock()
|
|
|
e, ok := emap[extension.Field]
|
|
|
if !ok {
|
|
|
// defaultExtensionValue returns the default value or
|
|
|
@@ -349,7 +472,7 @@ func decodeExtension(b []byte, extension *ExtensionDesc) (interface{}, error) {
|
|
|
// GetExtensions returns a slice of the extensions present in pb that are also listed in es.
|
|
|
// The returned slice has the same length as es; missing extensions will appear as nil elements.
|
|
|
func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, err error) {
|
|
|
- epb, ok := pb.(extendableProto)
|
|
|
+ epb, ok := extendable(pb)
|
|
|
if !ok {
|
|
|
return nil, errors.New("proto: not an extendable proto")
|
|
|
}
|
|
|
@@ -368,7 +491,7 @@ func GetExtensions(pb Message, es []*ExtensionDesc) (extensions []interface{}, e
|
|
|
|
|
|
// SetExtension sets the specified extension of pb to the specified value.
|
|
|
func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error {
|
|
|
- epb, ok := pb.(extendableProto)
|
|
|
+ epb, ok := extendable(pb)
|
|
|
if !ok {
|
|
|
return errors.New("proto: not an extendable proto")
|
|
|
}
|
|
|
@@ -388,17 +511,18 @@ func SetExtension(pb Message, extension *ExtensionDesc, value interface{}) error
|
|
|
return fmt.Errorf("proto: SetExtension called with nil value of type %T", value)
|
|
|
}
|
|
|
|
|
|
- epb.ExtensionMap()[extension.Field] = Extension{desc: extension, value: value}
|
|
|
+ extmap := epb.extensionsWrite()
|
|
|
+ extmap[extension.Field] = Extension{desc: extension, value: value}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// ClearAllExtensions clears all extensions from pb.
|
|
|
func ClearAllExtensions(pb Message) {
|
|
|
- epb, ok := pb.(extendableProto)
|
|
|
+ epb, ok := extendable(pb)
|
|
|
if !ok {
|
|
|
return
|
|
|
}
|
|
|
- m := epb.ExtensionMap()
|
|
|
+ m := epb.extensionsWrite()
|
|
|
for k := range m {
|
|
|
delete(m, k)
|
|
|
}
|