Skip to content

Commit b11b8e8

Browse files
committed
Refactor RQL configuration and result handling
1 parent d0a7388 commit b11b8e8

File tree

57 files changed

+218
-209
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+218
-209
lines changed

samples/Rql.Sample.Api/Extensions/RqlExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class RqlExtensions
1010
public static IServiceCollection AddSoftwareOneRql(this IServiceCollection services)
1111
=> services.AddSoftwareOneRql(null);
1212

13-
public static IServiceCollection AddSoftwareOneRql(this IServiceCollection services, Action<RqlConfiguration>? configure)
13+
public static IServiceCollection AddSoftwareOneRql(this IServiceCollection services, Action<IRqlConfiguration>? configure)
1414
{
1515
services.AddScoped<IErrorResultProvider, ErrorResultProvider>();
1616
services.AddScoped(typeof(IRqlRequest<>), typeof(RqlRequest<>));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma warning disable IDE0130
2+
using SoftwareOne.Rql.Abstractions.Configuration;
3+
using SoftwareOne.Rql.Abstractions.Operators;
4+
using System.Reflection;
5+
6+
namespace SoftwareOne.Rql;
7+
8+
public interface IRqlConfiguration
9+
{
10+
GlobalRqlSettings Settings { get; init; }
11+
12+
IRqlConfiguration ScanForMappers(Assembly assembly);
13+
IRqlConfiguration SetComparisonHandler<TOperator, THandler>()
14+
where TOperator : IComparisonOperator, IActualOperator
15+
where THandler : TOperator;
16+
IRqlConfiguration SetListHandler<TOperator, THandler>()
17+
where TOperator : IListOperator, IActualOperator
18+
where THandler : TOperator;
19+
IRqlConfiguration SetPropertyNameProvider<T>() where T : IPropertyNameProvider;
20+
IRqlConfiguration SetSearchHandler<TOperator, THandler>()
21+
where TOperator : ISearchOperator, IActualOperator
22+
where THandler : TOperator;
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace SoftwareOne.Rql.Abstractions.Operators
2+
{
3+
public interface IActualOperator { }
4+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using SoftwareOne.Rql.Abstractions;
2-
using SoftwareOne.Rql.Linq.Core.Result;
1+
using SoftwareOne.Rql.Abstractions.Result;
32
using System.Linq.Expressions;
43

5-
namespace SoftwareOne.Rql.Linq.Services.Filtering.Operators.Comparison;
4+
namespace SoftwareOne.Rql.Abstractions.Operators;
65

76
public interface IComparisonOperator : IOperator
87
{
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using SoftwareOne.Rql.Abstractions;
2-
using SoftwareOne.Rql.Linq.Core.Result;
1+
using SoftwareOne.Rql.Abstractions.Result;
32
using System.Linq.Expressions;
43

5-
namespace SoftwareOne.Rql.Linq.Services.Filtering.Operators.List;
4+
namespace SoftwareOne.Rql.Abstractions.Operators;
65

76
public interface IListOperator : IOperator
87
{
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace SoftwareOne.Rql.Abstractions.Operators;
2+
3+
public interface IOperator
4+
{
5+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using SoftwareOne.Rql.Abstractions;
2-
using SoftwareOne.Rql.Linq.Core.Result;
2+
using SoftwareOne.Rql.Abstractions.Result;
33
using System.Linq.Expressions;
44

5-
namespace SoftwareOne.Rql.Linq.Services.Filtering.Operators.Search;
5+
namespace SoftwareOne.Rql.Abstractions.Operators;
66

77
public interface ISearchOperator : IOperator
88
{

src/SoftwareOne.Rql.Linq/Core/Result.cs renamed to src/SoftwareOne.Rql.Abstractions/Result/Result.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using SoftwareOne.Rql.Abstractions.Result;
2-
3-
namespace SoftwareOne.Rql.Linq.Core.Result;
1+
namespace SoftwareOne.Rql.Abstractions.Result;
42

53
public class Result<T>
64
{
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using SoftwareOne.Rql.Abstractions;
2-
using SoftwareOne.Rql.Linq.Core.Result;
2+
using SoftwareOne.Rql.Abstractions.Result;
33
using System.Linq.Expressions;
44

5-
namespace SoftwareOne.Rql.Linq.Core
6-
{
7-
internal interface IPathInfoBuilder
8-
{
9-
Result<MemberPathInfo> Build(Expression root, RqlExpression rqlExpression);
5+
namespace SoftwareOne.Rql.Linq.Core;
106

11-
Result<MemberPathInfo> Build(Expression root, string path);
12-
}
7+
internal interface IPathInfoBuilder
8+
{
9+
Result<MemberPathInfo> Build(Expression root, RqlExpression rqlExpression);
1310

14-
internal record MemberPathInfo(string FullPath, ReadOnlyMemory<char> Path, RqlPropertyInfo PropertyInfo, Expression Expression);
11+
Result<MemberPathInfo> Build(Expression root, string path);
1512
}
13+
14+
internal record MemberPathInfo(string FullPath, ReadOnlyMemory<char> Path, RqlPropertyInfo PropertyInfo, Expression Expression);

src/SoftwareOne.Rql.Linq/Core/PathInfoBuilder.cs

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,78 @@
33
using SoftwareOne.Rql.Abstractions.Argument.Pointer;
44
using SoftwareOne.Rql.Abstractions.Result;
55
using SoftwareOne.Rql.Linq.Core.Metadata;
6-
using SoftwareOne.Rql.Linq.Core.Result;
76
using SoftwareOne.Rql.Linq.Services.Context;
87
using System.Linq.Expressions;
98

10-
namespace SoftwareOne.Rql.Linq.Core
9+
namespace SoftwareOne.Rql.Linq.Core;
10+
11+
internal abstract class PathInfoBuilder : IPathInfoBuilder
1112
{
12-
internal abstract class PathInfoBuilder : IPathInfoBuilder
13-
{
14-
private readonly IMetadataProvider _metadataProvider;
15-
private readonly IBuilderContext _builderContext;
13+
private readonly IMetadataProvider _metadataProvider;
14+
private readonly IBuilderContext _builderContext;
1615

17-
protected PathInfoBuilder(IMetadataProvider metadataProvider, IBuilderContext builderContext)
18-
{
19-
_metadataProvider = metadataProvider;
20-
_builderContext = builderContext;
21-
}
16+
protected PathInfoBuilder(IMetadataProvider metadataProvider, IBuilderContext builderContext)
17+
{
18+
_metadataProvider = metadataProvider;
19+
_builderContext = builderContext;
20+
}
2221

23-
public Result<MemberPathInfo> Build(Expression root, RqlExpression rqlExpression)
22+
public Result<MemberPathInfo> Build(Expression root, RqlExpression rqlExpression)
23+
{
24+
switch (rqlExpression)
2425
{
25-
switch (rqlExpression)
26-
{
27-
case RqlSelf self:
28-
{
29-
if (self.Inner != null)
30-
return Build(root, self.Inner);
31-
32-
var path = string.Empty;
33-
return new MemberPathInfo(path, path.AsMemory(0, 0), RqlPropertyInfo.Root, root);
34-
}
35-
case RqlConstant constant:
36-
{
37-
var path = Build(root, constant.Value);
38-
if (path.IsError)
39-
return path.Errors;
40-
return path.Value!;
41-
}
42-
default:
43-
return Error.Validation("Unsupported property node.");
44-
}
45-
}
26+
case RqlSelf self:
27+
{
28+
if (self.Inner != null)
29+
return Build(root, self.Inner);
4630

47-
public Result<MemberPathInfo> Build(Expression root, string path)
48-
{
49-
var nameSegments = path.Split('.');
50-
var aggregatedInfo = nameSegments.Aggregate(
51-
new Result<MemberPathInfo>(new MemberPathInfo(path, path.AsMemory(0, 0), null!, root)),
52-
(current, segment) =>
31+
var path = string.Empty;
32+
return new MemberPathInfo(path, path.AsMemory(0, 0), RqlPropertyInfo.Root, root);
33+
}
34+
case RqlConstant constant:
5335
{
54-
if (current.IsError)
55-
return current;
36+
var path = Build(root, constant.Value);
37+
if (path.IsError)
38+
return path.Errors;
39+
return path.Value!;
40+
}
41+
default:
42+
return Error.Validation("Unsupported property node.");
43+
}
44+
}
5645

57-
var previousLength = current.Value!.Path.Length;
58-
var cumulativePath = current.Value.FullPath.AsMemory(0, (previousLength > 0 ? previousLength + 1 : previousLength) + segment.Length);
46+
public Result<MemberPathInfo> Build(Expression root, string path)
47+
{
48+
var nameSegments = path.Split('.');
49+
var aggregatedInfo = nameSegments.Aggregate(
50+
new Result<MemberPathInfo>(new MemberPathInfo(path, path.AsMemory(0, 0), null!, root)),
51+
(current, segment) =>
52+
{
53+
if (current.IsError)
54+
return current;
5955

60-
if (!_metadataProvider.TryGetPropertyByDisplayName(current.Value.Expression.Type, segment, out var propInfo) || propInfo!.IsIgnored)
61-
return Error.Validation("Invalid property path.", _builderContext.GetFullPath(cumulativePath.ToString()));
56+
var previousLength = current.Value!.Path.Length;
57+
var cumulativePath = current.Value.FullPath.AsMemory(0, (previousLength > 0 ? previousLength + 1 : previousLength) + segment.Length);
6258

63-
var expression = (Expression)Expression.MakeMemberAccess(current.Value!.Expression, propInfo!.Property!);
64-
var pathInfo = new MemberPathInfo(current.Value.FullPath, cumulativePath, propInfo, expression);
59+
if (!_metadataProvider.TryGetPropertyByDisplayName(current.Value.Expression.Type, segment, out var propInfo) || propInfo!.IsIgnored)
60+
return Error.Validation("Invalid property path.", _builderContext.GetFullPath(cumulativePath.ToString()));
6561

66-
var validationResult = ValidatePath(pathInfo);
62+
var expression = (Expression)Expression.MakeMemberAccess(current.Value!.Expression, propInfo!.Property!);
63+
var pathInfo = new MemberPathInfo(current.Value.FullPath, cumulativePath, propInfo, expression);
6764

68-
if (validationResult.IsError)
69-
return validationResult.Errors;
65+
var validationResult = ValidatePath(pathInfo);
7066

71-
return pathInfo;
72-
});
67+
if (validationResult.IsError)
68+
return validationResult.Errors;
7369

74-
if (aggregatedInfo.IsError)
75-
return aggregatedInfo.Errors;
70+
return pathInfo;
71+
});
7672

77-
return aggregatedInfo.Value!;
78-
}
73+
if (aggregatedInfo.IsError)
74+
return aggregatedInfo.Errors;
7975

80-
protected abstract Result<bool> ValidatePath(MemberPathInfo pathInfo);
76+
return aggregatedInfo.Value!;
8177
}
78+
79+
protected abstract Result<bool> ValidatePath(MemberPathInfo pathInfo);
8280
}

0 commit comments

Comments
 (0)