Remove dead HasValue code for ExtensionValue and add null-checks to ExtensionSet.Set
This commit is contained in:
parent
8dc69ede45
commit
f4cfd2def3
@ -115,6 +115,8 @@ namespace Google.Protobuf
|
||||
/// </summary>
|
||||
public static void Set<TTarget, TValue>(ref ExtensionSet<TTarget> set, Extension<TTarget, TValue> extension, TValue value) where TTarget : IExtendableMessage<TTarget>
|
||||
{
|
||||
ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value));
|
||||
|
||||
IExtensionValue extensionValue;
|
||||
if (set == null)
|
||||
{
|
||||
|
@ -47,7 +47,6 @@ namespace Google.Protobuf
|
||||
|
||||
internal sealed class ExtensionValue<T> : IExtensionValue
|
||||
{
|
||||
private bool hasValue;
|
||||
private T field;
|
||||
private FieldCodec<T> codec;
|
||||
|
||||
@ -59,10 +58,6 @@ namespace Google.Protobuf
|
||||
|
||||
public int CalculateSize()
|
||||
{
|
||||
if (!hasValue)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return codec.CalculateSizeWithTag(field);
|
||||
}
|
||||
|
||||
@ -70,7 +65,6 @@ namespace Google.Protobuf
|
||||
{
|
||||
return new ExtensionValue<T>(codec)
|
||||
{
|
||||
hasValue = hasValue,
|
||||
field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field
|
||||
};
|
||||
}
|
||||
@ -82,7 +76,6 @@ namespace Google.Protobuf
|
||||
|
||||
return other is ExtensionValue<T>
|
||||
&& codec.Equals((other as ExtensionValue<T>).codec)
|
||||
&& hasValue.Equals((other as ExtensionValue<T>).hasValue)
|
||||
&& Equals(field, (other as ExtensionValue<T>).field);
|
||||
// we check for equality in the codec since we could have equal field values however the values could be written in different ways
|
||||
}
|
||||
@ -92,7 +85,6 @@ namespace Google.Protobuf
|
||||
unchecked
|
||||
{
|
||||
int hash = 17;
|
||||
hash = hash * 31 + hasValue.GetHashCode();
|
||||
hash = hash * 31 + field.GetHashCode();
|
||||
hash = hash * 31 + codec.GetHashCode();
|
||||
return hash;
|
||||
@ -101,7 +93,6 @@ namespace Google.Protobuf
|
||||
|
||||
public void MergeFrom(CodedInputStream input)
|
||||
{
|
||||
hasValue = true;
|
||||
codec.ValueMerger(input, ref field);
|
||||
}
|
||||
|
||||
@ -109,24 +100,18 @@ namespace Google.Protobuf
|
||||
{
|
||||
if (value is ExtensionValue<T>)
|
||||
{
|
||||
var extensionValue = value as ExtensionValue<T>;
|
||||
if (extensionValue.hasValue)
|
||||
{
|
||||
hasValue |= codec.FieldMerger(ref field, extensionValue.field);
|
||||
}
|
||||
var extensionValue = value as ExtensionValue<T>;
|
||||
codec.FieldMerger(ref field, extensionValue.field);
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteTo(CodedOutputStream output)
|
||||
{
|
||||
if (hasValue)
|
||||
output.WriteTag(codec.Tag);
|
||||
codec.ValueWriter(output, field);
|
||||
if (codec.EndTag != 0)
|
||||
{
|
||||
output.WriteTag(codec.Tag);
|
||||
codec.ValueWriter(output, field);
|
||||
if (codec.EndTag != 0)
|
||||
{
|
||||
output.WriteTag(codec.EndTag);
|
||||
}
|
||||
output.WriteTag(codec.EndTag);
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,15 +119,19 @@ namespace Google.Protobuf
|
||||
|
||||
public void SetValue(T value)
|
||||
{
|
||||
hasValue = true;
|
||||
field = value;
|
||||
}
|
||||
|
||||
public bool HasValue => hasValue;
|
||||
|
||||
public bool IsInitialized()
|
||||
{
|
||||
return HasValue && field is IMessage && (field as IMessage).IsInitialized();
|
||||
if (field is IMessage)
|
||||
{
|
||||
return (field as IMessage).IsInitialized();
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -254,11 +254,8 @@ namespace Google.Protobuf.Reflection
|
||||
if (extensionValue is ExtensionValue<T>)
|
||||
{
|
||||
ExtensionValue<T> single = extensionValue as ExtensionValue<T>;
|
||||
if (single.HasValue)
|
||||
{
|
||||
value = single.GetValue();
|
||||
return true;
|
||||
}
|
||||
value = single.GetValue();
|
||||
return true;
|
||||
}
|
||||
else if (extensionValue is RepeatedExtensionValue<T>)
|
||||
{
|
||||
@ -279,11 +276,8 @@ namespace Google.Protobuf.Reflection
|
||||
var typeArgs = typeInfo.GenericTypeArguments;
|
||||
if (typeArgs.Length == 1 && typeArgs[0].GetTypeInfo().IsEnum)
|
||||
{
|
||||
if ((bool)typeInfo.GetDeclaredProperty(nameof(ExtensionValue<T>.HasValue)).GetValue(extensionValue))
|
||||
{
|
||||
value = (T)typeInfo.GetDeclaredMethod(nameof(ExtensionValue<T>.GetValue)).Invoke(extensionValue, EmptyParameters);
|
||||
return true;
|
||||
}
|
||||
value = (T)typeInfo.GetDeclaredMethod(nameof(ExtensionValue<T>.GetValue)).Invoke(extensionValue, EmptyParameters);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (type.GetGenericTypeDefinition() == typeof(RepeatedExtensionValue<>))
|
||||
|
Loading…
Reference in New Issue
Block a user