Skip to content

Commit 2aecb17

Browse files
committed
Resharper: convert to switch expression
1 parent 250cfdf commit 2aecb17

File tree

2 files changed

+18
-45
lines changed

2 files changed

+18
-45
lines changed

src/JsonApiDotNetCore.Annotations/CollectionConverter.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,15 @@ private Type ToConcreteCollectionType(Type collectionType)
6868
/// </summary>
6969
public IReadOnlyCollection<IIdentifiable> ExtractResources(object? value)
7070
{
71-
if (value is List<IIdentifiable> resourceList)
71+
return value switch
7272
{
73-
return resourceList;
74-
}
75-
76-
if (value is HashSet<IIdentifiable> resourceSet)
77-
{
78-
return resourceSet;
79-
}
80-
81-
if (value is IReadOnlyCollection<IIdentifiable> resourceCollection)
82-
{
83-
return resourceCollection;
84-
}
85-
86-
if (value is IEnumerable<IIdentifiable> resources)
87-
{
88-
return resources.ToList();
89-
}
90-
91-
if (value is IIdentifiable resource)
92-
{
93-
return resource.AsArray();
94-
}
95-
96-
return Array.Empty<IIdentifiable>();
73+
List<IIdentifiable> resourceList => resourceList,
74+
HashSet<IIdentifiable> resourceSet => resourceSet,
75+
IReadOnlyCollection<IIdentifiable> resourceCollection => resourceCollection,
76+
IEnumerable<IIdentifiable> resources => resources.ToList(),
77+
IIdentifiable resource => resource.AsArray(),
78+
_ => Array.Empty<IIdentifiable>()
79+
};
9780
}
9881

9982
/// <summary>

src/JsonApiDotNetCore/Queries/QueryableBuilding/WhereClauseBuilder.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,12 @@ public override Expression VisitMatchText(MatchTextExpression expression, QueryC
100100

101101
Expression text = Visit(expression.TextValue, context);
102102

103-
if (expression.MatchKind == TextMatchKind.StartsWith)
103+
return expression.MatchKind switch
104104
{
105-
return Expression.Call(property, "StartsWith", null, text);
106-
}
107-
108-
if (expression.MatchKind == TextMatchKind.EndsWith)
109-
{
110-
return Expression.Call(property, "EndsWith", null, text);
111-
}
112-
113-
return Expression.Call(property, "Contains", null, text);
105+
TextMatchKind.StartsWith => Expression.Call(property, "StartsWith", null, text),
106+
TextMatchKind.EndsWith => Expression.Call(property, "EndsWith", null, text),
107+
_ => Expression.Call(property, "Contains", null, text)
108+
};
114109
}
115110

116111
public override Expression VisitAny(AnyExpression expression, QueryClauseBuilderContext context)
@@ -137,17 +132,12 @@ public override Expression VisitLogical(LogicalExpression expression, QueryClaus
137132
{
138133
var termQueue = new Queue<Expression>(expression.Terms.Select(filter => Visit(filter, context)));
139134

140-
if (expression.Operator == LogicalOperator.And)
141-
{
142-
return Compose(termQueue, Expression.AndAlso);
143-
}
144-
145-
if (expression.Operator == LogicalOperator.Or)
135+
return expression.Operator switch
146136
{
147-
return Compose(termQueue, Expression.OrElse);
148-
}
149-
150-
throw new InvalidOperationException($"Unknown logical operator '{expression.Operator}'.");
137+
LogicalOperator.And => Compose(termQueue, Expression.AndAlso),
138+
LogicalOperator.Or => Compose(termQueue, Expression.OrElse),
139+
_ => throw new InvalidOperationException($"Unknown logical operator '{expression.Operator}'.")
140+
};
151141
}
152142

153143
private static BinaryExpression Compose(Queue<Expression> argumentQueue, Func<Expression, Expression, BinaryExpression> applyOperator)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy