xref: /aosp_15_r20/external/golang-protobuf/reflect/protoreflect/value.go (revision 1c12ee1efe575feb122dbf939ff15148a3b3e8f2)
1*1c12ee1eSDan Willemsen// Copyright 2018 The Go Authors. All rights reserved.
2*1c12ee1eSDan Willemsen// Use of this source code is governed by a BSD-style
3*1c12ee1eSDan Willemsen// license that can be found in the LICENSE file.
4*1c12ee1eSDan Willemsen
5*1c12ee1eSDan Willemsenpackage protoreflect
6*1c12ee1eSDan Willemsen
7*1c12ee1eSDan Willemsenimport "google.golang.org/protobuf/encoding/protowire"
8*1c12ee1eSDan Willemsen
9*1c12ee1eSDan Willemsen// Enum is a reflection interface for a concrete enum value,
10*1c12ee1eSDan Willemsen// which provides type information and a getter for the enum number.
11*1c12ee1eSDan Willemsen// Enum does not provide a mutable API since enums are commonly backed by
12*1c12ee1eSDan Willemsen// Go constants, which are not addressable.
13*1c12ee1eSDan Willemsentype Enum interface {
14*1c12ee1eSDan Willemsen	// Descriptor returns enum descriptor, which contains only the protobuf
15*1c12ee1eSDan Willemsen	// type information for the enum.
16*1c12ee1eSDan Willemsen	Descriptor() EnumDescriptor
17*1c12ee1eSDan Willemsen
18*1c12ee1eSDan Willemsen	// Type returns the enum type, which encapsulates both Go and protobuf
19*1c12ee1eSDan Willemsen	// type information. If the Go type information is not needed,
20*1c12ee1eSDan Willemsen	// it is recommended that the enum descriptor be used instead.
21*1c12ee1eSDan Willemsen	Type() EnumType
22*1c12ee1eSDan Willemsen
23*1c12ee1eSDan Willemsen	// Number returns the enum value as an integer.
24*1c12ee1eSDan Willemsen	Number() EnumNumber
25*1c12ee1eSDan Willemsen}
26*1c12ee1eSDan Willemsen
27*1c12ee1eSDan Willemsen// Message is a reflective interface for a concrete message value,
28*1c12ee1eSDan Willemsen// encapsulating both type and value information for the message.
29*1c12ee1eSDan Willemsen//
30*1c12ee1eSDan Willemsen// Accessor/mutators for individual fields are keyed by FieldDescriptor.
31*1c12ee1eSDan Willemsen// For non-extension fields, the descriptor must exactly match the
32*1c12ee1eSDan Willemsen// field known by the parent message.
33*1c12ee1eSDan Willemsen// For extension fields, the descriptor must implement ExtensionTypeDescriptor,
34*1c12ee1eSDan Willemsen// extend the parent message (i.e., have the same message FullName), and
35*1c12ee1eSDan Willemsen// be within the parent's extension range.
36*1c12ee1eSDan Willemsen//
37*1c12ee1eSDan Willemsen// Each field Value can be a scalar or a composite type (Message, List, or Map).
38*1c12ee1eSDan Willemsen// See Value for the Go types associated with a FieldDescriptor.
39*1c12ee1eSDan Willemsen// Providing a Value that is invalid or of an incorrect type panics.
40*1c12ee1eSDan Willemsentype Message interface {
41*1c12ee1eSDan Willemsen	// Descriptor returns message descriptor, which contains only the protobuf
42*1c12ee1eSDan Willemsen	// type information for the message.
43*1c12ee1eSDan Willemsen	Descriptor() MessageDescriptor
44*1c12ee1eSDan Willemsen
45*1c12ee1eSDan Willemsen	// Type returns the message type, which encapsulates both Go and protobuf
46*1c12ee1eSDan Willemsen	// type information. If the Go type information is not needed,
47*1c12ee1eSDan Willemsen	// it is recommended that the message descriptor be used instead.
48*1c12ee1eSDan Willemsen	Type() MessageType
49*1c12ee1eSDan Willemsen
50*1c12ee1eSDan Willemsen	// New returns a newly allocated and mutable empty message.
51*1c12ee1eSDan Willemsen	New() Message
52*1c12ee1eSDan Willemsen
53*1c12ee1eSDan Willemsen	// Interface unwraps the message reflection interface and
54*1c12ee1eSDan Willemsen	// returns the underlying ProtoMessage interface.
55*1c12ee1eSDan Willemsen	Interface() ProtoMessage
56*1c12ee1eSDan Willemsen
57*1c12ee1eSDan Willemsen	// Range iterates over every populated field in an undefined order,
58*1c12ee1eSDan Willemsen	// calling f for each field descriptor and value encountered.
59*1c12ee1eSDan Willemsen	// Range returns immediately if f returns false.
60*1c12ee1eSDan Willemsen	// While iterating, mutating operations may only be performed
61*1c12ee1eSDan Willemsen	// on the current field descriptor.
62*1c12ee1eSDan Willemsen	Range(f func(FieldDescriptor, Value) bool)
63*1c12ee1eSDan Willemsen
64*1c12ee1eSDan Willemsen	// Has reports whether a field is populated.
65*1c12ee1eSDan Willemsen	//
66*1c12ee1eSDan Willemsen	// Some fields have the property of nullability where it is possible to
67*1c12ee1eSDan Willemsen	// distinguish between the default value of a field and whether the field
68*1c12ee1eSDan Willemsen	// was explicitly populated with the default value. Singular message fields,
69*1c12ee1eSDan Willemsen	// member fields of a oneof, and proto2 scalar fields are nullable. Such
70*1c12ee1eSDan Willemsen	// fields are populated only if explicitly set.
71*1c12ee1eSDan Willemsen	//
72*1c12ee1eSDan Willemsen	// In other cases (aside from the nullable cases above),
73*1c12ee1eSDan Willemsen	// a proto3 scalar field is populated if it contains a non-zero value, and
74*1c12ee1eSDan Willemsen	// a repeated field is populated if it is non-empty.
75*1c12ee1eSDan Willemsen	Has(FieldDescriptor) bool
76*1c12ee1eSDan Willemsen
77*1c12ee1eSDan Willemsen	// Clear clears the field such that a subsequent Has call reports false.
78*1c12ee1eSDan Willemsen	//
79*1c12ee1eSDan Willemsen	// Clearing an extension field clears both the extension type and value
80*1c12ee1eSDan Willemsen	// associated with the given field number.
81*1c12ee1eSDan Willemsen	//
82*1c12ee1eSDan Willemsen	// Clear is a mutating operation and unsafe for concurrent use.
83*1c12ee1eSDan Willemsen	Clear(FieldDescriptor)
84*1c12ee1eSDan Willemsen
85*1c12ee1eSDan Willemsen	// Get retrieves the value for a field.
86*1c12ee1eSDan Willemsen	//
87*1c12ee1eSDan Willemsen	// For unpopulated scalars, it returns the default value, where
88*1c12ee1eSDan Willemsen	// the default value of a bytes scalar is guaranteed to be a copy.
89*1c12ee1eSDan Willemsen	// For unpopulated composite types, it returns an empty, read-only view
90*1c12ee1eSDan Willemsen	// of the value; to obtain a mutable reference, use Mutable.
91*1c12ee1eSDan Willemsen	Get(FieldDescriptor) Value
92*1c12ee1eSDan Willemsen
93*1c12ee1eSDan Willemsen	// Set stores the value for a field.
94*1c12ee1eSDan Willemsen	//
95*1c12ee1eSDan Willemsen	// For a field belonging to a oneof, it implicitly clears any other field
96*1c12ee1eSDan Willemsen	// that may be currently set within the same oneof.
97*1c12ee1eSDan Willemsen	// For extension fields, it implicitly stores the provided ExtensionType.
98*1c12ee1eSDan Willemsen	// When setting a composite type, it is unspecified whether the stored value
99*1c12ee1eSDan Willemsen	// aliases the source's memory in any way. If the composite value is an
100*1c12ee1eSDan Willemsen	// empty, read-only value, then it panics.
101*1c12ee1eSDan Willemsen	//
102*1c12ee1eSDan Willemsen	// Set is a mutating operation and unsafe for concurrent use.
103*1c12ee1eSDan Willemsen	Set(FieldDescriptor, Value)
104*1c12ee1eSDan Willemsen
105*1c12ee1eSDan Willemsen	// Mutable returns a mutable reference to a composite type.
106*1c12ee1eSDan Willemsen	//
107*1c12ee1eSDan Willemsen	// If the field is unpopulated, it may allocate a composite value.
108*1c12ee1eSDan Willemsen	// For a field belonging to a oneof, it implicitly clears any other field
109*1c12ee1eSDan Willemsen	// that may be currently set within the same oneof.
110*1c12ee1eSDan Willemsen	// For extension fields, it implicitly stores the provided ExtensionType
111*1c12ee1eSDan Willemsen	// if not already stored.
112*1c12ee1eSDan Willemsen	// It panics if the field does not contain a composite type.
113*1c12ee1eSDan Willemsen	//
114*1c12ee1eSDan Willemsen	// Mutable is a mutating operation and unsafe for concurrent use.
115*1c12ee1eSDan Willemsen	Mutable(FieldDescriptor) Value
116*1c12ee1eSDan Willemsen
117*1c12ee1eSDan Willemsen	// NewField returns a new value that is assignable to the field
118*1c12ee1eSDan Willemsen	// for the given descriptor. For scalars, this returns the default value.
119*1c12ee1eSDan Willemsen	// For lists, maps, and messages, this returns a new, empty, mutable value.
120*1c12ee1eSDan Willemsen	NewField(FieldDescriptor) Value
121*1c12ee1eSDan Willemsen
122*1c12ee1eSDan Willemsen	// WhichOneof reports which field within the oneof is populated,
123*1c12ee1eSDan Willemsen	// returning nil if none are populated.
124*1c12ee1eSDan Willemsen	// It panics if the oneof descriptor does not belong to this message.
125*1c12ee1eSDan Willemsen	WhichOneof(OneofDescriptor) FieldDescriptor
126*1c12ee1eSDan Willemsen
127*1c12ee1eSDan Willemsen	// GetUnknown retrieves the entire list of unknown fields.
128*1c12ee1eSDan Willemsen	// The caller may only mutate the contents of the RawFields
129*1c12ee1eSDan Willemsen	// if the mutated bytes are stored back into the message with SetUnknown.
130*1c12ee1eSDan Willemsen	GetUnknown() RawFields
131*1c12ee1eSDan Willemsen
132*1c12ee1eSDan Willemsen	// SetUnknown stores an entire list of unknown fields.
133*1c12ee1eSDan Willemsen	// The raw fields must be syntactically valid according to the wire format.
134*1c12ee1eSDan Willemsen	// An implementation may panic if this is not the case.
135*1c12ee1eSDan Willemsen	// Once stored, the caller must not mutate the content of the RawFields.
136*1c12ee1eSDan Willemsen	// An empty RawFields may be passed to clear the fields.
137*1c12ee1eSDan Willemsen	//
138*1c12ee1eSDan Willemsen	// SetUnknown is a mutating operation and unsafe for concurrent use.
139*1c12ee1eSDan Willemsen	SetUnknown(RawFields)
140*1c12ee1eSDan Willemsen
141*1c12ee1eSDan Willemsen	// IsValid reports whether the message is valid.
142*1c12ee1eSDan Willemsen	//
143*1c12ee1eSDan Willemsen	// An invalid message is an empty, read-only value.
144*1c12ee1eSDan Willemsen	//
145*1c12ee1eSDan Willemsen	// An invalid message often corresponds to a nil pointer of the concrete
146*1c12ee1eSDan Willemsen	// message type, but the details are implementation dependent.
147*1c12ee1eSDan Willemsen	// Validity is not part of the protobuf data model, and may not
148*1c12ee1eSDan Willemsen	// be preserved in marshaling or other operations.
149*1c12ee1eSDan Willemsen	IsValid() bool
150*1c12ee1eSDan Willemsen
151*1c12ee1eSDan Willemsen	// ProtoMethods returns optional fast-path implementations of various operations.
152*1c12ee1eSDan Willemsen	// This method may return nil.
153*1c12ee1eSDan Willemsen	//
154*1c12ee1eSDan Willemsen	// The returned methods type is identical to
155*1c12ee1eSDan Willemsen	// "google.golang.org/protobuf/runtime/protoiface".Methods.
156*1c12ee1eSDan Willemsen	// Consult the protoiface package documentation for details.
157*1c12ee1eSDan Willemsen	ProtoMethods() *methods
158*1c12ee1eSDan Willemsen}
159*1c12ee1eSDan Willemsen
160*1c12ee1eSDan Willemsen// RawFields is the raw bytes for an ordered sequence of fields.
161*1c12ee1eSDan Willemsen// Each field contains both the tag (representing field number and wire type),
162*1c12ee1eSDan Willemsen// and also the wire data itself.
163*1c12ee1eSDan Willemsentype RawFields []byte
164*1c12ee1eSDan Willemsen
165*1c12ee1eSDan Willemsen// IsValid reports whether b is syntactically correct wire format.
166*1c12ee1eSDan Willemsenfunc (b RawFields) IsValid() bool {
167*1c12ee1eSDan Willemsen	for len(b) > 0 {
168*1c12ee1eSDan Willemsen		_, _, n := protowire.ConsumeField(b)
169*1c12ee1eSDan Willemsen		if n < 0 {
170*1c12ee1eSDan Willemsen			return false
171*1c12ee1eSDan Willemsen		}
172*1c12ee1eSDan Willemsen		b = b[n:]
173*1c12ee1eSDan Willemsen	}
174*1c12ee1eSDan Willemsen	return true
175*1c12ee1eSDan Willemsen}
176*1c12ee1eSDan Willemsen
177*1c12ee1eSDan Willemsen// List is a zero-indexed, ordered list.
178*1c12ee1eSDan Willemsen// The element Value type is determined by FieldDescriptor.Kind.
179*1c12ee1eSDan Willemsen// Providing a Value that is invalid or of an incorrect type panics.
180*1c12ee1eSDan Willemsentype List interface {
181*1c12ee1eSDan Willemsen	// Len reports the number of entries in the List.
182*1c12ee1eSDan Willemsen	// Get, Set, and Truncate panic with out of bound indexes.
183*1c12ee1eSDan Willemsen	Len() int
184*1c12ee1eSDan Willemsen
185*1c12ee1eSDan Willemsen	// Get retrieves the value at the given index.
186*1c12ee1eSDan Willemsen	// It never returns an invalid value.
187*1c12ee1eSDan Willemsen	Get(int) Value
188*1c12ee1eSDan Willemsen
189*1c12ee1eSDan Willemsen	// Set stores a value for the given index.
190*1c12ee1eSDan Willemsen	// When setting a composite type, it is unspecified whether the set
191*1c12ee1eSDan Willemsen	// value aliases the source's memory in any way.
192*1c12ee1eSDan Willemsen	//
193*1c12ee1eSDan Willemsen	// Set is a mutating operation and unsafe for concurrent use.
194*1c12ee1eSDan Willemsen	Set(int, Value)
195*1c12ee1eSDan Willemsen
196*1c12ee1eSDan Willemsen	// Append appends the provided value to the end of the list.
197*1c12ee1eSDan Willemsen	// When appending a composite type, it is unspecified whether the appended
198*1c12ee1eSDan Willemsen	// value aliases the source's memory in any way.
199*1c12ee1eSDan Willemsen	//
200*1c12ee1eSDan Willemsen	// Append is a mutating operation and unsafe for concurrent use.
201*1c12ee1eSDan Willemsen	Append(Value)
202*1c12ee1eSDan Willemsen
203*1c12ee1eSDan Willemsen	// AppendMutable appends a new, empty, mutable message value to the end
204*1c12ee1eSDan Willemsen	// of the list and returns it.
205*1c12ee1eSDan Willemsen	// It panics if the list does not contain a message type.
206*1c12ee1eSDan Willemsen	AppendMutable() Value
207*1c12ee1eSDan Willemsen
208*1c12ee1eSDan Willemsen	// Truncate truncates the list to a smaller length.
209*1c12ee1eSDan Willemsen	//
210*1c12ee1eSDan Willemsen	// Truncate is a mutating operation and unsafe for concurrent use.
211*1c12ee1eSDan Willemsen	Truncate(int)
212*1c12ee1eSDan Willemsen
213*1c12ee1eSDan Willemsen	// NewElement returns a new value for a list element.
214*1c12ee1eSDan Willemsen	// For enums, this returns the first enum value.
215*1c12ee1eSDan Willemsen	// For other scalars, this returns the zero value.
216*1c12ee1eSDan Willemsen	// For messages, this returns a new, empty, mutable value.
217*1c12ee1eSDan Willemsen	NewElement() Value
218*1c12ee1eSDan Willemsen
219*1c12ee1eSDan Willemsen	// IsValid reports whether the list is valid.
220*1c12ee1eSDan Willemsen	//
221*1c12ee1eSDan Willemsen	// An invalid list is an empty, read-only value.
222*1c12ee1eSDan Willemsen	//
223*1c12ee1eSDan Willemsen	// Validity is not part of the protobuf data model, and may not
224*1c12ee1eSDan Willemsen	// be preserved in marshaling or other operations.
225*1c12ee1eSDan Willemsen	IsValid() bool
226*1c12ee1eSDan Willemsen}
227*1c12ee1eSDan Willemsen
228*1c12ee1eSDan Willemsen// Map is an unordered, associative map.
229*1c12ee1eSDan Willemsen// The entry MapKey type is determined by FieldDescriptor.MapKey.Kind.
230*1c12ee1eSDan Willemsen// The entry Value type is determined by FieldDescriptor.MapValue.Kind.
231*1c12ee1eSDan Willemsen// Providing a MapKey or Value that is invalid or of an incorrect type panics.
232*1c12ee1eSDan Willemsentype Map interface {
233*1c12ee1eSDan Willemsen	// Len reports the number of elements in the map.
234*1c12ee1eSDan Willemsen	Len() int
235*1c12ee1eSDan Willemsen
236*1c12ee1eSDan Willemsen	// Range iterates over every map entry in an undefined order,
237*1c12ee1eSDan Willemsen	// calling f for each key and value encountered.
238*1c12ee1eSDan Willemsen	// Range calls f Len times unless f returns false, which stops iteration.
239*1c12ee1eSDan Willemsen	// While iterating, mutating operations may only be performed
240*1c12ee1eSDan Willemsen	// on the current map key.
241*1c12ee1eSDan Willemsen	Range(f func(MapKey, Value) bool)
242*1c12ee1eSDan Willemsen
243*1c12ee1eSDan Willemsen	// Has reports whether an entry with the given key is in the map.
244*1c12ee1eSDan Willemsen	Has(MapKey) bool
245*1c12ee1eSDan Willemsen
246*1c12ee1eSDan Willemsen	// Clear clears the entry associated with they given key.
247*1c12ee1eSDan Willemsen	// The operation does nothing if there is no entry associated with the key.
248*1c12ee1eSDan Willemsen	//
249*1c12ee1eSDan Willemsen	// Clear is a mutating operation and unsafe for concurrent use.
250*1c12ee1eSDan Willemsen	Clear(MapKey)
251*1c12ee1eSDan Willemsen
252*1c12ee1eSDan Willemsen	// Get retrieves the value for an entry with the given key.
253*1c12ee1eSDan Willemsen	// It returns an invalid value for non-existent entries.
254*1c12ee1eSDan Willemsen	Get(MapKey) Value
255*1c12ee1eSDan Willemsen
256*1c12ee1eSDan Willemsen	// Set stores the value for an entry with the given key.
257*1c12ee1eSDan Willemsen	// It panics when given a key or value that is invalid or the wrong type.
258*1c12ee1eSDan Willemsen	// When setting a composite type, it is unspecified whether the set
259*1c12ee1eSDan Willemsen	// value aliases the source's memory in any way.
260*1c12ee1eSDan Willemsen	//
261*1c12ee1eSDan Willemsen	// Set is a mutating operation and unsafe for concurrent use.
262*1c12ee1eSDan Willemsen	Set(MapKey, Value)
263*1c12ee1eSDan Willemsen
264*1c12ee1eSDan Willemsen	// Mutable retrieves a mutable reference to the entry for the given key.
265*1c12ee1eSDan Willemsen	// If no entry exists for the key, it creates a new, empty, mutable value
266*1c12ee1eSDan Willemsen	// and stores it as the entry for the key.
267*1c12ee1eSDan Willemsen	// It panics if the map value is not a message.
268*1c12ee1eSDan Willemsen	Mutable(MapKey) Value
269*1c12ee1eSDan Willemsen
270*1c12ee1eSDan Willemsen	// NewValue returns a new value assignable as a map value.
271*1c12ee1eSDan Willemsen	// For enums, this returns the first enum value.
272*1c12ee1eSDan Willemsen	// For other scalars, this returns the zero value.
273*1c12ee1eSDan Willemsen	// For messages, this returns a new, empty, mutable value.
274*1c12ee1eSDan Willemsen	NewValue() Value
275*1c12ee1eSDan Willemsen
276*1c12ee1eSDan Willemsen	// IsValid reports whether the map is valid.
277*1c12ee1eSDan Willemsen	//
278*1c12ee1eSDan Willemsen	// An invalid map is an empty, read-only value.
279*1c12ee1eSDan Willemsen	//
280*1c12ee1eSDan Willemsen	// An invalid message often corresponds to a nil Go map value,
281*1c12ee1eSDan Willemsen	// but the details are implementation dependent.
282*1c12ee1eSDan Willemsen	// Validity is not part of the protobuf data model, and may not
283*1c12ee1eSDan Willemsen	// be preserved in marshaling or other operations.
284*1c12ee1eSDan Willemsen	IsValid() bool
285*1c12ee1eSDan Willemsen}
286