From ca541b68020447ace7a2d9cc8d50a38934070387 Mon Sep 17 00:00:00 2001 From: Obed Kooijman Date: Sat, 14 Dec 2024 02:02:32 +0100 Subject: [PATCH 1/4] fix conversion for FrozenSets --- .../ValueConversion/NpgsqlArrayConverter.cs | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs index 982d58eaa..bf28f8e12 100644 --- a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs +++ b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.Collections.Frozen; using static System.Linq.Expressions.Expression; namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.ValueConversion; @@ -107,7 +108,12 @@ private static Expression> ArrayConversionExpression) + ? typeof(HashSet<>).MakeGenericType(outputElementType) + : typeof(TConcreteOutput); + var output = Parameter(mutableOutput, "result"); + var lengthVariable = Variable(typeof(int), "length"); var expressions = new List(); @@ -185,11 +191,11 @@ private static Expression> ArrayConversionExpression> ArrayConversionExpression).MakeGenericType(outputElementType)) + { + // return output.ToFrozenSet(null); + expressions.Add( + Call( + typeof(FrozenSet), nameof(FrozenSet.ToFrozenSet), + [outputElementType], + output, + Constant(null, typeof(IEqualityComparer<>).MakeGenericType(outputElementType)))); + } + else + { + // return output; + expressions.Add(output); + } + return Lambda>( // First, check if the given array value is null and return null immediately if so From 885280625621e00165a95e2e070c00dd0c4aa58f Mon Sep 17 00:00:00 2001 From: Obed Kooijman Date: Sat, 14 Dec 2024 02:09:40 +0100 Subject: [PATCH 2/4] some cleanups --- .../ValueConversion/NpgsqlArrayConverter.cs | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs index bf28f8e12..ce68e962f 100644 --- a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs +++ b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs @@ -108,10 +108,13 @@ private static Expression> ArrayConversionExpression) + + var outputIsImmutable = typeof(TConcreteOutput) is { IsGenericType: true } generic + && generic == typeof(FrozenSet<>).MakeGenericType(outputElementType); + var mutableOutput = outputIsImmutable ? typeof(HashSet<>).MakeGenericType(outputElementType) : typeof(TConcreteOutput); + var output = Parameter(mutableOutput, "result"); var lengthVariable = Variable(typeof(int), "length"); @@ -288,22 +291,14 @@ elementConversionExpression is null Call(enumeratorVariable, typeof(IDisposable).GetMethod(nameof(IDisposable.Dispose))!))); } - if (typeof(TConcreteOutput) == typeof(FrozenSet<>).MakeGenericType(outputElementType)) - { - // return output.ToFrozenSet(null); - expressions.Add( - Call( + expressions.Add( + outputIsImmutable + ? Call( typeof(FrozenSet), nameof(FrozenSet.ToFrozenSet), [outputElementType], output, - Constant(null, typeof(IEqualityComparer<>).MakeGenericType(outputElementType)))); - } - else - { - // return output; - expressions.Add(output); - } - + Constant(null, typeof(IEqualityComparer<>).MakeGenericType(outputElementType))) + : output); return Lambda>( // First, check if the given array value is null and return null immediately if so From 0ba9964ed1b36758d9d621b0c9945de98a1e356f Mon Sep 17 00:00:00 2001 From: Obed Kooijman Date: Sat, 14 Dec 2024 02:17:48 +0100 Subject: [PATCH 3/4] further cleanup --- .../ValueConversion/NpgsqlArrayConverter.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs index ce68e962f..ce8fa5d1b 100644 --- a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs +++ b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs @@ -111,11 +111,11 @@ private static Expression> ArrayConversionExpression).MakeGenericType(outputElementType); - var mutableOutput = outputIsImmutable + var mutableOutputType = outputIsImmutable ? typeof(HashSet<>).MakeGenericType(outputElementType) : typeof(TConcreteOutput); - var output = Parameter(mutableOutput, "result"); + var output = Parameter(mutableOutputType, "result"); var lengthVariable = Variable(typeof(int), "length"); @@ -194,11 +194,11 @@ private static Expression> ArrayConversionExpression> ArrayConversionExpression Date: Sat, 14 Dec 2024 02:21:22 +0100 Subject: [PATCH 4/4] readded removed comments --- src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs index ce8fa5d1b..e1496a953 100644 --- a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs +++ b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs @@ -293,11 +293,13 @@ elementConversionExpression is null expressions.Add( outputIsImmutable + // return output.ToFrozenSet(null); ? Call( typeof(FrozenSet), nameof(FrozenSet.ToFrozenSet), [outputElementType], output, Constant(null, typeof(IEqualityComparer<>).MakeGenericType(outputElementType))) + // return output; : output); return Lambda>(