Skip to content

Commit

Permalink
task: added support for Minimum and Maximum constraints on Single sou…
Browse files Browse the repository at this point in the history
…rce-gen types
  • Loading branch information
dtanglr committed Apr 3, 2024
1 parent e6e87ae commit fe7879e
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Primitively.Abstractions" Version="1.5.0-alpha.18" />
<PackageReference Include="Primitively.Abstractions" Version="1.5.0-alpha.24" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Primitively" Version="1.5.0-alpha.18" PrivateAssets="all" />
<PackageReference Include="Primitively" Version="1.5.0-alpha.24" PrivateAssets="all" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<PackageOutputPath>$(MSBuildThisFileDirectory)..\artifacts</PackageOutputPath>
<version>1.5.0-alpha.18</version>
<version>1.5.0-alpha.24</version>
</PropertyGroup>

<ItemGroup>
Expand Down
21 changes: 17 additions & 4 deletions src/Primitively.Abstractions/DoubleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
/// public partial record struct Example;
/// </code>
/// <code>
/// [Double(Minimum = 1)]
/// [Double(Minimum = 1.10d)]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Double(Minimum = 1, Maximum = 100)]
/// [Double(Minimum = -10.10d, Maximum = 10.10d)]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Double(2, Minimum = -10.10d, Maximum = 10.10d)]
/// public partial record struct Example;
/// </code>
/// </example>
Expand Down Expand Up @@ -55,27 +59,36 @@ public DoubleAttribute(int digits, MidpointRounding mode)
/// <summary>
/// Gets the number of fractional digits in the value of the source generated Primitively <see cref="IDouble"/> type.
/// </summary>
/// <remarks>
/// Valid values are: -1 to 15.
/// Values above 15 will default to: -1.
/// Values below 0 with default to: -1.
/// A value of -1 will result in: no rounding.
/// </remarks>
public int Digits { get; }

/// <summary>
/// Gets or sets the maximum value supported by the source generated Primitively <see cref="IDouble"/> type.
/// </summary>
/// <value>
/// The default value is 255. An assigned value should not be less than the <see cref="Minimum"/> value.
/// The default value is double.MaxValue. An assigned value should not be less than the <see cref="Minimum"/> value.
/// </value>
public new double Maximum { get; set; }

/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="IDouble"/> type.
/// </summary>
/// <value>
/// The default value is 0. An assigned value should not be greater than the <see cref="Maximum"/> value.
/// The default value is double.MinValue. An assigned value should not be greater than the <see cref="Maximum"/> value.
/// </value>
public new double Minimum { get; set; }

/// <summary>
/// Gets the rounding specification for how to round value of the source generated Primitively <see cref="IDouble"/> type
/// if it is midway between two other numbers.
/// </summary>
/// <remarks>
/// If the value of <see cref="Digits"/> is -1, this property will have no effect.
/// </remarks>
public MidpointRounding Mode { get; }
}
59 changes: 55 additions & 4 deletions src/Primitively.Abstractions/SingleAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
/// public partial record struct Example;
/// </code>
/// <code>
/// [Single(Minimum = 1)]
/// [Single(Minimum = 1.10f)]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Single(Minimum = 1, Maximum = 100)]
/// [Single(Minimum = -10.10f, Maximum = 10.10f)]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Single(2, Minimum = -10.10f, Maximum = 10.10f)]
/// public partial record struct Example;
/// </code>
/// </example>
Expand All @@ -25,19 +29,66 @@
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class SingleAttribute : NumericAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="SingleAttribute"/> class.
/// </summary>
public SingleAttribute()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SingleAttribute"/> class with the specified number of fractional digits.
/// </summary>
/// <param name="digits">The number of fractional digits in the value of the source generated Primitively <see cref="ISingle"/> type.</param>
public SingleAttribute(int digits)
{
Digits = digits;
}

/// <summary>
/// Initializes a new instance of the <see cref="SingleAttribute"/> class with the specified number of fractional digits and rounding mode.
/// </summary>
/// <param name="digits">The number of fractional digits in the value of the source generated Primitively <see cref="ISingle"/> type.</param>
/// <param name="mode">The rounding specification for how to round value of the source generated Primitively <see cref="ISingle"/> type if it is midway between two other numbers.</param>
public SingleAttribute(int digits, MidpointRounding mode)
{
Digits = digits;
Mode = mode;
}

/// <summary>
/// Gets the number of fractional digits in the value of the source generated Primitively <see cref="IDouble"/> type.
/// </summary>
/// <remarks>
/// Valid values are: -1 to 6.
/// Values above 6 will default to: -1.
/// Values below 0 with default to: -1.
/// A value of -1 will result in: no rounding.
/// </remarks>
public int Digits { get; }

/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="ISingle"/> type.
/// </summary>
/// <value>
/// The default value is 0. An assigned value should not be greater than the <see cref="Maximum"/> value.
/// The default value is float.MaxValue. An assigned value should not be greater than the <see cref="Maximum"/> value.
/// </value>
public new float Minimum { get; set; }

/// <summary>
/// Gets or sets the maximum value supported by the source generated Primitively <see cref="ISingle"/> type.
/// </summary>
/// <value>
/// The default value is 255. An assigned value should not be less than the <see cref="Minimum"/> value.
/// The default value is float.MinValue. An assigned value should not be less than the <see cref="Minimum"/> value.
/// </value>
public new float Maximum { get; set; }

/// <summary>
/// Gets the rounding specification for how to round value of the source generated Primitively <see cref="ISingle"/> type
/// if it is midway between two other numbers.
/// </summary>
/// <remarks>
/// If the value of <see cref="Digits"/> is -1, this property will have no effect.
/// </remarks>
public MidpointRounding Mode { get; }
}
26 changes: 26 additions & 0 deletions src/Primitively.Abstractions/SingleInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Primitively;

/// <summary>
/// This class represents metadata properties common to all source generated Primitively numeric types.
/// </summary>
/// <param name="DataType">The <see cref="DataType"/> enum representation of the Primitively type.</param>
/// <param name="Type">The .NET type of the Primitively type.</param>
/// <param name="ValueType">The .NET type of the encapsulated value.</param>
/// <param name="Example">An optional example of the integer.</param>
/// <param name="CreateFrom">A function that creates an instance of the Primitively type from a string.</param>
/// <param name="Minimum">The minimum value that can be set on the source generated Primitively type.</param>
/// <param name="Maximum">The maximum value that can be set on the source generated Primitively type.</param>
/// <param name="Digits">The number of fractional digits in the value on the source generated Primitively type</param>
/// <param name="Mode">The rounding specification for how to round value of the source generated Primitively type
/// if it is midway between two other numbers.</param>
public record SingleInfo(
DataType DataType,
Type Type,
Type ValueType,
string? Example,
Func<string?, IPrimitive> CreateFrom,
float Minimum,
float Maximum,
int Digits,
MidpointRounding Mode)
: NumericInfo<float>(DataType, Type, ValueType, Example, CreateFrom, Minimum, Maximum);
3 changes: 2 additions & 1 deletion src/Primitively/EmbeddedResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ public readonly struct Numeric
public readonly struct FloatingPoint
{
public static readonly string Base = GetEmbeddedResource(nameof(Numeric), nameof(FloatingPoint), nameof(Base));
public static readonly string DoublePreMatchCheckMethod = GetEmbeddedResource(nameof(Numeric), nameof(FloatingPoint), nameof(DoublePreMatchCheckMethod));
public static readonly string SinglePreMatchCheckMethod = GetEmbeddedResource(nameof(Numeric), nameof(FloatingPoint), nameof(SinglePreMatchCheckMethod));
}

public readonly struct Integer
{
public static readonly string Base = GetEmbeddedResource(nameof(Numeric), nameof(Integer), nameof(Base));
}

public static readonly string Base = GetEmbeddedResource(nameof(Numeric), nameof(Base));
public static readonly string JsonConverter = GetEmbeddedResource(nameof(Numeric), nameof(JsonConverter));
public static readonly string TypeConverter = GetEmbeddedResource(nameof(Numeric), nameof(TypeConverter));
}
Expand Down
53 changes: 0 additions & 53 deletions src/Primitively/EmbeddedResources/Numeric/Base.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/Primitively/EmbeddedResources/Numeric/FloatingPoint/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,3 @@ private PRIMITIVE_TYPE(string 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;

private static void PreMatchCheck(ref global::PRIMITIVE_VALUE_TYPE value)
{
if (Digits > 0)
{
value = global::System.Math.Round(value, Digits, Mode);
}
else if (Digits == 0)
{
value = global::System.Math.Round(value, Mode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

private static void PreMatchCheck(ref global::PRIMITIVE_VALUE_TYPE value)
{
if (Digits >= 0)
{
value = global::System.Math.Round(value, Digits, Mode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

private static void PreMatchCheck(ref global::PRIMITIVE_VALUE_TYPE value)
{
if (Digits >= 0)
{
#if NETCOREAPP2_0_OR_GREATER
value = global::System.MathF.Round(value, Digits, Mode);
#else
value = (float)global::System.Math.Round(value, Digits, Mode);
#endif
}
}
32 changes: 17 additions & 15 deletions src/Primitively/MetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,31 +194,33 @@ public readonly struct UShort
public static readonly string Type = typeof(ushort).FullName;
}

/// <summary>
/// Contains metadata about the Single (float) Primitively type.
/// </summary>
public readonly struct Single
{
public static readonly string Example = $"{0.60344}";
public static readonly string Interface = typeof(ISingle).FullName;
public static readonly string JsonReaderMethod = "TryGetSingle";
public static readonly float Maximum = float.MaxValue;
public static readonly float Minimum = float.MinValue;
public static readonly string Type = typeof(float).FullName;
}

/// <summary>
/// Contains metadata about the Double Primitively type.
/// </summary>
public readonly struct Double
{
public static readonly string Example = $"{0.60344}";
public static readonly string Example = string.Format("{0:N15}", double.MaxValue / 2);
public static readonly string Interface = typeof(IDouble).FullName;
public static readonly string JsonReaderMethod = "TryGetDouble";
public static readonly double Maximum = double.MaxValue;
public static readonly double Minimum = double.MinValue;
public static readonly string Type = typeof(double).FullName;
public static readonly int Digits = -1;
public static readonly int Digits = -1; // Value of -1 means no rounding. Minimum value is 0. Maximum value is 15.
public static readonly MidpointRounding Mode = MidpointRounding.ToEven;
}

/// <summary>
/// Contains metadata about the Single (float) Primitively type.
/// </summary>
public readonly struct Single
{
public static readonly string Example = string.Format("{0:N6}", float.MaxValue / 2);
public static readonly string Interface = typeof(ISingle).FullName;
public static readonly string JsonReaderMethod = "TryGetSingle";
public static readonly float Maximum = float.MaxValue;
public static readonly float Minimum = float.MinValue;
public static readonly string Type = typeof(float).FullName;
public static readonly int Digits = -1; // Value of -1 means no rounding. Minimum value is 0. Maximum value is 6.
public static readonly MidpointRounding Mode = MidpointRounding.ToEven;
}
}
Expand Down
Loading

0 comments on commit fe7879e

Please sign in to comment.