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

Dont throw in scalar to property converter factory #1723

Merged
merged 2 commits into from
Sep 6, 2024
Merged
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
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 @@ -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