From f8c151f21e821371c55d1cd02b89ac1b8f50460f Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 3 Jul 2015 11:56:29 +0100 Subject: [PATCH 1/6] Initial implementation of JSON formatting - No parsing - Reflection based, so not hugely efficient - No line breaks or indentation --- .../ProtocolBuffers.Test/JsonFormatterTest.cs | 217 ++++++++ .../ProtocolBuffers.Test.csproj | 1 + .../Descriptors/EnumDescriptor.cs | 1 + .../FieldAccess/IFieldAccessor.cs | 2 + csharp/src/ProtocolBuffers/IMessage.cs | 6 +- csharp/src/ProtocolBuffers/JsonFormatter.cs | 521 ++++++++++++++++++ .../ProtocolBuffers/ProtocolBuffers.csproj | 1 + 7 files changed, 746 insertions(+), 3 deletions(-) create mode 100644 csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs create mode 100644 csharp/src/ProtocolBuffers/JsonFormatter.cs diff --git a/csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs b/csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs new file mode 100644 index 0000000000..5f80a49999 --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs @@ -0,0 +1,217 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Google.Protobuf.TestProtos; +using NUnit.Framework; + +namespace Google.Protobuf +{ + public class JsonFormatterTest + { + [Test] + public void DefaultValues_WhenOmitted() + { + var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: false)); + + Assert.AreEqual("{ }", formatter.Format(new ForeignMessage())); + Assert.AreEqual("{ }", formatter.Format(new TestAllTypes())); + Assert.AreEqual("{ }", formatter.Format(new TestMap())); + } + + [Test] + public void DefaultValues_WhenIncluded() + { + var formatter = new JsonFormatter(new JsonFormatter.Settings(formatDefaultValues: true)); + Assert.AreEqual("{ \"c\": 0 }", formatter.Format(new ForeignMessage())); + } + + [Test] + public void AllSingleFields() + { + var message = new TestAllTypes + { + SingleBool = true, + SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), + SingleDouble = 23.5, + SingleFixed32 = 23, + SingleFixed64 = 1234567890123, + SingleFloat = 12.25f, + SingleForeignEnum = ForeignEnum.FOREIGN_BAR, + SingleForeignMessage = new ForeignMessage { C = 10 }, + SingleImportEnum = ImportEnum.IMPORT_BAZ, + SingleImportMessage = new ImportMessage { D = 20 }, + SingleInt32 = 100, + SingleInt64 = 3210987654321, + SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO, + SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, + SinglePublicImportMessage = new PublicImportMessage { E = 54 }, + SingleSfixed32 = -123, + SingleSfixed64 = -12345678901234, + SingleSint32 = -456, + SingleSint64 = -12345678901235, + SingleString = "test\twith\ttabs", + SingleUint32 = uint.MaxValue, + SingleUint64 = ulong.MaxValue, + }; + var actualText = JsonFormatter.Default.Format(message); + + // Fields in declaration order, which matches numeric order. + var expectedText = "{ " + + "\"singleInt32\": 100, " + + "\"singleInt64\": \"3210987654321\", " + + "\"singleUint32\": 4294967295, " + + "\"singleUint64\": \"18446744073709551615\", " + + "\"singleSint32\": -456, " + + "\"singleSint64\": \"-12345678901235\", " + + "\"singleFixed32\": 23, " + + "\"singleFixed64\": \"1234567890123\", " + + "\"singleSfixed32\": -123, " + + "\"singleSfixed64\": \"-12345678901234\", " + + "\"singleFloat\": 12.25, " + + "\"singleDouble\": 23.5, " + + "\"singleBool\": true, " + + "\"singleString\": \"test\\twith\\ttabs\", " + + "\"singleBytes\": \"AQIDBA==\", " + + "\"singleNestedMessage\": { \"bb\": 35 }, " + + "\"singleForeignMessage\": { \"c\": 10 }, " + + "\"singleImportMessage\": { \"d\": 20 }, " + + "\"singleNestedEnum\": \"FOO\", " + + "\"singleForeignEnum\": \"FOREIGN_BAR\", " + + "\"singleImportEnum\": \"IMPORT_BAZ\", " + + "\"singlePublicImportMessage\": { \"e\": 54 }" + + " }"; + Assert.AreEqual(expectedText, actualText); + } + + [Test] + public void RepeatedField() + { + Assert.AreEqual("{ \"repeatedInt32\": [ 1, 2, 3, 4, 5 ] }", + JsonFormatter.Default.Format(new TestAllTypes { RepeatedInt32 = { 1, 2, 3, 4, 5 } })); + } + + [Test] + public void MapField_StringString() + { + Assert.AreEqual("{ \"mapStringString\": { \"with spaces\": \"bar\", \"a\": \"b\" } }", + JsonFormatter.Default.Format(new TestMap { MapStringString = { { "with spaces", "bar" }, { "a", "b" } } })); + } + + [Test] + public void MapField_Int32Int32() + { + // The keys are quoted, but the values aren't. + Assert.AreEqual("{ \"mapInt32Int32\": { \"0\": 1, \"2\": 3 } }", + JsonFormatter.Default.Format(new TestMap { MapInt32Int32 = { { 0, 1 }, { 2, 3 } } })); + } + + [Test] + public void MapField_BoolBool() + { + // The keys are quoted, but the values aren't. + Assert.AreEqual("{ \"mapBoolBool\": { \"false\": true, \"true\": false } }", + JsonFormatter.Default.Format(new TestMap { MapBoolBool = { { false, true }, { true, false } } })); + } + + [TestCase(1.0, "1")] + [TestCase(double.NaN, "\"NaN\"")] + [TestCase(double.PositiveInfinity, "\"Infinity\"")] + [TestCase(double.NegativeInfinity, "\"-Infinity\"")] + public void DoubleRepresentations(double value, string expectedValueText) + { + var message = new TestAllTypes { SingleDouble = value }; + string actualText = JsonFormatter.Default.Format(message); + string expectedText = "{ \"singleDouble\": " + expectedValueText + " }"; + Assert.AreEqual(expectedText, actualText); + } + + [Test] + public void UnknownEnumValue() + { + var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 }; + Assert.AreEqual("{ \"singleForeignEnum\": 100 }", JsonFormatter.Default.Format(message)); + } + + [Test] + public void NullValueForMessage() + { + var message = new TestMap { MapInt32ForeignMessage = { { 10, null } } }; + Assert.AreEqual("{ \"mapInt32ForeignMessage\": { \"10\": null } }", JsonFormatter.Default.Format(message)); + } + + [Test] + [TestCase("a\u17b4b", "a\\u17b4b")] // Explicit + [TestCase("a\u0601b", "a\\u0601b")] // Ranged + [TestCase("a\u0605b", "a\u0605b")] // Passthrough (note lack of double backslash...) + public void SimpleNonAscii(string text, string encoded) + { + var message = new TestAllTypes { SingleString = text }; + Assert.AreEqual("{ \"singleString\": \"" + encoded + "\" }", JsonFormatter.Default.Format(message)); + } + + [Test] + public void SurrogatePairEscaping() + { + var message = new TestAllTypes { SingleString = "a\uD801\uDC01b" }; + Assert.AreEqual("{ \"singleString\": \"a\\ud801\\udc01b\" }", JsonFormatter.Default.Format(message)); + } + + [Test] + public void InvalidSurrogatePairsFail() + { + // Note: don't use TestCase for these, as the strings can't be reliably represented + // See http://codeblog.jonskeet.uk/2014/11/07/when-is-a-string-not-a-string/ + + // Lone low surrogate + var message = new TestAllTypes { SingleString = "a\uDC01b" }; + Assert.Throws(() => JsonFormatter.Default.Format(message)); + + // Lone high surrogate + message = new TestAllTypes { SingleString = "a\uD801b" }; + Assert.Throws(() => JsonFormatter.Default.Format(message)); + } + + [Test] + [TestCase("foo_bar", "fooBar")] + [TestCase("bananaBanana", "bananaBanana")] + [TestCase("BANANABanana", "bananaBanana")] + public void ToCamelCase(string original, string expected) + { + Assert.AreEqual(expected, JsonFormatter.ToCamelCase(original)); + } + } +} diff --git a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj index b02abe70a4..269961c7d9 100644 --- a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj +++ b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj @@ -80,6 +80,7 @@ + diff --git a/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs b/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs index a6db5268eb..57860e6190 100644 --- a/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs +++ b/csharp/src/ProtocolBuffers/Descriptors/EnumDescriptor.cs @@ -89,6 +89,7 @@ namespace Google.Protobuf.Descriptors /// /// Finds an enum value by number. If multiple enum values have the /// same number, this returns the first defined value with that number. + /// If there is no value for the given number, this returns null. /// public EnumValueDescriptor FindValueByNumber(int number) { diff --git a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs index 77e7146dc9..d1727cb46e 100644 --- a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs +++ b/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs @@ -44,6 +44,8 @@ namespace Google.Protobuf.FieldAccess /// FieldDescriptor Descriptor { get; } + // TODO: Should the argument type for these messages by IReflectedMessage? + /// /// Clears the field in the specified message. (For repeated fields, /// this clears the list.) diff --git a/csharp/src/ProtocolBuffers/IMessage.cs b/csharp/src/ProtocolBuffers/IMessage.cs index ad44668c1b..3324e9ae99 100644 --- a/csharp/src/ProtocolBuffers/IMessage.cs +++ b/csharp/src/ProtocolBuffers/IMessage.cs @@ -40,9 +40,9 @@ namespace Google.Protobuf // TODO(jonskeet): Split these interfaces into separate files when we're happy with them. /// - /// Reflection support for a specific message type. + /// Reflection support for accessing field values. /// - public interface IReflectedMessage + public interface IReflectedMessage : IMessage { FieldAccessorTable Fields { get; } // TODO(jonskeet): Descriptor? Or a single property which has "all you need for reflection"? @@ -81,7 +81,7 @@ namespace Google.Protobuf /// the implementation class. /// /// The message type. - public interface IMessage : IMessage, IEquatable, IDeepCloneable, IFreezable where T : IMessage + public interface IMessage : IReflectedMessage, IEquatable, IDeepCloneable, IFreezable where T : IMessage { /// /// Merges the given message into this one. diff --git a/csharp/src/ProtocolBuffers/JsonFormatter.cs b/csharp/src/ProtocolBuffers/JsonFormatter.cs new file mode 100644 index 0000000000..a6aa552fb3 --- /dev/null +++ b/csharp/src/ProtocolBuffers/JsonFormatter.cs @@ -0,0 +1,521 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +using System; +using System.Collections; +using System.Globalization; +using System.Text; +using Google.Protobuf.Descriptors; +using Google.Protobuf.FieldAccess; + +namespace Google.Protobuf +{ + /// + /// Reflection-based converter from messages to JSON. + /// + /// + /// + /// Instances of this class are thread-safe, with no mutable state. + /// + /// + /// This is a simple start to get JSON formatting working. As it's reflection-based, + /// it's not as quick as baking calls into generated messages - but is a simpler implementation. + /// (This code is generally not heavily optimized.) + /// + /// + public sealed class JsonFormatter + { + private static JsonFormatter defaultInstance = new JsonFormatter(Settings.Default); + + /// + /// Returns a formatter using the default settings. + /// + public static JsonFormatter Default { get { return defaultInstance; } } + + /// + /// The JSON representation of the first 160 characters of Unicode. + /// Empty strings are replaced by the static constructor. + /// + private static readonly string[] CommonRepresentations = { + // C0 (ASCII and derivatives) control characters + "\\u0000", "\\u0001", "\\u0002", "\\u0003", // 0x00 + "\\u0004", "\\u0005", "\\u0006", "\\u0007", + "\\b", "\\t", "\\n", "\\u000b", + "\\f", "\\r", "\\u000e", "\\u000f", + "\\u0010", "\\u0011", "\\u0012", "\\u0013", // 0x10 + "\\u0014", "\\u0015", "\\u0016", "\\u0017", + "\\u0018", "\\u0019", "\\u001a", "\\u001b", + "\\u001c", "\\u001d", "\\u001e", "\\u001f", + // Escaping of " and \ are required by www.json.org string definition. + // Escaping of < and > are required for HTML security. + "", "", "\\\"", "", "", "", "", "", // 0x20 + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", // 0x30 + "", "", "", "", "\\u003c", "", "\\u003e", "", + "", "", "", "", "", "", "", "", // 0x40 + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", // 0x50 + "", "", "", "", "\\\\", "", "", "", + "", "", "", "", "", "", "", "", // 0x60 + "", "", "", "", "", "", "", "", + "", "", "", "", "", "", "", "", // 0x70 + "", "", "", "", "", "", "", "\\u007f", + // C1 (ISO 8859 and Unicode) extended control characters + "\\u0080", "\\u0081", "\\u0082", "\\u0083", // 0x80 + "\\u0084", "\\u0085", "\\u0086", "\\u0087", + "\\u0088", "\\u0089", "\\u008a", "\\u008b", + "\\u008c", "\\u008d", "\\u008e", "\\u008f", + "\\u0090", "\\u0091", "\\u0092", "\\u0093", // 0x90 + "\\u0094", "\\u0095", "\\u0096", "\\u0097", + "\\u0098", "\\u0099", "\\u009a", "\\u009b", + "\\u009c", "\\u009d", "\\u009e", "\\u009f" + }; + + static JsonFormatter() + { + for (int i = 0; i < CommonRepresentations.Length; i++) + { + if (CommonRepresentations[i] == "") + { + CommonRepresentations[i] = ((char) i).ToString(); + } + } + } + + private readonly Settings settings; + + public JsonFormatter(Settings settings) + { + this.settings = settings; + } + + public string Format(IReflectedMessage message) + { + ThrowHelper.ThrowIfNull(message, "message"); + StringBuilder builder = new StringBuilder(); + WriteMessage(builder, message); + return builder.ToString(); + } + + private void WriteMessage(StringBuilder builder, IReflectedMessage message) + { + if (message == null) + { + WriteNull(builder); + return; + } + builder.Append("{ "); + var fields = message.Fields; + bool first = true; + foreach (var accessor in fields.Accessors) + { + object value = accessor.GetValue(message); + if (!settings.FormatDefaultValues && IsDefaultValue(accessor, value)) + { + continue; + } + if (!first) + { + builder.Append(", "); + } + WriteString(builder, ToCamelCase(accessor.Descriptor.Name)); + builder.Append(": "); + WriteValue(builder, accessor, value); + first = false; + } + builder.Append(first ? "}" : " }"); + } + + // Converted from src/google/protobuf/util/internal/utility.cc ToCamelCase + internal static string ToCamelCase(string input) + { + bool capitalizeNext = false; + bool wasCap = true; + bool isCap = false; + bool firstWord = true; + StringBuilder result = new StringBuilder(input.Length); + + for (int i = 0; i < input.Length; i++, wasCap = isCap) + { + isCap = char.IsUpper(input[i]); + if (input[i] == '_') + { + capitalizeNext = true; + if (result.Length != 0) + { + firstWord = false; + } + continue; + } + else if (firstWord) + { + // Consider when the current character B is capitalized, + // first word ends when: + // 1) following a lowercase: "...aB..." + // 2) followed by a lowercase: "...ABc..." + if (result.Length != 0 && isCap && + (!wasCap || (i + 1 < input.Length && char.IsLower(input[i + 1])))) + { + firstWord = false; + } + else + { + result.Append(char.ToLowerInvariant(input[i])); + continue; + } + } + else if (capitalizeNext) + { + capitalizeNext = false; + if (char.IsLower(input[i])) + { + result.Append(char.ToUpperInvariant(input[i])); + continue; + } + } + result.Append(input[i]); + } + return result.ToString(); + } + + private static void WriteNull(StringBuilder builder) + { + builder.Append("null"); + } + + private static bool IsDefaultValue(IFieldAccessor accessor, object value) + { + if (accessor.Descriptor.IsMap) + { + IDictionary dictionary = (IDictionary) value; + return dictionary.Count == 0; + } + if (accessor.Descriptor.IsRepeated) + { + IList list = (IList) value; + return list.Count == 0; + } + switch (accessor.Descriptor.FieldType) + { + case FieldType.Bool: + return (bool) value == false; + case FieldType.Bytes: + return (ByteString) value == ByteString.Empty; + case FieldType.String: + return (string) value == ""; + case FieldType.Double: + return (double) value == 0.0; + case FieldType.SInt32: + case FieldType.Int32: + case FieldType.SFixed32: + case FieldType.Enum: + return (int) value == 0; + case FieldType.Fixed32: + case FieldType.UInt32: + return (uint) value == 0; + case FieldType.Fixed64: + case FieldType.UInt64: + return (ulong) value == 0; + case FieldType.SFixed64: + case FieldType.Int64: + case FieldType.SInt64: + return (long) value == 0; + case FieldType.Float: + return (float) value == 0f; + case FieldType.Message: + case FieldType.Group: // Never expect to get this, but... + return value == null; + default: + throw new ArgumentException("Invalid field type"); + } + } + + private void WriteValue(StringBuilder builder, IFieldAccessor accessor, object value) + { + if (accessor.Descriptor.IsMap) + { + WriteDictionary(builder, accessor, (IDictionary) value); + } + else if (accessor.Descriptor.IsRepeated) + { + WriteList(builder, accessor, (IList) value); + } + else + { + WriteSingleValue(builder, accessor.Descriptor, value); + } + } + + private void WriteSingleValue(StringBuilder builder, FieldDescriptor descriptor, object value) + { + switch (descriptor.FieldType) + { + case FieldType.Bool: + builder.Append((bool) value ? "true" : "false"); + break; + case FieldType.Bytes: + // Nothing in Base64 needs escaping + builder.Append('"'); + builder.Append(((ByteString) value).ToBase64()); + builder.Append('"'); + break; + case FieldType.String: + WriteString(builder, (string) value); + break; + case FieldType.Fixed32: + case FieldType.UInt32: + case FieldType.SInt32: + case FieldType.Int32: + case FieldType.SFixed32: + { + IFormattable formattable = (IFormattable) value; + builder.Append(formattable.ToString("d", CultureInfo.InvariantCulture)); + break; + } + case FieldType.Enum: + EnumValueDescriptor enumValue = descriptor.EnumType.FindValueByNumber((int) value); + if (enumValue != null) + { + WriteString(builder, enumValue.Name); + } + else + { + // ??? Need more documentation + builder.Append(((int) value).ToString("d", CultureInfo.InvariantCulture)); + } + break; + case FieldType.Fixed64: + case FieldType.UInt64: + case FieldType.SFixed64: + case FieldType.Int64: + case FieldType.SInt64: + { + builder.Append('"'); + IFormattable formattable = (IFormattable) value; + builder.Append(formattable.ToString("d", CultureInfo.InvariantCulture)); + builder.Append('"'); + break; + } + case FieldType.Double: + case FieldType.Float: + string text = ((IFormattable) value).ToString("r", CultureInfo.InvariantCulture); + if (text == "NaN" || text == "Infinity" || text == "-Infinity") + { + builder.Append('"'); + builder.Append(text); + builder.Append('"'); + } + else + { + builder.Append(text); + } + break; + case FieldType.Message: + case FieldType.Group: // Never expect to get this, but... + WriteMessage(builder, (IReflectedMessage) value); + break; + default: + throw new ArgumentException("Invalid field type: " + descriptor.FieldType); + } + } + + private void WriteList(StringBuilder builder, IFieldAccessor accessor, IList list) + { + builder.Append("[ "); + bool first = true; + foreach (var value in list) + { + if (!first) + { + builder.Append(", "); + } + WriteSingleValue(builder, accessor.Descriptor, value); + first = false; + } + builder.Append(first ? "]" : " ]"); + } + + private void WriteDictionary(StringBuilder builder, IFieldAccessor accessor, IDictionary dictionary) + { + builder.Append("{ "); + bool first = true; + FieldDescriptor keyType = accessor.Descriptor.MessageType.FindFieldByNumber(1); + FieldDescriptor valueType = accessor.Descriptor.MessageType.FindFieldByNumber(2); + // This will box each pair. Could use IDictionaryEnumerator, but that's ugly in terms of disposal. + foreach (DictionaryEntry pair in dictionary) + { + if (!first) + { + builder.Append(", "); + } + string keyText; + switch (keyType.FieldType) + { + case FieldType.String: + keyText = (string) pair.Key; + break; + case FieldType.Bool: + keyText = (bool) pair.Key ? "true" : "false"; + break; + case FieldType.Fixed32: + case FieldType.Fixed64: + case FieldType.SFixed32: + case FieldType.SFixed64: + case FieldType.Int32: + case FieldType.Int64: + case FieldType.SInt32: + case FieldType.SInt64: + case FieldType.UInt32: + case FieldType.UInt64: + keyText = ((IFormattable) pair.Key).ToString("d", CultureInfo.InvariantCulture); + break; + default: + throw new ArgumentException("Invalid key type: " + keyType.FieldType); + } + WriteString(builder, keyText); + builder.Append(": "); + WriteSingleValue(builder, valueType, pair.Value); + first = false; + } + builder.Append(first ? "}" : " }"); + } + + /// + /// Writes a string (including leading and trailing double quotes) to a builder, escaping as required. + /// + /// + /// Other than surrogate pair handling, this code is mostly taken from src/google/protobuf/util/internal/json_escaping.cc. + /// + private void WriteString(StringBuilder builder, string text) + { + builder.Append('"'); + for (int i = 0; i < text.Length; i++) + { + char c = text[i]; + if (c < 0xa0) + { + builder.Append(CommonRepresentations[c]); + continue; + } + if (char.IsHighSurrogate(c)) + { + // Encountered first part of a surrogate pair. + // Check that we have the whole pair, and encode both parts as hex. + i++; + if (i == text.Length || !char.IsLowSurrogate(text[i])) + { + throw new ArgumentException("String contains low surrogate not followed by high surrogate"); + } + HexEncodeUtf16CodeUnit(builder, c); + HexEncodeUtf16CodeUnit(builder, text[i]); + continue; + } + else if (char.IsLowSurrogate(c)) + { + throw new ArgumentException("String contains high surrogate not preceded by low surrogate"); + } + switch ((uint) c) + { + // These are not required by json spec + // but used to prevent security bugs in javascript. + case 0xfeff: // Zero width no-break space + case 0xfff9: // Interlinear annotation anchor + case 0xfffa: // Interlinear annotation separator + case 0xfffb: // Interlinear annotation terminator + + case 0x00ad: // Soft-hyphen + case 0x06dd: // Arabic end of ayah + case 0x070f: // Syriac abbreviation mark + case 0x17b4: // Khmer vowel inherent Aq + case 0x17b5: // Khmer vowel inherent Aa + HexEncodeUtf16CodeUnit(builder, c); + break; + + default: + if ((c >= 0x0600 && c <= 0x0603) || // Arabic signs + (c >= 0x200b && c <= 0x200f) || // Zero width etc. + (c >= 0x2028 && c <= 0x202e) || // Separators etc. + (c >= 0x2060 && c <= 0x2064) || // Invisible etc. + (c >= 0x206a && c <= 0x206f)) + { + HexEncodeUtf16CodeUnit(builder, c); + } + else + { + // No handling of surrogates here - that's done earlier + builder.Append(c); + } + break; + } + } + builder.Append('"'); + } + + private const string Hex = "0123456789abcdef"; + private static void HexEncodeUtf16CodeUnit(StringBuilder builder, char c) + { + uint utf16 = c; + builder.Append("\\u"); + builder.Append(Hex[(c >> 12) & 0xf]); + builder.Append(Hex[(c >> 8) & 0xf]); + builder.Append(Hex[(c >> 4) & 0xf]); + builder.Append(Hex[(c >> 0) & 0xf]); + } + + /// + /// Settings controlling JSON formatting. + /// + public sealed class Settings + { + private static readonly Settings defaultInstance = new Settings(false); + + /// + /// Default settings, as used by + /// + public static Settings Default { get { return defaultInstance; } } + + private readonly bool formatDefaultValues; + + + /// + /// Whether fields whose values are the default for the field type (e.g. 0 for integers) + /// should be formatted (true) or omitted (false). + /// + public bool FormatDefaultValues { get { return formatDefaultValues; } } + + public Settings(bool formatDefaultValues) + { + this.formatDefaultValues = formatDefaultValues; + } + } + } +} diff --git a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj index aa4adcc0c2..17532de8f5 100644 --- a/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj +++ b/csharp/src/ProtocolBuffers/ProtocolBuffers.csproj @@ -81,6 +81,7 @@ + From 5b9288e47d7add219717d472aa95a5cfe1141ac9 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 3 Jul 2015 12:45:36 +0100 Subject: [PATCH 2/6] Use the new JsonFormatter to implement ToString on generated messages. --- csharp/src/AddressBook/Addressbook.cs | 12 ++ .../TestProtos/MapUnittestProto3.cs | 176 ++++++++++++++++++ .../TestProtos/UnittestImportProto3.cs | 4 + .../TestProtos/UnittestImportPublicProto3.cs | 4 + .../TestProtos/UnittestIssues.cs | 28 +++ .../TestProtos/UnittestProto3.cs | 140 ++++++++++++++ .../DescriptorProtos/DescriptorProtoFile.cs | 88 +++++++++ .../compiler/csharp/csharp_field_base.h | 1 + .../compiler/csharp/csharp_map_field.cc | 7 +- .../compiler/csharp/csharp_message.cc | 5 +- 10 files changed, 459 insertions(+), 6 deletions(-) diff --git a/csharp/src/AddressBook/Addressbook.cs b/csharp/src/AddressBook/Addressbook.cs index 5f4a5139e4..b7a08ef042 100644 --- a/csharp/src/AddressBook/Addressbook.cs +++ b/csharp/src/AddressBook/Addressbook.cs @@ -160,6 +160,10 @@ namespace Google.Protobuf.Examples.AddressBook { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -330,6 +334,10 @@ namespace Google.Protobuf.Examples.AddressBook { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Number.Length != 0) { output.WriteRawTag(10); @@ -463,6 +471,10 @@ namespace Google.Protobuf.Examples.AddressBook { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { person_.WriteTo(output, _repeated_person_codec); } diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs index 55434c534a..0e261e1b77 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs @@ -602,6 +602,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { mapInt32Int32_.WriteTo(output, _map_mapInt32Int32_codec); mapInt64Int64_.WriteTo(output, _map_mapInt64Int64_codec); @@ -836,6 +840,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -978,6 +986,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0L) { output.WriteRawTag(8); @@ -1120,6 +1132,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -1262,6 +1278,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0UL) { output.WriteRawTag(8); @@ -1404,6 +1424,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -1546,6 +1570,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0L) { output.WriteRawTag(8); @@ -1688,6 +1716,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(13); @@ -1830,6 +1862,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0UL) { output.WriteRawTag(9); @@ -1972,6 +2008,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(13); @@ -2114,6 +2154,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0L) { output.WriteRawTag(9); @@ -2256,6 +2300,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -2398,6 +2446,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -2540,6 +2592,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != false) { output.WriteRawTag(8); @@ -2682,6 +2738,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key.Length != 0) { output.WriteRawTag(10); @@ -2824,6 +2884,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -2966,6 +3030,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -3109,6 +3177,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -3250,6 +3322,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (testMap_ != null) { output.WriteRawTag(10); @@ -3370,6 +3446,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { mapInt32Message_.WriteTo(output, _map_mapInt32Message_codec); } @@ -3493,6 +3573,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -3644,6 +3728,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { map1_.WriteTo(output, _map_map1_codec); map2_.WriteTo(output, _map_map2_codec); @@ -3773,6 +3861,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -3915,6 +4007,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -4216,6 +4312,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { mapInt32Int32_.WriteTo(output, _map_mapInt32Int32_codec); mapInt64Int64_.WriteTo(output, _map_mapInt64Int64_codec); @@ -4436,6 +4536,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -4578,6 +4682,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0L) { output.WriteRawTag(8); @@ -4720,6 +4828,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -4862,6 +4974,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0UL) { output.WriteRawTag(8); @@ -5004,6 +5120,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -5146,6 +5266,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0L) { output.WriteRawTag(8); @@ -5288,6 +5412,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(13); @@ -5430,6 +5558,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0UL) { output.WriteRawTag(9); @@ -5572,6 +5704,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(13); @@ -5714,6 +5850,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0L) { output.WriteRawTag(9); @@ -5856,6 +5996,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -5998,6 +6142,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -6140,6 +6288,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != false) { output.WriteRawTag(8); @@ -6282,6 +6434,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -6425,6 +6581,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -6564,6 +6724,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { type_.WriteTo(output, _map_type_codec); } @@ -6691,6 +6855,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); @@ -6830,6 +6998,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { entry_.WriteTo(output, _map_entry_codec); } @@ -6952,6 +7124,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Key != 0) { output.WriteRawTag(8); diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs index d30f22db6e..14c844feae 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs @@ -124,6 +124,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (D != 0) { output.WriteRawTag(8); diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs index 5aa03a641a..b8c62bc9a9 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs @@ -109,6 +109,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (E != 0) { output.WriteRawTag(8); diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs index 0e409c8b72..59c551dc1b 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs @@ -148,6 +148,10 @@ namespace UnitTest.Issues.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -237,6 +241,10 @@ namespace UnitTest.Issues.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -326,6 +334,10 @@ namespace UnitTest.Issues.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -459,6 +471,10 @@ namespace UnitTest.Issues.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Value != global::UnitTest.Issues.TestProtos.NegativeEnum.NEGATIVE_ENUM_ZERO) { output.WriteRawTag(8); @@ -577,6 +593,10 @@ namespace UnitTest.Issues.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -746,6 +766,10 @@ namespace UnitTest.Issues.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (PrimitiveValue != 0) { output.WriteRawTag(8); @@ -918,6 +942,10 @@ namespace UnitTest.Issues.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Item != 0) { output.WriteRawTag(8); diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs index d2437bf4b7..0e63e1d6f9 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs @@ -1019,6 +1019,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (SingleInt32 != 0) { output.WriteRawTag(8); @@ -1695,6 +1699,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Bb != 0) { output.WriteRawTag(8); @@ -1842,6 +1850,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (child_ != null) { output.WriteRawTag(10); @@ -1991,6 +2003,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (DeprecatedInt32 != 0) { output.WriteRawTag(8); @@ -2106,6 +2122,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (C != 0) { output.WriteRawTag(8); @@ -2208,6 +2228,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -2310,6 +2334,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (foreignNested_ != null) { output.WriteRawTag(10); @@ -2444,6 +2472,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (A != 0) { output.WriteRawTag(8); @@ -2587,6 +2619,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (a_ != null) { output.WriteRawTag(10); @@ -2723,6 +2759,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (bb_ != null) { output.WriteRawTag(10); @@ -2858,6 +2898,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (a_ != null) { output.WriteRawTag(10); @@ -3080,6 +3124,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (PrimitiveField != 0) { output.WriteRawTag(8); @@ -3313,6 +3361,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (MyInt != 0L) { output.WriteRawTag(8); @@ -3490,6 +3542,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Bb != 0) { output.WriteRawTag(8); @@ -3624,6 +3680,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (SparseEnum != global::Google.Protobuf.TestProtos.TestSparseEnum.TEST_SPARSE_ENUM_UNSPECIFIED) { output.WriteRawTag(8); @@ -3739,6 +3799,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data.Length != 0) { output.WriteRawTag(10); @@ -3853,6 +3917,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { data_.WriteTo(output, _repeated_data_codec); } @@ -3961,6 +4029,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data.Length != 0) { output.WriteRawTag(10); @@ -4076,6 +4148,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data.Length != 0) { output.WriteRawTag(10); @@ -4191,6 +4267,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data != 0) { output.WriteRawTag(8); @@ -4306,6 +4386,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data != 0) { output.WriteRawTag(8); @@ -4421,6 +4505,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data != 0L) { output.WriteRawTag(8); @@ -4536,6 +4624,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data != 0UL) { output.WriteRawTag(8); @@ -4651,6 +4743,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Data != false) { output.WriteRawTag(8); @@ -4820,6 +4916,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (fooCase_ == FooOneofCase.FooInt) { output.WriteRawTag(8); @@ -5125,6 +5225,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { packedInt32_.WriteTo(output, _repeated_packedInt32_codec); packedInt64_.WriteTo(output, _repeated_packedInt64_codec); @@ -5492,6 +5596,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { unpackedInt32_.WriteTo(output, _repeated_unpackedInt32_codec); unpackedInt64_.WriteTo(output, _repeated_unpackedInt64_codec); @@ -5764,6 +5872,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { repeatedFixed32_.WriteTo(output, _repeated_repeatedFixed32_codec); repeatedInt32_.WriteTo(output, _repeated_repeatedInt32_codec); @@ -5913,6 +6025,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (A.Length != 0) { output.WriteRawTag(10); @@ -6015,6 +6131,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -6103,6 +6223,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -6191,6 +6315,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -6279,6 +6407,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -6367,6 +6499,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } @@ -6455,6 +6591,10 @@ namespace Google.Protobuf.TestProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { } diff --git a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs index c87b367a32..a7d1d72628 100644 --- a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs +++ b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs @@ -299,6 +299,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { file_.WriteTo(output, _repeated_file_codec); } @@ -545,6 +549,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -889,6 +897,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -1094,6 +1106,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Start != 0) { output.WriteRawTag(8); @@ -1236,6 +1252,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Start != 0) { output.WriteRawTag(8); @@ -1475,6 +1495,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -1741,6 +1765,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -1882,6 +1910,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -2051,6 +2083,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -2226,6 +2262,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -2434,6 +2474,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Name.Length != 0) { output.WriteRawTag(10); @@ -2806,6 +2850,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (JavaPackage.Length != 0) { output.WriteRawTag(10); @@ -3173,6 +3221,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (MessageSetWireFormat != false) { output.WriteRawTag(8); @@ -3414,6 +3466,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Ctype != global::Google.Protobuf.DescriptorProtos.FieldOptions.Types.CType.STRING) { output.WriteRawTag(8); @@ -3649,6 +3705,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (AllowAlias != false) { output.WriteRawTag(16); @@ -3797,6 +3857,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Deprecated != false) { output.WriteRawTag(8); @@ -3931,6 +3995,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Deprecated != false) { output.WriteRawTag(136, 2); @@ -4065,6 +4133,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (Deprecated != false) { output.WriteRawTag(136, 2); @@ -4264,6 +4336,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { name_.WriteTo(output, _repeated_name_codec); if (IdentifierValue.Length != 0) { @@ -4470,6 +4546,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { if (NamePart_.Length != 0) { output.WriteRawTag(10); @@ -4603,6 +4683,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { location_.WriteTo(output, _repeated_location_codec); } @@ -4761,6 +4845,10 @@ namespace Google.Protobuf.DescriptorProtos { return hash; } + public override string ToString() { + return pb::JsonFormatter.Default.Format(this); + } + public void WriteTo(pb::CodedOutputStream output) { path_.WriteTo(output, _repeated_path_codec); span_.WriteTo(output, _repeated_span_codec); diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.h b/src/google/protobuf/compiler/csharp/csharp_field_base.h index 349d835bac..bffa206239 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.h +++ b/src/google/protobuf/compiler/csharp/csharp_field_base.h @@ -58,6 +58,7 @@ class FieldGeneratorBase : public SourceGeneratorBase { virtual void WriteHash(io::Printer* printer) = 0; virtual void WriteEquals(io::Printer* printer) = 0; + // Currently unused, as we use reflection to generate JSON virtual void WriteToString(io::Printer* printer) = 0; protected: diff --git a/src/google/protobuf/compiler/csharp/csharp_map_field.cc b/src/google/protobuf/compiler/csharp/csharp_map_field.cc index cb7ce5f006..32c05232c4 100644 --- a/src/google/protobuf/compiler/csharp/csharp_map_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_map_field.cc @@ -117,12 +117,9 @@ void MapFieldGenerator::WriteEquals(io::Printer* printer) { variables_, "if (!$property_name$.Equals(other.$property_name$)) return false;\n"); } + void MapFieldGenerator::WriteToString(io::Printer* printer) { - /* - variables_["field_name"] = GetFieldName(descriptor_); - printer->Print( - variables_, - "PrintField(\"$field_name$\", has$property_name$, $name$_, writer);\n");*/ + // TODO: If we ever actually use ToString, we'll need to impleme this... } void MapFieldGenerator::GenerateCloningCode(io::Printer* printer) { diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index 13544b2681..ac13595178 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -429,7 +429,10 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) { printer->Outdent(); printer->Print("}\n\n"); - // TODO(jonskeet): ToString. + printer->Print( + "public override string ToString() {\n" + " return pb::JsonFormatter.Default.Format(this);\n" + "}\n\n"); } void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer) { From ef3464dff648362683a75ddf593c9d47ec3c33ce Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 10 Jul 2015 14:04:53 +0100 Subject: [PATCH 3/6] Oneof reflection support. (Generated code changes in next commit.) --- .../GeneratedMessageTest.cs | 20 +++++++ .../FieldAccess/FieldAccessorTable.cs | 29 +++++++++- .../FieldAccess/OneofAccessor.cs | 57 +++++++++---------- .../FieldAccess/ReflectionUtil.cs | 15 ++++- .../compiler/csharp/csharp_message.cc | 1 + 5 files changed, 87 insertions(+), 35 deletions(-) diff --git a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs index 8cee98207a..28c2195f9f 100644 --- a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs +++ b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs @@ -734,5 +734,25 @@ namespace Google.Protobuf var message = SampleMessages.CreateFullTestAllTypes(); Assert.Throws(() => message.Fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap())); } + + [Test] + public void Reflection_Oneof() + { + var message = new TestAllTypes(); + var fields = message.Fields; + Assert.AreEqual(1, fields.Oneofs.Count); + var oneof = fields.Oneofs[0]; + Assert.AreEqual("oneof_field", oneof.Descriptor.Name); + Assert.IsNull(oneof.GetCaseFieldDescriptor(message)); + + message.OneofString = "foo"; + Assert.AreSame(fields[TestAllTypes.OneofStringFieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message)); + + message.OneofUint32 = 10; + Assert.AreSame(fields[TestAllTypes.OneofUint32FieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message)); + + oneof.Clear(message); + Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); + } } } diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs index 57ea9c8768..c69612fb63 100644 --- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs +++ b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs @@ -42,6 +42,7 @@ namespace Google.Protobuf.FieldAccess public sealed class FieldAccessorTable { private readonly ReadOnlyCollection accessors; + private readonly ReadOnlyCollection oneofs; private readonly MessageDescriptor descriptor; /// @@ -51,7 +52,7 @@ namespace Google.Protobuf.FieldAccess /// The CLR type for the message. /// The type's descriptor /// The Pascal-case names of all the field-based properties in the message. - public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames) + public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames, string[] oneofPropertyNames) { this.descriptor = descriptor; var accessorsArray = new IFieldAccessor[descriptor.Fields.Count]; @@ -65,7 +66,13 @@ namespace Google.Protobuf.FieldAccess : (IFieldAccessor) new SingleFieldAccessor(type, name, field); } accessors = new ReadOnlyCollection(accessorsArray); - // TODO(jonskeet): Oneof support + var oneofsArray = new OneofAccessor[descriptor.Oneofs.Count]; + for (int i = 0; i < oneofsArray.Length; i++) + { + var oneof = descriptor.Oneofs[i]; + oneofsArray[i] = new OneofAccessor(type, oneofPropertyNames[i], oneof); + } + oneofs = new ReadOnlyCollection(oneofsArray); } // TODO: Validate the name here... should possibly make this type a more "general reflection access" type, @@ -75,6 +82,10 @@ namespace Google.Protobuf.FieldAccess /// public ReadOnlyCollection Accessors { get { return accessors; } } + public ReadOnlyCollection Oneofs { get { return oneofs; } } + + // TODO: Review the API for the indexers. Now that we have fields and oneofs, it's not as clear... + public IFieldAccessor this[int fieldNumber] { get @@ -84,7 +95,7 @@ namespace Google.Protobuf.FieldAccess } } - internal IFieldAccessor this[FieldDescriptor field] + public IFieldAccessor this[FieldDescriptor field] { get { @@ -95,5 +106,17 @@ namespace Google.Protobuf.FieldAccess return accessors[field.Index]; } } + + public OneofAccessor this[OneofDescriptor oneof] + { + get + { + if (oneof.ContainingType != descriptor) + { + throw new ArgumentException("OneofDescriptor does not match message type."); + } + return oneofs[oneof.Index]; + } + } } } \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs index feaa6232d1..590b63097c 100644 --- a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs +++ b/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs @@ -30,62 +30,57 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endregion +using Google.Protobuf.Descriptors; +using System; +using System.Reflection; + namespace Google.Protobuf.FieldAccess { - // TODO(jonskeet): Add "new" oneof API support - /// - /// Access for an oneof + /// Reflection access for a oneof, allowing clear and "get case" actions. /// - internal class OneofAccessor where TMessage : IMessage + public sealed class OneofAccessor { - /* - private readonly Func caseDelegate; - private readonly Func clearDelegate; - private MessageDescriptor descriptor; + private readonly Func caseDelegate; + private readonly Action clearDelegate; + private OneofDescriptor descriptor; - internal OneofAccessor(MessageDescriptor descriptor, string name) + internal OneofAccessor(Type type, string propertyName, OneofDescriptor descriptor) { - this.descriptor = descriptor; - MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name); - PropertyInfo caseProperty = typeof(TMessage).GetProperty(name + "Case"); - if (clearMethod == null || caseProperty == null) + PropertyInfo property = type.GetProperty(propertyName + "Case"); + if (property == null || !property.CanRead) { - throw new ArgumentException("Not all required properties/methods available for oneof"); + throw new ArgumentException("Not all required properties/methods available"); } - + this.descriptor = descriptor; + caseDelegate = ReflectionUtil.CreateFuncObjectT(property.GetGetMethod()); - clearDelegate = ReflectionUtil.CreateDelegateFunc(clearMethod); - caseDelegate = ReflectionUtil.CreateUpcastDelegate(caseProperty.GetGetMethod()); + this.descriptor = descriptor; + MethodInfo clearMethod = type.GetMethod("Clear" + propertyName); + clearDelegate = ReflectionUtil.CreateActionObject(clearMethod); } - /// - /// Indicates whether the specified message has set any field in the oneof. - /// - public bool Has(TMessage message) - { - return ((int) caseDelegate(message) != 0); - } + public OneofDescriptor Descriptor { get { return descriptor; } } /// - /// Clears the oneof in the specified builder. + /// Clears the oneof in the specified message. /// - public void Clear(TBuilder builder) + public void Clear(object message) { - clearDelegate(builder); + clearDelegate(message); } /// /// Indicates which field in the oneof is set for specified message /// - public virtual FieldDescriptor GetOneofFieldDescriptor(TMessage message) + public FieldDescriptor GetCaseFieldDescriptor(object message) { - int fieldNumber = (int) caseDelegate(message); + int fieldNumber = caseDelegate(message); if (fieldNumber > 0) { - return descriptor.FindFieldByNumber(fieldNumber); + return descriptor.ContainingType.FindFieldByNumber(fieldNumber); } return null; - }*/ + } } } diff --git a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs index d305392089..08ef6c0c01 100644 --- a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs +++ b/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs @@ -63,7 +63,20 @@ namespace Google.Protobuf.FieldAccess Expression upcast = Expression.Convert(call, typeof(object)); return Expression.Lambda>(upcast, parameter).Compile(); } - + + /// + /// Creates a delegate which will cast the argument to the appropriate method target type, + /// call the method on it, then convert the result to the specified type. + /// + internal static Func CreateFuncObjectT(MethodInfo method) + { + ParameterExpression parameter = Expression.Parameter(typeof(object), "p"); + Expression downcast = Expression.Convert(parameter, method.DeclaringType); + Expression call = Expression.Call(downcast, method); + Expression upcast = Expression.Convert(call, typeof(T)); + return Expression.Lambda>(upcast, parameter).Compile(); + } + /// /// Creates a delegate which will execute the given method after casting the first argument to /// the target type of the method, and the second argument to the first parameter type of the method. diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index ac13595178..3fbec2b5aa 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/src/google/protobuf/compiler/csharp/csharp_message.cc @@ -151,6 +151,7 @@ void MessageGenerator::GenerateStaticVariableInitializers(io::Printer* printer) printer->Print("\"$property_name$\", ", "property_name", GetPropertyName(descriptor_->field(i))); } + printer->Print("}, new string[] { "); for (int i = 0; i < descriptor_->oneof_decl_count(); i++) { printer->Print("\"$oneof_name$\", ", "oneof_name", From 68fc689e523a4e27a689b93f2fa4a054529708d0 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 10 Jul 2015 14:05:17 +0100 Subject: [PATCH 4/6] Generated code changes for previous submit --- csharp/src/AddressBook/Addressbook.cs | 6 +- .../TestProtos/MapUnittestProto3.cs | 88 +++++++++---------- .../TestProtos/UnittestImportProto3.cs | 2 +- .../TestProtos/UnittestImportPublicProto3.cs | 2 +- .../TestProtos/UnittestIssues.cs | 14 +-- .../TestProtos/UnittestProto3.cs | 70 +++++++-------- .../DescriptorProtos/DescriptorProtoFile.cs | 44 +++++----- 7 files changed, 113 insertions(+), 113 deletions(-) diff --git a/csharp/src/AddressBook/Addressbook.cs b/csharp/src/AddressBook/Addressbook.cs index b7a08ef042..afdb172898 100644 --- a/csharp/src/AddressBook/Addressbook.cs +++ b/csharp/src/AddressBook/Addressbook.cs @@ -40,13 +40,13 @@ namespace Google.Protobuf.Examples.AddressBook { }); internal__static_tutorial_Person__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.Examples.AddressBook.Person), descriptor.MessageTypes[0], - new string[] { "Name", "Id", "Email", "Phone", }); + new string[] { "Name", "Id", "Email", "Phone", }, new string[] { }); internal__static_tutorial_Person_PhoneNumber__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.Examples.AddressBook.Person.Types.PhoneNumber), descriptor.MessageTypes[0].NestedTypes[0], - new string[] { "Number", "Type", }); + new string[] { "Number", "Type", }, new string[] { }); internal__static_tutorial_AddressBook__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.Examples.AddressBook.AddressBook), descriptor.MessageTypes[1], - new string[] { "Person", }); + new string[] { "Person", }, new string[] { }); } #endregion diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs index 0e261e1b77..05fe7fc4f5 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/MapUnittestProto3.cs @@ -197,136 +197,136 @@ namespace Google.Protobuf.TestProtos { }); internal__static_protobuf_unittest_TestMap__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap), descriptor.MessageTypes[0], - new string[] { "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapStringString", "MapInt32Bytes", "MapInt32Enum", "MapInt32ForeignMessage", }); + new string[] { "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapStringString", "MapInt32Bytes", "MapInt32Enum", "MapInt32ForeignMessage", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapInt32Int32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapInt32Int32Entry), descriptor.MessageTypes[0].NestedTypes[0], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapInt64Int64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapInt64Int64Entry), descriptor.MessageTypes[0].NestedTypes[1], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapUint32Uint32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapUint32Uint32Entry), descriptor.MessageTypes[0].NestedTypes[2], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapUint64Uint64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapUint64Uint64Entry), descriptor.MessageTypes[0].NestedTypes[3], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapSint32Sint32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapSint32Sint32Entry), descriptor.MessageTypes[0].NestedTypes[4], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapSint64Sint64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapSint64Sint64Entry), descriptor.MessageTypes[0].NestedTypes[5], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapFixed32Fixed32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapFixed32Fixed32Entry), descriptor.MessageTypes[0].NestedTypes[6], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapFixed64Fixed64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapFixed64Fixed64Entry), descriptor.MessageTypes[0].NestedTypes[7], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapSfixed32Sfixed32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapSfixed32Sfixed32Entry), descriptor.MessageTypes[0].NestedTypes[8], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapSfixed64Sfixed64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapSfixed64Sfixed64Entry), descriptor.MessageTypes[0].NestedTypes[9], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapInt32FloatEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapInt32FloatEntry), descriptor.MessageTypes[0].NestedTypes[10], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapInt32DoubleEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapInt32DoubleEntry), descriptor.MessageTypes[0].NestedTypes[11], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapBoolBoolEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapBoolBoolEntry), descriptor.MessageTypes[0].NestedTypes[12], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapStringStringEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapStringStringEntry), descriptor.MessageTypes[0].NestedTypes[13], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapInt32BytesEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapInt32BytesEntry), descriptor.MessageTypes[0].NestedTypes[14], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapInt32EnumEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapInt32EnumEntry), descriptor.MessageTypes[0].NestedTypes[15], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMap_MapInt32ForeignMessageEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMap.Types.MapInt32ForeignMessageEntry), descriptor.MessageTypes[0].NestedTypes[16], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestMapSubmessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMapSubmessage), descriptor.MessageTypes[1], - new string[] { "TestMap", }); + new string[] { "TestMap", }, new string[] { }); internal__static_protobuf_unittest_TestMessageMap__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMessageMap), descriptor.MessageTypes[2], - new string[] { "MapInt32Message", }); + new string[] { "MapInt32Message", }, new string[] { }); internal__static_protobuf_unittest_TestMessageMap_MapInt32MessageEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMessageMap.Types.MapInt32MessageEntry), descriptor.MessageTypes[2].NestedTypes[0], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestSameTypeMap__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestSameTypeMap), descriptor.MessageTypes[3], - new string[] { "Map1", "Map2", }); + new string[] { "Map1", "Map2", }, new string[] { }); internal__static_protobuf_unittest_TestSameTypeMap_Map1Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestSameTypeMap.Types.Map1Entry), descriptor.MessageTypes[3].NestedTypes[0], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestSameTypeMap_Map2Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestSameTypeMap.Types.Map2Entry), descriptor.MessageTypes[3].NestedTypes[1], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap), descriptor.MessageTypes[4], - new string[] { "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapInt32Enum", "MapInt32ForeignMessage", }); + new string[] { "MapInt32Int32", "MapInt64Int64", "MapUint32Uint32", "MapUint64Uint64", "MapSint32Sint32", "MapSint64Sint64", "MapFixed32Fixed32", "MapFixed64Fixed64", "MapSfixed32Sfixed32", "MapSfixed64Sfixed64", "MapInt32Float", "MapInt32Double", "MapBoolBool", "MapInt32Enum", "MapInt32ForeignMessage", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapInt32Int32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapInt32Int32Entry), descriptor.MessageTypes[4].NestedTypes[0], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapInt64Int64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapInt64Int64Entry), descriptor.MessageTypes[4].NestedTypes[1], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapUint32Uint32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapUint32Uint32Entry), descriptor.MessageTypes[4].NestedTypes[2], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapUint64Uint64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapUint64Uint64Entry), descriptor.MessageTypes[4].NestedTypes[3], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapSint32Sint32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapSint32Sint32Entry), descriptor.MessageTypes[4].NestedTypes[4], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapSint64Sint64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapSint64Sint64Entry), descriptor.MessageTypes[4].NestedTypes[5], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapFixed32Fixed32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapFixed32Fixed32Entry), descriptor.MessageTypes[4].NestedTypes[6], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapFixed64Fixed64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapFixed64Fixed64Entry), descriptor.MessageTypes[4].NestedTypes[7], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapSfixed32Sfixed32Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapSfixed32Sfixed32Entry), descriptor.MessageTypes[4].NestedTypes[8], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapSfixed64Sfixed64Entry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapSfixed64Sfixed64Entry), descriptor.MessageTypes[4].NestedTypes[9], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapInt32FloatEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapInt32FloatEntry), descriptor.MessageTypes[4].NestedTypes[10], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapInt32DoubleEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapInt32DoubleEntry), descriptor.MessageTypes[4].NestedTypes[11], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapBoolBoolEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapBoolBoolEntry), descriptor.MessageTypes[4].NestedTypes[12], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapInt32EnumEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapInt32EnumEntry), descriptor.MessageTypes[4].NestedTypes[13], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_TestArenaMap_MapInt32ForeignMessageEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestArenaMap.Types.MapInt32ForeignMessageEntry), descriptor.MessageTypes[4].NestedTypes[14], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_MessageContainingEnumCalledType__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType), descriptor.MessageTypes[5], - new string[] { "Type", }); + new string[] { "Type", }, new string[] { }); internal__static_protobuf_unittest_MessageContainingEnumCalledType_TypeEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.MessageContainingEnumCalledType.Types.TypeEntry), descriptor.MessageTypes[5].NestedTypes[0], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); internal__static_protobuf_unittest_MessageContainingMapCalledEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.MessageContainingMapCalledEntry), descriptor.MessageTypes[6], - new string[] { "Entry", }); + new string[] { "Entry", }, new string[] { }); internal__static_protobuf_unittest_MessageContainingMapCalledEntry_EntryEntry__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.MessageContainingMapCalledEntry.Types.EntryEntry), descriptor.MessageTypes[6].NestedTypes[0], - new string[] { "Key", "Value", }); + new string[] { "Key", "Value", }, new string[] { }); } #endregion diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs index 14c844feae..3cc5949b8c 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportProto3.cs @@ -38,7 +38,7 @@ namespace Google.Protobuf.TestProtos { }); internal__static_protobuf_unittest_import_ImportMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.ImportMessage), descriptor.MessageTypes[0], - new string[] { "D", }); + new string[] { "D", }, new string[] { }); } #endregion diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs index b8c62bc9a9..d3abe5d519 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestImportPublicProto3.cs @@ -33,7 +33,7 @@ namespace Google.Protobuf.TestProtos { }); internal__static_protobuf_unittest_import_PublicImportMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.PublicImportMessage), descriptor.MessageTypes[0], - new string[] { "E", }); + new string[] { "E", }, new string[] { }); } #endregion diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs index 59c551dc1b..c296cd24ee 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestIssues.cs @@ -53,25 +53,25 @@ namespace UnitTest.Issues.TestProtos { }); internal__static_unittest_issues_Issue307__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::UnitTest.Issues.TestProtos.Issue307), descriptor.MessageTypes[0], - new string[] { }); + new string[] { }, new string[] { }); internal__static_unittest_issues_Issue307_NestedOnce__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce), descriptor.MessageTypes[0].NestedTypes[0], - new string[] { }); + new string[] { }, new string[] { }); internal__static_unittest_issues_Issue307_NestedOnce_NestedTwice__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::UnitTest.Issues.TestProtos.Issue307.Types.NestedOnce.Types.NestedTwice), descriptor.MessageTypes[0].NestedTypes[0].NestedTypes[0], - new string[] { }); + new string[] { }, new string[] { }); internal__static_unittest_issues_NegativeEnumMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::UnitTest.Issues.TestProtos.NegativeEnumMessage), descriptor.MessageTypes[1], - new string[] { "Value", "Values", "PackedValues", }); + new string[] { "Value", "Values", "PackedValues", }, new string[] { }); internal__static_unittest_issues_DeprecatedChild__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::UnitTest.Issues.TestProtos.DeprecatedChild), descriptor.MessageTypes[2], - new string[] { }); + new string[] { }, new string[] { }); internal__static_unittest_issues_DeprecatedFieldsMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::UnitTest.Issues.TestProtos.DeprecatedFieldsMessage), descriptor.MessageTypes[3], - new string[] { "PrimitiveValue", "PrimitiveArray", "MessageValue", "MessageArray", "EnumValue", "EnumArray", }); + new string[] { "PrimitiveValue", "PrimitiveArray", "MessageValue", "MessageArray", "EnumValue", "EnumArray", }, new string[] { }); internal__static_unittest_issues_ItemField__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::UnitTest.Issues.TestProtos.ItemField), descriptor.MessageTypes[4], - new string[] { "Item", }); + new string[] { "Item", }, new string[] { }); } #endregion diff --git a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs index 0e63e1d6f9..a5e37bd6b1 100644 --- a/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs +++ b/csharp/src/ProtocolBuffers.Test/TestProtos/UnittestProto3.cs @@ -191,109 +191,109 @@ namespace Google.Protobuf.TestProtos { }); internal__static_protobuf_unittest_TestAllTypes__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestAllTypes), descriptor.MessageTypes[0], - new string[] { "SingleInt32", "SingleInt64", "SingleUint32", "SingleUint64", "SingleSint32", "SingleSint64", "SingleFixed32", "SingleFixed64", "SingleSfixed32", "SingleSfixed64", "SingleFloat", "SingleDouble", "SingleBool", "SingleString", "SingleBytes", "SingleNestedMessage", "SingleForeignMessage", "SingleImportMessage", "SingleNestedEnum", "SingleForeignEnum", "SingleImportEnum", "SinglePublicImportMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedImportMessage", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedImportEnum", "RepeatedPublicImportMessage", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofBytes", "OneofField", }); + new string[] { "SingleInt32", "SingleInt64", "SingleUint32", "SingleUint64", "SingleSint32", "SingleSint64", "SingleFixed32", "SingleFixed64", "SingleSfixed32", "SingleSfixed64", "SingleFloat", "SingleDouble", "SingleBool", "SingleString", "SingleBytes", "SingleNestedMessage", "SingleForeignMessage", "SingleImportMessage", "SingleNestedEnum", "SingleForeignEnum", "SingleImportEnum", "SinglePublicImportMessage", "RepeatedInt32", "RepeatedInt64", "RepeatedUint32", "RepeatedUint64", "RepeatedSint32", "RepeatedSint64", "RepeatedFixed32", "RepeatedFixed64", "RepeatedSfixed32", "RepeatedSfixed64", "RepeatedFloat", "RepeatedDouble", "RepeatedBool", "RepeatedString", "RepeatedBytes", "RepeatedNestedMessage", "RepeatedForeignMessage", "RepeatedImportMessage", "RepeatedNestedEnum", "RepeatedForeignEnum", "RepeatedImportEnum", "RepeatedPublicImportMessage", "OneofUint32", "OneofNestedMessage", "OneofString", "OneofBytes", }, new string[] { "OneofField", }); internal__static_protobuf_unittest_TestAllTypes_NestedMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestAllTypes.Types.NestedMessage), descriptor.MessageTypes[0].NestedTypes[0], - new string[] { "Bb", }); + new string[] { "Bb", }, new string[] { }); internal__static_protobuf_unittest_NestedTestAllTypes__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.NestedTestAllTypes), descriptor.MessageTypes[1], - new string[] { "Child", "Payload", "RepeatedChild", }); + new string[] { "Child", "Payload", "RepeatedChild", }, new string[] { }); internal__static_protobuf_unittest_TestDeprecatedFields__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestDeprecatedFields), descriptor.MessageTypes[2], - new string[] { "DeprecatedInt32", }); + new string[] { "DeprecatedInt32", }, new string[] { }); internal__static_protobuf_unittest_ForeignMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.ForeignMessage), descriptor.MessageTypes[3], - new string[] { "C", }); + new string[] { "C", }, new string[] { }); internal__static_protobuf_unittest_TestReservedFields__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestReservedFields), descriptor.MessageTypes[4], - new string[] { }); + new string[] { }, new string[] { }); internal__static_protobuf_unittest_TestForeignNested__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestForeignNested), descriptor.MessageTypes[5], - new string[] { "ForeignNested", }); + new string[] { "ForeignNested", }, new string[] { }); internal__static_protobuf_unittest_TestReallyLargeTagNumber__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestReallyLargeTagNumber), descriptor.MessageTypes[6], - new string[] { "A", "Bb", }); + new string[] { "A", "Bb", }, new string[] { }); internal__static_protobuf_unittest_TestRecursiveMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestRecursiveMessage), descriptor.MessageTypes[7], - new string[] { "A", "I", }); + new string[] { "A", "I", }, new string[] { }); internal__static_protobuf_unittest_TestMutualRecursionA__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMutualRecursionA), descriptor.MessageTypes[8], - new string[] { "Bb", }); + new string[] { "Bb", }, new string[] { }); internal__static_protobuf_unittest_TestMutualRecursionB__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestMutualRecursionB), descriptor.MessageTypes[9], - new string[] { "A", "OptionalInt32", }); + new string[] { "A", "OptionalInt32", }, new string[] { }); internal__static_protobuf_unittest_TestCamelCaseFieldNames__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestCamelCaseFieldNames), descriptor.MessageTypes[10], - new string[] { "PrimitiveField", "StringField", "EnumField", "MessageField", "RepeatedPrimitiveField", "RepeatedStringField", "RepeatedEnumField", "RepeatedMessageField", }); + new string[] { "PrimitiveField", "StringField", "EnumField", "MessageField", "RepeatedPrimitiveField", "RepeatedStringField", "RepeatedEnumField", "RepeatedMessageField", }, new string[] { }); internal__static_protobuf_unittest_TestFieldOrderings__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestFieldOrderings), descriptor.MessageTypes[11], - new string[] { "MyString", "MyInt", "MyFloat", "SingleNestedMessage", }); + new string[] { "MyString", "MyInt", "MyFloat", "SingleNestedMessage", }, new string[] { }); internal__static_protobuf_unittest_TestFieldOrderings_NestedMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestFieldOrderings.Types.NestedMessage), descriptor.MessageTypes[11].NestedTypes[0], - new string[] { "Oo", "Bb", }); + new string[] { "Oo", "Bb", }, new string[] { }); internal__static_protobuf_unittest_SparseEnumMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.SparseEnumMessage), descriptor.MessageTypes[12], - new string[] { "SparseEnum", }); + new string[] { "SparseEnum", }, new string[] { }); internal__static_protobuf_unittest_OneString__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.OneString), descriptor.MessageTypes[13], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_MoreString__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.MoreString), descriptor.MessageTypes[14], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_OneBytes__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.OneBytes), descriptor.MessageTypes[15], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_MoreBytes__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.MoreBytes), descriptor.MessageTypes[16], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_Int32Message__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.Int32Message), descriptor.MessageTypes[17], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_Uint32Message__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.Uint32Message), descriptor.MessageTypes[18], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_Int64Message__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.Int64Message), descriptor.MessageTypes[19], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_Uint64Message__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.Uint64Message), descriptor.MessageTypes[20], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_BoolMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.BoolMessage), descriptor.MessageTypes[21], - new string[] { "Data", }); + new string[] { "Data", }, new string[] { }); internal__static_protobuf_unittest_TestOneof__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestOneof), descriptor.MessageTypes[22], - new string[] { "FooInt", "FooString", "FooMessage", "Foo", }); + new string[] { "FooInt", "FooString", "FooMessage", }, new string[] { "Foo", }); internal__static_protobuf_unittest_TestPackedTypes__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestPackedTypes), descriptor.MessageTypes[23], - new string[] { "PackedInt32", "PackedInt64", "PackedUint32", "PackedUint64", "PackedSint32", "PackedSint64", "PackedFixed32", "PackedFixed64", "PackedSfixed32", "PackedSfixed64", "PackedFloat", "PackedDouble", "PackedBool", "PackedEnum", }); + new string[] { "PackedInt32", "PackedInt64", "PackedUint32", "PackedUint64", "PackedSint32", "PackedSint64", "PackedFixed32", "PackedFixed64", "PackedSfixed32", "PackedSfixed64", "PackedFloat", "PackedDouble", "PackedBool", "PackedEnum", }, new string[] { }); internal__static_protobuf_unittest_TestUnpackedTypes__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestUnpackedTypes), descriptor.MessageTypes[24], - new string[] { "UnpackedInt32", "UnpackedInt64", "UnpackedUint32", "UnpackedUint64", "UnpackedSint32", "UnpackedSint64", "UnpackedFixed32", "UnpackedFixed64", "UnpackedSfixed32", "UnpackedSfixed64", "UnpackedFloat", "UnpackedDouble", "UnpackedBool", "UnpackedEnum", }); + new string[] { "UnpackedInt32", "UnpackedInt64", "UnpackedUint32", "UnpackedUint64", "UnpackedSint32", "UnpackedSint64", "UnpackedFixed32", "UnpackedFixed64", "UnpackedSfixed32", "UnpackedSfixed64", "UnpackedFloat", "UnpackedDouble", "UnpackedBool", "UnpackedEnum", }, new string[] { }); internal__static_protobuf_unittest_TestRepeatedScalarDifferentTagSizes__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestRepeatedScalarDifferentTagSizes), descriptor.MessageTypes[25], - new string[] { "RepeatedFixed32", "RepeatedInt32", "RepeatedFixed64", "RepeatedInt64", "RepeatedFloat", "RepeatedUint64", }); + new string[] { "RepeatedFixed32", "RepeatedInt32", "RepeatedFixed64", "RepeatedInt64", "RepeatedFloat", "RepeatedUint64", }, new string[] { }); internal__static_protobuf_unittest_TestCommentInjectionMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.TestCommentInjectionMessage), descriptor.MessageTypes[26], - new string[] { "A", }); + new string[] { "A", }, new string[] { }); internal__static_protobuf_unittest_FooRequest__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.FooRequest), descriptor.MessageTypes[27], - new string[] { }); + new string[] { }, new string[] { }); internal__static_protobuf_unittest_FooResponse__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.FooResponse), descriptor.MessageTypes[28], - new string[] { }); + new string[] { }, new string[] { }); internal__static_protobuf_unittest_FooClientMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.FooClientMessage), descriptor.MessageTypes[29], - new string[] { }); + new string[] { }, new string[] { }); internal__static_protobuf_unittest_FooServerMessage__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.FooServerMessage), descriptor.MessageTypes[30], - new string[] { }); + new string[] { }, new string[] { }); internal__static_protobuf_unittest_BarRequest__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.BarRequest), descriptor.MessageTypes[31], - new string[] { }); + new string[] { }, new string[] { }); internal__static_protobuf_unittest_BarResponse__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.TestProtos.BarResponse), descriptor.MessageTypes[32], - new string[] { }); + new string[] { }, new string[] { }); } #endregion diff --git a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs index a7d1d72628..52f0c4b289 100644 --- a/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs +++ b/csharp/src/ProtocolBuffers/DescriptorProtos/DescriptorProtoFile.cs @@ -161,70 +161,70 @@ namespace Google.Protobuf.DescriptorProtos { }); internal__static_google_protobuf_FileDescriptorSet__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FileDescriptorSet), descriptor.MessageTypes[0], - new string[] { "File", }); + new string[] { "File", }, new string[] { }); internal__static_google_protobuf_FileDescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FileDescriptorProto), descriptor.MessageTypes[1], - new string[] { "Name", "Package", "Dependency", "PublicDependency", "WeakDependency", "MessageType", "EnumType", "Service", "Extension", "Options", "SourceCodeInfo", "Syntax", }); + new string[] { "Name", "Package", "Dependency", "PublicDependency", "WeakDependency", "MessageType", "EnumType", "Service", "Extension", "Options", "SourceCodeInfo", "Syntax", }, new string[] { }); internal__static_google_protobuf_DescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.DescriptorProto), descriptor.MessageTypes[2], - new string[] { "Name", "Field", "Extension", "NestedType", "EnumType", "ExtensionRange", "OneofDecl", "Options", "ReservedRange", "ReservedName", }); + new string[] { "Name", "Field", "Extension", "NestedType", "EnumType", "ExtensionRange", "OneofDecl", "Options", "ReservedRange", "ReservedName", }, new string[] { }); internal__static_google_protobuf_DescriptorProto_ExtensionRange__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ExtensionRange), descriptor.MessageTypes[2].NestedTypes[0], - new string[] { "Start", "End", }); + new string[] { "Start", "End", }, new string[] { }); internal__static_google_protobuf_DescriptorProto_ReservedRange__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.DescriptorProto.Types.ReservedRange), descriptor.MessageTypes[2].NestedTypes[1], - new string[] { "Start", "End", }); + new string[] { "Start", "End", }, new string[] { }); internal__static_google_protobuf_FieldDescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FieldDescriptorProto), descriptor.MessageTypes[3], - new string[] { "Name", "Number", "Label", "Type", "TypeName", "Extendee", "DefaultValue", "OneofIndex", "Options", }); + new string[] { "Name", "Number", "Label", "Type", "TypeName", "Extendee", "DefaultValue", "OneofIndex", "Options", }, new string[] { }); internal__static_google_protobuf_OneofDescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.OneofDescriptorProto), descriptor.MessageTypes[4], - new string[] { "Name", }); + new string[] { "Name", }, new string[] { }); internal__static_google_protobuf_EnumDescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumDescriptorProto), descriptor.MessageTypes[5], - new string[] { "Name", "Value", "Options", }); + new string[] { "Name", "Value", "Options", }, new string[] { }); internal__static_google_protobuf_EnumValueDescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumValueDescriptorProto), descriptor.MessageTypes[6], - new string[] { "Name", "Number", "Options", }); + new string[] { "Name", "Number", "Options", }, new string[] { }); internal__static_google_protobuf_ServiceDescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.ServiceDescriptorProto), descriptor.MessageTypes[7], - new string[] { "Name", "Method", "Options", }); + new string[] { "Name", "Method", "Options", }, new string[] { }); internal__static_google_protobuf_MethodDescriptorProto__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.MethodDescriptorProto), descriptor.MessageTypes[8], - new string[] { "Name", "InputType", "OutputType", "Options", "ClientStreaming", "ServerStreaming", }); + new string[] { "Name", "InputType", "OutputType", "Options", "ClientStreaming", "ServerStreaming", }, new string[] { }); internal__static_google_protobuf_FileOptions__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FileOptions), descriptor.MessageTypes[9], - new string[] { "JavaPackage", "JavaOuterClassname", "JavaMultipleFiles", "JavaGenerateEqualsAndHash", "JavaStringCheckUtf8", "OptimizeFor", "GoPackage", "CcGenericServices", "JavaGenericServices", "PyGenericServices", "Deprecated", "CcEnableArenas", "ObjcClassPrefix", "CsharpNamespace", "UninterpretedOption", }); + new string[] { "JavaPackage", "JavaOuterClassname", "JavaMultipleFiles", "JavaGenerateEqualsAndHash", "JavaStringCheckUtf8", "OptimizeFor", "GoPackage", "CcGenericServices", "JavaGenericServices", "PyGenericServices", "Deprecated", "CcEnableArenas", "ObjcClassPrefix", "CsharpNamespace", "UninterpretedOption", }, new string[] { }); internal__static_google_protobuf_MessageOptions__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.MessageOptions), descriptor.MessageTypes[10], - new string[] { "MessageSetWireFormat", "NoStandardDescriptorAccessor", "Deprecated", "MapEntry", "UninterpretedOption", }); + new string[] { "MessageSetWireFormat", "NoStandardDescriptorAccessor", "Deprecated", "MapEntry", "UninterpretedOption", }, new string[] { }); internal__static_google_protobuf_FieldOptions__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.FieldOptions), descriptor.MessageTypes[11], - new string[] { "Ctype", "Packed", "Jstype", "Lazy", "Deprecated", "Weak", "UninterpretedOption", }); + new string[] { "Ctype", "Packed", "Jstype", "Lazy", "Deprecated", "Weak", "UninterpretedOption", }, new string[] { }); internal__static_google_protobuf_EnumOptions__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumOptions), descriptor.MessageTypes[12], - new string[] { "AllowAlias", "Deprecated", "UninterpretedOption", }); + new string[] { "AllowAlias", "Deprecated", "UninterpretedOption", }, new string[] { }); internal__static_google_protobuf_EnumValueOptions__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.EnumValueOptions), descriptor.MessageTypes[13], - new string[] { "Deprecated", "UninterpretedOption", }); + new string[] { "Deprecated", "UninterpretedOption", }, new string[] { }); internal__static_google_protobuf_ServiceOptions__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.ServiceOptions), descriptor.MessageTypes[14], - new string[] { "Deprecated", "UninterpretedOption", }); + new string[] { "Deprecated", "UninterpretedOption", }, new string[] { }); internal__static_google_protobuf_MethodOptions__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.MethodOptions), descriptor.MessageTypes[15], - new string[] { "Deprecated", "UninterpretedOption", }); + new string[] { "Deprecated", "UninterpretedOption", }, new string[] { }); internal__static_google_protobuf_UninterpretedOption__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.UninterpretedOption), descriptor.MessageTypes[16], - new string[] { "Name", "IdentifierValue", "PositiveIntValue", "NegativeIntValue", "DoubleValue", "StringValue", "AggregateValue", }); + new string[] { "Name", "IdentifierValue", "PositiveIntValue", "NegativeIntValue", "DoubleValue", "StringValue", "AggregateValue", }, new string[] { }); internal__static_google_protobuf_UninterpretedOption_NamePart__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.UninterpretedOption.Types.NamePart), descriptor.MessageTypes[16].NestedTypes[0], - new string[] { "NamePart_", "IsExtension", }); + new string[] { "NamePart_", "IsExtension", }, new string[] { }); internal__static_google_protobuf_SourceCodeInfo__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.SourceCodeInfo), descriptor.MessageTypes[17], - new string[] { "Location", }); + new string[] { "Location", }, new string[] { }); internal__static_google_protobuf_SourceCodeInfo_Location__FieldAccessorTable = new pb::FieldAccess.FieldAccessorTable(typeof(global::Google.Protobuf.DescriptorProtos.SourceCodeInfo.Types.Location), descriptor.MessageTypes[17].NestedTypes[0], - new string[] { "Path", "Span", "LeadingComments", "TrailingComments", "LeadingDetachedComments", }); + new string[] { "Path", "Span", "LeadingComments", "TrailingComments", "LeadingDetachedComments", }, new string[] { }); } #endregion From 6ea9bc7aa3af5c392fcd8f4eeebf71342c1ce961 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 10 Jul 2015 14:05:52 +0100 Subject: [PATCH 5/6] Fixes to JsonFormatter - Handle oneof properly - Omit unknown enum values --- .../ProtocolBuffers.Test/JsonFormatterTest.cs | 48 +++++++++++- csharp/src/ProtocolBuffers/JsonFormatter.cs | 75 ++++++++++++++++--- 2 files changed, 112 insertions(+), 11 deletions(-) diff --git a/csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs b/csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs index 5f80a49999..5441bf4797 100644 --- a/csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs +++ b/csharp/src/ProtocolBuffers.Test/JsonFormatterTest.cs @@ -160,10 +160,34 @@ namespace Google.Protobuf } [Test] - public void UnknownEnumValue() + public void UnknownEnumValueOmitted_SingleField() { var message = new TestAllTypes { SingleForeignEnum = (ForeignEnum) 100 }; - Assert.AreEqual("{ \"singleForeignEnum\": 100 }", JsonFormatter.Default.Format(message)); + Assert.AreEqual("{ }", JsonFormatter.Default.Format(message)); + } + + [Test] + public void UnknownEnumValueOmitted_RepeatedField() + { + var message = new TestAllTypes { RepeatedForeignEnum = { ForeignEnum.FOREIGN_BAZ, (ForeignEnum) 100, ForeignEnum.FOREIGN_FOO } }; + Assert.AreEqual("{ \"repeatedForeignEnum\": [ \"FOREIGN_BAZ\", \"FOREIGN_FOO\" ] }", JsonFormatter.Default.Format(message)); + } + + [Test] + public void UnknownEnumValueOmitted_MapField() + { + // This matches the C++ behaviour. + var message = new TestMap { MapInt32Enum = { { 1, MapEnum.MAP_ENUM_FOO }, { 2, (MapEnum) 100 }, { 3, MapEnum.MAP_ENUM_BAR } } }; + Assert.AreEqual("{ \"mapInt32Enum\": { \"1\": \"MAP_ENUM_FOO\", \"3\": \"MAP_ENUM_BAR\" } }", JsonFormatter.Default.Format(message)); + } + + [Test] + public void UnknownEnumValueOmitted_RepeatedField_AllEntriesUnknown() + { + // *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write... + // but this is what happens at the moment, and it doesn't seem too awful. + var message = new TestAllTypes { RepeatedForeignEnum = { (ForeignEnum) 200, (ForeignEnum) 100 } }; + Assert.AreEqual("{ \"repeatedForeignEnum\": [ ] }", JsonFormatter.Default.Format(message)); } [Test] @@ -213,5 +237,25 @@ namespace Google.Protobuf { Assert.AreEqual(expected, JsonFormatter.ToCamelCase(original)); } + + [Test] + [TestCase(null, "{ }")] + [TestCase("x", "{ \"fooString\": \"x\" }")] + [TestCase("", "{ \"fooString\": \"\" }")] + [TestCase(null, "{ }")] + public void Oneof(string fooStringValue, string expectedJson) + { + var message = new TestOneof(); + if (fooStringValue != null) + { + message.FooString = fooStringValue; + } + + // We should get the same result both with and without "format default values". + var formatter = new JsonFormatter(new JsonFormatter.Settings(false)); + Assert.AreEqual(expectedJson, formatter.Format(message)); + formatter = new JsonFormatter(new JsonFormatter.Settings(true)); + Assert.AreEqual(expectedJson, formatter.Format(message)); + } } } diff --git a/csharp/src/ProtocolBuffers/JsonFormatter.cs b/csharp/src/ProtocolBuffers/JsonFormatter.cs index a6aa552fb3..3e2244d653 100644 --- a/csharp/src/ProtocolBuffers/JsonFormatter.cs +++ b/csharp/src/ProtocolBuffers/JsonFormatter.cs @@ -136,13 +136,28 @@ namespace Google.Protobuf builder.Append("{ "); var fields = message.Fields; bool first = true; + // First non-oneof fields foreach (var accessor in fields.Accessors) { + var descriptor = accessor.Descriptor; + // Oneofs are written later + if (descriptor.ContainingOneof != null) + { + continue; + } + // Omit default values unless we're asked to format them object value = accessor.GetValue(message); if (!settings.FormatDefaultValues && IsDefaultValue(accessor, value)) { continue; } + // Omit awkward (single) values such as unknown enum values + if (!descriptor.IsRepeated && !descriptor.IsMap && !CanWriteSingleValue(accessor.Descriptor, value)) + { + continue; + } + + // Okay, all tests complete: let's write the field value... if (!first) { builder.Append(", "); @@ -152,6 +167,32 @@ namespace Google.Protobuf WriteValue(builder, accessor, value); first = false; } + + // Now oneofs + foreach (var accessor in fields.Oneofs) + { + var fieldDescriptor = accessor.GetCaseFieldDescriptor(message); + if (fieldDescriptor == null) + { + continue; + } + var fieldAccessor = fields[fieldDescriptor]; + object value = fieldAccessor.GetValue(message); + // Omit awkward (single) values such as unknown enum values + if (!fieldDescriptor.IsRepeated && !fieldDescriptor.IsMap && !CanWriteSingleValue(fieldDescriptor, value)) + { + continue; + } + + if (!first) + { + builder.Append(", "); + } + WriteString(builder, ToCamelCase(fieldDescriptor.Name)); + builder.Append(": "); + WriteValue(builder, fieldAccessor, value); + first = false; + } builder.Append(first ? "}" : " }"); } @@ -303,15 +344,8 @@ namespace Google.Protobuf } case FieldType.Enum: EnumValueDescriptor enumValue = descriptor.EnumType.FindValueByNumber((int) value); - if (enumValue != null) - { - WriteString(builder, enumValue.Name); - } - else - { - // ??? Need more documentation - builder.Append(((int) value).ToString("d", CultureInfo.InvariantCulture)); - } + // We will already have validated that this is a known value. + WriteString(builder, enumValue.Name); break; case FieldType.Fixed64: case FieldType.UInt64: @@ -354,6 +388,10 @@ namespace Google.Protobuf bool first = true; foreach (var value in list) { + if (!CanWriteSingleValue(accessor.Descriptor, value)) + { + continue; + } if (!first) { builder.Append(", "); @@ -373,6 +411,10 @@ namespace Google.Protobuf // This will box each pair. Could use IDictionaryEnumerator, but that's ugly in terms of disposal. foreach (DictionaryEntry pair in dictionary) { + if (!CanWriteSingleValue(valueType, pair.Value)) + { + continue; + } if (!first) { builder.Append(", "); @@ -409,6 +451,21 @@ namespace Google.Protobuf builder.Append(first ? "}" : " }"); } + /// + /// Returns whether or not a singular value can be represented in JSON. + /// Currently only relevant for enums, where unknown values can't be represented. + /// For repeated/map fields, this always returns true. + /// + private bool CanWriteSingleValue(FieldDescriptor descriptor, object value) + { + if (descriptor.FieldType == FieldType.Enum) + { + EnumValueDescriptor enumValue = descriptor.EnumType.FindValueByNumber((int) value); + return enumValue != null; + } + return true; + } + /// /// Writes a string (including leading and trailing double quotes) to a builder, escaping as required. /// From 0f34daad07153a66b492ab938e85f17e82b91706 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Tue, 14 Jul 2015 09:41:28 +0100 Subject: [PATCH 6/6] Changes suggested during review. - Remove the indexers in FieldAccessorTable - Add a TODO for field ordering in oneof --- .../FieldAccess/FieldAccessorTable.cs | 28 ++----------------- csharp/src/ProtocolBuffers/JsonFormatter.cs | 3 +- 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs index c69612fb63..80be93f548 100644 --- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs +++ b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs @@ -84,8 +84,8 @@ namespace Google.Protobuf.FieldAccess public ReadOnlyCollection Oneofs { get { return oneofs; } } - // TODO: Review the API for the indexers. Now that we have fields and oneofs, it's not as clear... - + // TODO: Review this, as it's easy to get confused between FieldNumber and Index. + // Currently only used to get an accessor related to a oneof... maybe just make that simpler? public IFieldAccessor this[int fieldNumber] { get @@ -94,29 +94,5 @@ namespace Google.Protobuf.FieldAccess return accessors[field.Index]; } } - - public IFieldAccessor this[FieldDescriptor field] - { - get - { - if (field.ContainingType != descriptor) - { - throw new ArgumentException("FieldDescriptor does not match message type."); - } - return accessors[field.Index]; - } - } - - public OneofAccessor this[OneofDescriptor oneof] - { - get - { - if (oneof.ContainingType != descriptor) - { - throw new ArgumentException("OneofDescriptor does not match message type."); - } - return oneofs[oneof.Index]; - } - } } } \ No newline at end of file diff --git a/csharp/src/ProtocolBuffers/JsonFormatter.cs b/csharp/src/ProtocolBuffers/JsonFormatter.cs index 3e2244d653..9bd628eb50 100644 --- a/csharp/src/ProtocolBuffers/JsonFormatter.cs +++ b/csharp/src/ProtocolBuffers/JsonFormatter.cs @@ -141,6 +141,7 @@ namespace Google.Protobuf { var descriptor = accessor.Descriptor; // Oneofs are written later + // TODO: Change to write out fields in order, interleaving oneofs appropriately (as per binary format) if (descriptor.ContainingOneof != null) { continue; @@ -176,7 +177,7 @@ namespace Google.Protobuf { continue; } - var fieldAccessor = fields[fieldDescriptor]; + var fieldAccessor = fields[fieldDescriptor.FieldNumber]; object value = fieldAccessor.GetValue(message); // Omit awkward (single) values such as unknown enum values if (!fieldDescriptor.IsRepeated && !fieldDescriptor.IsMap && !CanWriteSingleValue(fieldDescriptor, value))