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

Cover DateTimeOffset deserialization #1721

Merged
merged 7 commits into from
Sep 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ExRam.Gremlinq.Core.Transformation;
using ExRam.Gremlinq.Core;
using System.Globalization;
using static System.Globalization.DateTimeStyles;

namespace ExRam.Gremlinq.Support.NewtonsoftJson
{
Expand All @@ -13,7 +14,7 @@ internal sealed class DateTimeOffsetConverterFactory : FixedTypeConverterFactory
{
{ Value: DateTimeOffset dateTimeOffset } => dateTimeOffset,
{ Value: DateTime dateTime } => new DateTimeOffset(dateTime),
{ Value: string dateTimeString } when DateTime.TryParse(dateTimeString, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var parseResult) => parseResult,
{ Value: string dateTimeString } when DateTimeOffset.TryParse(dateTimeString, CultureInfo.InvariantCulture, AdjustToUniversal | AssumeLocal, out var parseResult) => parseResult,
{ Type: JTokenType.Integer } => DateTimeOffset.FromUnixTimeMilliseconds(jValue.Value<long>()),
_ => default(DateTimeOffset?)
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ExRam.Gremlinq.Core.Transformation;
using ExRam.Gremlinq.Core;
using System.Linq.Expressions;
using System.Reflection;

namespace ExRam.Gremlinq.Support.NewtonsoftJson
{
Expand All @@ -15,19 +16,14 @@ private sealed class ScalarToPropertyConverter<TTargetProperty, TTargetPropertyV
private readonly IGremlinQueryEnvironment _environment;
private readonly Func<TTargetPropertyValue, TTargetProperty?> _constructor;

public ScalarToPropertyConverter(IGremlinQueryEnvironment environment)
public ScalarToPropertyConverter(IGremlinQueryEnvironment environment, ConstructorInfo constructor)
{
if (typeof(TTargetProperty).GetConstructor(new[] { typeof(TTargetPropertyValue) }) is { } constructor)
{
var lambdaParam = Expression.Parameter(typeof(TTargetPropertyValue));
var lambdaParam = Expression.Parameter(typeof(TTargetPropertyValue));

_environment = environment;
_constructor = Expression
.Lambda<Func<TTargetPropertyValue, TTargetProperty?>>(Expression.New(constructor, lambdaParam), lambdaParam)
.Compile();
}
else
throw new ArgumentException($"{typeof(TTargetProperty).Name} does not contain a constructor that takes a value of type {typeof(TTargetPropertyValue).Name}.");
_environment = environment;
_constructor = Expression
.Lambda<Func<TTargetPropertyValue, TTargetProperty?>>(Expression.New(constructor, lambdaParam), lambdaParam)
.Compile();
}

public bool TryConvert(JValue serialized, ITransformer defer, ITransformer recurse, [NotNullWhen(true)] out TTargetProperty? value)
Expand All @@ -46,8 +42,18 @@ public bool TryConvert(JValue serialized, ITransformer defer, ITransformer recur
}
}

public IConverter<TSource, TTarget>? TryCreate<TSource, TTarget>(IGremlinQueryEnvironment environment) => typeof(TSource) == typeof(JValue) && typeof(Property).IsAssignableFrom(typeof(TTarget)) && typeof(TTarget).IsGenericType
? (IConverter<TSource, TTarget>?)Activator.CreateInstance(typeof(ScalarToPropertyConverter<,>).MakeGenericType(typeof(TTarget), typeof(TTarget).GetGenericArguments()[0]), environment)
: default;
public IConverter<TSource, TTarget>? TryCreate<TSource, TTarget>(IGremlinQueryEnvironment environment)
{
if (typeof(TSource) == typeof(JValue) && typeof(Property).IsAssignableFrom(typeof(TTarget)) && typeof(TTarget).IsGenericType)
{
if (typeof(TTarget).GetGenericArguments() is [var targetPropertyValueType])
{
if (typeof(TTarget).GetConstructor(new[] { targetPropertyValueType }) is { } constructor)
return (IConverter<TSource, TTarget>?)Activator.CreateInstance(typeof(ScalarToPropertyConverter<,>).MakeGenericType(typeof(TTarget), targetPropertyValueType), environment, constructor);
}
}

return default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public static IGremlinQueryEnvironment UseNewtonsoftJson(this IGremlinQueryEnvir

.Add(new ExtractPropertyValueConverterFactory())
.Add(new ScalarToPropertyConverterFactory())
.Add(new PropertyHeuristicConverterFactory())

.Add(new VertexOrEdgeConverterFactory())
.Add(new LabelLookupConverterFactory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
Country: PartitionKey,
Language: PartitionKey,
Person: 1586270616000,
TimeFrame: 28800000
TimeFrame: 28800000.0
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
Id: 12345678-9012-3456-7890-123456789012,
Label: Duration,
Properties: {},
Value: 7200000
Value: 7200000.0
},
{
Id: 12345678-9012-3456-7890-123456789012,
Expand All @@ -261,7 +261,7 @@
Id: 12345678-9012-3456-7890-123456789012,
Label: StartTime,
Properties: {},
Value: 28800000
Value: 28800000.0
},
{
Id: 12345678-9012-3456-7890-123456789012,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@
en,
PartitionKey,
PartitionKey,
7200000,
7200000.0,
false,
PartitionKey,
28800000,
28800000.0,
en,
PartitionKey,
21,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
2016-12-14 16:39:19.349 +0
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
2016-01-01 11:30 +0
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
2007-12-03 09:15:30 +0
]
18 changes: 18 additions & 0 deletions test/Providers.GremlinServer.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ public Task Contains_case_insensitive_reverse() => _g
.Where(x => x.Contains("LL", StringComparison.OrdinalIgnoreCase))
.Verify();

[Fact]
public Task DateTimeOffset_from_string_1() => _g
.Inject("2016-12-14T16:39:19.349Z")
.Cast<DateTimeOffset>()
.Verify();

[Fact(Skip = "Won't work on CI and everything that is not located in my time zone.")]
public Task DateTimeOffset_from_string_2() => _g
.Inject("2016-01-01T12:30")
.Cast<DateTimeOffset>()
.Verify();

[Fact]
public Task DateTimeOffset_from_string_3() => _g
.Inject("2007-12-03T10:15:30+01:00")
.Cast<DateTimeOffset>()
.Verify();

[Fact]
public async Task Deserialization_of_typed_results_is_only_called_once()
{
Expand Down