IBuilder.cs 9.5 KB

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