Skip to content

Commit d4e4871

Browse files
committed
Code style improvements. Small performance improvements on QueryStringExtensions.
1 parent 74ce0c6 commit d4e4871

File tree

6 files changed

+33
-25
lines changed

6 files changed

+33
-25
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
using System.Diagnostics.CodeAnalysis;
7+
8+
[assembly: SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments", Justification = "Unittests")]
9+
[assembly: SuppressMessage("Performance", "CA1866:Use char overload", Justification = "Unittests")]

src/ByteAether.QueryLink.Test/StringValueParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void Parse_EmptyArray_ShouldReturnArray()
7272
{
7373
var result = StringValueParser.Parse("[]");
7474
Assert.IsType<object[]>(result);
75-
Assert.Equal(new object[] { }, result);
75+
Assert.Equal(Array.Empty<object>(), result);
7676
}
7777

7878
[Fact]

src/ByteAether.QueryLink/IsExternalInit.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.ComponentModel;
22

3+
#pragma warning disable IDE0130 // Namespace does not match folder structure
34
namespace System.Runtime.CompilerServices;
5+
#pragma warning restore IDE0130 // Namespace does not match folder structure
46

57
/// <summary>
68
/// Bug workaround for .net standard

src/ByteAether.QueryLink/QueryStringExtensions.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,24 @@ namespace ByteAether.QueryLink;
1313
/// </summary>
1414
public static class QueryStringExtensions
1515
{
16-
private static readonly (FilterOperator Operator, string StringValue)[] _filterOperatorPairs
16+
private static readonly Dictionary<FilterOperator, string> _operatorToStringMap
1717
= ((FilterOperator[])Enum.GetValues(typeof(FilterOperator)))
18-
.Select(x => (
19-
Operator: x,
20-
StringValue: typeof(FilterOperator)
21-
.GetField(x.ToString())?
22-
.GetCustomAttribute<DescriptionAttribute>()?
23-
.Description ?? x.ToString()
24-
))
25-
.ToArray();
18+
.ToDictionary(
19+
x => x,
20+
x => typeof(FilterOperator)
21+
.GetField(x.ToString())?
22+
.GetCustomAttribute<DescriptionAttribute>()?
23+
.Description ?? x.ToString()
24+
);
25+
26+
private static readonly Dictionary<string, FilterOperator> _operatorFromStringMap
27+
= _operatorToStringMap.ToDictionary(x => x.Value, x => x.Key);
2628

2729
private static readonly Regex _filterSplitter = new(
2830
$"({string.Join(
2931
'|',
30-
_filterOperatorPairs
31-
.Select(x => x.StringValue)
32+
_operatorToStringMap
33+
.Values
3234
.OrderByDescending(x => x.Length)
3335
.Select(Regex.Escape)
3436
)})",
@@ -49,8 +51,8 @@ public static string ToQueryString(
4951
) => new StringBuilder()
5052
.Append(
5153
string.Join('&', definitions.Filters
52-
.Select(ToQueryString)
53-
.Select(x => $"{filterKey}[]={x}")
54+
.Select(ToQueryString)
55+
.Select(x => $"{filterKey}[]={x}")
5456
)
5557
)
5658
.Append(definitions.Orders.Any() ? $"&{orderKey}=" : string.Empty)
@@ -86,12 +88,12 @@ public static Definitions FromQueryString(
8688
if (queryParam.Key == $"{filterKey}[]")
8789
{
8890
var valParts = _filterSplitter.Split(queryParam.Value, 2);
89-
var op = OperatorFromString(valParts[1]);
91+
var op = _operatorFromStringMap[valParts[1]];
9092
var filterValue = StringValueParser.Parse(valParts[2]);
9193

9294
filters.Add(new FilterDefinition<object?>(
9395
valParts[0],
94-
OperatorFromString(valParts[1]),
96+
_operatorFromStringMap[valParts[1]],
9597
filterValue
9698
));
9799
}
@@ -127,12 +129,6 @@ private static string ToQueryString<T>(FilterDefinition<T> def)
127129
? "[" + string.Join(',', (def.Value as IEnumerable)!.OfType<object>().Select(x => x.ToString()?.Replace(",", "\\,"))) + "]"
128130
: def.Value?.ToString();
129131

130-
return HttpUtility.UrlEncode($"{def.Name}{OperatorToString(def.Operation)}{valueSet}");
132+
return HttpUtility.UrlEncode($"{def.Name}{_operatorToStringMap[def.Operation]}{valueSet}");
131133
}
132-
133-
private static string OperatorToString(FilterOperator op)
134-
=> _filterOperatorPairs.Single(x => x.Operator == op).StringValue;
135-
136-
private static FilterOperator OperatorFromString(string operatorString)
137-
=> _filterOperatorPairs.Single(x => x.StringValue == operatorString).Operator;
138134
}

src/ByteAether.QueryLink/QueryableExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ IEnumerable<Overrides<T>.Override<object, object>> overrides
133133
if (or != null)
134134
{
135135
expBody = GetTrueExpression(or.ValueReplace.Body);
136-
expParams = or.ValueReplace.Parameters.ToArray();
136+
expParams = [.. or.ValueReplace.Parameters];
137137
}
138138

139139
var conditionExpression = GetFilterExpression(expBody, Expression.Constant(def.Value), def.Operation);
@@ -179,7 +179,7 @@ IEnumerable<Overrides<T>.Override<object, object>> overrides
179179
if (or != null)
180180
{
181181
expBody = GetTrueExpression(or.ValueReplace.Body);
182-
expParams = or.ValueReplace.Parameters.ToArray();
182+
expParams = [.. or.ValueReplace.Parameters];
183183
}
184184

185185
var selector = Expression.Lambda(expBody, expParams);

src/ByteAether.QueryLink/StringValueParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.RegularExpressions;
33

44
namespace ByteAether.QueryLink;
5+
56
internal static partial class StringValueParser
67
{
78
private static readonly List<Func<string, object?>> _parsers = [

0 commit comments

Comments
 (0)