IBuilder.cs 10 KB

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