Commit 930db675 authored by Sydney Acksman's avatar Sydney Acksman

Revert changes for FieldPresenceAccessor

parent 29141f48
......@@ -35,8 +35,8 @@ using Proto2 = Google.Protobuf.TestProtos.Proto2;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
namespace Google.Protobuf.Reflection
......@@ -86,8 +86,8 @@ namespace Google.Protobuf.Reflection
var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].Accessor.GetValue(mapMessage);
Assert.AreEqual(mapMessage.MapStringString, dictionary);
Assert.AreEqual("value1", dictionary["key1"]);
}
}
[Test]
public void GetValue_IncorrectType()
{
......@@ -97,29 +97,29 @@ namespace Google.Protobuf.Reflection
}
[Test]
public void HasValue_Proto3()
{
IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.Fields;
Assert.Throws<InvalidOperationException>(() => (fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor as IFieldPresenceAccessor).HasValue(message));
public void HasValue_Proto3()
{
IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.Fields;
Assert.Throws<InvalidOperationException>(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.HasValue(message));
}
[Test]
public void HasValue()
{
IMessage message = new Proto2.TestAllTypes();
var fields = message.Descriptor.Fields;
var accessor = fields[Proto2.TestAllTypes.OptionalBoolFieldNumber].Accessor as IFieldPresenceAccessor;
Assert.False(accessor.HasValue(message));
accessor.SetValue(message, true);
Assert.True(accessor.HasValue(message));
accessor.Clear(message);
Assert.False(accessor.HasValue(message));
public void HasValue()
{
IMessage message = new Proto2.TestAllTypes();
var fields = message.Descriptor.Fields;
var accessor = fields[Proto2.TestAllTypes.OptionalBoolFieldNumber].Accessor;
Assert.False(accessor.HasValue(message));
accessor.SetValue(message, true);
Assert.True(accessor.HasValue(message));
accessor.Clear(message);
Assert.False(accessor.HasValue(message));
}
[Test]
public void SetValue_SingleFields()
{
......@@ -146,8 +146,8 @@ namespace Google.Protobuf.Reflection
};
Assert.AreEqual(expected, message);
}
}
[Test]
public void SetValue_SingleFields_WrongType()
{
......@@ -190,8 +190,8 @@ namespace Google.Protobuf.Reflection
oneof.Accessor.Clear(message);
Assert.AreEqual(TestProtos.TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
}
}
[Test]
public void Clear()
{
......@@ -243,26 +243,26 @@ namespace Google.Protobuf.Reflection
}
[Test]
public void GetExtensionValue()
{
var message = SampleMessages.CreateFullTestAllExtensions();
// test that the reflector works, since the reflector just runs through IExtendableMessage
Assert.AreEqual(message.GetExtension(OptionalBoolExtension), Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(OptionalBoolExtension.FieldNumber).Accessor.GetValue(message));
public void GetExtensionValue()
{
var message = SampleMessages.CreateFullTestAllExtensions();
// test that the reflector works, since the reflector just runs through IExtendableMessage
Assert.AreEqual(message.GetExtension(OptionalBoolExtension), Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(OptionalBoolExtension.FieldNumber).Accessor.GetValue(message));
}
[Test]
public void GetRepeatedExtensionValue()
{
// check to make sure repeated accessor uses GetOrRegister
var message = new Proto2.TestAllExtensions();
Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
Assert.IsNotNull(Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(RepeatedBoolExtension.FieldNumber).Accessor.GetValue(message));
Assert.IsNotNull(message.GetExtension(RepeatedBoolExtension));
message.ClearExtension(RepeatedBoolExtension);
Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
public void GetRepeatedExtensionValue()
{
// check to make sure repeated accessor uses GetOrRegister
var message = new Proto2.TestAllExtensions();
Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
Assert.IsNotNull(Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(RepeatedBoolExtension.FieldNumber).Accessor.GetValue(message));
Assert.IsNotNull(message.GetExtension(RepeatedBoolExtension));
message.ClearExtension(RepeatedBoolExtension);
Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
}
}
}
......@@ -183,7 +183,7 @@ namespace Google.Protobuf
}
else if (f.FieldType == FieldType.Message || f.FieldType == FieldType.Group)
{
if ((f.Accessor as IFieldPresenceAccessor).HasValue(message))
if (f.Accessor.HasValue(message))
{
return ((IMessage)f.Accessor.GetValue(message)).IsInitialized();
}
......@@ -194,7 +194,7 @@ namespace Google.Protobuf
}
else if (f.IsRequired)
{
return (f.Accessor as IFieldPresenceAccessor).HasValue(message);
return f.Accessor.HasValue(message);
}
else
{
......
......@@ -58,6 +58,11 @@ namespace Google.Protobuf.Reflection
/// </summary>
object GetValue(IMessage message);
/// <summary>
/// Indicates whether the field in the specified message is set. For proto3 fields, this throws an <see cref="InvalidOperationException"/>
/// </summary>
bool HasValue(IMessage message);
/// <summary>
/// Mutator for single "simple" fields only.
/// </summary>
......@@ -68,15 +73,4 @@ namespace Google.Protobuf.Reflection
/// <exception cref="InvalidOperationException">The field is not a "simple" field.</exception>
void SetValue(IMessage message, object value);
}
/// <summary>
/// Allows field presence to be checked reflectively. This is implemented for all single field accessors
/// </summary>
public interface IFieldPresenceAccessor : IFieldAccessor
{
/// <summary>
/// Indicates whether the field in the specified message is set. For proto3 fields, this throws an <see cref="InvalidOperationException"/>
/// </summary>
bool HasValue(IMessage message);
}
}
\ No newline at end of file
......@@ -39,7 +39,7 @@ namespace Google.Protobuf.Reflection
/// <summary>
/// Accessor for single fields.
/// </summary>
internal sealed class SingleFieldAccessor : FieldAccessorBase, IFieldPresenceAccessor
internal sealed class SingleFieldAccessor : FieldAccessorBase
{
// All the work here is actually done in the constructor - it creates the appropriate delegates.
// There are various cases to consider, based on the property type (message, string/bytes, or "genuine" primitive)
......
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