From 5b9288e47d7add219717d472aa95a5cfe1141ac9 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 3 Jul 2015 12:45:36 +0100 Subject: Use the new JsonFormatter to implement ToString on generated messages. --- src/google/protobuf/compiler/csharp/csharp_field_base.h | 1 + src/google/protobuf/compiler/csharp/csharp_map_field.cc | 7 ++----- src/google/protobuf/compiler/csharp/csharp_message.cc | 5 ++++- 3 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.h b/src/google/protobuf/compiler/csharp/csharp_field_base.h index 349d835b..bffa2062 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 cb7ce5f0..32c05232 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 13544b26..ac135951 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) { -- cgit v1.2.3 From ef3464dff648362683a75ddf593c9d47ec3c33ce Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 10 Jul 2015 14:04:53 +0100 Subject: Oneof reflection support. (Generated code changes in next commit.) --- .../ProtocolBuffers.Test/GeneratedMessageTest.cs | 20 ++++++++ .../FieldAccess/FieldAccessorTable.cs | 29 +++++++++-- .../ProtocolBuffers/FieldAccess/OneofAccessor.cs | 57 ++++++++++------------ .../ProtocolBuffers/FieldAccess/ReflectionUtil.cs | 15 +++++- .../protobuf/compiler/csharp/csharp_message.cc | 1 + 5 files changed, 87 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs index 8cee9820..28c2195f 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 57ea9c87..c69612fb 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 feaa6232..590b6309 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 d3053920..08ef6c0c 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 ac135951..3fbec2b5 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", -- cgit v1.2.3