Skip to content

Commit 13026ae

Browse files
committed
fix: additional tweaks to patch integer HasValue bugs
1 parent cb38b89 commit 13026ae

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/Primitively/EmbeddedResources/Integer/Base.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
public const global::PRIMITIVE_VALUE_TYPE Minimum = PRIMITIVE_MINIMUM;
77
public const global::PRIMITIVE_VALUE_TYPE Maximum = PRIMITIVE_MAXIMUM;
88

9+
public PRIMITIVE_TYPE()
10+
{
11+
HasValue = IsMatch(_value);
12+
_value = default;
13+
}
14+
915
public PRIMITIVE_TYPE(global::PRIMITIVE_VALUE_TYPE value)
1016
{
11-
if (value >= Minimum && value <= Maximum)
12-
{
13-
_value = value;
14-
HasValue = true;
15-
}
17+
HasValue = IsMatch(value);
18+
_value = HasValue ? value : default;
1619
}
1720

1821
private PRIMITIVE_TYPE(string value)
1922
{
20-
if (global::PRIMITIVE_VALUE_TYPE.TryParse(value, out var result) && result >= Minimum && result <= Maximum)
21-
{
22-
_value = result;
23-
HasValue = true;
24-
}
23+
HasValue = global::PRIMITIVE_VALUE_TYPE.TryParse(value, out var result) && IsMatch(result);
24+
_value = HasValue ? result : default;
2525
}
2626

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

5050
public static PRIMITIVE_TYPE Parse(string value) => new(value);
5151
public static bool TryParse(string value, out PRIMITIVE_TYPE result) => (result = new(value)).HasValue;
52+
53+
private static bool IsMatch(global::PRIMITIVE_VALUE_TYPE value) => value >= Minimum && value <= Maximum;

test/Primitively.IntegrationTests/PrimitiveJsonConverterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void JsonConverter_CanReadDefault()
4848

4949
var result = converter.Read(ref reader, typeof(TPrimitive), new JsonSerializerOptions());
5050
result.Should().BeAssignableTo(typeof(TPrimitive));
51-
result.Should().BeEquivalentTo(default(TPrimitive));
51+
result.Should().BeEquivalentTo(new TPrimitive());
5252
}
5353

5454
[Fact]
@@ -67,7 +67,7 @@ public void JsonConverter_CanReadNull()
6767

6868
var result = converter.Read(ref reader, typeof(TPrimitive), new JsonSerializerOptions());
6969
result.Should().BeAssignableTo(typeof(TPrimitive));
70-
result.Should().BeEquivalentTo(default(TPrimitive));
70+
result.Should().BeEquivalentTo(new TPrimitive());
7171
}
7272

7373
#if NET6_0_OR_GREATER

0 commit comments

Comments
 (0)