value.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. // TODO: Remove this.
  13. // Deprecated: Use Descriptor instead.
  14. Type() EnumType
  15. // Number returns the enum value as an integer.
  16. Number() EnumNumber
  17. }
  18. // Message is a reflective interface for a concrete message value,
  19. // which provides type information and getters/setters for individual fields.
  20. //
  21. // Concrete types may implement interfaces defined in proto/protoiface,
  22. // which provide specialized, performant implementations of high-level
  23. // operations such as Marshal and Unmarshal.
  24. type Message interface {
  25. Descriptor() MessageDescriptor
  26. // TODO: Remove this.
  27. // Deprecated: Use Descriptor instead.
  28. Type() MessageType
  29. // KnownFields returns an interface to access/mutate known fields.
  30. KnownFields() KnownFields
  31. // UnknownFields returns an interface to access/mutate unknown fields.
  32. UnknownFields() UnknownFields
  33. // New returns a newly allocated empty message.
  34. New() Message
  35. // Interface unwraps the message reflection interface and
  36. // returns the underlying proto.Message interface.
  37. Interface() ProtoMessage
  38. }
  39. // KnownFields provides accessor and mutator methods for known fields.
  40. //
  41. // Each field Value can either be a scalar, Message, List, or Map.
  42. // The field is a List or Map if FieldDescriptor.Cardinality is Repeated and
  43. // a Map if and only if FieldDescriptor.IsMap is true. The scalar type or
  44. // underlying repeated element type is determined by the FieldDescriptor.Kind.
  45. // See Value for a list of Go types associated with each Kind.
  46. //
  47. // Field extensions are handled as known fields once the extension type has been
  48. // registered with KnownFields.ExtensionTypes.
  49. //
  50. // Len, Has, Get, Range, and ExtensionTypes are safe for concurrent use.
  51. type KnownFields interface {
  52. // Len reports the number of fields that are populated.
  53. Len() int
  54. // Has reports whether a field is populated.
  55. //
  56. // Some fields have the property of nullability where it is possible to
  57. // distinguish between the default value of a field and whether the field
  58. // was explicitly populated with the default value. Only scalars in proto2,
  59. // member fields of a oneof, and singular messages are nullable.
  60. //
  61. // A nullable field is populated only if explicitly set.
  62. // A scalar field in proto3 is populated if it contains a non-zero value.
  63. // A repeated field is populated only if it is non-empty.
  64. Has(FieldNumber) bool
  65. // Get retrieves the value for a field with the given field number.
  66. // If the field is unpopulated, it returns the default value for scalars,
  67. // a mutable empty List for empty repeated fields, a mutable empty Map for
  68. // empty map fields, and an invalid value for message fields.
  69. // If the field is unknown (does not appear in MessageDescriptor.Fields
  70. // or ExtensionFieldTypes), it returns an invalid value.
  71. Get(FieldNumber) Value
  72. // Set stores the value for a field with the given field number.
  73. // Setting a field belonging to a oneof implicitly clears any other field
  74. // that may be currently set by the same oneof.
  75. //
  76. // When setting a composite type, it is unspecified whether the set
  77. // value aliases the source's memory in any way.
  78. //
  79. // It panics if the field number does not correspond with a known field
  80. // in MessageDescriptor.Fields or an extension field in ExtensionTypes.
  81. Set(FieldNumber, Value)
  82. // TODO: Document memory aliasing behavior when a field is cleared?
  83. // For example, if Mutable is called later, can it reuse memory?
  84. // Clear clears the field such that a subsequent call to Has reports false.
  85. // The operation does nothing if the field number does not correspond with
  86. // a known field or extension field.
  87. Clear(FieldNumber)
  88. // WhichOneof reports which field within the named oneof is populated.
  89. // It returns 0 if the oneof does not exist or no fields are populated.
  90. WhichOneof(Name) FieldNumber
  91. // Range iterates over every populated field in an undefined order,
  92. // calling f for each field number and value encountered.
  93. // Range calls f Len times unless f returns false, which stops iteration.
  94. // While iterating, mutating operations through Set, Clear, or Mutable
  95. // may only be performed on the current field number.
  96. Range(f func(FieldNumber, Value) bool)
  97. // NewMessage returns a newly allocated empty message assignable to
  98. // the field of the given number.
  99. // It panics if the field is not a singular message.
  100. NewMessage(FieldNumber) Message
  101. // ExtensionTypes are extension field types that are known by this
  102. // specific message instance.
  103. ExtensionTypes() ExtensionFieldTypes
  104. }
  105. // UnknownFields are a list of unknown or unparsed fields and may contain
  106. // field numbers corresponding with defined fields or extension fields.
  107. // The ordering of fields is maintained for fields of the same field number.
  108. // However, the relative ordering of fields with different field numbers
  109. // is undefined.
  110. //
  111. // Len, Get, and Range are safe for concurrent use.
  112. type UnknownFields interface {
  113. // Len reports the number of fields that are populated.
  114. Len() int
  115. // Get retrieves the raw bytes of fields with the given field number.
  116. // It returns an empty RawFields if there are no populated fields.
  117. //
  118. // The caller must not mutate the content of the retrieved RawFields.
  119. Get(FieldNumber) RawFields
  120. // Set stores the raw bytes of fields with the given field number.
  121. // The RawFields must be valid and correspond with the given field number;
  122. // an implementation may panic if the fields are invalid.
  123. // An empty RawFields may be passed to clear the fields.
  124. //
  125. // The caller must not mutate the content of the RawFields being stored.
  126. Set(FieldNumber, RawFields)
  127. // Range iterates over every populated field in an undefined order,
  128. // calling f for each field number and raw field value encountered.
  129. // Range calls f Len times unless f returns false, which stops iteration.
  130. // While iterating, mutating operations through Set may only be performed
  131. // on the current field number.
  132. //
  133. // While the iteration order is undefined, it is deterministic.
  134. // It is recommended, but not required, that fields be presented
  135. // in the order that they were encountered in the wire data.
  136. Range(f func(FieldNumber, RawFields) bool)
  137. // TODO: Should IsSupported be renamed as ReadOnly?
  138. // TODO: Should IsSupported panic on Set instead of silently ignore?
  139. // IsSupported reports whether this message supports unknown fields.
  140. // If false, UnknownFields ignores all Set operations.
  141. IsSupported() bool
  142. }
  143. // RawFields is the raw bytes for an ordered sequence of fields.
  144. // Each field contains both the tag (representing field number and wire type),
  145. // and also the wire data itself.
  146. //
  147. // Once stored, the content of a RawFields must be treated as immutable.
  148. // The capacity of RawFields may be treated as mutable only for the use-case of
  149. // appending additional data to store back into UnknownFields.
  150. type RawFields []byte
  151. // IsValid reports whether RawFields is syntactically correct wire format.
  152. // All fields must belong to the same field number.
  153. func (b RawFields) IsValid() bool {
  154. var want FieldNumber
  155. for len(b) > 0 {
  156. got, _, n := wire.ConsumeField(b)
  157. if n < 0 || (want > 0 && got != want) {
  158. return false
  159. }
  160. want = got
  161. b = b[n:]
  162. }
  163. return true
  164. }
  165. // ExtensionFieldTypes are the extension field types that this message instance
  166. // has been extended with.
  167. //
  168. // Len, Get, and Range are safe for concurrent use.
  169. type ExtensionFieldTypes interface {
  170. // Len reports the number of field extensions.
  171. Len() int
  172. // Register stores an ExtensionType.
  173. // The ExtensionType.ExtendedType must match the containing message type
  174. // and the field number must be within the valid extension ranges
  175. // (see MessageDescriptor.ExtensionRanges).
  176. // It panics if the extension has already been registered (i.e.,
  177. // a conflict by number or by full name).
  178. Register(ExtensionType)
  179. // Remove removes the ExtensionType.
  180. // It panics if a value for this extension field is still populated.
  181. // The operation does nothing if there is no associated type to remove.
  182. Remove(ExtensionType)
  183. // ByNumber looks up an extension by field number.
  184. // It returns nil if not found.
  185. ByNumber(FieldNumber) ExtensionType
  186. // ByName looks up an extension field by full name.
  187. // It returns nil if not found.
  188. ByName(FullName) ExtensionType
  189. // Range iterates over every registered field in an undefined order,
  190. // calling f for each extension descriptor encountered.
  191. // Range calls f Len times unless f returns false, which stops iteration.
  192. // While iterating, mutating operations through Remove may only
  193. // be performed on the current descriptor.
  194. Range(f func(ExtensionType) bool)
  195. }
  196. // List is an ordered list. Every element is considered populated
  197. // (i.e., Get never provides and Set never accepts invalid Values).
  198. // The element Value type is determined by the associated FieldDescriptor.Kind
  199. // and cannot be a Map or List.
  200. //
  201. // Len and Get are safe for concurrent use.
  202. type List interface {
  203. // Len reports the number of entries in the List.
  204. // Get, Set, Mutable, and Truncate panic with out of bound indexes.
  205. Len() int
  206. // Get retrieves the value at the given index.
  207. Get(int) Value
  208. // Set stores a value for the given index.
  209. //
  210. // When setting a composite type, it is unspecified whether the set
  211. // value aliases the source's memory in any way.
  212. Set(int, Value)
  213. // Append appends the provided value to the end of the list.
  214. //
  215. // When appending a composite type, it is unspecified whether the appended
  216. // value aliases the source's memory in any way.
  217. Append(Value)
  218. // TODO: Should truncate accept two indexes similar to slicing?
  219. // Truncate truncates the list to a smaller length.
  220. Truncate(int)
  221. // NewMessage returns a newly allocated empty message assignable to a list entry.
  222. // It panics if the list entry type is not a message.
  223. NewMessage() Message
  224. }
  225. // Map is an unordered, associative map. Only elements within the map
  226. // is considered populated. The entry Value type is determined by the associated
  227. // FieldDescripto.Kind and cannot be a Map or List.
  228. //
  229. // Len, Has, Get, and Range are safe for concurrent use.
  230. type Map interface {
  231. // Len reports the number of elements in the map.
  232. Len() int
  233. // Has reports whether an entry with the given key is in the map.
  234. Has(MapKey) bool
  235. // Get retrieves the value for an entry with the given key.
  236. // It returns an invalid value for non-existent entries.
  237. Get(MapKey) Value
  238. // Set stores the value for an entry with the given key.
  239. //
  240. // When setting a composite type, it is unspecified whether the set
  241. // value aliases the source's memory in any way.
  242. //
  243. // It panics if either the key or value are invalid.
  244. Set(MapKey, Value)
  245. // Clear clears the entry associated with they given key.
  246. // The operation does nothing if there is no entry associated with the key.
  247. Clear(MapKey)
  248. // Range iterates over every map entry in an undefined order,
  249. // calling f for each key and value encountered.
  250. // Range calls f Len times unless f returns false, which stops iteration.
  251. // While iterating, mutating operations through Set, Clear, or Mutable
  252. // may only be performed on the current map key.
  253. Range(f func(MapKey, Value) bool)
  254. // NewMessage returns a newly allocated empty message assignable to a map value.
  255. // It panics if the map value type is not a message.
  256. NewMessage() Message
  257. }