-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
RoutePatternParameterPart.cs
119 lines (100 loc) · 3.45 KB
/
RoutePatternParameterPart.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Text;
namespace Microsoft.AspNetCore.Routing.Patterns;
/// <summary>
/// Represents a parameter part in a route pattern. Instances of <see cref="RoutePatternParameterPart"/>
/// are immutable.
/// </summary>
[DebuggerDisplay("{DebuggerToString()}")]
#if !COMPONENTS
public sealed class RoutePatternParameterPart : RoutePatternPart
#else
internal sealed class RoutePatternParameterPart : RoutePatternPart
#endif
{
internal RoutePatternParameterPart(
string parameterName,
object? @default,
RoutePatternParameterKind parameterKind,
RoutePatternParameterPolicyReference[] parameterPolicies)
: this(parameterName, @default, parameterKind, parameterPolicies, encodeSlashes: true)
{
}
internal RoutePatternParameterPart(
string parameterName,
object? @default,
RoutePatternParameterKind parameterKind,
RoutePatternParameterPolicyReference[] parameterPolicies,
bool encodeSlashes)
: base(RoutePatternPartKind.Parameter)
{
// See #475 - this code should have some asserts, but it can't because of the design of RouteParameterParser.
Name = parameterName;
Default = @default;
ParameterKind = parameterKind;
ParameterPolicies = parameterPolicies;
EncodeSlashes = encodeSlashes;
}
/// <summary>
/// Gets the list of parameter policies associated with this parameter.
/// </summary>
public IReadOnlyList<RoutePatternParameterPolicyReference> ParameterPolicies { get; }
/// <summary>
/// Gets the value indicating if slashes in current parameter's value should be encoded.
/// </summary>
public bool EncodeSlashes { get; }
/// <summary>
/// Gets the default value of this route parameter. May be null.
/// </summary>
public object? Default { get; }
/// <summary>
/// Returns <c>true</c> if this part is a catch-all parameter.
/// Otherwise returns <c>false</c>.
/// </summary>
public bool IsCatchAll => ParameterKind == RoutePatternParameterKind.CatchAll;
/// <summary>
/// Returns <c>true</c> if this part is an optional parameter.
/// Otherwise returns <c>false</c>.
/// </summary>
public bool IsOptional => ParameterKind == RoutePatternParameterKind.Optional;
/// <summary>
/// Gets the <see cref="RoutePatternParameterKind"/> of this parameter.
/// </summary>
public RoutePatternParameterKind ParameterKind { get; }
/// <summary>
/// Gets the parameter name.
/// </summary>
public string Name { get; }
internal override string DebuggerToString()
{
var builder = new StringBuilder();
builder.Append('{');
if (IsCatchAll)
{
builder.Append('*');
if (!EncodeSlashes)
{
builder.Append('*');
}
}
builder.Append(Name);
foreach (var constraint in ParameterPolicies)
{
builder.Append(':');
builder.Append(constraint.ParameterPolicy);
}
if (Default != null)
{
builder.Append('=');
builder.Append(Default);
}
if (IsOptional)
{
builder.Append('?');
}
builder.Append('}');
return builder.ToString();
}
}