value.go 11 KB

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