IBuilder.cs 11 KB

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