value.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/proto/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 reflection interface to 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. // Some fields have the property of nullability where it is possible to
  44. // distinguish between the zero value of a field and whether the field was
  45. // explicitly populated with the zero value. Only scalars in proto2,
  46. // members of a oneof field, and singular messages are nullable.
  47. // In the presence of unset fields, KnownFields.Get does not return defaults;
  48. // use the corresponding FieldDescriptor.DefaultValue for that information.
  49. //
  50. // Field extensions are handled as known fields once the extension type has been
  51. // registered with KnownFields.ExtensionTypes.
  52. //
  53. // List, Len, Get, Range, and ExtensionTypes are safe for concurrent access.
  54. type KnownFields interface {
  55. // List returns a new, unordered list of all fields that are populated.
  56. // A nullable field is populated only if explicitly set.
  57. // A scalar field in proto3 is populated if it contains a non-zero value.
  58. // A repeated field is populated only if it is non-empty.
  59. List() []FieldNumber
  60. // Len reports the number of fields that are populated.
  61. //
  62. // Invariant: f.Len() == len(f.List())
  63. Len() int
  64. // TODO: Should Get return FieldDescriptor.Default if unpopulated instead of
  65. // returning the Null variable? If so, we loose the ability to represent
  66. // nullability in Get and Set calls and also need to add Has and Clear.
  67. // Get retrieves the value for field with the given field number.
  68. // It returns Null for non-existent or nulled fields.
  69. Get(FieldNumber) Value
  70. // TODO: Document memory aliasing behavior when a field is cleared?
  71. // For example, if Mutable is called later, can it reuse memory?
  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. // Null may be used to explicitly clear a field containing a proto2 scalar,
  76. // a member of oneof, or a singular message.
  77. //
  78. // When setting a composite type, it is unspecified whether the set
  79. // value aliases the source's memory in any way.
  80. //
  81. // It panics if the field number does not correspond with a known field
  82. // in MessageDescriptor.Fields or an extension field in ExtensionTypes.
  83. Set(FieldNumber, Value)
  84. // Mutable returns a reference value for a field with a given field number.
  85. // If the field is unset, Mutable implicitly initializes the field with
  86. // a zero value instance of the Go type for that field.
  87. //
  88. // The returned Mutable reference is never nil, and is only valid until the
  89. // next Set or Mutable call.
  90. //
  91. // It panics if FieldNumber does not correspond with a known field
  92. // in MessageDescriptor.Fields or an extension field in ExtensionTypes.
  93. Mutable(FieldNumber) Mutable
  94. // Range calls f sequentially for each known field that is populated.
  95. // If f returns false, range stops the iteration.
  96. Range(f func(FieldNumber, Value) bool)
  97. // ExtensionTypes are extension field types that are known by this
  98. // specific message instance.
  99. ExtensionTypes() ExtensionFieldTypes
  100. }
  101. // UnknownFields are a list of unknown or unparsed fields and may contain
  102. // field numbers corresponding with defined fields or extension fields.
  103. // The ordering of fields is maintained for fields of the same field number.
  104. // However, the relative ordering of fields with different field numbers
  105. // is undefined.
  106. //
  107. // List, Len, Get, and Range are safe for concurrent access.
  108. type UnknownFields interface {
  109. // List returns a new, unordered list of all fields that are set.
  110. List() []FieldNumber
  111. // Len reports the number of fields that are set.
  112. //
  113. // Invariant: f.Len() == len(f.List())
  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 set 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 calls f sequentially for each unknown field that is populated.
  128. // If f returns false, range stops the iteration.
  129. Range(f func(FieldNumber, RawFields) bool)
  130. // TODO: Should IsSupported be renamed as ReadOnly?
  131. // TODO: Should IsSupported panic on Set instead of silently ignore?
  132. // IsSupported reports whether this message supports unknown fields.
  133. // If false, UnknownFields ignores all Set operations.
  134. IsSupported() bool
  135. }
  136. // RawFields is the raw bytes for an ordered sequence of fields.
  137. // Each field contains both the tag (representing field number and wire type),
  138. // and also the wire data itself.
  139. //
  140. // Once stored, the content of a RawFields must be treated as immutable.
  141. // (e.g., raw[:len(raw)] is immutable, but raw[len(raw):cap(raw)] is mutable).
  142. // Thus, appending to RawFields (with valid wire data) is permitted.
  143. type RawFields []byte
  144. // IsValid reports whether RawFields is syntactically correct wire format.
  145. func (b RawFields) IsValid() bool {
  146. for len(b) > 0 {
  147. _, _, n := wire.ConsumeField(b)
  148. if n < 0 {
  149. return false
  150. }
  151. b = b[n:]
  152. }
  153. return true
  154. }
  155. // ExtensionFieldTypes are the extension field types that this message instance
  156. // has been extended with.
  157. //
  158. // List, Len, Get, and Range are safe for concurrent access.
  159. type ExtensionFieldTypes interface {
  160. // List returns a new, unordered list of known extension field types.
  161. List() []ExtensionType
  162. // Len reports the number of field extensions.
  163. //
  164. // Invariant: f.Len() == len(f.List())
  165. Len() int
  166. // Register stores an ExtensionType.
  167. // The ExtensionType.ExtendedType must match the containing message type
  168. // and the field number must be within the valid extension ranges
  169. // (see MessageDescriptor.ExtensionRanges).
  170. // It panics if the extension has already been registered (i.e.,
  171. // a conflict by number or by full name).
  172. Register(ExtensionType)
  173. // Remove removes the ExtensionType.
  174. // It panics if a value for this extension field is still populated.
  175. Remove(ExtensionType)
  176. // ByNumber looks up an extension by field number.
  177. // It returns nil if not found.
  178. ByNumber(FieldNumber) ExtensionType
  179. // ByName looks up an extension field by full name.
  180. // It returns nil if not found.
  181. ByName(FullName) ExtensionType
  182. // Range calls f sequentially for each registered extension field type.
  183. // If f returns false, range stops the iteration.
  184. Range(f func(ExtensionType) bool)
  185. }
  186. // Vector is an ordered list. Every element is always considered populated
  187. // (i.e., Get never provides and Set never accepts Null).
  188. // The element Value type is determined by the associated FieldDescriptor.Kind
  189. // and cannot be a Map or Vector.
  190. //
  191. // Len and Get are safe for concurrent access.
  192. type Vector interface {
  193. // Len reports the number of entries in the Vector.
  194. // Get, Set, Mutable, and Truncate panic with out of bound indexes.
  195. Len() int
  196. // Get retrieves the value at the given index.
  197. Get(int) Value
  198. // Set stores a value for the given index.
  199. //
  200. // When setting a composite type, it is unspecified whether the set
  201. // value aliases the source's memory in any way.
  202. //
  203. // It panics if the value is Null.
  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. //
  210. // It panics if the value is Null.
  211. Append(Value)
  212. // Mutable returns a Mutable reference for the element with a given index.
  213. //
  214. // The returned reference is never nil, and is only valid until the
  215. // next Set, Mutable, Append, MutableAppend, or Truncate call.
  216. Mutable(int) Mutable
  217. // MutableAppend appends a new element and returns a mutable reference.
  218. //
  219. // The returned reference is never nil, and is only valid until the
  220. // next Set, Mutable, Append, MutableAppend, or Truncate call.
  221. MutableAppend() Mutable
  222. // TODO: Should truncate accept two indexes similar to slicing?M
  223. // Truncate truncates the vector to a smaller length.
  224. Truncate(int)
  225. // ProtoMutable is a marker method to implement the Mutable interface.
  226. ProtoMutable()
  227. }
  228. // Map is an unordered, associative map. Only elements within the map
  229. // is considered populated. The entry Value type is determined by the associated
  230. // FieldDescripto.Kind and cannot be a Map or Vector.
  231. //
  232. // List, Len, Get, and Range are safe for concurrent access.
  233. type Map interface {
  234. // List returns an unordered list of keys for all entries in the map.
  235. List() []MapKey
  236. // Len reports the number of elements in the map.
  237. //
  238. // Invariant: f.Len() == len(f.List())
  239. Len() int
  240. // Get retrieves the value for an entry with the given key.
  241. Get(MapKey) Value
  242. // Set stores the value for an entry with the given key.
  243. //
  244. // When setting a composite type, it is unspecified whether the set
  245. // value aliases the source's memory in any way.
  246. //
  247. // It panics if either the key or value are Null.
  248. Set(MapKey, Value)
  249. // Mutable returns a Mutable reference for the element with a given key,
  250. // allocating a new entry if necessary.
  251. //
  252. // The returned Mutable reference is never nil, and is only valid until the
  253. // next Set or Mutable call.
  254. Mutable(MapKey) Mutable
  255. // Range calls f sequentially for each key and value present in the map.
  256. // If f returns false, range stops the iteration.
  257. Range(f func(MapKey, Value) bool)
  258. // ProtoMutable is a marker method to implement the Mutable interface.
  259. ProtoMutable()
  260. }
  261. // Mutable is a mutable reference, where mutate operations also affect
  262. // the backing store. Possible Mutable types: Vector, Map, or Message.
  263. type Mutable interface{ ProtoMutable() }
  264. // Value is a union where only one Go type may be set at a time.
  265. // The Value is used to represent all possible values a field may take.
  266. // The following shows what Go type is used to represent each proto Kind:
  267. //
  268. // +------------+-------------------------------------+
  269. // | Go type | Protobuf kind |
  270. // +------------+-------------------------------------+
  271. // | bool | BoolKind |
  272. // | int32 | Int32Kind, Sint32Kind, Sfixed32Kind |
  273. // | int64 | Int64Kind, Sint64Kind, Sfixed64Kind |
  274. // | uint32 | Uint32Kind, Fixed32Kind |
  275. // | uint64 | Uint64Kind, Fixed64Kind |
  276. // | float32 | FloatKind |
  277. // | float64 | DoubleKind |
  278. // | string | StringKind |
  279. // | []byte | BytesKind |
  280. // | EnumNumber | EnumKind |
  281. // +------------+-------------------------------------+
  282. // | Message | MessageKind, GroupKind |
  283. // | Vector | |
  284. // | Map | |
  285. // +------------+-------------------------------------+
  286. //
  287. // Multiple protobuf Kinds may be represented by a single Go type if the type
  288. // can losslessly represent the information for the proto kind. For example,
  289. // Int64Kind, Sint64Kind, and Sfixed64Kind all represent int64,
  290. // but use different integer encoding methods.
  291. //
  292. // The Vector or Map types are used if the FieldDescriptor.Cardinality of the
  293. // corresponding field is Repeated and a Map if and only if
  294. // FieldDescriptor.IsMap is true.
  295. //
  296. // Converting to/from a Value and a concrete Go value panics on type mismatch.
  297. // For example, ValueOf("hello").Int() panics because this attempts to
  298. // retrieve an int64 from a string.
  299. type Value value
  300. // Null is an unpopulated Value.
  301. //
  302. // Since Value is incomparable, call Value.IsNull instead to test whether
  303. // a Value is empty.
  304. //
  305. // It is equivalent to Value{} or ValueOf(nil).
  306. var Null Value
  307. // MapKey is used to index maps, where the Go type of the MapKey must match
  308. // the specified key Kind (see MessageDescriptor.IsMapEntry).
  309. // The following shows what Go type is used to represent each proto Kind:
  310. //
  311. // +---------+-------------------------------------+
  312. // | Go type | Protobuf kind |
  313. // +---------+-------------------------------------+
  314. // | bool | BoolKind |
  315. // | int32 | Int32Kind, Sint32Kind, Sfixed32Kind |
  316. // | int64 | Int64Kind, Sint64Kind, Sfixed64Kind |
  317. // | uint32 | Uint32Kind, Fixed32Kind |
  318. // | uint64 | Uint64Kind, Fixed64Kind |
  319. // | string | StringKind |
  320. // +---------+-------------------------------------+
  321. //
  322. // A MapKey is constructed and accessed through a Value:
  323. // k := ValueOf("hash").MapKey() // convert string to MapKey
  324. // s := k.String() // convert MapKey to string
  325. //
  326. // The MapKey is a strict subset of valid types used in Value;
  327. // converting a Value to a MapKey with an invalid type panics.
  328. type MapKey value