now matches and returns templates and values@

This commit is contained in:
Tom Gardham-Pallister
2016-07-10 18:49:09 +01:00
parent cea6a557e9
commit 1993915564
5 changed files with 177 additions and 35 deletions

View File

@ -2,6 +2,6 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public interface IUrlPathToUrlPathTemplateMatcher
{
bool Match(string urlPath, string urlPathTemplate);
UrlPathMatch Match(string urlPath, string urlPathTemplate);
}
}

View File

@ -0,0 +1,13 @@
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public class TemplateVariableNameAndValue
{
public TemplateVariableNameAndValue(string templateVariableName, string templateVariableValue)
{
TemplateVariableName = templateVariableName;
TemplateVariableValue = templateVariableValue;
}
public string TemplateVariableName {get;private set;}
public string TemplateVariableValue {get;private set;}
}
}

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public class UrlPathMatch
{
public UrlPathMatch(bool match, List<TemplateVariableNameAndValue> templateVariableNameAndValues)
{
Match = match;
TemplateVariableNameAndValues = templateVariableNameAndValues;
}
public bool Match {get;private set;}
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues {get;private set;}
}
}

View File

@ -1,9 +1,14 @@
using System;
using System.Collections.Generic;
namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
public class UrlPathToUrlPathTemplateMatcher : IUrlPathToUrlPathTemplateMatcher
{
public bool Match(string urlPath, string urlPathTemplate)
public UrlPathMatch Match(string urlPath, string urlPathTemplate)
{
var templateKeysAndValues = new List<TemplateVariableNameAndValue>();
urlPath = urlPath.ToLower();
urlPathTemplate = urlPathTemplate.ToLower();
@ -16,20 +21,52 @@ namespace Ocelot.Library.Infrastructure.UrlPathMatcher
{
if (IsPlaceholder(urlPathTemplate[counterForTemplate]))
{
var variableName = GetPlaceholderVariableName(urlPathTemplate, counterForTemplate);
var variableValue = GetPlaceholderVariableValue(urlPath, counterForUrl);
var templateVariableNameAndValue = new TemplateVariableNameAndValue(variableName, variableValue);
templateKeysAndValues.Add(templateVariableNameAndValue);
counterForTemplate = GetNextCounterPosition(urlPathTemplate, counterForTemplate, '}');
counterForUrl = GetNextCounterPosition(urlPath, counterForUrl, '/');
continue;
}
else
{
return false;
return new UrlPathMatch(false, templateKeysAndValues);
}
}
counterForUrl++;
}
return true;
return new UrlPathMatch(true, templateKeysAndValues);
}
private string GetPlaceholderVariableValue(string urlPath, int counterForUrl)
{
var positionOfNextSlash = urlPath.IndexOf('/', counterForUrl);
if(positionOfNextSlash == -1)
{
positionOfNextSlash = urlPath.Length;
}
var variableValue = urlPath.Substring(counterForUrl, positionOfNextSlash - counterForUrl);
return variableValue;
}
private string GetPlaceholderVariableName(string urlPathTemplate, int counterForTemplate)
{
var postitionOfPlaceHolderClosingBracket = urlPathTemplate.IndexOf('}', counterForTemplate) + 1;
var variableName = urlPathTemplate.Substring(counterForTemplate, postitionOfPlaceHolderClosingBracket - counterForTemplate);
return variableName;
}
private int GetNextCounterPosition(string urlTemplate, int counterForTemplate, char delimiter)
{
var closingPlaceHolderPositionOnTemplate = urlTemplate.IndexOf(delimiter, counterForTemplate);