value.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. // TODO: Document the behavior of each Range operation when a mutation
  7. // occurs while ranging. Also document the order.
  8. // Enum is a reflection interface for a concrete enum value,
  9. // which provides type information and a getter for the enum number.
  10. // Enum does not provide a mutable API since enums are commonly backed by
  11. // Go constants, which are not addressable.
  12. type Enum interface {
  13. Type() EnumType
  14. // Number returns the enum value as an integer.
  15. Number() EnumNumber
  16. }
  17. // Message is a reflective interface for a concrete message value,
  18. // which provides type information and getters/setters for individual fields.
  19. //
  20. // Concrete types may implement interfaces defined in proto/protoiface,
  21. // which provide specialized, performant implementations of high-level
  22. // operations such as Marshal and Unmarshal.
  23. type Message interface {
  24. Type() MessageType
  25. // KnownFields returns an interface to access/mutate known fields.
  26. KnownFields() KnownFields
  27. // UnknownFields returns an interface to access/mutate unknown fields.
  28. UnknownFields() UnknownFields
  29. // Interface unwraps the message reflection interface and
  30. // returns the underlying proto.Message interface.
  31. Interface() ProtoMessage
  32. // ProtoMutable is a marker method to implement the Mutable interface.
  33. ProtoMutable()
  34. }
  35. // KnownFields provides accessor and mutator methods for known fields.
  36. //
  37. // Each field Value can either be a scalar, Message, Vector, or Map.
  38. // The field is a Vector or Map if FieldDescriptor.Cardinality is Repeated and
  39. // a Map if and only if FieldDescriptor.IsMap is true. The scalar type or
  40. // underlying repeated element type is determined by the FieldDescriptor.Kind.
  41. // See Value for a list of Go types associated with each Kind.
  42. //
  43. // Field extensions are handled as known fields once the extension type has been
  44. // registered with KnownFields.ExtensionTypes.
  45. //
  46. // List, Len, Has, Get, Range, and ExtensionTypes are safe for concurrent use.
  47. type KnownFields interface {
  48. // List returns a new, unordered list of all populated fields.
  49. List() []FieldNumber
  50. // Len reports the number of fields that are populated.
  51. //
  52. // Invariant: f.Len() == len(f.List())
  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. // It returns the default value for unpopulated fields.
  67. // It returns an invalid value for unknown fields.
  68. Get(FieldNumber) Value
  69. // Set stores the value for a field with the given field number.
  70. // Setting a field belonging to a oneof implicitly clears any other field
  71. // that may be currently set by the same oneof.
  72. //
  73. // When setting a composite type, it is unspecified whether the set
  74. // value aliases the source's memory in any way.
  75. //
  76. // It panics if the field number does not correspond with a known field
  77. // in MessageDescriptor.Fields or an extension field in ExtensionTypes.
  78. Set(FieldNumber, Value)
  79. // TODO: Document memory aliasing behavior when a field is cleared?
  80. // For example, if Mutable is called later, can it reuse memory?
  81. // Clear clears the field such that a subsequent call to Has reports false.
  82. //
  83. // It panics if the field number does not correspond with a known field
  84. // in MessageDescriptor.Fields or an extension field in ExtensionTypes.
  85. Clear(FieldNumber)
  86. // Mutable returns a reference value for a field with a given field number.
  87. // If the field is unset, Mutable implicitly initializes the field with
  88. // a zero value instance of the Go type for that field.
  89. //
  90. // The returned Mutable reference is never nil, and is only valid until the
  91. // next Set or Mutable call.
  92. //
  93. // It panics if the field number does not correspond with a known field
  94. // in MessageDescriptor.Fields or an extension field in ExtensionTypes.
  95. Mutable(FieldNumber) Mutable
  96. // Range calls f sequentially for each known field that is populated.
  97. // If f returns false, range stops the iteration.
  98. Range(f func(FieldNumber, Value) bool)
  99. // ExtensionTypes are extension field types that are known by this
  100. // specific message instance.
  101. ExtensionTypes() ExtensionFieldTypes
  102. }
  103. // UnknownFields are a list of unknown or unparsed fields and may contain
  104. // field numbers corresponding with defined fields or extension fields.
  105. // The ordering of fields is maintained for fields of the same field number.
  106. // However, the relative ordering of fields with different field numbers
  107. // is undefined.
  108. //
  109. // List, Len, Get, and Range are safe for concurrent use.
  110. type UnknownFields interface {
  111. // List returns a new, unordered list of all fields that are set.
  112. List() []FieldNumber
  113. // Len reports the number of fields that are set.
  114. //
  115. // Invariant: f.Len() == len(f.List())
  116. Len() int
  117. // Get retrieves the raw bytes of fields with the given field number.
  118. // It returns an empty RawFields if there are no set fields.
  119. //
  120. // The caller must not mutate the content of the retrieved RawFields.
  121. Get(FieldNumber) RawFields
  122. // Set stores the raw bytes of fields with the given field number.
  123. // The RawFields must be valid and correspond with the given field number;
  124. // an implementation may panic if the fields are invalid.
  125. // An empty RawFields may be passed to clear the fields.
  126. //
  127. // The caller must not mutate the content of the RawFields being stored.
  128. Set(FieldNumber, RawFields)
  129. // Range calls f sequentially for each unknown field that is populated.
  130. // If f returns false, range stops the iteration.
  131. Range(f func(FieldNumber, RawFields) bool)
  132. // TODO: Should IsSupported be renamed as ReadOnly?
  133. // TODO: Should IsSupported panic on Set instead of silently ignore?
  134. // IsSupported reports whether this message supports unknown fields.
  135. // If false, UnknownFields ignores all Set operations.
  136. IsSupported() bool
  137. }
  138. // RawFields is the raw bytes for an ordered sequence of fields.
  139. // Each field contains both the tag (representing field number and wire type),
  140. // and also the wire data itself.
  141. //
  142. // Once stored, the content of a RawFields must be treated as immutable.
  143. // (e.g., raw[:len(raw)] is immutable, but raw[len(raw):cap(raw)] is mutable).
  144. // Thus, appending to RawFields (with valid wire data) is permitted.
  145. type RawFields []byte
  146. // IsValid reports whether RawFields is syntactically correct wire format.
  147. func (b RawFields) IsValid() bool {
  148. for len(b) > 0 {
  149. _, _, n := wire.ConsumeField(b)
  150. if n < 0 {
  151. return false
  152. }
  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. // List, Len, Get, and Range are safe for concurrent use.
  161. type ExtensionFieldTypes interface {
  162. // List returns a new, unordered list of known extension field types.
  163. List() []ExtensionType
  164. // Len reports the number of field extensions.
  165. //
  166. // Invariant: f.Len() == len(f.List())
  167. Len() int
  168. // Register stores an ExtensionType.
  169. // The ExtensionType.ExtendedType must match the containing message type
  170. // and the field number must be within the valid extension ranges
  171. // (see MessageDescriptor.ExtensionRanges).
  172. // It panics if the extension has already been registered (i.e.,
  173. // a conflict by number or by full name).
  174. Register(ExtensionType)
  175. // Remove removes the ExtensionType.
  176. // It panics if a value for this extension field is still populated.
  177. Remove(ExtensionType)
  178. // ByNumber looks up an extension by field number.
  179. // It returns nil if not found.
  180. ByNumber(FieldNumber) ExtensionType
  181. // ByName looks up an extension field by full name.
  182. // It returns nil if not found.
  183. ByName(FullName) ExtensionType
  184. // Range calls f sequentially for each registered extension field type.
  185. // If f returns false, range stops the iteration.
  186. Range(f func(ExtensionType) bool)
  187. }
  188. // Vector 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 Vector.
  192. //
  193. // Len and Get are safe for concurrent use.
  194. type Vector interface {
  195. // Len reports the number of entries in the Vector.
  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 vector.
  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. // Mutable returns a Mutable reference for the element with a given index.
  211. //
  212. // The returned reference is never nil, and is only valid until the
  213. // next Set, Mutable, Append, MutableAppend, or Truncate call.
  214. Mutable(int) Mutable
  215. // MutableAppend appends a new element and returns a mutable reference.
  216. //
  217. // The returned reference is never nil, and is only valid until the
  218. // next Set, Mutable, Append, MutableAppend, or Truncate call.
  219. MutableAppend() Mutable
  220. // TODO: Should truncate accept two indexes similar to slicing?M
  221. // Truncate truncates the vector to a smaller length.
  222. Truncate(int)
  223. // ProtoMutable is a marker method to implement the Mutable interface.
  224. ProtoMutable()
  225. }
  226. // Map is an unordered, associative map. Only elements within the map
  227. // is considered populated. The entry Value type is determined by the associated
  228. // FieldDescripto.Kind and cannot be a Map or Vector.
  229. //
  230. // List, Len, Has, Get, and Range are safe for concurrent use.
  231. type Map interface {
  232. // List returns an unordered list of keys for all entries in the map.
  233. List() []MapKey
  234. // Len reports the number of elements in the map.
  235. //
  236. // Invariant: f.Len() == len(f.List())
  237. Len() int
  238. // Has reports whether an entry with the given key is in the map.
  239. Has(MapKey) bool
  240. // Get retrieves the value for an entry with the given key.
  241. // It returns an invalid value for non-existent entries.
  242. Get(MapKey) Value
  243. // Set stores the value for an entry with the given key.
  244. //
  245. // When setting a composite type, it is unspecified whether the set
  246. // value aliases the source's memory in any way.
  247. //
  248. // It panics if either the key or value are invalid.
  249. Set(MapKey, Value)
  250. // Clear clears the entry associated with they given key.
  251. Clear(MapKey)
  252. // Mutable returns a Mutable reference for the element with a given key,
  253. // allocating a new entry if necessary.
  254. //
  255. // The returned Mutable reference is never nil, and is only valid until the
  256. // next Set or Mutable call.
  257. Mutable(MapKey) Mutable
  258. // Range calls f sequentially for each key and value present in the map.
  259. // The Value provided is guaranteed to be valid.
  260. // If f returns false, range stops the iteration.
  261. Range(f func(MapKey, Value) bool)
  262. // ProtoMutable is a marker method to implement the Mutable interface.
  263. ProtoMutable()
  264. }
  265. // Mutable is a mutable reference, where mutate operations also affect
  266. // the backing store. Possible Mutable types: Vector, Map, or Message.
  267. type Mutable interface {
  268. ProtoMutable()
  269. }