Skip to content

Commit

Permalink
Automatic usage of collection initializers (#3051)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Jan 6, 2024
1 parent 5c744a8 commit e6d545a
Show file tree
Hide file tree
Showing 109 changed files with 720 additions and 779 deletions.
1 change: 1 addition & 0 deletions EFCore.PG.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
NuGet.config = NuGet.config
global.json = global.json
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8537E50E-CF7F-49CB-B4EF-3E2A1B11F050}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public NpgsqlNetTopologySuiteAggregateMethodCallTranslatorPlugin(
public class NpgsqlNetTopologySuiteAggregateMethodTranslator : IAggregateMethodCallTranslator
{
private static readonly MethodInfo GeometryCombineMethod
= typeof(GeometryCombiner).GetRuntimeMethod(nameof(GeometryCombiner.Combine), new[] { typeof(IEnumerable<Geometry>) })!;
= typeof(GeometryCombiner).GetRuntimeMethod(nameof(GeometryCombiner.Combine), [typeof(IEnumerable<Geometry>)])!;

private static readonly MethodInfo ConvexHullMethod
= typeof(ConvexHull).GetRuntimeMethod(nameof(ConvexHull.Create), new[] { typeof(IEnumerable<Geometry>) })!;
= typeof(ConvexHull).GetRuntimeMethod(nameof(ConvexHull.Create), [typeof(IEnumerable<Geometry>)])!;

private static readonly MethodInfo UnionMethod
= typeof(UnaryUnionOp).GetRuntimeMethod(nameof(UnaryUnionOp.Union), new[] { typeof(IEnumerable<Geometry>) })!;
= typeof(UnaryUnionOp).GetRuntimeMethod(nameof(UnaryUnionOp.Union), [typeof(IEnumerable<Geometry>)])!;

private static readonly MethodInfo EnvelopeCombineMethod
= typeof(EnvelopeCombiner).GetRuntimeMethod(nameof(EnvelopeCombiner.CombineAsGeometry), new[] { typeof(IEnumerable<Geometry>) })!;
= typeof(EnvelopeCombiner).GetRuntimeMethod(nameof(EnvelopeCombiner.CombineAsGeometry), [typeof(IEnumerable<Geometry>)])!;

private readonly NpgsqlSqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class NpgsqlGeometryMemberTranslator : IMemberTranslator
private readonly IRelationalTypeMappingSource _typeMappingSource;
private readonly CaseWhenClause[] _ogcGeometryTypeWhenThenList;

private static readonly bool[][] TrueArrays = { Array.Empty<bool>(), new[] { true }, new[] { true, true }, new[] { true, true, true } };
private static readonly bool[][] TrueArrays = [[], [true], [true, true], [true, true, true]];

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -59,8 +59,8 @@ public NpgsqlGeometryMemberTranslator(
_sqlExpressionFactory = sqlExpressionFactory;
_typeMappingSource = typeMappingSource;

_ogcGeometryTypeWhenThenList = new[]
{
_ogcGeometryTypeWhenThenList =
[
new CaseWhenClause(
_sqlExpressionFactory.Constant("ST_CircularString"), _sqlExpressionFactory.Constant(OgcGeometryType.CircularString)),
new CaseWhenClause(
Expand Down Expand Up @@ -88,7 +88,7 @@ public NpgsqlGeometryMemberTranslator(
_sqlExpressionFactory.Constant("ST_PolyhedralSurface"),
_sqlExpressionFactory.Constant(OgcGeometryType.PolyhedralSurface)),
new CaseWhenClause(_sqlExpressionFactory.Constant("ST_Tin"), _sqlExpressionFactory.Constant(OgcGeometryType.TIN))
};
];
}

/// <summary>
Expand Down Expand Up @@ -127,45 +127,45 @@ public NpgsqlGeometryMemberTranslator(

if (function is not null)
{
return Function(function, new[] { instance }, typeof(double));
return Function(function, [instance], typeof(double));
}
}

if (typeof(LineString).IsAssignableFrom(declaringType))
{
if (member.Name == "Count")
{
return Function("ST_NumPoints", new[] { instance }, typeof(int));
return Function("ST_NumPoints", [instance], typeof(int));
}
}

return member.Name switch
{
nameof(Geometry.Area) => Function("ST_Area", new[] { instance }, typeof(double)),
nameof(Geometry.Boundary) => Function("ST_Boundary", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Centroid) => Function("ST_Centroid", new[] { instance }, typeof(Point), ResultGeometryMapping()),
nameof(GeometryCollection.Count) => Function("ST_NumGeometries", new[] { instance }, typeof(int)),
nameof(Geometry.Dimension) => Function("ST_Dimension", new[] { instance }, typeof(Dimension)),
nameof(LineString.EndPoint) => Function("ST_EndPoint", new[] { instance }, typeof(Point), ResultGeometryMapping()),
nameof(Geometry.Envelope) => Function("ST_Envelope", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
nameof(Polygon.ExteriorRing) => Function("ST_ExteriorRing", new[] { instance }, typeof(LineString), ResultGeometryMapping()),
nameof(Geometry.GeometryType) => Function("GeometryType", new[] { instance }, typeof(string)),
nameof(LineString.IsClosed) => Function("ST_IsClosed", new[] { instance }, typeof(bool)),
nameof(Geometry.IsEmpty) => Function("ST_IsEmpty", new[] { instance }, typeof(bool)),
nameof(LineString.IsRing) => Function("ST_IsRing", new[] { instance }, typeof(bool)),
nameof(Geometry.IsSimple) => Function("ST_IsSimple", new[] { instance }, typeof(bool)),
nameof(Geometry.IsValid) => Function("ST_IsValid", new[] { instance }, typeof(bool)),
nameof(Geometry.Length) => Function("ST_Length", new[] { instance }, typeof(double)),
nameof(Geometry.NumGeometries) => Function("ST_NumGeometries", new[] { instance }, typeof(int)),
nameof(Polygon.NumInteriorRings) => Function("ST_NumInteriorRings", new[] { instance }, typeof(int)),
nameof(Geometry.NumPoints) => Function("ST_NumPoints", new[] { instance }, typeof(int)),
nameof(Geometry.PointOnSurface) => Function("ST_PointOnSurface", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.InteriorPoint) => Function("ST_PointOnSurface", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.SRID) => Function("ST_SRID", new[] { instance }, typeof(int)),
nameof(LineString.StartPoint) => Function("ST_StartPoint", new[] { instance }, typeof(Point), ResultGeometryMapping()),
nameof(Geometry.Area) => Function("ST_Area", [instance], typeof(double)),
nameof(Geometry.Boundary) => Function("ST_Boundary", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Centroid) => Function("ST_Centroid", [instance], typeof(Point), ResultGeometryMapping()),
nameof(GeometryCollection.Count) => Function("ST_NumGeometries", [instance], typeof(int)),
nameof(Geometry.Dimension) => Function("ST_Dimension", [instance], typeof(Dimension)),
nameof(LineString.EndPoint) => Function("ST_EndPoint", [instance], typeof(Point), ResultGeometryMapping()),
nameof(Geometry.Envelope) => Function("ST_Envelope", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Polygon.ExteriorRing) => Function("ST_ExteriorRing", [instance], typeof(LineString), ResultGeometryMapping()),
nameof(Geometry.GeometryType) => Function("GeometryType", [instance], typeof(string)),
nameof(LineString.IsClosed) => Function("ST_IsClosed", [instance], typeof(bool)),
nameof(Geometry.IsEmpty) => Function("ST_IsEmpty", [instance], typeof(bool)),
nameof(LineString.IsRing) => Function("ST_IsRing", [instance], typeof(bool)),
nameof(Geometry.IsSimple) => Function("ST_IsSimple", [instance], typeof(bool)),
nameof(Geometry.IsValid) => Function("ST_IsValid", [instance], typeof(bool)),
nameof(Geometry.Length) => Function("ST_Length", [instance], typeof(double)),
nameof(Geometry.NumGeometries) => Function("ST_NumGeometries", [instance], typeof(int)),
nameof(Polygon.NumInteriorRings) => Function("ST_NumInteriorRings", [instance], typeof(int)),
nameof(Geometry.NumPoints) => Function("ST_NumPoints", [instance], typeof(int)),
nameof(Geometry.PointOnSurface) => Function("ST_PointOnSurface", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.InteriorPoint) => Function("ST_PointOnSurface", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.SRID) => Function("ST_SRID", [instance], typeof(int)),
nameof(LineString.StartPoint) => Function("ST_StartPoint", [instance], typeof(Point), ResultGeometryMapping()),

nameof(Geometry.OgcGeometryType) => _sqlExpressionFactory.Case(
Function("ST_GeometryType", new[] { instance }, typeof(string)),
Function("ST_GeometryType", [instance], typeof(string)),
_ogcGeometryTypeWhenThenList,
elseResult: null),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public class NpgsqlGeometryMethodTranslator : IMethodCallTranslator
private readonly IRelationalTypeMappingSource _typeMappingSource;

private static readonly bool[][] TrueArrays =
{
Array.Empty<bool>(), new[] { true }, new[] { true, true }, new[] { true, true, true }, new[] { true, true, true, true }
};
[
[], [true], [true, true], [true, true, true], [true, true, true, true]
];

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down Expand Up @@ -157,65 +157,65 @@ public NpgsqlGeometryMethodTranslator(
return method.Name switch
{
nameof(Geometry.AsBinary)
=> Function("ST_AsBinary", new[] { instance }, typeof(byte[])),
=> Function("ST_AsBinary", [instance], typeof(byte[])),
nameof(Geometry.AsText)
=> Function("ST_AsText", new[] { instance }, typeof(string)),
=> Function("ST_AsText", [instance], typeof(string)),
nameof(Geometry.Buffer)
=> Function("ST_Buffer", new[] { instance }.Concat(arguments).ToArray(), typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Contains)
=> Function("ST_Contains", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Contains", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.ConvexHull)
=> Function("ST_ConvexHull", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_ConvexHull", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.CoveredBy)
=> Function("ST_CoveredBy", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_CoveredBy", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.Covers)
=> Function("ST_Covers", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Covers", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.Crosses)
=> Function("ST_Crosses", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Crosses", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.Disjoint)
=> Function("ST_Disjoint", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Disjoint", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.Difference)
=> Function("ST_Difference", new[] { instance, arguments[0] }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_Difference", [instance, arguments[0]], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Distance)
=> Function("ST_Distance", new[] { instance }.Concat(arguments).ToArray(), typeof(double)),
nameof(Geometry.EqualsExact)
=> Function("ST_OrderingEquals", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_OrderingEquals", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.EqualsTopologically)
=> Function("ST_Equals", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Equals", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.GetGeometryN)
=> Function("ST_GeometryN", new[] { instance, OneBased(arguments[0]) }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_GeometryN", [instance, OneBased(arguments[0])], typeof(Geometry), ResultGeometryMapping()),
nameof(Polygon.GetInteriorRingN)
=> Function("ST_InteriorRingN", new[] { instance, OneBased(arguments[0]) }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_InteriorRingN", [instance, OneBased(arguments[0])], typeof(Geometry), ResultGeometryMapping()),
nameof(LineString.GetPointN)
=> Function("ST_PointN", new[] { instance, OneBased(arguments[0]) }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_PointN", [instance, OneBased(arguments[0])], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Intersection)
=> Function("ST_Intersection", new[] { instance, arguments[0] }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_Intersection", [instance, arguments[0]], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Intersects)
=> Function("ST_Intersects", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Intersects", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.IsWithinDistance)
=> Function("ST_DWithin", new[] { instance }.Concat(arguments).ToArray(), typeof(bool)),
nameof(Geometry.Normalized)
=> Function("ST_Normalize", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_Normalize", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Overlaps)
=> Function("ST_Overlaps", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Overlaps", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.Relate)
=> Function("ST_Relate", new[] { instance, arguments[0], arguments[1] }, typeof(bool)),
=> Function("ST_Relate", [instance, arguments[0], arguments[1]], typeof(bool)),
nameof(Geometry.Reverse)
=> Function("ST_Reverse", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_Reverse", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.SymmetricDifference)
=> Function("ST_SymDifference", new[] { instance, arguments[0] }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_SymDifference", [instance, arguments[0]], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.ToBinary)
=> Function("ST_AsBinary", new[] { instance }, typeof(byte[])),
=> Function("ST_AsBinary", [instance], typeof(byte[])),
nameof(Geometry.ToText)
=> Function("ST_AsText", new[] { instance }, typeof(string)),
=> Function("ST_AsText", [instance], typeof(string)),
nameof(Geometry.Touches)
=> Function("ST_Touches", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Touches", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.Within)
=> Function("ST_Within", new[] { instance, arguments[0] }, typeof(bool)),
=> Function("ST_Within", [instance, arguments[0]], typeof(bool)),
nameof(Geometry.Union) when arguments.Count == 0
=> Function("ST_UnaryUnion", new[] { instance }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_UnaryUnion", [instance], typeof(Geometry), ResultGeometryMapping()),
nameof(Geometry.Union) when arguments.Count == 1
=> Function("ST_Union", new[] { instance, arguments[0] }, typeof(Geometry), ResultGeometryMapping()),
=> Function("ST_Union", [instance, arguments[0]], typeof(Geometry), ResultGeometryMapping()),

_ => null
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public NpgsqlNodaTimeAggregateMethodCallTranslatorPlugin(
/// </summary>
public class NpgsqlNodaTimeAggregateMethodTranslator : IAggregateMethodCallTranslator
{
private static readonly bool[][] FalseArrays = { Array.Empty<bool>(), new[] { false } };
private static readonly bool[][] FalseArrays = [[], [false]];

private readonly NpgsqlSqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.NodaTime.Query.Internal;
public class NpgsqlNodaTimeEvaluatableExpressionFilterPlugin : IEvaluatableExpressionFilterPlugin
{
private static readonly MethodInfo GetCurrentInstantMethod =
typeof(SystemClock).GetRuntimeMethod(nameof(SystemClock.GetCurrentInstant), Array.Empty<Type>())!;
typeof(SystemClock).GetRuntimeMethod(nameof(SystemClock.GetCurrentInstant), [])!;

private static readonly MemberInfo SystemClockInstanceMember =
typeof(SystemClock).GetMember(nameof(SystemClock.Instance)).FirstOrDefault()!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public NpgsqlNodaTimeMemberTranslator(
_localDateTimeTypeMapping = typeMappingSource.FindMapping(typeof(LocalDateTime))!;
}

private static readonly bool[][] TrueArrays = { Array.Empty<bool>(), new[] { true }, new[] { true, true } };
private static readonly bool[][] TrueArrays = [[], [true], [true, true]];

/// <inheritdoc />
public virtual SqlExpression? Translate(
Expand Down
Loading

0 comments on commit e6d545a

Please sign in to comment.