From 0fa2433e0a70c7a77caa24d7c75fd81779bd14df Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Fri, 6 Sep 2024 15:14:02 +0200 Subject: [PATCH 1/2] Another new CosmosDb emulator release, another way of outputting numbers. --- .../IntegrationTests.Group_with_key_and_value2.verified.txt | 2 +- .../IntegrationTests.Properties1.verified.txt | 4 ++-- .../IntegrationTests.Value.verified.txt | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/Providers.CosmosDb.Tests/IntegrationTests.Group_with_key_and_value2.verified.txt b/test/Providers.CosmosDb.Tests/IntegrationTests.Group_with_key_and_value2.verified.txt index e6acfc9a04..8729e76bff 100644 --- a/test/Providers.CosmosDb.Tests/IntegrationTests.Group_with_key_and_value2.verified.txt +++ b/test/Providers.CosmosDb.Tests/IntegrationTests.Group_with_key_and_value2.verified.txt @@ -4,6 +4,6 @@ Country: PartitionKey, Language: PartitionKey, Person: 1586270616000, - TimeFrame: 28800000 + TimeFrame: 28800000.0 } ] \ No newline at end of file diff --git a/test/Providers.CosmosDb.Tests/IntegrationTests.Properties1.verified.txt b/test/Providers.CosmosDb.Tests/IntegrationTests.Properties1.verified.txt index be7cf65041..1b331f6fd6 100644 --- a/test/Providers.CosmosDb.Tests/IntegrationTests.Properties1.verified.txt +++ b/test/Providers.CosmosDb.Tests/IntegrationTests.Properties1.verified.txt @@ -243,7 +243,7 @@ Id: 12345678-9012-3456-7890-123456789012, Label: Duration, Properties: {}, - Value: 7200000 + Value: 7200000.0 }, { Id: 12345678-9012-3456-7890-123456789012, @@ -261,7 +261,7 @@ Id: 12345678-9012-3456-7890-123456789012, Label: StartTime, Properties: {}, - Value: 28800000 + Value: 28800000.0 }, { Id: 12345678-9012-3456-7890-123456789012, diff --git a/test/Providers.CosmosDb.Tests/IntegrationTests.Value.verified.txt b/test/Providers.CosmosDb.Tests/IntegrationTests.Value.verified.txt index 3d7ce84637..dff0e0eccf 100644 --- a/test/Providers.CosmosDb.Tests/IntegrationTests.Value.verified.txt +++ b/test/Providers.CosmosDb.Tests/IntegrationTests.Value.verified.txt @@ -80,10 +80,10 @@ en, PartitionKey, PartitionKey, - 7200000, + 7200000.0, false, PartitionKey, - 28800000, + 28800000.0, en, PartitionKey, 21, From 87311126a9f02c0fe65690f9da4633df9f5850d7 Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Fri, 6 Sep 2024 15:26:26 +0200 Subject: [PATCH 2/2] ScalarToPropertyConverterFactory: Don't throw when things don't fit, just don't create a converter. --- .../ScalarToPropertyConverterFactory.cs | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Support.NewtonsoftJson/Converters/ScalarToPropertyConverterFactory.cs b/src/Support.NewtonsoftJson/Converters/ScalarToPropertyConverterFactory.cs index dab7fec79e..e90cf49f74 100644 --- a/src/Support.NewtonsoftJson/Converters/ScalarToPropertyConverterFactory.cs +++ b/src/Support.NewtonsoftJson/Converters/ScalarToPropertyConverterFactory.cs @@ -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 { @@ -15,19 +16,14 @@ private sealed class ScalarToPropertyConverter _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>(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>(Expression.New(constructor, lambdaParam), lambdaParam) + .Compile(); } public bool TryConvert(JValue serialized, ITransformer defer, ITransformer recurse, [NotNullWhen(true)] out TTargetProperty? value) @@ -46,8 +42,18 @@ public bool TryConvert(JValue serialized, ITransformer defer, ITransformer recur } } - public IConverter? TryCreate(IGremlinQueryEnvironment environment) => typeof(TSource) == typeof(JValue) && typeof(Property).IsAssignableFrom(typeof(TTarget)) && typeof(TTarget).IsGenericType - ? (IConverter?)Activator.CreateInstance(typeof(ScalarToPropertyConverter<,>).MakeGenericType(typeof(TTarget), typeof(TTarget).GetGenericArguments()[0]), environment) - : default; + public IConverter? TryCreate(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?)Activator.CreateInstance(typeof(ScalarToPropertyConverter<,>).MakeGenericType(typeof(TTarget), targetPropertyValueType), environment, constructor); + } + } + + return default; + } } }