11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Text . RegularExpressions ;
45using Simplify . Web . Controllers . Meta . Routing ;
56
67namespace Simplify . Web . Controllers . V1 . Routing ;
78
89public 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