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

Add NodaTime scalar types #3795

Merged
merged 22 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"boost.tool": {
"version": "0.2.2",
"version": "0.2.6",
"commands": [
"boo"
]
Expand Down
58 changes: 44 additions & 14 deletions src/HotChocolate/Core/HotChocolate.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,39 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Execution.Benc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Fetching", "src\Fetching\HotChocolate.Fetching.csproj", "{3ED68361-0D3D-45F5-8A83-444DD3D5C1BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Subscriptions.Redis.Tests", "test\Subscriptions.Redis.Tests\HotChocolate.Subscriptions.Redis.Tests.csproj", "{14648C7D-8958-4C94-B837-7F90F2F10081}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Subscriptions.Redis.Tests", "test\Subscriptions.Redis.Tests\HotChocolate.Subscriptions.Redis.Tests.csproj", "{14648C7D-8958-4C94-B837-7F90F2F10081}"
michaelstaib marked this conversation as resolved.
Show resolved Hide resolved
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Subscriptions.InMemory.Tests", "test\Subscriptions.InMemory.Tests\HotChocolate.Subscriptions.InMemory.Tests.csproj", "{F72E72DE-F163-4BCC-A442-94DB331D3C35}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Subscriptions.InMemory.Tests", "test\Subscriptions.InMemory.Tests\HotChocolate.Subscriptions.InMemory.Tests.csproj", "{F72E72DE-F163-4BCC-A442-94DB331D3C35}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Fetching.Tests", "test\Fetching.Tests\HotChocolate.Fetching.Tests.csproj", "{2A319702-6C2E-4184-8E71-43F87745B38D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Fetching.Tests", "test\Fetching.Tests\HotChocolate.Fetching.Tests.csproj", "{2A319702-6C2E-4184-8E71-43F87745B38D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.StarWars", "benchmark\StarWars\HotChocolate.StarWars.csproj", "{BB53E6F4-2CA0-494C-85FD-DF22530476C0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.StarWars", "benchmark\StarWars\HotChocolate.StarWars.csproj", "{BB53E6F4-2CA0-494C-85FD-DF22530476C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.CursorPagination", "src\Types.CursorPagination\HotChocolate.Types.CursorPagination.csproj", "{91F00C73-78E5-4400-8F9F-910BE99EDFD5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.CursorPagination", "src\Types.CursorPagination\HotChocolate.Types.CursorPagination.csproj", "{91F00C73-78E5-4400-8F9F-910BE99EDFD5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.OffsetPagination", "src\Types.OffsetPagination\HotChocolate.Types.OffsetPagination.csproj", "{738F2483-286E-4517-9660-5BFEEF343595}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.OffsetPagination", "src\Types.OffsetPagination\HotChocolate.Types.OffsetPagination.csproj", "{738F2483-286E-4517-9660-5BFEEF343595}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.CursorPagination.Tests", "test\Types.CursorPagination.Tests\HotChocolate.Types.CursorPagination.Tests.csproj", "{31AF3FED-A6C5-4DD5-99DF-03D4B4186D39}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.CursorPagination.Tests", "test\Types.CursorPagination.Tests\HotChocolate.Types.CursorPagination.Tests.csproj", "{31AF3FED-A6C5-4DD5-99DF-03D4B4186D39}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.OffsetPagination.Tests", "test\Types.OffsetPagination.Tests\HotChocolate.Types.OffsetPagination.Tests.csproj", "{9EF119AD-4168-49F8-B2B2-8F6F1B5E47C8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.OffsetPagination.Tests", "test\Types.OffsetPagination.Tests\HotChocolate.Types.OffsetPagination.Tests.csproj", "{9EF119AD-4168-49F8-B2B2-8F6F1B5E47C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.Records.Tests", "test\Types.Records.Tests\HotChocolate.Types.Records.Tests.csproj", "{CF2FA420-6295-4F83-8F87-340D8365282D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.Records.Tests", "test\Types.Records.Tests\HotChocolate.Types.Records.Tests.csproj", "{CF2FA420-6295-4F83-8F87-340D8365282D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark10", "benchmark10", "{4882F732-028B-48E9-99D6-E94E56B6406E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Execution.Benchmarks", "benchmark10\Execution.Benchmarks\HotChocolate.Execution.Benchmarks.csproj", "{CBA38330-E94C-472F-8FFE-DA70DF0368EC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Execution.Benchmarks", "benchmark10\Execution.Benchmarks\HotChocolate.Execution.Benchmarks.csproj", "{CBA38330-E94C-472F-8FFE-DA70DF0368EC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.StarWars", "benchmark10\StarWars\HotChocolate.StarWars.csproj", "{0BE70B30-33CC-4BB7-8EEA-4981A2E9417C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.StarWars", "benchmark10\StarWars\HotChocolate.StarWars.csproj", "{0BE70B30-33CC-4BB7-8EEA-4981A2E9417C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.Scalars", "src\Types.Scalars\HotChocolate.Types.Scalars.csproj", "{8B7EEF6D-DFF2-4D7B-9E5D-6CA34A8BC260}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.Scalars", "src\Types.Scalars\HotChocolate.Types.Scalars.csproj", "{8B7EEF6D-DFF2-4D7B-9E5D-6CA34A8BC260}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.Scalars.Tests", "test\Types.Scalars.Tests\HotChocolate.Types.Scalars.Tests.csproj", "{480E09E8-A0C4-49A7-AAC4-4AA5EC0733C5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.Scalars.Tests", "test\Types.Scalars.Tests\HotChocolate.Types.Scalars.Tests.csproj", "{480E09E8-A0C4-49A7-AAC4-4AA5EC0733C5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HotChocolate.Types.Scalars.Upload", "src\Types.Scalars.Upload\HotChocolate.Types.Scalars.Upload.csproj", "{670D3B20-401F-4004-AD0B-865560D02CC3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.Scalars.Upload", "src\Types.Scalars.Upload\HotChocolate.Types.Scalars.Upload.csproj", "{670D3B20-401F-4004-AD0B-865560D02CC3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.NodaTime", "src\Types.NodaTime\HotChocolate.Types.NodaTime.csproj", "{07F3E312-EC4C-4B71-A095-E478DF4CB52B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HotChocolate.Types.NodaTime.Tests", "test\Types.NodaTime.Tests\HotChocolate.Types.NodaTime.Tests.csproj", "{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -569,6 +573,30 @@ Global
{670D3B20-401F-4004-AD0B-865560D02CC3}.Release|x64.Build.0 = Release|Any CPU
{670D3B20-401F-4004-AD0B-865560D02CC3}.Release|x86.ActiveCfg = Release|Any CPU
{670D3B20-401F-4004-AD0B-865560D02CC3}.Release|x86.Build.0 = Release|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Debug|x64.ActiveCfg = Debug|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Debug|x64.Build.0 = Debug|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Debug|x86.ActiveCfg = Debug|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Debug|x86.Build.0 = Debug|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Release|Any CPU.Build.0 = Release|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Release|x64.ActiveCfg = Release|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Release|x64.Build.0 = Release|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Release|x86.ActiveCfg = Release|Any CPU
{07F3E312-EC4C-4B71-A095-E478DF4CB52B}.Release|x86.Build.0 = Release|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Debug|x64.ActiveCfg = Debug|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Debug|x64.Build.0 = Debug|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Debug|x86.ActiveCfg = Debug|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Debug|x86.Build.0 = Debug|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Release|Any CPU.Build.0 = Release|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Release|x64.ActiveCfg = Release|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Release|x64.Build.0 = Release|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Release|x86.ActiveCfg = Release|Any CPU
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -613,6 +641,8 @@ Global
{8B7EEF6D-DFF2-4D7B-9E5D-6CA34A8BC260} = {37B9D3B1-CA34-4720-9A0B-CFF1E64F52C2}
{480E09E8-A0C4-49A7-AAC4-4AA5EC0733C5} = {7462D089-D350-44D6-8131-896D949A65B7}
{670D3B20-401F-4004-AD0B-865560D02CC3} = {37B9D3B1-CA34-4720-9A0B-CFF1E64F52C2}
{07F3E312-EC4C-4B71-A095-E478DF4CB52B} = {37B9D3B1-CA34-4720-9A0B-CFF1E64F52C2}
{414B6DBE-1A3A-42DC-9116-3F9A433C6BBB} = {7462D089-D350-44D6-8131-896D949A65B7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4D94C77-6657-4630-9D42-0A9AC5153A1B}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using HotChocolate.Language;
using System.Diagnostics.CodeAnalysis;
using static HotChocolate.Types.NodaTime.Properties.NodaTimeResources;

namespace HotChocolate.Types.NodaTime
{
/// <summary>
/// This base class provides serialization functionality for noda time scalars
/// that have a <see cref="int"/> result value and a struct runtime value.
/// </summary>
/// <typeparam name="TRuntimeType">
/// The runtime type.
/// </typeparam>
public abstract class IntToStructBaseType<TRuntimeType>
: ScalarType<TRuntimeType, IntValueNode>
where TRuntimeType : struct
{
/// <summary>
/// Initializes a new instance of <see cref="IntToStructBaseType"/>.
/// </summary>
/// <param name="name">
/// The name of the scalar.
/// </param>
protected IntToStructBaseType(string name)
: base(name, BindingBehavior.Implicit)
{
}

/// <inheritdoc />
protected override TRuntimeType ParseLiteral(IntValueNode literal)
{
if (TryDeserialize(literal.ToInt32(), out TRuntimeType? value))
{
return value.Value;
}

throw new SerializationException(
string.Format(IntToStructBaseType_ParseLiteral_UnableToDeserializeInt, Name),
this);
}

/// <inheritdoc />
protected override IntValueNode ParseValue(TRuntimeType value)
{
if (TrySerialize(value, out var val))
{
return new IntValueNode(val.Value);
}

throw new SerializationException(
string.Format(IntToStructBaseType_ParseLiteral_UnableToDeserializeInt, Name),
this);
}

/// <inheritdoc />
public override IValueNode ParseResult(object? resultValue)
{
if (resultValue is null)
{
return NullValueNode.Default;
}

if (resultValue is int s)
{
return new IntValueNode(s);
}

if (resultValue is TRuntimeType v)
{
return ParseValue(v);
}

throw new SerializationException(
string.Format(IntToStructBaseType_ParseLiteral_UnableToDeserializeInt, Name),
this);
}

/// <inheritdoc />
public override bool TrySerialize(
object? runtimeValue,
out object? resultValue)
{
if (runtimeValue is null)
{
resultValue = null;
return true;
}

if (runtimeValue is TRuntimeType dt && TrySerialize(dt, out var val))
{
resultValue = val.Value;
return true;
}

resultValue = null;
return false;
}

/// <summary>
/// Tries to serialize the .net runtime representation to the
/// serialized result representation.
/// </summary>
/// <param name="runtimeValue">
/// The .net runtime representation.
/// </param>
/// <param name="resultValue">
/// The serialized result value.
/// </param>
/// <returns>
/// Returns the serialized result value.
/// </returns>
protected abstract bool TrySerialize(
TRuntimeType runtimeValue,
[NotNullWhen(true)] out int? resultValue);

/// <inheritdoc />
public override bool TryDeserialize(
object? resultValue,
out object? runtimeValue)
{
if (resultValue is null)
{
runtimeValue = null;
return true;
}

if (resultValue is int i && TryDeserialize(i, out TRuntimeType? val))
{
runtimeValue = val;
return true;
}

runtimeValue = null;
return false;
}

/// <summary>
/// Tries to deserializes the value from the output format to the .net
/// runtime representation.
/// </summary>
/// <param name="resultValue">
/// The serialized result value.
/// </param>
/// <param name="runtimeValue">
/// The .net runtime representation.
/// </param>
/// <returns>
/// <c>true</c> if the serialized value was correctly deserialized; otherwise, <c>false</c>.
/// </returns>
protected abstract bool TryDeserialize(
int resultValue,
[NotNullWhen(true)] out TRuntimeType? runtimeValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Language;
using static HotChocolate.Types.NodaTime.Properties.NodaTimeResources;

namespace HotChocolate.Types.NodaTime
{
/// <summary>
/// This base class provides serialization functionality for noda time scalars
/// that have a <see cref="string"/> result value and a class runtime value.
/// </summary>
/// <typeparam name="TRuntimeType">
/// The runtime type.
/// </typeparam>
public abstract class StringToClassBaseType<TRuntimeType>
: ScalarType<TRuntimeType, StringValueNode>
where TRuntimeType : class
{
/// <summary>
/// Initializes a new instance of <see cref="StringToClassBaseType"/>.
/// </summary>
/// <param name="name">
/// The name of the scalar.
/// </param>
public StringToClassBaseType(string name)
: base(name, BindingBehavior.Implicit)
{
}

/// <inheritdoc />
protected override TRuntimeType ParseLiteral(StringValueNode literal)
{
if (TryDeserialize(literal.Value, out TRuntimeType? value))
{
return value;
}

throw new SerializationException(
string.Format(StringToClassBaseType_ParseLiteral_UnableToDeserializeString, Name),
this);
}

/// <inheritdoc />
protected override StringValueNode ParseValue(TRuntimeType value) =>
new(Serialize(value));

/// <inheritdoc />
public override IValueNode ParseResult(object? resultValue)
{
if (resultValue is null)
{
return NullValueNode.Default;
}

if (resultValue is string s)
{
return new StringValueNode(s);
}

if (resultValue is TRuntimeType v)
{
return ParseValue(v);
}

throw new SerializationException(
string.Format(StringToClassBaseType_ParseLiteral_UnableToDeserializeString, Name),
this);
}

/// <inheritdoc />
public override bool TrySerialize(object? runtimeValue, out object? resultValue)
{
if (runtimeValue is null)
{
resultValue = null;
return true;
}

if (runtimeValue is TRuntimeType dt)
{
resultValue = Serialize(dt);
return true;
}

resultValue = null;
return false;
}

/// <summary>
/// Serializes the .net runtime representation to the serialized result representation.
/// </summary>
/// <param name="runtimeValue">
/// The .net value representation.
/// </param>
/// <returns>
/// Returns the serialized result value.
/// </returns>
protected abstract string Serialize(TRuntimeType runtimeValue);

/// <inheritdoc />
public override bool TryDeserialize(object? resultValue, out object? runtimeValue)
{
if (resultValue is null)
{
runtimeValue = null;
return true;
}

if (resultValue is string s && TryDeserialize(s, out TRuntimeType? val))
{
runtimeValue = val;
return true;
}

runtimeValue = null;
return false;
}

// <summary>
/// Tries to deserializes the value from the output format to the .net
/// runtime representation.
/// </summary>
/// <param name="resultValue">
/// The serialized result value.
/// </param>
/// <param name="runtimeValue">
/// The .net runtime representation.
/// </param>
/// <returns>
/// <c>true</c> if the serialized value was correctly deserialized; otherwise, <c>false</c>.
/// </returns>
protected abstract bool TryDeserialize(
string resultValue,
[NotNullWhen(true)] out TRuntimeType? runtimeValue);
}
}
Loading