Skip to content

Commit 7d6cf9f

Browse files
committed
[r] namespace
[r] Controller1PathParser
1 parent ba0efe3 commit 7d6cf9f

4 files changed

Lines changed: 55 additions & 60 deletions

File tree

src/Simplify.Web.Tests/Controllers/V1/Matcher/Controller1PathParserTests.cs renamed to src/Simplify.Web.Tests/Controllers/V1/Routing/Controller1PathParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Simplify.Web.Controllers.Meta.Routing;
33
using Simplify.Web.Controllers.V1.Routing;
44

5-
namespace Simplify.Web.Tests.Controllers.V1.Matcher;
5+
namespace Simplify.Web.Tests.Controllers.V1.Routing;
66

77
[TestFixture]
88
public class Controller1PathParserTests

src/Simplify.Web.Tests/Controllers/V1/Matcher/Controller1RouteMatcherTests.cs renamed to src/Simplify.Web.Tests/Controllers/V1/Routing/Controller1RouteMatcherTests.cs

File renamed without changes.

src/Simplify.Web/Bootstrapper/Setup/BaseBootstrapperControllersRouteMatching.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Simplify.DI;
44
using Simplify.Web.Controllers.RouteMatching;
55
using Simplify.Web.Controllers.RouteMatching.Resolver;
6-
using Simplify.Web.Controllers.V1;
6+
using Simplify.Web.Controllers.V1.Routing;
77

88
namespace Simplify.Web.Bootstrapper.Setup;
99

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,63 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text.RegularExpressions;
45
using Simplify.Web.Controllers.Meta.Routing;
56

67
namespace Simplify.Web.Controllers.V1.Routing;
78

89
public static class Controller1PathParser
910
{
10-
/// <summary>
11-
/// Parses the specified controller path.
12-
/// </summary>
13-
/// <param name="controllerPath">The controller path.</param>
14-
/// <exception cref="ControllerRouteException">
15-
/// Bad controller path: + controllerPath
16-
/// or
17-
/// </exception>
18-
public static IList<PathItem> Parse(string controllerPath)
19-
{
20-
var items = controllerPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
21-
var pathItems = new List<PathItem>();
22-
23-
foreach (var item in items)
24-
{
25-
if (item.Contains("{") || item.Contains("}") || item.Contains(":"))
26-
{
27-
var matches = Regex.Matches(item, @"^{[a-zA-Z0-9:_\-\[\]]+}$");
28-
29-
if (matches.Count == 0)
30-
throw new ControllerRouteException("Bad controller path: " + controllerPath);
31-
32-
var subItem = item.Substring(1, item.Length - 2);
33-
34-
if (subItem.Contains(":"))
35-
{
36-
var parameterData = subItem.Split(':');
37-
var type = ParseParameterType(parameterData[1])
38-
?? throw new ControllerRouteException(
39-
$"Undefined controller parameter type '{parameterData[1]}', path: {controllerPath}");
40-
41-
pathItems.Add(new PathParameter(parameterData[0], type));
42-
}
43-
else
44-
pathItems.Add(new PathParameter(subItem, typeof(string)));
45-
}
46-
else
47-
pathItems.Add(new PathSegment(item));
48-
}
49-
50-
return pathItems;
51-
}
52-
53-
private static Type? ParseParameterType(string typeData)
54-
{
55-
return typeData switch
56-
{
57-
"int" => typeof(int),
58-
"decimal" => typeof(decimal),
59-
"bool" => typeof(bool),
60-
"[]" => typeof(string[]),
61-
"string[]" => typeof(string[]),
62-
"int[]" => typeof(int[]),
63-
"decimal[]" => typeof(decimal[]),
64-
"bool[]" => typeof(bool[]),
65-
_ => null
66-
};
67-
}
11+
private static readonly char[] RequiredSymbols = ['{', '}', ':'];
12+
13+
/// <summary>
14+
/// Parses the specified controller path.
15+
/// </summary>
16+
/// <param name="controllerPath">The controller path.</param>
17+
/// <exception cref="ControllerRouteException">
18+
/// Bad controller path: + controllerPath
19+
/// or
20+
/// </exception>
21+
public static IList<PathItem> Parse(string controllerPath) =>
22+
controllerPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
23+
.Select(item =>
24+
Array.TrueForAll(RequiredSymbols, symbol => !item.Contains(symbol))
25+
? new PathSegment(item)
26+
: ParsePathItem(item, controllerPath))
27+
.ToList();
28+
29+
private static PathItem ParsePathItem(string item, string controllerPath)
30+
{
31+
var matches = Regex.Matches(item, @"^{[a-zA-Z0-9:_\-\[\]]+}$");
32+
33+
if (matches.Count == 0)
34+
throw new ControllerRouteException("Bad controller path: " + controllerPath);
35+
36+
var subItem = item.Substring(1, item.Length - 2);
37+
38+
if (!subItem.Contains(':'))
39+
return new PathParameter(subItem, typeof(string));
40+
41+
var parameterData = subItem.Split(':');
42+
43+
var type = ParseParameterType(parameterData[1])
44+
?? throw new ControllerRouteException(
45+
$"Undefined controller parameter type '{parameterData[1]}', path: {controllerPath}");
46+
47+
return new PathParameter(parameterData[0], type);
48+
}
49+
50+
private static Type? ParseParameterType(string typeData) =>
51+
typeData switch
52+
{
53+
"int" => typeof(int),
54+
"decimal" => typeof(decimal),
55+
"bool" => typeof(bool),
56+
"[]" => typeof(string[]),
57+
"string[]" => typeof(string[]),
58+
"int[]" => typeof(int[]),
59+
"decimal[]" => typeof(decimal[]),
60+
"bool[]" => typeof(bool[]),
61+
_ => null
62+
};
6863
}

0 commit comments

Comments
 (0)