Skip to content

Commit

Permalink
task: updated DateOnly template
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Apr 11, 2024
1 parent fb59c6e commit 4f6ead1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 108 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-alpha.34</version>
<version>1.5.0-alpha.36</version>
</PropertyGroup>

<ItemGroup>
Expand Down
66 changes: 4 additions & 62 deletions src/Primitively/EmbeddedResources/DateOnly/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
global::System.IEquatable<PRIMITIVE_TYPE>,
global::System.IComparable<PRIMITIVE_TYPE>PRIMITIVE_IVALIDATABLEOBJECT
{
#if NET6_0_OR_GREATER
public const string Example = "PRIMITIVE_EXAMPLE";
public const string Format = "PRIMITIVE_FORMAT";
public const int Length = PRIMITIVE_LENGTH;
Expand All @@ -20,10 +19,12 @@

private readonly global::System.DateTime _value;

#if NET6_0_OR_GREATER
public PRIMITIVE_TYPE(global::System.DateOnly value)
{
_value = new global::System.DateTime(value.Year, value.Month, value.Day);
}
#endif

public PRIMITIVE_TYPE(global::System.DateTime value)
{
Expand Down Expand Up @@ -61,8 +62,10 @@ private PRIMITIVE_TYPE(string value)
public override string ToString() => _value.ToString(Format);

public static implicit operator string(PRIMITIVE_TYPE value) => value.ToString();
#if NET6_0_OR_GREATER
public static implicit operator global::System.DateOnly(PRIMITIVE_TYPE value) => DateOnly.FromDateTime(value._value);
public static explicit operator PRIMITIVE_TYPE(global::System.DateOnly value) => new(value);
#endif
public static explicit operator global::System.DateTime(PRIMITIVE_TYPE value) => value._value;
public static explicit operator PRIMITIVE_TYPE(global::System.DateTime value) => new(value);
public static explicit operator PRIMITIVE_TYPE(string value) => new(value);
Expand All @@ -71,64 +74,3 @@ 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;
#else
public const string Example = "PRIMITIVE_EXAMPLE";
public const string Format = "PRIMITIVE_FORMAT";
public const int Length = PRIMITIVE_LENGTH;

private static readonly global::PRIMITIVE_INFO_TYPE _info = new
(
Type: typeof(PRIMITIVE_TYPE),
Example: Example,
CreateFrom: (value) => (PRIMITIVE_TYPE)value,
Format: Format,
Length: Length
);

private readonly global::System.DateTime _value;

public PRIMITIVE_TYPE(global::System.DateTime value)
{
_value = new global::System.DateTime(value.Year, value.Month, value.Day);
}

private PRIMITIVE_TYPE(string value)
{
global::System.DateTime.TryParseExact(value, Format, global::System.Globalization.CultureInfo.InvariantCulture, global::System.Globalization.DateTimeStyles.None, out var result);

_value = result;
}

object global::Primitively.IPrimitive.Value => _value;

global::System.DateTime global::Primitively.IPrimitive<global::System.DateTime>.Value => _value;

global::Primitively.PrimitiveInfo global::Primitively.IPrimitiveInfo.Info => _info;

[global::System.Text.Json.Serialization.JsonIgnore]
public global::Primitively.DataType DataType => global::Primitively.DataType.PRIMITIVE_DATA_TYPE;

[global::System.Text.Json.Serialization.JsonIgnore]
public bool HasValue => _value != default;

[global::System.Text.Json.Serialization.JsonIgnore]
public global::PRIMITIVE_INFO_TYPE Info => _info;

[global::System.Text.Json.Serialization.JsonIgnore]
public global::System.Type ValueType => typeof(global::System.DateTime);

public bool Equals(PRIMITIVE_TYPE other) => _value == other._value;
public int CompareTo(PRIMITIVE_TYPE other) => _value.CompareTo(other._value);
public override int GetHashCode() => _value.GetHashCode();
public override string ToString() => _value.ToString(Format);

public static implicit operator string(PRIMITIVE_TYPE value) => value.ToString();
public static implicit operator global::System.DateTime(PRIMITIVE_TYPE value) => value._value;
public static explicit operator PRIMITIVE_TYPE(global::System.DateTime value) => new(value);
public static explicit operator PRIMITIVE_TYPE(string value) => new(value);

public static global::PRIMITIVE_INFO_TYPE TypeInfo => _info;

public static PRIMITIVE_TYPE Parse(string value) => new(value);
public static bool TryParse(string value, out PRIMITIVE_TYPE result) => (result = new(value)).HasValue;
#endif
74 changes: 37 additions & 37 deletions src/Primitively/MetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal readonly struct MetaData
public readonly struct DateOnly
{
public static readonly string Interface = typeof(IDateOnly).FullName;
public static readonly string Type = typeof(System.DateTime).FullName;
public static readonly string Type = typeof(DateTime).FullName;
public static readonly string InfoType = typeof(DateOnlyInfo).FullName;

/// <summary>
Expand Down Expand Up @@ -106,6 +106,24 @@ public readonly struct Byte
public static readonly string InfoType = typeof(ByteInfo).FullName;
}

/// <summary>
/// Contains metadata about the Double Primitively type.
/// </summary>
public readonly struct Double
{
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;
public static readonly string InfoType = typeof(DoubleInfo).FullName;
}

/// <summary>
/// Contains metadata about the Int Primitively type.
/// </summary>
Expand Down Expand Up @@ -162,6 +180,24 @@ public readonly struct Short
public static readonly string InfoType = typeof(ShortInfo).FullName;
}

/// <summary>
/// Contains metadata about the Single (float) Primitively type.
/// </summary>
public readonly struct Single
{
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;
public static readonly string InfoType = typeof(SingleInfo).FullName;
}

/// <summary>
/// Contains metadata about the UInt Primitively type.
/// </summary>
Expand Down Expand Up @@ -203,42 +239,6 @@ public readonly struct UShort
public static readonly string Type = typeof(ushort).FullName;
public static readonly string InfoType = typeof(UShortInfo).FullName;
}

/// <summary>
/// Contains metadata about the Double Primitively type.
/// </summary>
public readonly struct Double
{
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;
public static readonly string InfoType = typeof(DoubleInfo).FullName;
}

/// <summary>
/// Contains metadata about the Single (float) Primitively type.
/// </summary>
public readonly struct Single
{
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;
public static readonly string InfoType = typeof(SingleInfo).FullName;
}
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions src/Primitively/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ internal static class Parser
{
private static readonly List<string> _attributeFullNames =
[
typeof(ByteAttribute).FullName,
typeof(DateOnlyAttribute).FullName,
typeof(DoubleAttribute).FullName,
typeof(GuidAttribute).FullName,
typeof(ByteAttribute).FullName,
typeof(SByteAttribute).FullName,
typeof(ShortAttribute).FullName,
typeof(UShortAttribute).FullName,
typeof(IntAttribute).FullName,
typeof(UIntAttribute).FullName,
typeof(LongAttribute).FullName,
typeof(ULongAttribute).FullName,
typeof(SByteAttribute).FullName,
typeof(ShortAttribute).FullName,
typeof(SingleAttribute).FullName,
typeof(DoubleAttribute).FullName,
typeof(StringAttribute).FullName,
typeof(UIntAttribute).FullName,
typeof(ULongAttribute).FullName,
typeof(UShortAttribute).FullName
];

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void ConvertFromThisToThatWithExpectedResults(string from, bool hasValue
var @this = (BirthDate)from;
string to = @this;
var that = BirthDate.Parse(to);
var and = new BirthDate(that);
var and = new BirthDate((DateTime)that);
string back = and;

@this.HasValue.Should().Be(hasValue);
Expand Down

0 comments on commit 4f6ead1

Please sign in to comment.