Responding to comments

This commit is contained in:
harshgMSFT 2014-05-21 17:53:37 -07:00
parent 9885830200
commit ff3f011ca8
3 changed files with 23 additions and 12 deletions

View File

@ -9,23 +9,23 @@ using Microsoft.AspNet.Routing.Template;
namespace Microsoft.AspNet.Routing namespace Microsoft.AspNet.Routing
{ {
internal static class InlineRouteParameterParser public static class InlineRouteParameterParser
{ {
// One or more characters, matches "id" // One or more characters, matches "id"
private const string ParameterNameRegex = @"(?<parameterName>.+?)"; private const string ParameterNamePattern = @"(?<parameterName>.+?)";
// Zero or more inline constraints that start with a colon followed by zero or more characters // Zero or more inline constraints that start with a colon followed by zero or more characters
// Optionally the constraint can have arguments within parentheses // Optionally the constraint can have arguments within parentheses
// - necessary to capture characters like ":" and "}" // - necessary to capture characters like ":" and "}"
// Matches ":int", ":length(2)", ":regex(\})", ":regex(:)" zero or more times // Matches ":int", ":length(2)", ":regex(\})", ":regex(:)" zero or more times
private const string ConstraintRegex = @"(:(?<constraint>.*?(\(.*?\))?))*"; private const string ConstraintPattern = @"(:(?<constraint>.*?(\(.*?\))?))*";
// A default value with an equal sign followed by zero or more characters // A default value with an equal sign followed by zero or more characters
// Matches "=", "=abc" // Matches "=", "=abc"
private const string DefaultValueRegex = @"(?<defaultValue>(=.*?))?"; private const string DefaultValueParameter = @"(?<defaultValue>(=.*?))?";
private static readonly Regex _parameterRegex = new Regex( private static readonly Regex _parameterRegex = new Regex(
"^" + ParameterNameRegex + ConstraintRegex + DefaultValueRegex + "$", "^" + ParameterNamePattern + ConstraintPattern + DefaultValueParameter + "$",
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
public static TemplatePart ParseRouteParameter([NotNull] string routeParameter, public static TemplatePart ParseRouteParameter([NotNull] string routeParameter,
[NotNull] IInlineConstraintResolver constraintResolver) [NotNull] IInlineConstraintResolver constraintResolver)
@ -37,7 +37,17 @@ namespace Microsoft.AspNet.Routing
routeParameter = isOptional ? routeParameter.Substring(0, routeParameter.Length - 1) : routeParameter; routeParameter = isOptional ? routeParameter.Substring(0, routeParameter.Length - 1) : routeParameter;
var parameterMatch = _parameterRegex.Match(routeParameter); var parameterMatch = _parameterRegex.Match(routeParameter);
string parameterName = parameterMatch.Groups["parameterName"].Value; if (!parameterMatch.Success)
{
return TemplatePart.CreateParameter(name: string.Empty,
isCatchAll: isCatchAll,
isOptional: isOptional,
defaultValue: null,
inlineConstraint: null
);
}
var parameterName = parameterMatch.Groups["parameterName"].Value;
// Add the default value if present // Add the default value if present
var defaultValueGroup = parameterMatch.Groups["defaultValue"]; var defaultValueGroup = parameterMatch.Groups["defaultValue"];
@ -54,11 +64,11 @@ namespace Microsoft.AspNet.Routing
inlineConstraint); inlineConstraint);
} }
private static object GetDefaultValue(Group defaultValueGroup) private static string GetDefaultValue(Group defaultValueGroup)
{ {
if (defaultValueGroup.Success) if (defaultValueGroup.Success)
{ {
string defaultValueMatch = defaultValueGroup.Value; var defaultValueMatch = defaultValueGroup.Value;
// Strip out the equal sign at the beginning // Strip out the equal sign at the beginning
Contract.Assert(defaultValueMatch.StartsWith("=", StringComparison.Ordinal)); Contract.Assert(defaultValueMatch.StartsWith("=", StringComparison.Ordinal));
@ -74,13 +84,13 @@ namespace Microsoft.AspNet.Routing
var parameterConstraints = new List<IRouteConstraint>(); var parameterConstraints = new List<IRouteConstraint>();
foreach (Capture constraintCapture in constraintGroup.Captures) foreach (Capture constraintCapture in constraintGroup.Captures)
{ {
string inlineConstraint = constraintCapture.Value; var inlineConstraint = constraintCapture.Value;
var constraint = constraintResolver.ResolveConstraint(inlineConstraint); var constraint = constraintResolver.ResolveConstraint(inlineConstraint);
if (constraint == null) if (constraint == null)
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
Resources.FormatInlineRouteParser_CouldNotResolveConstraint(constraintResolver.GetType().Name, Resources.FormatInlineRouteParser_CouldNotResolveConstraint(
inlineConstraint)); constraintResolver.GetType().Name, inlineConstraint));
} }
parameterConstraints.Add(constraint); parameterConstraints.Add(constraint);

View File

@ -173,7 +173,7 @@ namespace Microsoft.AspNet.Routing.Template
} }
var rawParameter = context.Capture(); var rawParameter = context.Capture();
// At this point, we need to parse the raw name for inline constraint, // At this point, we need to parse the raw name for inline constraint,
// default values and optional parameters. // default values and optional parameters.
var templatePart = InlineRouteParameterParser.ParseRouteParameter(rawParameter, var templatePart = InlineRouteParameterParser.ParseRouteParameter(rawParameter,

View File

@ -191,6 +191,7 @@ namespace Microsoft.AspNet.Routing.Template
{ {
_constraints[parameter.Name] = parameter.InlineConstraint; _constraints[parameter.Name] = parameter.InlineConstraint;
} }
}
} }
if (parameter.DefaultValue != null) if (parameter.DefaultValue != null)