Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved LocalDate/LocalTime to HotChocolate.Types and added LocalDateTime #7749

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions src/HotChocolate/Core/src/Types.Scalars/ScalarResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,6 @@
<data name="IsbnType_IsInvalid_ParseValue" xml:space="preserve">
<value>IsbnType cannot parse the provided value. The provided value is not a valid ISBN number.</value>
</data>
<data name="LocalDateType_Description" xml:space="preserve">
<value>The `LocalDate` scalar type represents a ISO date string, represented as UTF-8 character sequences YYYY-MM-DD. The scalar follows the specification defined in RFC3339</value>
</data>
<data name="LocalDateType_IsInvalid_ParseLiteral" xml:space="preserve">
<value>LocalDateType cannot parse the provided literal. The provided value is not a valid Local Date.</value>
</data>
<data name="LocalDateType_IsInvalid_ParseValue" xml:space="preserve">
<value>LocalDateType cannot parse the provided value. The provided value is not a valid Local Date.</value>
</data>
<data name="LatitudeType_Description" xml:space="preserve">
<value>The Latitude scalar type represents represents a valid decimal degrees latitude number.</value>
</data>
Expand All @@ -207,15 +198,6 @@
<data name="LatitudeType_IsInvalid_ParseValue" xml:space="preserve">
<value>LatitudeType cannot parse the provided value. The provided value was not a valid decimal degrees latitude number.</value>
</data>
<data name="LocalTimeType_Description" xml:space="preserve">
<value>The LocalTime scalar type is a local time string (i.e., with no associated timezone) in 24-hr HH:mm:ss.</value>
</data>
<data name="LocalTimeType_IsInvalid_ParseLiteral" xml:space="preserve">
<value>LocalTimeType cannot parse the provided literal. The provided value is not a valid Local Time.</value>
</data>
<data name="LocalTimeType_IsInvalid_ParseValue" xml:space="preserve">
<value>LocalTimeType cannot parse the provided value. The provided value is not a valid Local Time.</value>
</data>
<data name="LocalCurrencyType_Description" xml:space="preserve">
<value>The LocalCurrency scalar type is a currency string.</value>
</data>
Expand Down
46 changes: 1 addition & 45 deletions src/HotChocolate/Core/src/Types.Scalars/ThrowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,28 +178,6 @@ public static SerializationException LatitudeType_ParseLiteral_IsInvalid(IType t
type);
}

public static SerializationException LocalDateType_ParseValue_IsInvalid(IType type)
{
return new SerializationException(
ErrorBuilder.New()
.SetMessage(ScalarResources.LocalDateType_IsInvalid_ParseValue)
.SetCode(ErrorCodes.Scalars.InvalidRuntimeType)
.SetExtension("actualType", WellKnownScalarTypes.LocalDate)
.Build(),
type);
}

public static SerializationException LocalDateType_ParseLiteral_IsInvalid(IType type)
{
return new SerializationException(
ErrorBuilder.New()
.SetMessage(ScalarResources.LocalDateType_IsInvalid_ParseLiteral)
.SetCode(ErrorCodes.Scalars.InvalidSyntaxFormat)
.SetExtension("actualType", WellKnownScalarTypes.LocalDate)
.Build(),
type);
}

public static SerializationException LocalCurrencyType_ParseValue_IsInvalid(IType type)
{
return new SerializationException(
Expand All @@ -215,35 +193,13 @@ public static SerializationException LocalCurrencyType_ParseLiteral_IsInvalid(IT
{
return new SerializationException(
ErrorBuilder.New()
.SetMessage(ScalarResources.LocalDateType_IsInvalid_ParseLiteral)
.SetMessage(ScalarResources.LocalCurrencyType_IsInvalid_ParseLiteral)
.SetCode(ErrorCodes.Scalars.InvalidSyntaxFormat)
.SetExtension("actualType", WellKnownScalarTypes.LocalCurrency)
.Build(),
type);
}

public static SerializationException LocalTimeType_ParseValue_IsInvalid(IType type)
{
return new SerializationException(
ErrorBuilder.New()
.SetMessage(ScalarResources.LocalTimeType_IsInvalid_ParseValue)
.SetCode(ErrorCodes.Scalars.InvalidRuntimeType)
.SetExtension("actualType", WellKnownScalarTypes.LocalTime)
.Build(),
type);
}

public static SerializationException LocalTimeType_ParseLiteral_IsInvalid(IType type)
{
return new SerializationException(
ErrorBuilder.New()
.SetMessage(ScalarResources.LocalTimeType_IsInvalid_ParseLiteral)
.SetCode(ErrorCodes.Scalars.InvalidSyntaxFormat)
.SetExtension("actualType", WellKnownScalarTypes.LocalTime)
.Build(),
type);
}

public static SerializationException LongitudeType_ParseValue_IsInvalid(IType type)
{
return new SerializationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ internal static class WellKnownScalarTypes
public const string IPv6 = nameof(IPv6);
public const string Isbn = nameof(Isbn);
public const string Latitude = nameof(Latitude);
public const string LocalDate = nameof(LocalDate);
public const string LocalCurrency = nameof(LocalCurrency);
public const string LocalTime = nameof(LocalTime);
public const string Longitude = nameof(Longitude);
public const string MacAddress = nameof(MacAddress);
public const string NegativeFloat = nameof(NegativeFloat);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/HotChocolate/Core/src/Types/Properties/TypeResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1003,4 +1003,13 @@ Type: `{0}`</value>
<data name="ObjectToDictionaryConverter_CycleInObjectGraph" xml:space="preserve">
<value>Cycle in object graph detected.</value>
</data>
<data name="LocalDateTimeType_Description" xml:space="preserve">
<value>The `LocalDateTime` scalar type is a local date/time string (i.e., with no associated timezone) with the format `YYYY-MM-DDThh:mm:ss`.</value>
</data>
<data name="LocalDateType_Description" xml:space="preserve">
<value>The `LocalDate` scalar type represents a ISO date string, represented as UTF-8 character sequences YYYY-MM-DD. The scalar follows the specification defined in RFC3339</value>
</data>
<data name="LocalTimeType_Description" xml:space="preserve">
<value>The LocalTime scalar type is a local time string (i.e., with no associated timezone) in 24-hr HH:mm:ss.</value>
</data>
</root>
136 changes: 136 additions & 0 deletions src/HotChocolate/Core/src/Types/Types/Scalars/LocalDateTimeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using HotChocolate.Language;
using HotChocolate.Properties;

#nullable enable

namespace HotChocolate.Types;

/// <summary>
/// The `LocalDateTime` scalar type is a local date/time string (i.e., with no associated timezone)
/// with the format `YYYY-MM-DDThh:mm:ss`.
/// </summary>
public class LocalDateTimeType : ScalarType<DateTime, StringValueNode>
{
private const string _localFormat = "yyyy-MM-ddTHH\\:mm\\:ss";

/// <summary>
/// Initializes a new instance of the <see cref="LocalDateTimeType"/> class.
/// </summary>
public LocalDateTimeType(
string name,
string? description = null,
BindingBehavior bind = BindingBehavior.Explicit)
: base(name, bind)
{
Description = description;
}

/// <summary>
/// Initializes a new instance of the <see cref="LocalDateTimeType"/> class.
/// </summary>
[ActivatorUtilitiesConstructor]
public LocalDateTimeType()
: this(
ScalarNames.LocalDateTime,
TypeResources.LocalDateTimeType_Description)
{
}

public override IValueNode ParseResult(object? resultValue)
{
return resultValue switch
{
null => NullValueNode.Default,
string s => new StringValueNode(s),
DateTimeOffset o => ParseValue(o.DateTime),
DateTime dt => ParseValue(dt),
_ => throw new SerializationException(
TypeResourceHelper.Scalar_Cannot_ParseResult(Name, resultValue.GetType()), this)
};
}

protected override DateTime ParseLiteral(StringValueNode valueSyntax)
{
if (TryDeserializeFromString(valueSyntax.Value, out var value))
{
return value.Value;
}

throw new SerializationException(
TypeResourceHelper.Scalar_Cannot_ParseLiteral(Name, valueSyntax.GetType()),
this);
}

protected override StringValueNode ParseValue(DateTime runtimeValue)
{
return new(Serialize(runtimeValue));
}

public override bool TrySerialize(object? runtimeValue, out object? resultValue)
{
switch (runtimeValue)
{
case null:
resultValue = null;
return true;
case DateTimeOffset o:
resultValue = Serialize(o);
return true;
case DateTime dt:
resultValue = Serialize(dt);
return true;
default:
resultValue = null;
return false;
}
}

public override bool TryDeserialize(object? resultValue, out object? runtimeValue)
{
switch (resultValue)
{
case null:
runtimeValue = null;
return true;
case string s when TryDeserializeFromString(s, out var d):
runtimeValue = d;
return true;
case DateTimeOffset o:
runtimeValue = o.DateTime;
return true;
case DateTime dt:
runtimeValue = dt;
return true;
default:
runtimeValue = null;
return false;
}
}

private static string Serialize(IFormattable value)
{
return value.ToString(_localFormat, CultureInfo.InvariantCulture);
}

private static bool TryDeserializeFromString(
string? serialized,
[NotNullWhen(true)] out DateTime? value)
{
if (serialized is not null
&& DateTime.TryParseExact(
serialized,
_localFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out var dateTime))
{
value = dateTime;
return true;
}

value = null;
return false;
}
}
Loading
Loading