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; ...@@ -35,8 +35,8 @@ using Proto2 = Google.Protobuf.TestProtos.Proto2;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions; using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
namespace Google.Protobuf.Reflection namespace Google.Protobuf.Reflection
...@@ -86,8 +86,8 @@ namespace Google.Protobuf.Reflection ...@@ -86,8 +86,8 @@ namespace Google.Protobuf.Reflection
var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].Accessor.GetValue(mapMessage); var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].Accessor.GetValue(mapMessage);
Assert.AreEqual(mapMessage.MapStringString, dictionary); Assert.AreEqual(mapMessage.MapStringString, dictionary);
Assert.AreEqual("value1", dictionary["key1"]); Assert.AreEqual("value1", dictionary["key1"]);
} }
[Test] [Test]
public void GetValue_IncorrectType() public void GetValue_IncorrectType()
{ {
...@@ -97,29 +97,29 @@ namespace Google.Protobuf.Reflection ...@@ -97,29 +97,29 @@ namespace Google.Protobuf.Reflection
} }
[Test] [Test]
public void HasValue_Proto3() public void HasValue_Proto3()
{ {
IMessage message = SampleMessages.CreateFullTestAllTypes(); IMessage message = SampleMessages.CreateFullTestAllTypes();
var fields = message.Descriptor.Fields; var fields = message.Descriptor.Fields;
Assert.Throws<InvalidOperationException>(() => (fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor as IFieldPresenceAccessor).HasValue(message)); Assert.Throws<InvalidOperationException>(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.HasValue(message));
} }
[Test] [Test]
public void HasValue() public void HasValue()
{ {
IMessage message = new Proto2.TestAllTypes(); IMessage message = new Proto2.TestAllTypes();
var fields = message.Descriptor.Fields; var fields = message.Descriptor.Fields;
var accessor = fields[Proto2.TestAllTypes.OptionalBoolFieldNumber].Accessor as IFieldPresenceAccessor; var accessor = fields[Proto2.TestAllTypes.OptionalBoolFieldNumber].Accessor;
Assert.False(accessor.HasValue(message)); Assert.False(accessor.HasValue(message));
accessor.SetValue(message, true); accessor.SetValue(message, true);
Assert.True(accessor.HasValue(message)); Assert.True(accessor.HasValue(message));
accessor.Clear(message); accessor.Clear(message);
Assert.False(accessor.HasValue(message)); Assert.False(accessor.HasValue(message));
} }
[Test] [Test]
public void SetValue_SingleFields() public void SetValue_SingleFields()
{ {
...@@ -146,8 +146,8 @@ namespace Google.Protobuf.Reflection ...@@ -146,8 +146,8 @@ namespace Google.Protobuf.Reflection
}; };
Assert.AreEqual(expected, message); Assert.AreEqual(expected, message);
} }
[Test] [Test]
public void SetValue_SingleFields_WrongType() public void SetValue_SingleFields_WrongType()
{ {
...@@ -190,8 +190,8 @@ namespace Google.Protobuf.Reflection ...@@ -190,8 +190,8 @@ namespace Google.Protobuf.Reflection
oneof.Accessor.Clear(message); oneof.Accessor.Clear(message);
Assert.AreEqual(TestProtos.TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); Assert.AreEqual(TestProtos.TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
} }
[Test] [Test]
public void Clear() public void Clear()
{ {
...@@ -243,26 +243,26 @@ namespace Google.Protobuf.Reflection ...@@ -243,26 +243,26 @@ namespace Google.Protobuf.Reflection
} }
[Test] [Test]
public void GetExtensionValue() public void GetExtensionValue()
{ {
var message = SampleMessages.CreateFullTestAllExtensions(); var message = SampleMessages.CreateFullTestAllExtensions();
// test that the reflector works, since the reflector just runs through IExtendableMessage // 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)); Assert.AreEqual(message.GetExtension(OptionalBoolExtension), Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(OptionalBoolExtension.FieldNumber).Accessor.GetValue(message));
} }
[Test] [Test]
public void GetRepeatedExtensionValue() public void GetRepeatedExtensionValue()
{ {
// check to make sure repeated accessor uses GetOrRegister // check to make sure repeated accessor uses GetOrRegister
var message = new Proto2.TestAllExtensions(); var message = new Proto2.TestAllExtensions();
Assert.IsNull(message.GetExtension(RepeatedBoolExtension)); Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
Assert.IsNotNull(Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(RepeatedBoolExtension.FieldNumber).Accessor.GetValue(message)); Assert.IsNotNull(Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(RepeatedBoolExtension.FieldNumber).Accessor.GetValue(message));
Assert.IsNotNull(message.GetExtension(RepeatedBoolExtension)); Assert.IsNotNull(message.GetExtension(RepeatedBoolExtension));
message.ClearExtension(RepeatedBoolExtension); message.ClearExtension(RepeatedBoolExtension);
Assert.IsNull(message.GetExtension(RepeatedBoolExtension)); Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
} }
} }
} }
...@@ -183,7 +183,7 @@ namespace Google.Protobuf ...@@ -183,7 +183,7 @@ namespace Google.Protobuf
} }
else if (f.FieldType == FieldType.Message || f.FieldType == FieldType.Group) 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(); return ((IMessage)f.Accessor.GetValue(message)).IsInitialized();
} }
...@@ -194,7 +194,7 @@ namespace Google.Protobuf ...@@ -194,7 +194,7 @@ namespace Google.Protobuf
} }
else if (f.IsRequired) else if (f.IsRequired)
{ {
return (f.Accessor as IFieldPresenceAccessor).HasValue(message); return f.Accessor.HasValue(message);
} }
else else
{ {
......
...@@ -58,6 +58,11 @@ namespace Google.Protobuf.Reflection ...@@ -58,6 +58,11 @@ namespace Google.Protobuf.Reflection
/// </summary> /// </summary>
object GetValue(IMessage message); 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> /// <summary>
/// Mutator for single "simple" fields only. /// Mutator for single "simple" fields only.
/// </summary> /// </summary>
...@@ -68,15 +73,4 @@ namespace Google.Protobuf.Reflection ...@@ -68,15 +73,4 @@ namespace Google.Protobuf.Reflection
/// <exception cref="InvalidOperationException">The field is not a "simple" field.</exception> /// <exception cref="InvalidOperationException">The field is not a "simple" field.</exception>
void SetValue(IMessage message, object value); 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 ...@@ -39,7 +39,7 @@ namespace Google.Protobuf.Reflection
/// <summary> /// <summary>
/// Accessor for single fields. /// Accessor for single fields.
/// </summary> /// </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. // 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) // 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