@@ -58,7 +58,7 @@ public static RoutePattern Parse([StringSyntax("Route")] string pattern, object?
58
58
ArgumentNullException . ThrowIfNull ( pattern ) ;
59
59
60
60
var original = RoutePatternParser . Parse ( pattern ) ;
61
- return PatternCore ( original . RawText , Wrap ( defaults ) , Wrap ( parameterPolicies ) , requiredValues : null , original . PathSegments ) ;
61
+ return PatternCore ( original . RawText , Wrap ( defaults ) , CreateRoutePatternPolicyReferences ( Wrap ( parameterPolicies ) ) , requiredValues : null , original . PathSegments ) ;
62
62
}
63
63
64
64
/// <summary>
@@ -83,7 +83,7 @@ public static RoutePattern Parse([StringSyntax("Route")] string pattern, RouteVa
83
83
ArgumentNullException . ThrowIfNull ( pattern ) ;
84
84
85
85
var original = RoutePatternParser . Parse ( pattern ) ;
86
- return PatternCore ( original . RawText , defaults , parameterPolicies , requiredValues : null , original . PathSegments ) ;
86
+ return PatternCore ( original . RawText , defaults , CreateRoutePatternPolicyReferences ( parameterPolicies ) , requiredValues : null , original . PathSegments ) ;
87
87
}
88
88
89
89
/// <summary>
@@ -112,7 +112,7 @@ public static RoutePattern Parse([StringSyntax("Route")] string pattern, object?
112
112
ArgumentNullException . ThrowIfNull ( pattern ) ;
113
113
114
114
var original = RoutePatternParser . Parse ( pattern ) ;
115
- return PatternCore ( original . RawText , Wrap ( defaults ) , Wrap ( parameterPolicies ) , Wrap ( requiredValues ) , original . PathSegments ) ;
115
+ return PatternCore ( original . RawText , Wrap ( defaults ) , CreateRoutePatternPolicyReferences ( Wrap ( parameterPolicies ) ) , Wrap ( requiredValues ) , original . PathSegments ) ;
116
116
}
117
117
118
118
/// <summary>
@@ -140,7 +140,7 @@ public static RoutePattern Parse([StringSyntax("Route")] string pattern, RouteVa
140
140
ArgumentNullException . ThrowIfNull ( pattern ) ;
141
141
142
142
var original = RoutePatternParser . Parse ( pattern ) ;
143
- return PatternCore ( original . RawText , defaults , parameterPolicies , requiredValues , original . PathSegments ) ;
143
+ return PatternCore ( original . RawText , defaults , CreateRoutePatternPolicyReferences ( parameterPolicies ) , requiredValues , original . PathSegments ) ;
144
144
}
145
145
146
146
/// <summary>
@@ -193,7 +193,7 @@ public static RoutePattern Pattern(
193
193
{
194
194
ArgumentNullException . ThrowIfNull ( segments ) ;
195
195
196
- return PatternCore ( null , new RouteValueDictionary ( defaults ) , new RouteValueDictionary ( parameterPolicies ) , requiredValues : null , segments ) ;
196
+ return PatternCore ( null , Wrap ( defaults ) , CreateRoutePatternPolicyReferences ( Wrap ( parameterPolicies ) ) , requiredValues : null , segments ) ;
197
197
}
198
198
199
199
/// <summary>
@@ -220,7 +220,7 @@ public static RoutePattern Pattern(
220
220
{
221
221
ArgumentNullException . ThrowIfNull ( segments ) ;
222
222
223
- return PatternCore ( null , defaults , parameterPolicies , requiredValues : null , segments ) ;
223
+ return PatternCore ( null , defaults , CreateRoutePatternPolicyReferences ( parameterPolicies ) , requiredValues : null , segments ) ;
224
224
}
225
225
226
226
/// <summary>
@@ -250,7 +250,7 @@ public static RoutePattern Pattern(
250
250
{
251
251
ArgumentNullException . ThrowIfNull ( segments ) ;
252
252
253
- return PatternCore ( rawText , new RouteValueDictionary ( defaults ) , new RouteValueDictionary ( parameterPolicies ) , requiredValues : null , segments ) ;
253
+ return PatternCore ( rawText , Wrap ( defaults ) , CreateRoutePatternPolicyReferences ( Wrap ( parameterPolicies ) ) , requiredValues : null , segments ) ;
254
254
}
255
255
256
256
/// <summary>
@@ -279,7 +279,7 @@ public static RoutePattern Pattern(
279
279
{
280
280
ArgumentNullException . ThrowIfNull ( segments ) ;
281
281
282
- return PatternCore ( rawText , defaults , parameterPolicies , requiredValues : null , segments ) ;
282
+ return PatternCore ( rawText , defaults , CreateRoutePatternPolicyReferences ( parameterPolicies ) , requiredValues : null , segments ) ;
283
283
}
284
284
285
285
/// <summary>
@@ -332,7 +332,7 @@ public static RoutePattern Pattern(
332
332
{
333
333
ArgumentNullException . ThrowIfNull ( segments ) ;
334
334
335
- return PatternCore ( null , new RouteValueDictionary ( defaults ) , new RouteValueDictionary ( parameterPolicies ) , requiredValues : null , segments ) ;
335
+ return PatternCore ( null , Wrap ( defaults ) , CreateRoutePatternPolicyReferences ( Wrap ( parameterPolicies ) ) , requiredValues : null , segments ) ;
336
336
}
337
337
338
338
/// <summary>
@@ -359,7 +359,7 @@ public static RoutePattern Pattern(
359
359
{
360
360
ArgumentNullException . ThrowIfNull ( segments ) ;
361
361
362
- return PatternCore ( null , defaults , parameterPolicies , requiredValues : null , segments ) ;
362
+ return PatternCore ( null , defaults , CreateRoutePatternPolicyReferences ( parameterPolicies ) , requiredValues : null , segments ) ;
363
363
}
364
364
365
365
/// <summary>
@@ -389,7 +389,7 @@ public static RoutePattern Pattern(
389
389
{
390
390
ArgumentNullException . ThrowIfNull ( segments ) ;
391
391
392
- return PatternCore ( rawText , new RouteValueDictionary ( defaults ) , new RouteValueDictionary ( parameterPolicies ) , requiredValues : null , segments ) ;
392
+ return PatternCore ( rawText , Wrap ( defaults ) , CreateRoutePatternPolicyReferences ( Wrap ( parameterPolicies ) ) , requiredValues : null , segments ) ;
393
393
}
394
394
395
395
/// <summary>
@@ -418,13 +418,13 @@ public static RoutePattern Pattern(
418
418
{
419
419
ArgumentNullException . ThrowIfNull ( segments ) ;
420
420
421
- return PatternCore ( rawText , defaults , parameterPolicies , requiredValues : null , segments ) ;
421
+ return PatternCore ( rawText , defaults , CreateRoutePatternPolicyReferences ( parameterPolicies ) , requiredValues : null , segments ) ;
422
422
}
423
423
424
424
private static RoutePattern PatternCore (
425
425
string ? rawText ,
426
426
RouteValueDictionary ? defaults ,
427
- RouteValueDictionary ? parameterPolicies ,
427
+ Dictionary < string , List < RoutePatternParameterPolicyReference > > ? parameterPolicyReferences ,
428
428
RouteValueDictionary ? requiredValues ,
429
429
IEnumerable < RoutePatternPathSegment > segments )
430
430
{
@@ -450,43 +450,6 @@ private static RoutePattern PatternCore(
450
450
}
451
451
}
452
452
453
- Dictionary < string , List < RoutePatternParameterPolicyReference > > ? updatedParameterPolicies = null ;
454
- if ( parameterPolicies != null && parameterPolicies . Count > 0 )
455
- {
456
- updatedParameterPolicies = new Dictionary < string , List < RoutePatternParameterPolicyReference > > ( parameterPolicies . Count , StringComparer . OrdinalIgnoreCase ) ;
457
-
458
- foreach ( var kvp in parameterPolicies )
459
- {
460
- var policyReferences = new List < RoutePatternParameterPolicyReference > ( ) ;
461
-
462
- if ( kvp . Value is IParameterPolicy parameterPolicy )
463
- {
464
- policyReferences . Add ( ParameterPolicy ( parameterPolicy ) ) ;
465
- }
466
- else if ( kvp . Value is string )
467
- {
468
- // Constraint will convert string values into regex constraints
469
- policyReferences . Add ( Constraint ( kvp . Value ) ) ;
470
- }
471
- else if ( kvp . Value is IEnumerable multiplePolicies )
472
- {
473
- foreach ( var item in multiplePolicies )
474
- {
475
- // Constraint will convert string values into regex constraints
476
- policyReferences . Add ( item is IParameterPolicy p ? ParameterPolicy ( p ) : Constraint ( item ) ) ;
477
- }
478
- }
479
- else
480
- {
481
- throw new InvalidOperationException ( Resources . FormatRoutePattern_InvalidConstraintReference (
482
- kvp . Value ?? "null" ,
483
- typeof ( IRouteConstraint ) ) ) ;
484
- }
485
-
486
- updatedParameterPolicies . Add ( kvp . Key , policyReferences ) ;
487
- }
488
- }
489
-
490
453
List < RoutePatternParameterPart > ? parameters = null ;
491
454
var updatedSegments = segments . ToArray ( ) ;
492
455
for ( var i = 0 ; i < updatedSegments . Length ; i ++ )
@@ -554,8 +517,8 @@ private static RoutePattern PatternCore(
554
517
return new RoutePattern (
555
518
rawText ,
556
519
updatedDefaults ?? EmptyDictionary ,
557
- updatedParameterPolicies != null
558
- ? updatedParameterPolicies . ToDictionary ( kvp => kvp . Key , kvp => ( IReadOnlyList < RoutePatternParameterPolicyReference > ) kvp . Value . ToArray ( ) )
520
+ parameterPolicyReferences != null
521
+ ? parameterPolicyReferences . ToDictionary ( kvp => kvp . Key , kvp => ( IReadOnlyList < RoutePatternParameterPolicyReference > ) kvp . Value . ToArray ( ) )
559
522
: EmptyPoliciesDictionary ,
560
523
requiredValues ?? EmptyDictionary ,
561
524
( IReadOnlyList < RoutePatternParameterPart > ? ) parameters ?? Array . Empty < RoutePatternParameterPart > ( ) ,
@@ -627,16 +590,16 @@ RoutePatternPart VisitPart(RoutePatternPart part)
627
590
}
628
591
629
592
List < RoutePatternParameterPolicyReference > ? parameterConstraints = null ;
630
- if ( ( updatedParameterPolicies == null || ! updatedParameterPolicies . TryGetValue ( parameter . Name , out parameterConstraints ) ) &&
593
+ if ( ( parameterPolicyReferences == null || ! parameterPolicyReferences . TryGetValue ( parameter . Name , out parameterConstraints ) ) &&
631
594
parameter . ParameterPolicies . Count > 0 )
632
595
{
633
- if ( updatedParameterPolicies == null )
596
+ if ( parameterPolicyReferences == null )
634
597
{
635
- updatedParameterPolicies = new Dictionary < string , List < RoutePatternParameterPolicyReference > > ( StringComparer . OrdinalIgnoreCase ) ;
598
+ parameterPolicyReferences = new Dictionary < string , List < RoutePatternParameterPolicyReference > > ( StringComparer . OrdinalIgnoreCase ) ;
636
599
}
637
600
638
601
parameterConstraints = new List < RoutePatternParameterPolicyReference > ( parameter . ParameterPolicies . Count ) ;
639
- updatedParameterPolicies . Add ( parameter . Name , parameterConstraints ) ;
602
+ parameterPolicyReferences . Add ( parameter . Name , parameterConstraints ) ;
640
603
}
641
604
642
605
if ( parameter . ParameterPolicies . Count > 0 )
@@ -661,6 +624,52 @@ RoutePatternPart VisitPart(RoutePatternPart part)
661
624
}
662
625
}
663
626
627
+ /// <summary>
628
+ /// String policy references are infered to be regex constraints. Creating them is moved here to its own method so apps can
629
+ /// trim away the regex dependency when RoutePatternFactory.Parse(string) is used. This is the method typically used by the various Map methods.
630
+ /// </summary>
631
+ private static Dictionary < string , List < RoutePatternParameterPolicyReference > > ? CreateRoutePatternPolicyReferences ( RouteValueDictionary ? parameterPolicies )
632
+ {
633
+ Dictionary < string , List < RoutePatternParameterPolicyReference > > ? updatedParameterPolicies = null ;
634
+ if ( parameterPolicies != null && parameterPolicies . Count > 0 )
635
+ {
636
+ updatedParameterPolicies = new Dictionary < string , List < RoutePatternParameterPolicyReference > > ( parameterPolicies . Count , StringComparer . OrdinalIgnoreCase ) ;
637
+
638
+ foreach ( var kvp in parameterPolicies )
639
+ {
640
+ var policyReferences = new List < RoutePatternParameterPolicyReference > ( ) ;
641
+
642
+ if ( kvp . Value is IParameterPolicy parameterPolicy )
643
+ {
644
+ policyReferences . Add ( ParameterPolicy ( parameterPolicy ) ) ;
645
+ }
646
+ else if ( kvp . Value is string )
647
+ {
648
+ // Constraint will convert string values into regex constraints
649
+ policyReferences . Add ( Constraint ( kvp . Value ) ) ;
650
+ }
651
+ else if ( kvp . Value is IEnumerable multiplePolicies )
652
+ {
653
+ foreach ( var item in multiplePolicies )
654
+ {
655
+ // Constraint will convert string values into regex constraints
656
+ policyReferences . Add ( item is IParameterPolicy p ? ParameterPolicy ( p ) : Constraint ( item ) ) ;
657
+ }
658
+ }
659
+ else
660
+ {
661
+ throw new InvalidOperationException ( Resources . FormatRoutePattern_InvalidConstraintReference (
662
+ kvp . Value ?? "null" ,
663
+ typeof ( IRouteConstraint ) ) ) ;
664
+ }
665
+
666
+ updatedParameterPolicies . Add ( kvp . Key , policyReferences ) ;
667
+ }
668
+ }
669
+
670
+ return updatedParameterPolicies ;
671
+ }
672
+
664
673
/// <summary>
665
674
/// Creates a <see cref="RoutePatternPathSegment"/> from the provided collection
666
675
/// of parts.
0 commit comments