Skip to content

Commit

Permalink
fix: additional tweaks to patch integer HasValue bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Mar 30, 2024
1 parent cb38b89 commit 13026ae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/Primitively/EmbeddedResources/Integer/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
public const global::PRIMITIVE_VALUE_TYPE Minimum = PRIMITIVE_MINIMUM;
public const global::PRIMITIVE_VALUE_TYPE Maximum = PRIMITIVE_MAXIMUM;

public PRIMITIVE_TYPE()
{
HasValue = IsMatch(_value);
_value = default;
}

public PRIMITIVE_TYPE(global::PRIMITIVE_VALUE_TYPE value)
{
if (value >= Minimum && value <= Maximum)
{
_value = value;
HasValue = true;
}
HasValue = IsMatch(value);
_value = HasValue ? value : default;
}

private PRIMITIVE_TYPE(string value)
{
if (global::PRIMITIVE_VALUE_TYPE.TryParse(value, out var result) && result >= Minimum && result <= Maximum)
{
_value = result;
HasValue = true;
}
HasValue = global::PRIMITIVE_VALUE_TYPE.TryParse(value, out var result) && IsMatch(result);
_value = HasValue ? result : default;
}

object global::Primitively.IPrimitive.Value => _value;
Expand Down Expand Up @@ -49,3 +49,5 @@ private PRIMITIVE_TYPE(string value)

public static PRIMITIVE_TYPE Parse(string value) => new(value);
public static bool TryParse(string value, out PRIMITIVE_TYPE result) => (result = new(value)).HasValue;

private static bool IsMatch(global::PRIMITIVE_VALUE_TYPE value) => value >= Minimum && value <= Maximum;
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void JsonConverter_CanReadDefault()

var result = converter.Read(ref reader, typeof(TPrimitive), new JsonSerializerOptions());
result.Should().BeAssignableTo(typeof(TPrimitive));
result.Should().BeEquivalentTo(default(TPrimitive));
result.Should().BeEquivalentTo(new TPrimitive());
}

[Fact]
Expand All @@ -67,7 +67,7 @@ public void JsonConverter_CanReadNull()

var result = converter.Read(ref reader, typeof(TPrimitive), new JsonSerializerOptions());
result.Should().BeAssignableTo(typeof(TPrimitive));
result.Should().BeEquivalentTo(default(TPrimitive));
result.Should().BeEquivalentTo(new TPrimitive());
}

#if NET6_0_OR_GREATER
Expand Down

0 comments on commit 13026ae

Please sign in to comment.