IBuilder.cs 11 KB

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