Commit c5c9c6a7 authored by Jie Luo's avatar Jie Luo

field presence Reflection and tests

parent 8bae6c09
syntax = "proto3";
package field_presence_test;
option java_package = "com.google.protobuf";
option java_outer_classname = "FieldPresenceTestProto";
option java_generate_equals_and_hash = true;
message TestAllTypes {
enum NestedEnum {
FOO = 0;
BAR = 1;
BAZ = 2;
}
message NestedMessage {
optional int32 value = 1;
}
optional int32 optional_int32 = 1;
optional string optional_string = 2;
optional bytes optional_bytes = 3;
optional NestedEnum optional_nested_enum = 4;
optional NestedMessage optional_nested_message = 5;
}
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2015 Google Inc. All rights reserved.
// Author: jieluo@google.com (Jie Luo)
//
// 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
// 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.Reflection;
using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Google.ProtocolBuffers
{
[TestClass]
class FieldPresenceTest
{
private void CheckHasMethodRemoved(Type proto2Type, Type proto3Type, string name)
{
Assert.NotNull(proto2Type.GetProperty(name));
Assert.NotNull(proto2Type.GetProperty("Has" + name));
Assert.NotNull(proto3Type.GetProperty(name));
Assert.Null(proto3Type.GetProperty("Has" + name));
}
[TestMethod]
public void TestHasMethod()
{
// Optional non-message fields don't have HasFoo method generated
Type proto2Type = typeof(TestAllTypes);
Type proto3Type = typeof(field_presence_test.TestAllTypes);
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");
proto2Type = typeof(TestAllTypes.Builder);
proto3Type = typeof(field_presence_test.TestAllTypes.Builder);
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");
// message fields still have the HasFoo method generated
Assert.False(field_presence_test.TestAllTypes.CreateBuilder().Build().HasOptionalNestedMessage);
Assert.False(field_presence_test.TestAllTypes.CreateBuilder().HasOptionalNestedMessage);
}
[TestMethod]
public void TestFieldPresence()
{
// Optional non-message fields set to their default value are treated the same
// way as not set.
// Serialization will ignore such fields.
field_presence_test.TestAllTypes.Builder builder = field_presence_test.TestAllTypes.CreateBuilder();
builder.SetOptionalInt32(0);
builder.SetOptionalString("");
builder.SetOptionalBytes(ByteString.Empty);
builder.SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.FOO);
field_presence_test.TestAllTypes message = builder.Build();
Assert.AreEqual(0, message.SerializedSize);
// Test merge
field_presence_test.TestAllTypes.Builder a = field_presence_test.TestAllTypes.CreateBuilder();
a.SetOptionalInt32(1);
a.SetOptionalString("x");
a.SetOptionalBytes(ByteString.CopyFromUtf8("y"));
a.SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.BAR);
a.MergeFrom(message);
field_presence_test.TestAllTypes messageA = a.Build();
Assert.AreEqual(1, messageA.OptionalInt32);
Assert.AreEqual("x", messageA.OptionalString);
Assert.AreEqual(ByteString.CopyFromUtf8("y"), messageA.OptionalBytes);
Assert.AreEqual(field_presence_test.TestAllTypes.Types.NestedEnum.BAR, messageA.OptionalNestedEnum);
// equals/hashCode should produce the same results
field_presence_test.TestAllTypes empty = field_presence_test.TestAllTypes.CreateBuilder().Build();
Assert.True(empty.Equals(message));
Assert.True(message.Equals(empty));
Assert.AreEqual(empty.GetHashCode(), message.GetHashCode());
}
[TestMethod]
public void TestFieldPresenceReflection()
{
MessageDescriptor descriptor = field_presence_test.TestAllTypes.Descriptor;
FieldDescriptor optionalInt32Field = descriptor.FindFieldByName("optional_int32");
FieldDescriptor optionalStringField = descriptor.FindFieldByName("optional_string");
FieldDescriptor optionalBytesField = descriptor.FindFieldByName("optional_bytes");
FieldDescriptor optionalNestedEnumField = descriptor.FindFieldByName("optional_nested_enum");
field_presence_test.TestAllTypes message = field_presence_test.TestAllTypes.CreateBuilder().Build();
Assert.False(message.HasField(optionalInt32Field));
Assert.False(message.HasField(optionalStringField));
Assert.False(message.HasField(optionalBytesField));
Assert.False(message.HasField(optionalNestedEnumField));
// Set to default value is seen as not present
message = field_presence_test.TestAllTypes.CreateBuilder()
.SetOptionalInt32(0)
.SetOptionalString("")
.SetOptionalBytes(ByteString.Empty)
.SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.FOO)
.Build();
Assert.False(message.HasField(optionalInt32Field));
Assert.False(message.HasField(optionalStringField));
Assert.False(message.HasField(optionalBytesField));
Assert.False(message.HasField(optionalNestedEnumField));
Assert.AreEqual(0, message.AllFields.Count);
// Set t0 non-defalut value is seen as present
message = field_presence_test.TestAllTypes.CreateBuilder()
.SetOptionalInt32(1)
.SetOptionalString("x")
.SetOptionalBytes(ByteString.CopyFromUtf8("y"))
.SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.BAR)
.Build();
Assert.True(message.HasField(optionalInt32Field));
Assert.True(message.HasField(optionalStringField));
Assert.True(message.HasField(optionalBytesField));
Assert.True(message.HasField(optionalNestedEnumField));
Assert.AreEqual(4, message.AllFields.Count);
}
[TestMethod]
public void TestMessageField()
{
field_presence_test.TestAllTypes.Builder builder = field_presence_test.TestAllTypes.CreateBuilder();
Assert.False(builder.HasOptionalNestedMessage);
Assert.False(builder.Build().HasOptionalNestedMessage);
// Unlike non-message fields, if we set default value to message field, the field
// shoule be seem as present.
builder.SetOptionalNestedMessage(field_presence_test.TestAllTypes.Types.NestedMessage.DefaultInstance);
Assert.True(builder.HasOptionalNestedMessage);
Assert.True(builder.Build().HasOptionalNestedMessage);
}
[TestMethod]
public void TestSeralizeAndParese()
{
field_presence_test.TestAllTypes.Builder builder = field_presence_test.TestAllTypes.CreateBuilder();
builder.SetOptionalInt32(1234);
builder.SetOptionalString("hello");
builder.SetOptionalNestedMessage(field_presence_test.TestAllTypes.Types.NestedMessage.DefaultInstance);
ByteString data = builder.Build().ToByteString();
field_presence_test.TestAllTypes message = field_presence_test.TestAllTypes.ParseFrom(data);
Assert.AreEqual(1234, message.OptionalInt32);
Assert.AreEqual("hello", message.OptionalString);
Assert.AreEqual(ByteString.Empty, message.OptionalBytes);
Assert.AreEqual(field_presence_test.TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
Assert.True(message.HasOptionalNestedMessage);
Assert.AreEqual(0, message.OptionalNestedMessage.Value);
}
}
}
......@@ -71,6 +71,7 @@
</Compile>
<Compile Include="AbstractMessageTest.cs" />
<Compile Include="ByteStringTest.cs" />
<Compile Include="FieldPresenceTest.cs" />
<Compile Include="CodedInputStreamTest.cs" />
<Compile Include="CodedOutputStreamTest.cs" />
<Compile Include="Collections\PopsicleListTest.cs" />
......@@ -82,6 +83,7 @@
<Compile Include="Compatibility\TextCompatibilityTests.cs" />
<Compile Include="Compatibility\XmlCompatibilityTests.cs" />
<Compile Include="SerializableAttribute.cs" />
<Compile Include="TestProtos\FieldPresense.cs" />
<Compile Include="TestProtos\GoogleSize.cs" />
<Compile Include="TestProtos\GoogleSpeed.cs" />
<Compile Include="TestProtos\Unittest.cs" />
......
This diff is collapsed.
......@@ -54,6 +54,22 @@ namespace Google.ProtocolBuffers.Descriptors
private readonly IList<FileDescriptor> publicDependencies;
private readonly DescriptorPool pool;
public enum Syntax
{
UNKNOWN,
PROTO2,
PROTO3
}
public Syntax GetSyntax()
{
if (proto.Syntax == "proto3")
{
return Syntax.PROTO3;
}
return Syntax.PROTO2;
}
private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies)
{
this.pool = pool;
......
......@@ -68,16 +68,21 @@ namespace Google.ProtocolBuffers.FieldAccess
{
this.descriptor = descriptor;
accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count];
bool supportFieldPresence = false;
if (descriptor.File.GetSyntax() == FileDescriptor.Syntax.PROTO2)
{
supportFieldPresence = true;
}
for (int i = 0; i < accessors.Length; i++)
{
accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i]);
accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], supportFieldPresence);
}
}
/// <summary>
/// Creates an accessor for a single field
/// </summary>
private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name)
private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name, bool supportFieldPresence)
{
if (field.IsRepeated)
{
......@@ -98,9 +103,9 @@ namespace Google.ProtocolBuffers.FieldAccess
case MappedType.Message:
return new SingleMessageAccessor<TMessage, TBuilder>(name);
case MappedType.Enum:
return new SingleEnumAccessor<TMessage, TBuilder>(field, name);
return new SingleEnumAccessor<TMessage, TBuilder>(field, name, supportFieldPresence);
default:
return new SinglePrimitiveAccessor<TMessage, TBuilder>(name);
return new SinglePrimitiveAccessor<TMessage, TBuilder>(field, name, supportFieldPresence);
}
}
}
......
......@@ -42,7 +42,7 @@ namespace Google.ProtocolBuffers.FieldAccess
{
private readonly EnumDescriptor enumDescriptor;
internal SingleEnumAccessor(FieldDescriptor field, string name) : base(name)
internal SingleEnumAccessor(FieldDescriptor field, string name, bool supportFieldPresence) : base(field, name, supportFieldPresence)
{
enumDescriptor = field.EnumType;
}
......
......@@ -48,7 +48,7 @@ namespace Google.ProtocolBuffers.FieldAccess
/// </summary>
private readonly Func<IBuilder> createBuilderDelegate;
internal SingleMessageAccessor(string name) : base(name)
internal SingleMessageAccessor(string name) : base(null, name, true)
{
MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", ReflectionUtil.EmptyTypes);
if (createBuilderMethod == null)
......
......@@ -31,6 +31,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Reflection;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers.FieldAccess
{
......@@ -42,6 +43,7 @@ namespace Google.ProtocolBuffers.FieldAccess
where TBuilder : IBuilder<TMessage, TBuilder>
{
private readonly Type clrType;
private readonly FieldDescriptor field;
private readonly Func<TMessage, object> getValueDelegate;
private readonly Action<TBuilder, object> setValueDelegate;
private readonly Func<TMessage, bool> hasDelegate;
......@@ -56,18 +58,28 @@ namespace Google.ProtocolBuffers.FieldAccess
get { return clrType; }
}
internal SinglePrimitiveAccessor(string name)
internal SinglePrimitiveAccessor(FieldDescriptor fieldDesriptor, string name, bool supportFieldPresence)
{
field = fieldDesriptor;
PropertyInfo messageProperty = typeof(TMessage).GetProperty(name, null, ReflectionUtil.EmptyTypes);
PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes);
PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name);
MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name);
if (messageProperty == null || builderProperty == null || hasProperty == null || clearMethod == null)
if (messageProperty == null || builderProperty == null || clearMethod == null)
{
throw new ArgumentException("Not all required properties/methods available");
}
if (supportFieldPresence)
{
PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name);
if (hasProperty == null)
{
throw new ArgumentException("Has properties not available");
}
hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
}
clrType = messageProperty.PropertyType;
hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod);
getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod());
......@@ -75,6 +87,10 @@ namespace Google.ProtocolBuffers.FieldAccess
public bool Has(TMessage message)
{
if (hasDelegate == null)
{
return !GetValue(message).Equals(field.DefaultValue);
}
return hasDelegate(message);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment