Sfoglia il codice sorgente

Add the ability to print a builder (not just a message),
and override ToString in AbstractBuilder.

This fixes issue 73.

Jon Skeet 11 anni fa
parent
commit
8bb0d7288e

+ 7 - 0
src/ProtocolBuffers.Test/MessageTest.cs

@@ -141,6 +141,13 @@ namespace Google.ProtocolBuffers
             Assert.IsTrue(builder.IsInitialized);
         }
 
+        [TestMethod]
+        public void UninitializedBuilderToString()
+        {
+            TestRequired.Builder builder = TestRequired.CreateBuilder().SetA(1);
+            Assert.AreEqual("a: 1\n", builder.ToString());
+        }
+
         [TestMethod]
         public void RequiredForeign()
         {

+ 14 - 0
src/ProtocolBuffers.Test/TextFormatTest.cs

@@ -99,6 +99,20 @@ namespace Google.ProtocolBuffers
                                                 });
         }
 
+        /// <summary>
+        /// Tests that a builder prints the same way as a message.
+        /// </summary>
+        [TestMethod]
+        public void PrintBuilder()
+        {
+            TestUtil.TestInMultipleCultures(() =>
+            {
+                string messageText = TextFormat.PrintToString(TestUtil.GetAllSet());
+                string builderText = TextFormat.PrintToString(TestUtil.GetAllSet().ToBuilder());
+                Assert.AreEqual(messageText, builderText);
+            });
+        }
+
         /// <summary>
         /// Print TestAllExtensions and compare with golden file.
         /// </summary>

+ 13 - 0
src/ProtocolBuffers/AbstractBuilder.cs

@@ -249,5 +249,18 @@ namespace Google.ProtocolBuffers
         }
 
         #endregion
+
+        /// <summary>
+        /// Converts this builder to a string using <see cref="TextFormat" />.
+        /// </summary>
+        /// <remarks>
+        /// This method is not sealed (in the way that it is in <see cref="AbstractMessage{TMessage, TBuilder}" />
+        /// as it was added after earlier releases; some other implementations may already be overriding the
+        /// method.
+        /// </remarks>
+        public override string ToString()
+        {
+            return TextFormat.PrintToString(this);
+        }
     }
 }

+ 26 - 0
src/ProtocolBuffers/TextFormat.cs

@@ -61,6 +61,16 @@ namespace Google.ProtocolBuffers
             Print(message, generator);
         }
 
+        /// <summary>
+        /// Outputs a textual representation of the Protocol Message builder supplied into
+        /// the parameter output.
+        /// </summary>
+        public static void Print(IBuilder builder, TextWriter output)
+        {
+            TextGenerator generator = new TextGenerator(output, "\n");
+            Print(builder, generator);
+        }
+
         /// <summary>
         /// Outputs a textual representation of <paramref name="fields" /> to <paramref name="output"/>.
         /// </summary>
@@ -77,6 +87,13 @@ namespace Google.ProtocolBuffers
             return text.ToString();
         }
 
+        public static string PrintToString(IBuilder builder)
+        {
+            StringWriter text = new StringWriter();
+            Print(builder, text);
+            return text.ToString();
+        }
+
         public static string PrintToString(UnknownFieldSet fields)
         {
             StringWriter text = new StringWriter();
@@ -93,6 +110,15 @@ namespace Google.ProtocolBuffers
             PrintUnknownFields(message.UnknownFields, generator);
         }
 
+        private static void Print(IBuilder message, TextGenerator generator)
+        {
+            foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields)
+            {
+                PrintField(entry.Key, entry.Value, generator);
+            }
+            PrintUnknownFields(message.UnknownFields, generator);
+        }
+
         internal static void PrintField(FieldDescriptor field, object value, TextGenerator generator)
         {
             if (field.IsRepeated)