IBuilder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. namespace Google.ProtocolBuffers {
  20. /// <summary>
  21. /// Non-generic interface for all members whose signatures don't require knowledge of
  22. /// the type being built. The generic interface extends this one. Some methods return
  23. /// either an IBuilder or an IMessage; in these cases the generic interface redeclares
  24. /// the same method with a type-specific signature. Implementations are encouraged to
  25. /// use explicit interface implemenation for the non-generic form. This mirrors
  26. /// how IEnumerable and IEnumerable&lt;T&gt; work.
  27. /// </summary>
  28. public interface IBuilder {
  29. IBuilder MergeFrom(CodedInputStream codedInputStream, ExtensionRegistry extensionRegistry);
  30. /// <summary>
  31. /// Returns true iff all required fields in the message and all
  32. /// embedded messages are set.
  33. /// </summary>
  34. bool Initialized { get; }
  35. /// <summary>
  36. /// Behaves like the equivalent property in IMessage&lt;T&gt;.
  37. /// The returned map may or may not reflect future changes to the builder.
  38. /// Either way, the returned map is unmodifiable.
  39. /// </summary>
  40. IDictionary<ProtocolBuffers.Descriptors.FieldDescriptor, object> AllFields { get; }
  41. /// <summary>
  42. /// Allows getting and setting of a field.
  43. /// <see cref="IMessage{T}.Item(Descriptors.FieldDescriptor)"/>
  44. /// </summary>
  45. /// <param name="field"></param>
  46. /// <returns></returns>
  47. object this[Descriptors.FieldDescriptor field] { get; set; }
  48. /// <summary>
  49. /// Get the message's type's descriptor.
  50. /// <see cref="IMessage{T}.DescriptorForType"/>
  51. /// </summary>
  52. Descriptors.Descriptor DescriptorForType { get; }
  53. /// <summary>
  54. /// <see cref="IMessage{T}.GetRepeatedFieldCount"/>
  55. /// </summary>
  56. /// <param name="field"></param>
  57. /// <returns></returns>
  58. int GetRepeatedFieldCount(Descriptors.FieldDescriptor field);
  59. /// <summary>
  60. /// Allows getting and setting of a repeated field value.
  61. /// <see cref="IMessage{T}.Item(Descriptors.FieldDescriptor, int)"/>
  62. /// </summary>
  63. object this[Descriptors.FieldDescriptor field, int index] { get; set; }
  64. /// <summary>
  65. /// <see cref="IMessage{T}.HasField"/>
  66. /// </summary>
  67. bool HasField(Descriptors.FieldDescriptor field);
  68. }
  69. /// <summary>
  70. /// Interface implemented by Protocol Message builders.
  71. /// TODO(jonskeet): Consider "SetXXX" methods returning the builder, as well as the properties.
  72. /// </summary>
  73. /// <typeparam name="T">Type of message</typeparam>
  74. public interface IBuilder<T> : IBuilder where T : IMessage<T> {
  75. /// <summary>
  76. /// Resets all fields to their default values.
  77. /// </summary>
  78. IBuilder<T> Clear();
  79. /// <summary>
  80. /// Merge the specified other message into the message being
  81. /// built. Merging occurs as follows. For each field:
  82. /// For singular primitive fields, if the field is set in <paramref name="other"/>,
  83. /// then <paramref name="other"/>'s value overwrites the value in this message.
  84. /// For singular message fields, if the field is set in <paramref name="other"/>,
  85. /// it is merged into the corresponding sub-message of this message using the same
  86. /// merging rules.
  87. /// For repeated fields, the elements in <paramref name="other"/> are concatenated
  88. /// with the elements in this message.
  89. /// </summary>
  90. /// <param name="other"></param>
  91. /// <returns></returns>
  92. IBuilder<T> MergeFrom(IMessage<T> other);
  93. /// <summary>
  94. /// Constructs the final message. Once this is called, this Builder instance
  95. /// is no longer valid, and calling any other method may throw a
  96. /// NullReferenceException. If you need to continue working with the builder
  97. /// after calling Build, call Clone first.
  98. /// </summary>
  99. /// <exception cref="UninitializedMessageException">the message
  100. /// is missing one or more required fields; use BuildPartial to bypass
  101. /// this check</exception>
  102. IMessage<T> Build();
  103. /// <summary>
  104. /// Like Build(), but does not throw an exception if the message is missing
  105. /// required fields. Instead, a partial message is returned.
  106. /// </summary>
  107. /// <returns></returns>
  108. IMessage<T> BuildPartial();
  109. /// <summary>
  110. /// Clones this builder.
  111. /// TODO(jonskeet): Explain depth of clone.
  112. /// </summary>
  113. IBuilder<T> Clone();
  114. /// <summary>
  115. /// Parses a message of this type from the input and merges it with this
  116. /// message, as if using MergeFrom(IMessage&lt;T&gt;).
  117. /// </summary>
  118. /// <remarks>
  119. /// Warning: This does not verify that all required fields are present
  120. /// in the input message. If you call Build() without setting all
  121. /// required fields, it will throw an UninitializedMessageException.
  122. /// There are a few good ways to deal with this:
  123. /// <list>
  124. /// <item>Call Initialized to verify to verify that all required fields are
  125. /// set before building.</item>
  126. /// <item>Parse the message separately using one of the static ParseFrom
  127. /// methods, then use MergeFrom(IMessage&lt;T&gt;) to merge it with
  128. /// this one. ParseFrom will throw an InvalidProtocolBufferException
  129. /// (an IOException) if some required fields are missing.
  130. /// Use BuildPartial to build, which ignores missing required fields.
  131. /// </list>
  132. /// </remarks>
  133. IBuilder<T> MergeFrom(CodedInputStream input);
  134. /// <summary>
  135. /// Like MergeFrom(CodedInputStream), but also parses extensions.
  136. /// The extensions that you want to be able to parse must be registered
  137. /// in <paramref name="extensionRegistry"/>. Extensions not in the registry
  138. /// will be treated as unknown fields.
  139. /// </summary>
  140. new IBuilder<T> MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry);
  141. /// <summary>
  142. /// Get's the message's type's default instance.
  143. /// <see cref="IMessage{T}.DefaultInstanceForType" />
  144. /// </summary>
  145. IMessage<T> DefaultInstanceForType { get; }
  146. /// <summary>
  147. /// Create a builder for messages of the appropriate type for the given field.
  148. /// Messages built with this can then be passed to the various mutation properties
  149. /// and methods.
  150. /// </summary>
  151. /// <typeparam name="TField"></typeparam>
  152. /// <param name="field"></param>
  153. /// <returns></returns>
  154. IBuilder<TField> NewBuilderForField<TField>(Descriptors.FieldDescriptor field) where TField : IMessage<TField>;
  155. /// <summary>
  156. /// Clears the field. This is exactly equivalent to calling the generated
  157. /// Clear method corresponding to the field.
  158. /// </summary>
  159. /// <param name="field"></param>
  160. /// <returns></returns>
  161. IBuilder<T> ClearField(Descriptors.FieldDescriptor field);
  162. /// <summary>
  163. /// Appends the given value as a new element for the specified repeated field.
  164. /// </summary>
  165. /// <exception cref="ArgumentException">the field is not a repeated field,
  166. /// the field does not belong to this builder's type, or the value is
  167. /// of the incorrect type
  168. /// </exception>
  169. IBuilder<T> AddRepeatedField(Descriptors.FieldDescriptor field, object value);
  170. /// <summary>
  171. /// <see cref="IMessage{T}.UnknownFields"/>
  172. /// </summary>
  173. UnknownFieldSet UnknownFields { get; set; }
  174. /// <summary>
  175. /// Merge some unknown fields into the set for this message.
  176. /// </summary>
  177. IBuilder<T> MergeUnknownFields(UnknownFieldSet unknownFields);
  178. #region Convenience methods
  179. // TODO(jonskeet): Implement these as extension methods?
  180. /// <summary>
  181. /// Parse <paramref name="data"/> as a message of this type and merge
  182. /// it with the message being built. This is just a small wrapper around
  183. /// MergeFrom(CodedInputStream).
  184. /// </summary>
  185. IBuilder<T> MergeFrom(ByteString data);
  186. /// <summary>
  187. /// Parse <paramref name="data"/> as a message of this type and merge
  188. /// it with the message being built. This is just a small wrapper around
  189. /// MergeFrom(CodedInputStream, ExtensionRegistry).
  190. /// </summary>
  191. IBuilder<T> MergeFrom(ByteString data, ExtensionRegistry extensionRegistry);
  192. /// <summary>
  193. /// Parse <paramref name="data"/> as a message of this type and merge
  194. /// it with the message being built. This is just a small wrapper around
  195. /// MergeFrom(CodedInputStream).
  196. /// </summary>
  197. IBuilder<T> MergeFrom(byte[] data);
  198. /// <summary>
  199. /// Parse <paramref name="data"/> as a message of this type and merge
  200. /// it with the message being built. This is just a small wrapper around
  201. /// MergeFrom(CodedInputStream, ExtensionRegistry).
  202. /// </summary>
  203. IBuilder<T> MergeFrom(byte[] data, ExtensionRegistry extensionRegistry);
  204. /// <summary>
  205. /// Parse <paramref name="data"/> as a message of this type and merge
  206. /// it with the message being built. This is just a small wrapper around
  207. /// MergeFrom(CodedInputStream). Note that this method always reads
  208. /// the entire input (unless it throws an exception). If you want it to
  209. /// stop earlier, you will need to wrap the input in a wrapper
  210. /// stream which limits reading. Despite usually reading the entire
  211. /// stream, this method never closes the stream.
  212. /// </summary>
  213. IBuilder<T> MergeFrom(Stream input);
  214. /// <summary>
  215. /// Parse <paramref name="data"/> as a message of this type and merge
  216. /// it with the message being built. This is just a small wrapper around
  217. /// MergeFrom(CodedInputStream, ExtensionRegistry).
  218. /// </summary>
  219. IBuilder<T> MergeFrom(Stream input, ExtensionRegistry extensionRegistry);
  220. #endregion
  221. }
  222. }