Skip to content

Commit

Permalink
Merge pull request #5 from dtanglr/feature/floating-point-numerics
Browse files Browse the repository at this point in the history
task: added support for floating-point numeric types
  • Loading branch information
dtanglr authored Mar 22, 2024
2 parents bbf8c38 + bf9d0dd commit e8da8a6
Show file tree
Hide file tree
Showing 128 changed files with 2,395 additions and 324 deletions.
2 changes: 1 addition & 1 deletion src/Primitively.Abstractions/ByteAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class ByteAttribute : IntegerAttribute
public sealed class ByteAttribute : NumericAttribute
{
/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="IByte"/> type.
Expand Down
17 changes: 16 additions & 1 deletion src/Primitively.Abstractions/DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,20 @@ public enum DataType
/// <summary>
/// Represents an <see cref="IUShort"/> type that encapsulates a <see cref="ushort"/> value.
/// </summary>
UShort
UShort,

/// <summary>
/// Represents an <see cref="ISingle"/> type that encapsulates a <see cref="float"/> value.
/// </summary>
Single,

/// <summary>
/// Represents an <see cref="IDouble"/> type that encapsulates a <see cref="double"/> value.
/// </summary>
Double,

/// <summary>
/// Represents an <see cref="IDecimal"/> type that encapsulates a <see cref="decimal"/> value.
/// </summary>
Decimal
}
43 changes: 43 additions & 0 deletions src/Primitively.Abstractions/DecimalAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Primitively;

/// <summary>
/// This attribute should be used on a <c>partial record struct</c> to source generate
/// a Primitively <see cref="IDecimal"/> type that encapsulates a <see cref="decimal"/> value.
/// </summary>
/// <example>
/// These examples show how to use the Decimal attribute to source generate a Primitively <see cref="IDecimal"/> type.
/// <code>
/// [Decimal]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Decimal(Minimum = 1)]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Decimal(Minimum = 1, Maximum = 100)]
/// public partial record struct Example;
/// </code>
/// </example>
/// <remarks>
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class DecimalAttribute : NumericAttribute
{
/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="IDecimal"/> type.
/// </summary>
/// <value>
/// The default value is 0. An assigned value should not be greater than the <see cref="Maximum"/> value.
/// </value>
public new decimal Minimum { get; set; }

/// <summary>
/// Gets or sets the maximum value supported by the source generated Primitively <see cref="IDecimal"/> type.
/// </summary>
/// <value>
/// The default value is 255. An assigned value should not be less than the <see cref="Minimum"/> value.
/// </value>
public new decimal Maximum { get; set; }
}
43 changes: 43 additions & 0 deletions src/Primitively.Abstractions/DoubleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Primitively;

/// <summary>
/// This attribute should be used on a <c>partial record struct</c> to source generate
/// a Primitively <see cref="IDouble"/> type that encapsulates a <see cref="double"/> value.
/// </summary>
/// <example>
/// These examples show how to use the Double attribute to source generate a Primitively <see cref="IDouble"/> type.
/// <code>
/// [Double]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Double(Minimum = 1)]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Double(Minimum = 1, Maximum = 100)]
/// public partial record struct Example;
/// </code>
/// </example>
/// <remarks>
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class DoubleAttribute : NumericAttribute
{
/// <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.
/// </value>
public new double Minimum { get; set; }

/// <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.
/// </value>
public new double Maximum { get; set; }
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/IByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates a <see cref="byte"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IByte : IPrimitive<byte>, IInteger
public interface IByte : IPrimitive<byte>, INumeric
{
}
11 changes: 11 additions & 0 deletions src/Primitively.Abstractions/IDecimal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Primitively;

/// <summary>
/// Defines a contract for a Primitively type that encapsulates a <see cref="decimal"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IDecimal : IPrimitive<decimal>, INumeric
{
}
11 changes: 11 additions & 0 deletions src/Primitively.Abstractions/IDouble.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Primitively;

/// <summary>
/// Defines a contract for a Primitively type that encapsulates a <see cref="double"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IDouble : IPrimitive<double>, INumeric
{
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/IInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates an <see cref="int"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IInt : IPrimitive<int>, IInteger
public interface IInt : IPrimitive<int>, INumeric
{
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/ILong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates a <see cref="long"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface ILong : IPrimitive<long>, IInteger
public interface ILong : IPrimitive<long>, INumeric
{
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Primitively;

/// <summary>
/// Defines a contract for a Primitively type that encapsulates an integer value.
/// Defines a contract for a Primitively type that encapsulates a numeric value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from the <see cref="IPrimitive"/> interface.
/// </remarks>
public interface IInteger : IPrimitive
public interface INumeric : IPrimitive
{
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/ISByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates an <see cref="sbyte"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface ISByte : IPrimitive<sbyte>, IInteger
public interface ISByte : IPrimitive<sbyte>, INumeric
{
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/IShort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates a <see cref="short"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IShort : IPrimitive<short>, IInteger
public interface IShort : IPrimitive<short>, INumeric
{
}
11 changes: 11 additions & 0 deletions src/Primitively.Abstractions/ISingle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Primitively;

/// <summary>
/// Defines a contract for a Primitively type that encapsulates a <see cref="float"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface ISingle : IPrimitive<float>, INumeric
{
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/IUInt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates a <see cref="uint"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IUInt : IPrimitive<uint>, IInteger
public interface IUInt : IPrimitive<uint>, INumeric
{
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/IULong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates a <see cref="ulong"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IULong : IPrimitive<ulong>, IInteger
public interface IULong : IPrimitive<ulong>, INumeric
{
}
4 changes: 2 additions & 2 deletions src/Primitively.Abstractions/IUShort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
/// Defines a contract for a Primitively type that encapsulates a <see cref="ushort"/> value.
/// </summary>
/// <remarks>
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="IInteger"/> interfaces.
/// Implementations of this interface are source generated by Primitively and inherit from both <see cref="IPrimitive{T}"/> and <see cref="INumeric"/> interfaces.
/// </remarks>
public interface IUShort : IPrimitive<ushort>, IInteger
public interface IUShort : IPrimitive<ushort>, INumeric
{
}
2 changes: 1 addition & 1 deletion src/Primitively.Abstractions/IntAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class IntAttribute : IntegerAttribute
public sealed class IntAttribute : NumericAttribute
{
/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="IInt"/> type.
Expand Down
2 changes: 1 addition & 1 deletion src/Primitively.Abstractions/LongAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class LongAttribute : IntegerAttribute
public sealed class LongAttribute : NumericAttribute
{
/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="ILong"/> type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = true, AllowMultiple = false)]
public abstract class IntegerAttribute : PrimitiveAttribute
public abstract class NumericAttribute : PrimitiveAttribute
{
/// <summary>
/// Gets or sets the minimum value that can be set on the source generated Primitively type.
/// </summary>
public decimal Minimum { get; set; }
public object? Minimum { get; set; }

/// <summary>
/// Gets or sets the maximum value that can be set on the source generated Primitively type.
/// </summary>
public decimal Maximum { get; set; }
public object? Maximum { get; set; }
}
12 changes: 12 additions & 0 deletions src/Primitively.Abstractions/NumericInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Primitively;

/// <summary>
/// This abstract class represents metadata properties common to all source generated Primitively integer 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>
public abstract record NumericInfo(DataType DataType, Type Type, Type ValueType, string? Example, Func<string?, IPrimitive> CreateFrom)
: PrimitiveInfo(DataType, Type, ValueType, Example, CreateFrom);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Primitively;

/// <summary>
/// This abstract class represents metadata properties common to all source generated Primitively integer types.
/// 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>
Expand All @@ -10,5 +10,5 @@
/// <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>
public record IntegerInfo(DataType DataType, Type Type, Type ValueType, string? Example, Func<string?, IPrimitive> CreateFrom, decimal Minimum, decimal Maximum)
: PrimitiveInfo(DataType, Type, ValueType, Example, CreateFrom);
public record NumericInfo<T>(DataType DataType, Type Type, Type ValueType, string? Example, Func<string?, IPrimitive> CreateFrom, T Minimum, T Maximum)
: NumericInfo(DataType, Type, ValueType, Example, CreateFrom);
2 changes: 1 addition & 1 deletion src/Primitively.Abstractions/SByteAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class SByteAttribute : IntegerAttribute
public sealed class SByteAttribute : NumericAttribute
{
/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="ISByte"/> type.
Expand Down
2 changes: 1 addition & 1 deletion src/Primitively.Abstractions/ShortAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class ShortAttribute : IntegerAttribute
public sealed class ShortAttribute : NumericAttribute
{
/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="IShort"/> type.
Expand Down
43 changes: 43 additions & 0 deletions src/Primitively.Abstractions/SingleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Primitively;

/// <summary>
/// This attribute should be used on a <c>partial record struct</c> to source generate
/// a Primitively <see cref="ISingle"/> type that encapsulates a <see cref="float"/> value.
/// </summary>
/// <example>
/// These examples show how to use the Single attribute to source generate a Primitively <see cref="ISingle"/> type.
/// <code>
/// [Single]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Single(Minimum = 1)]
/// public partial record struct Example;
/// </code>
/// <code>
/// [Single(Minimum = 1, Maximum = 100)]
/// public partial record struct Example;
/// </code>
/// </example>
/// <remarks>
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class SingleAttribute : NumericAttribute
{
/// <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.
/// </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.
/// </value>
public new float Maximum { get; set; }
}
2 changes: 1 addition & 1 deletion src/Primitively.Abstractions/UIntAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// The generated Primitively type will enforce the specified minimum and maximum value constraints.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
public sealed class UIntAttribute : IntegerAttribute
public sealed class UIntAttribute : NumericAttribute
{
/// <summary>
/// Gets or sets the minimum value supported by the source generated Primitively <see cref="IUInt"/> type.
Expand Down
Loading

0 comments on commit e8da8a6

Please sign in to comment.