IBuilder.cs 8.9 KB

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