Skip to content

Commit 97488e2

Browse files
committed
[r] to string converter
1 parent aeed3fd commit 97488e2

1 file changed

Lines changed: 3 additions & 69 deletions

File tree

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Linq;
43
using Simplify.Web.Controllers.Meta;
54
using Simplify.Web.Controllers.Meta.Routing;
65
using Simplify.Web.Controllers.RouteMatching;
76
using Simplify.Web.Controllers.V1.Metadata;
7+
using Simplify.Web.System;
88

99
namespace Simplify.Web.Controllers.V1.Routing;
1010

1111
public class Controller1RouteMatcher : IRouteMatcher
1212
{
13-
private static readonly Dictionary<Type, Func<string, object?>> ParameterValueConverters =
14-
new()
15-
{
16-
{ typeof(string), sourceValue => sourceValue },
17-
{ typeof(int), GetIntParameterValue },
18-
{ typeof(decimal), GetDecimalParameterValue },
19-
{ typeof(bool), GetBoolParameterValue },
20-
{ typeof(string[]), GetStringArrayParameterValue },
21-
{ typeof(int[]), GetIntArrayParameterValue },
22-
{ typeof(decimal[]), GetDecimalArrayParameterValue },
23-
{ typeof(bool[]), GetBoolArrayParameterValue },
24-
};
25-
2613
public bool CanHandle(IControllerMetadata controller) => controller is IController1Metadata;
2714

2815
public IRouteMatchResult Match(IList<string> currentPath, IControllerRoute controllerRoute)
@@ -52,7 +39,7 @@ private static bool MatchPathItem(PathItem item, string currentPathSegment, Dict
5239
return segment.Name == currentPathSegment;
5340

5441
case PathParameter parameter:
55-
var value = GetParameterValue(parameter, currentPathSegment);
42+
var value = StringConverter.TryConvert(parameter.Type, currentPathSegment);
5643

5744
if (value == null)
5845
return false;
@@ -64,57 +51,4 @@ private static bool MatchPathItem(PathItem item, string currentPathSegment, Dict
6451
return false;
6552
}
6653
}
67-
68-
private static object? GetParameterValue(PathParameter pathParameter, string sourceValue) =>
69-
ParameterValueConverters.TryGetValue(pathParameter.Type, out var converter)
70-
? converter(sourceValue)
71-
: null;
72-
73-
private static object? GetIntParameterValue(string source)
74-
{
75-
if (!int.TryParse(source, out var buffer))
76-
return null;
77-
78-
return buffer;
79-
}
80-
81-
private static object? GetDecimalParameterValue(string source)
82-
{
83-
if (!decimal.TryParse(source, out var buffer))
84-
return null;
85-
86-
return buffer;
87-
}
88-
89-
private static object? GetBoolParameterValue(string source)
90-
{
91-
if (!bool.TryParse(source, out var buffer))
92-
return null;
93-
94-
return buffer;
95-
}
96-
97-
private static IList<string> GetStringArrayParameterValue(string source) =>
98-
source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
99-
100-
private static IList<int> GetIntArrayParameterValue(string source) =>
101-
source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
102-
.Select(GetIntParameterValue)
103-
.Where(x => x != null)
104-
.Cast<int>()
105-
.ToList();
106-
107-
private static IList<decimal> GetDecimalArrayParameterValue(string source) =>
108-
source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
109-
.Select(GetDecimalParameterValue)
110-
.Where(x => x != null)
111-
.Cast<decimal>()
112-
.ToList();
113-
114-
private static IList<bool> GetBoolArrayParameterValue(string source) =>
115-
source.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
116-
.Select(GetBoolParameterValue)
117-
.Where(x => x != null)
118-
.Cast<bool>()
119-
.ToList();
12054
}

0 commit comments

Comments
 (0)