value.go 9.6 KB

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