1
// Copyright 2019 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
11
"google.golang.org/protobuf/encoding/protowire"
12
"google.golang.org/protobuf/internal/errors"
13
"google.golang.org/protobuf/reflect/protoreflect"
16
type extensionFieldInfo struct {
19
unmarshalNeedsValue bool
21
validation validationInfo
24
func getExtensionFieldInfo(xt protoreflect.ExtensionType) *extensionFieldInfo {
25
if xi, ok := xt.(*ExtensionInfo); ok {
29
// Ideally we'd cache the resulting *extensionFieldInfo so we don't have to
30
// recompute this metadata repeatedly. But without support for something like
31
// weak references, such a cache would pin temporary values (like dynamic
32
// extension types, constructed for the duration of a user request) to the
33
// heap forever, causing memory usage of the cache to grow unbounded.
34
// See discussion in https://github.com/golang/protobuf/issues/1521.
35
return makeExtensionFieldInfo(xt.TypeDescriptor())
38
func makeExtensionFieldInfo(xd protoreflect.ExtensionDescriptor) *extensionFieldInfo {
41
wiretag = protowire.EncodeTag(xd.Number(), wireTypes[xd.Kind()])
43
wiretag = protowire.EncodeTag(xd.Number(), protowire.BytesType)
45
e := &extensionFieldInfo{
47
tagsize: protowire.SizeVarint(wiretag),
48
funcs: encoderFuncsForValue(xd),
50
// Does the unmarshal function need a value passed to it?
51
// This is true for composite types, where we pass in a message, list, or map to fill in,
52
// and for enums, where we pass in a prototype value to specify the concrete enum type.
54
case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.EnumKind:
55
e.unmarshalNeedsValue = true
57
if xd.Cardinality() == protoreflect.Repeated {
58
e.unmarshalNeedsValue = true
64
type lazyExtensionValue struct {
65
atomicOnce uint32 // atomically set if value is valid
67
xi *extensionFieldInfo
68
value protoreflect.Value
70
fn func() protoreflect.Value
73
type ExtensionField struct {
74
typ protoreflect.ExtensionType
76
// value is either the value of GetValue,
77
// or a *lazyExtensionValue that then returns the value of GetValue.
78
value protoreflect.Value
79
lazy *lazyExtensionValue
82
func (f *ExtensionField) appendLazyBytes(xt protoreflect.ExtensionType, xi *extensionFieldInfo, num protowire.Number, wtyp protowire.Type, b []byte) {
84
f.lazy = &lazyExtensionValue{xi: xi}
88
f.lazy.b = protowire.AppendTag(f.lazy.b, num, wtyp)
89
f.lazy.b = append(f.lazy.b, b...)
92
func (f *ExtensionField) canLazy(xt protoreflect.ExtensionType) bool {
96
if f.typ == xt && f.lazy != nil && atomic.LoadUint32(&f.lazy.atomicOnce) == 0 {
102
func (f *ExtensionField) lazyInit() {
104
defer f.lazy.mu.Unlock()
105
if atomic.LoadUint32(&f.lazy.atomicOnce) == 1 {
108
if f.lazy.xi != nil {
116
} else if len(b) >= 2 && b[1] < 128 {
117
tag = uint64(b[0]&0x7f) + uint64(b[1])<<7
121
tag, n = protowire.ConsumeVarint(b)
123
panic(errors.New("bad tag in lazy extension decoding"))
127
num := protowire.Number(tag >> 3)
128
wtyp := protowire.Type(tag & 7)
129
var out unmarshalOutput
131
val, out, err = f.lazy.xi.funcs.unmarshal(b, val, num, wtyp, lazyUnmarshalOptions)
133
panic(errors.New("decode failure in lazy extension decoding: %v", err))
139
f.lazy.value = f.lazy.fn()
144
atomic.StoreUint32(&f.lazy.atomicOnce, 1)
147
// Set sets the type and value of the extension field.
148
// This must not be called concurrently.
149
func (f *ExtensionField) Set(t protoreflect.ExtensionType, v protoreflect.Value) {
155
// SetLazy sets the type and a value that is to be lazily evaluated upon first use.
156
// This must not be called concurrently.
157
func (f *ExtensionField) SetLazy(t protoreflect.ExtensionType, fn func() protoreflect.Value) {
159
f.lazy = &lazyExtensionValue{fn: fn}
162
// Value returns the value of the extension field.
163
// This may be called concurrently.
164
func (f *ExtensionField) Value() protoreflect.Value {
166
if atomic.LoadUint32(&f.lazy.atomicOnce) == 0 {
174
// Type returns the type of the extension field.
175
// This may be called concurrently.
176
func (f ExtensionField) Type() protoreflect.ExtensionType {
180
// IsSet returns whether the extension field is set.
181
// This may be called concurrently.
182
func (f ExtensionField) IsSet() bool {
186
// IsLazy reports whether a field is lazily encoded.
187
// It is exported for testing.
188
func IsLazy(m protoreflect.Message, fd protoreflect.FieldDescriptor) bool {
191
switch m := m.(type) {
195
case *messageReflectWrapper:
201
xd, ok := fd.(protoreflect.ExtensionTypeDescriptor)
206
ext := mi.extensionMap(p)
210
f, ok := (*ext)[int32(fd.Number())]
214
return f.typ == xt && f.lazy != nil && atomic.LoadUint32(&f.lazy.atomicOnce) == 0