Skip to content

Commit

Permalink
task: updated examples for double and single source gen
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Apr 7, 2024
1 parent d5fdd3b commit 766f742
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
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-beta.1</version>
<version>1.5.0-alpha.25</version>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/Primitively.Abstractions/NumericAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
public abstract class NumericAttribute : PrimitiveAttribute
{
/// <summary>
/// Gets or sets the minimum value that can be set on the source generated Primitively type.
/// Gets or sets the maximum value that can be set on the source generated Primitively type.
/// </summary>
public object? Minimum { get; set; }
public object? Maximum { get; set; }

/// <summary>
/// Gets or sets the maximum value that can be set on the source generated Primitively type.
/// Gets or sets the minimum value that can be set on the source generated Primitively type.
/// </summary>
public object? Maximum { get; set; }
public object? Minimum { get; set; }
}
8 changes: 6 additions & 2 deletions src/Primitively/MetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ public readonly struct UShort
/// </summary>
public readonly struct Double
{
public static readonly string Example = string.Format("{0:N15}", double.MaxValue / 2);
public static readonly string Example = (double.MaxValue / 2).ToString("E");
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; // Value of -1 means no rounding. Minimum value is 0. Maximum value is 15.
public static readonly int MinDigits = 0;
public static readonly int MaxDigits = 15;
public static readonly MidpointRounding Mode = MidpointRounding.ToEven;
}

Expand All @@ -214,13 +216,15 @@ public readonly struct Double
/// </summary>
public readonly struct Single
{
public static readonly string Example = string.Format("{0:N6}", float.MaxValue / 2);
public static readonly string Example = (float.MaxValue / 2).ToString("E");
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 int MinDigits = 0;
public static readonly int MaxDigits = 6;
public static readonly MidpointRounding Mode = MidpointRounding.ToEven;
}
}
Expand Down
40 changes: 26 additions & 14 deletions src/Primitively/Parsers/NumericParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,21 @@ private static bool TryParseAttributeConstructorArguments(DataType dataType, Att
}

var defaultDigits = dataType == DataType.Double ? MetaData.Numeric.Double.Digits : MetaData.Numeric.Single.Digits;
var minDigits = dataType == DataType.Double ? MetaData.Numeric.Double.MinDigits : MetaData.Numeric.Single.MinDigits;
var maxDigits = dataType == DataType.Double ? MetaData.Numeric.Double.MaxDigits : MetaData.Numeric.Single.MaxDigits;
var digits = (int)args[0].Value!;
digits = digits < defaultDigits ? defaultDigits : digits;
digits = (digits < minDigits) || (digits > maxDigits) ? defaultDigits : digits;
recordStructData.Digits = digits;

var mode = dataType == DataType.Double ? MetaData.Numeric.Double.Mode : MetaData.Numeric.Single.Mode;

if (args.Length == 2)
{
mode = (MidpointRounding)args[1].Value!;
var mode = (MidpointRounding)args[1].Value!;
recordStructData.Mode = mode;
}

if (digits > defaultDigits)
{
var example = Math.Round((dataType == DataType.Double ? MetaData.Numeric.Double.Maximum : MetaData.Numeric.Single.Maximum) / 2, digits, mode);
recordStructData.Example = example.ToString();
recordStructData.Example = 123.123456789012345.ToString($"N{digits}");
}

return true;
Expand Down Expand Up @@ -249,6 +248,7 @@ private static bool TryParseNamedArguments(DataType dataType, AttributeData attr
}

// Set Example based on provided Min and Max settings
// TODO: Fix potential exeeptions in the calculation for the example value
if (rangeHasChanged)
{
switch (recordStructData.DataType)
Expand All @@ -262,41 +262,53 @@ private static bool TryParseNamedArguments(DataType dataType, AttributeData attr
case DataType.Long:
case DataType.ULong:
{
var minimum = Convert.ToDecimal(recordStructData.Minimum);
var maximum = Convert.ToDecimal(recordStructData.Maximum);
var range = new decimal[]
{
Convert.ToDecimal(recordStructData.Minimum),
Convert.ToDecimal(recordStructData.Maximum)
}
.OrderBy(r => r)
.ToArray();

var minimum = range[0];
var maximum = range[1];
recordStructData.Minimum = minimum;
recordStructData.Maximum = maximum;
recordStructData.Example = Math.Round(minimum + ((maximum - minimum) / 2)).ToString();
break;
}
case DataType.Double:
{
var minimum = Convert.ToDouble(recordStructData.Minimum);
var maximum = Convert.ToDouble(recordStructData.Maximum);
var defaultDigits = MetaData.Numeric.Double.Digits;

if (recordStructData.Digits.HasValue && recordStructData.Digits.Value > MetaData.Numeric.Double.Digits)
if (recordStructData.Digits.HasValue && recordStructData.Digits.Value > defaultDigits)
{
var digits = recordStructData.Digits.Value;
var mode = recordStructData.Mode ?? MetaData.Numeric.Double.Mode;
recordStructData.Example = string.Format($"{{0:N{digits}}}", minimum + ((maximum - minimum) / 2));
recordStructData.Example = (minimum + ((maximum - minimum) / 2)).ToString($"N{digits}");
break;
}

recordStructData.Example = string.Format("{0:N15}", minimum + ((maximum - minimum) / 2));
recordStructData.Example = (minimum + ((maximum - minimum) / 2)).ToString();
break;
}
case DataType.Single:
{
var minimum = Convert.ToSingle(recordStructData.Minimum);
var maximum = Convert.ToSingle(recordStructData.Maximum);
var defaultDigits = MetaData.Numeric.Single.Digits;

if (recordStructData.Digits.HasValue && recordStructData.Digits.Value > MetaData.Numeric.Single.Digits)
if (recordStructData.Digits.HasValue && recordStructData.Digits.Value > defaultDigits)
{
var digits = recordStructData.Digits.Value;
var mode = recordStructData.Mode ?? MetaData.Numeric.Single.Mode;
recordStructData.Example = string.Format($"{{0:N{digits}}}", minimum + ((maximum - minimum) / 2));
recordStructData.Example = (minimum + ((maximum - minimum) / 2)).ToString($"N{digits}");
break;
}

recordStructData.Example = string.Format("{0:N6}", minimum + ((maximum - minimum) / 2));
recordStructData.Example = (minimum + ((maximum - minimum) / 2)).ToString();
break;
}
default:
Expand Down

0 comments on commit 766f742

Please sign in to comment.