diff --git a/src/Primitively/EmbeddedResources/Integer/Base.cs b/src/Primitively/EmbeddedResources/Integer/Base.cs index 38b902d..d704f9e 100644 --- a/src/Primitively/EmbeddedResources/Integer/Base.cs +++ b/src/Primitively/EmbeddedResources/Integer/Base.cs @@ -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; @@ -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; diff --git a/test/Primitively.IntegrationTests/PrimitiveJsonConverterTests.cs b/test/Primitively.IntegrationTests/PrimitiveJsonConverterTests.cs index 6edf3c7..a49e917 100644 --- a/test/Primitively.IntegrationTests/PrimitiveJsonConverterTests.cs +++ b/test/Primitively.IntegrationTests/PrimitiveJsonConverterTests.cs @@ -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] @@ -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