IBuilder.cs 11 KB

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