value.go 12 KB

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