value.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2018 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.
  4. package protoreflect
  5. import "google.golang.org/protobuf/internal/encoding/wire"
  6. // Enum is a reflection interface for a concrete enum value,
  7. // which provides type information and a getter for the enum number.
  8. // Enum does not provide a mutable API since enums are commonly backed by
  9. // Go constants, which are not addressable.
  10. type Enum interface {
  11. Descriptor() EnumDescriptor
  12. // Number returns the enum value as an integer.
  13. Number() EnumNumber
  14. }
  15. // Message is a reflective interface for a concrete message value,
  16. // encapsulating both type and value information for the message.
  17. //
  18. // Accessor/mutators for individual fields are keyed by FieldDescriptor.
  19. // For non-extension fields, the descriptor must exactly match the
  20. // field known by the parent message.
  21. // For extension fields, the descriptor must implement ExtensionType,
  22. // extend the parent message (i.e., have the same message FullName), and
  23. // be within the parent's extension range.
  24. //
  25. // Each field Value can be a scalar or a composite type (Message, List, or Map).
  26. // See Value for the Go types associated with a FieldDescriptor.
  27. // Providing a Value that is invalid or of an incorrect type panics.
  28. type Message interface {
  29. Descriptor() MessageDescriptor
  30. // New returns a newly allocated and mutable empty message.
  31. New() Message
  32. // Interface unwraps the message reflection interface and
  33. // returns the underlying ProtoMessage interface.
  34. Interface() ProtoMessage
  35. // Range iterates over every populated field in an undefined order,
  36. // calling f for each field descriptor and value encountered.
  37. // Range returns immediately if f returns false.
  38. // While iterating, mutating operations may only be performed
  39. // on the current field descriptor.
  40. Range(f func(FieldDescriptor, Value) bool)
  41. // Has reports whether a field is populated.
  42. //
  43. // Some fields have the property of nullability where it is possible to
  44. // distinguish between the default value of a field and whether the field
  45. // was explicitly populated with the default value. Singular message fields,
  46. // member fields of a oneof, proto2 scalar fields, and extension fields
  47. // are nullable. Such fields are populated only if explicitly set.
  48. //
  49. // In other cases (aside from the nullable cases above),
  50. // a proto3 scalar field is populated if it contains a non-zero value, and
  51. // a repeated field is populated if it is non-empty.
  52. Has(FieldDescriptor) bool
  53. // Clear clears the field such that a subsequent Has call reports false.
  54. //
  55. // Clearing an extension field clears both the extension type and value
  56. // associated with the given field number.
  57. //
  58. // Clear is a mutating operation and unsafe for concurrent use.
  59. Clear(FieldDescriptor)
  60. // Get retrieves the value for a field.
  61. //
  62. // For unpopulated scalars, it returns the default value, where
  63. // the default value of a bytes scalar is guaranteed to be a copy.
  64. // For unpopulated composite types, it returns an empty, read-only view
  65. // of the value; to obtain a mutable reference, use Mutable.
  66. Get(FieldDescriptor) Value
  67. // TODO: Should Set of a empty, read-only value be equivalent to Clear?
  68. // Set stores the value for a field.
  69. //
  70. // For a field belonging to a oneof, it implicitly clears any other field
  71. // that may be currently set within the same oneof.
  72. // For extension fields, it implicitly stores the provided ExtensionType.
  73. // When setting a composite type, it is unspecified whether the stored value
  74. // aliases the source's memory in any way. If the composite value is an
  75. // empty, read-only value, then it panics.
  76. //
  77. // Set is a mutating operation and unsafe for concurrent use.
  78. Set(FieldDescriptor, Value)
  79. // Mutable returns a mutable reference to a composite type.
  80. //
  81. // If the field is unpopulated, it may allocate a composite value.
  82. // For a field belonging to a oneof, it implicitly clears any other field
  83. // that may be currently set within the same oneof.
  84. // For extension fields, it implicitly stores the provided ExtensionType
  85. // if not already stored.
  86. // It panics if the field does not contain a composite type.
  87. //
  88. // Mutable is a mutating operation and unsafe for concurrent use.
  89. Mutable(FieldDescriptor) Value
  90. // NewMessage returns a newly allocated empty message assignable to
  91. // the field of the given descriptor.
  92. // It panics if the field is not a singular message.
  93. NewMessage(FieldDescriptor) Message
  94. // WhichOneof reports which field within the oneof is populated,
  95. // returning nil if none are populated.
  96. // It panics if the oneof descriptor does not belong to this message.
  97. WhichOneof(OneofDescriptor) FieldDescriptor
  98. // GetUnknown retrieves the entire list of unknown fields.
  99. // The caller may only mutate the contents of the RawFields
  100. // if the mutated bytes are stored back into the message with SetUnknown.
  101. GetUnknown() RawFields
  102. // SetUnknown stores an entire list of unknown fields.
  103. // The raw fields must be syntactically valid according to the wire format.
  104. // An implementation may panic if this is not the case.
  105. // Once stored, the caller must not mutate the content of the RawFields.
  106. // An empty RawFields may be passed to clear the fields.
  107. //
  108. // SetUnknown is a mutating operation and unsafe for concurrent use.
  109. SetUnknown(RawFields)
  110. // TODO: Add method to retrieve ExtensionType by FieldNumber?
  111. }
  112. // RawFields is the raw bytes for an ordered sequence of fields.
  113. // Each field contains both the tag (representing field number and wire type),
  114. // and also the wire data itself.
  115. type RawFields []byte
  116. // IsValid reports whether b is syntactically correct wire format.
  117. func (b RawFields) IsValid() bool {
  118. for len(b) > 0 {
  119. _, _, n := wire.ConsumeField(b)
  120. if n < 0 {
  121. return false
  122. }
  123. b = b[n:]
  124. }
  125. return true
  126. }
  127. // List is a zero-indexed, ordered list.
  128. // The element Value type is determined by FieldDescriptor.Kind.
  129. // Providing a Value that is invalid or of an incorrect type panics.
  130. type List interface {
  131. // Len reports the number of entries in the List.
  132. // Get, Set, and Truncate panic with out of bound indexes.
  133. Len() int
  134. // Get retrieves the value at the given index.
  135. // It never returns an invalid value.
  136. Get(int) Value
  137. // Set stores a value for the given index.
  138. // When setting a composite type, it is unspecified whether the set
  139. // value aliases the source's memory in any way.
  140. //
  141. // Set is a mutating operation and unsafe for concurrent use.
  142. Set(int, Value)
  143. // Append appends the provided value to the end of the list.
  144. // When appending a composite type, it is unspecified whether the appended
  145. // value aliases the source's memory in any way.
  146. //
  147. // Append is a mutating operation and unsafe for concurrent use.
  148. Append(Value)
  149. // TODO: Should there be a Mutable and MutableAppend method?
  150. // TODO: Should truncate accept two indexes similar to slicing?
  151. // Truncate truncates the list to a smaller length.
  152. //
  153. // Truncate is a mutating operation and unsafe for concurrent use.
  154. Truncate(int)
  155. // NewMessage returns a newly allocated empty message assignable as a list entry.
  156. // It panics if the list entry type is not a message.
  157. NewMessage() Message
  158. }
  159. // Map is an unordered, associative map.
  160. // The entry MapKey type is determined by FieldDescriptor.MapKey.Kind.
  161. // The entry Value type is determined by FieldDescriptor.MapValue.Kind.
  162. // Providing a MapKey or Value that is invalid or of an incorrect type panics.
  163. type Map interface {
  164. // Len reports the number of elements in the map.
  165. Len() int
  166. // Range iterates over every map entry in an undefined order,
  167. // calling f for each key and value encountered.
  168. // Range calls f Len times unless f returns false, which stops iteration.
  169. // While iterating, mutating operations may only be performed
  170. // on the current map key.
  171. Range(f func(MapKey, Value) bool)
  172. // Has reports whether an entry with the given key is in the map.
  173. Has(MapKey) bool
  174. // Clear clears the entry associated with they given key.
  175. // The operation does nothing if there is no entry associated with the key.
  176. //
  177. // Clear is a mutating operation and unsafe for concurrent use.
  178. Clear(MapKey)
  179. // Get retrieves the value for an entry with the given key.
  180. // It returns an invalid value for non-existent entries.
  181. Get(MapKey) Value
  182. // Set stores the value for an entry with the given key.
  183. // It panics when given a key or value that is invalid or the wrong type.
  184. // When setting a composite type, it is unspecified whether the set
  185. // value aliases the source's memory in any way.
  186. //
  187. // Set is a mutating operation and unsafe for concurrent use.
  188. Set(MapKey, Value)
  189. // TODO: Should there be a Mutable method?
  190. // NewMessage returns a newly allocated empty message assignable as a map value.
  191. // It panics if the map value type is not a message.
  192. NewMessage() Message
  193. }