IBuilder.cs 9.0 KB

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