Skip to content

Commit 90176f4

Browse files
committed
Simplify UrlLookup
1 parent 402ea00 commit 90176f4

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

src/Nest/CommonAbstractions/Request/ApiUrls.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ internal class ApiUrls
1515
/// the cached string builders.
1616
/// </summary>
1717
private readonly string _fixedUrl;
18-
18+
1919
/// <summary>
2020
/// Creates a lookup for number of parts <=> list of routes with that number of parts.
21-
/// <see cref="UrlLookup.Predicate"/> allows us to quickly find the right url to use in the list.
21+
/// <see cref="UrlLookup.Matches"/> allows us to quickly find the right url to use in the list.
2222
/// </summary>
2323
public Dictionary<int, List<UrlLookup>> Routes { get; }
2424

@@ -36,9 +36,9 @@ internal ApiUrls(string[] routes)
3636
var bracketsCount = route.Count(c => c.Equals('{'));
3737
if (Routes == null) Routes = new Dictionary<int, List<UrlLookup>>();
3838
if (Routes.ContainsKey(bracketsCount))
39-
Routes[bracketsCount].Add(UrlLookup.FromRoute(route));
39+
Routes[bracketsCount].Add(new UrlLookup(route));
4040
else
41-
Routes.Add(bracketsCount, new List<UrlLookup> { UrlLookup.FromRoute(route) });
41+
Routes.Add(bracketsCount, new List<UrlLookup> { new UrlLookup(route) });
4242
}
4343
}
4444

@@ -48,26 +48,25 @@ internal ApiUrls(string[] routes)
4848
if (Routes == null) _fixedUrl = routes[0];
4949
}
5050

51-
5251
public string Resolve(RouteValues routeValues, IConnectionSettingsValues settings)
5352
{
5453
if (_fixedUrl != null) return _fixedUrl;
5554

5655
var resolved = routeValues.Resolve(settings);
57-
56+
5857
if (!Routes.TryGetValue(resolved.Count, out var routes))
59-
throw new Exception($"No route taking {resolved.Count} parameters" + _errorMessageSuffix);
58+
throw new Exception($"No route taking {resolved.Count} parameters{_errorMessageSuffix}");
6059

6160
if (routes.Count == 1)
62-
return routes[0].ToUrl(resolved, settings);
61+
return routes[0].ToUrl(resolved);
6362

6463
//find the first url that has all provided parameters
6564
foreach (var u in routes)
6665
{
67-
if (u.Predicate(resolved))
68-
return u.ToUrl(resolved, settings);
66+
if (u.Matches(resolved))
67+
return u.ToUrl(resolved);
6968
}
70-
throw new Exception($"No route taking {routeValues.Count} parameters" + _errorMessageSuffix);
69+
throw new Exception($"No route taking {routeValues.Count} parameters{_errorMessageSuffix}");
7170
}
7271
}
7372
}

src/Nest/CommonAbstractions/Request/UrlLookup.cs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,62 @@
22
using System.Linq;
33
using System.Text;
44

5-
namespace Nest {
5+
namespace Nest
6+
{
67
internal class UrlLookup
78
{
8-
private UrlLookup(Func<ResolvedRouteValues, bool> lookup, Func<ResolvedRouteValues, IConnectionSettingsValues, string> toString) =>
9-
(Predicate, ToUrl) = (lookup, toString);
9+
private readonly string[] _parts;
10+
private readonly string _route;
11+
private readonly string[] _tokenized;
12+
private readonly int _length;
1013

11-
public Func<ResolvedRouteValues, bool> Predicate { get; set; }
12-
public Func<ResolvedRouteValues, IConnectionSettingsValues, string> ToUrl { get; set; }
13-
14-
public static UrlLookup FromRoute(string route)
14+
public UrlLookup(string route)
1515
{
16-
var tokenized = route.Replace("{", "{@")
16+
_route = route;
17+
_tokenized = route.Replace("{", "{@")
1718
.Split(new[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries);
1819

19-
var parts = tokenized
20+
_parts = _tokenized
2021
.Where(p => p.StartsWith("@"))
2122
.Select(p => p.Remove(0, 1))
2223
.ToArray();
2324

24-
Func<ResolvedRouteValues, bool> lookup;
25-
Func<ResolvedRouteValues, IConnectionSettingsValues, string> toString;
26-
lookup = r => parts.All(p => r.ContainsKey(p));
27-
toString = (r ,s) =>
25+
_length = _route.Length + (_parts.Length * 4);
26+
}
27+
28+
public bool Matches(ResolvedRouteValues values)
29+
{
30+
for (var i = 0; i < _parts.Length; i++)
2831
{
29-
var sb = new StringBuilder();
30-
var i = 0;
31-
foreach (var t in tokenized)
32+
if (!values.ContainsKey(_parts[i]))
33+
return false;
34+
}
35+
return true;
36+
}
37+
38+
public string ToUrl(ResolvedRouteValues values)
39+
{
40+
var sb = new StringBuilder(_length);
41+
int i = 0;
42+
for (var index = 0; index < _tokenized.Length; index++)
43+
{
44+
var t = _tokenized[index];
45+
if (t[0] == '@')
3246
{
33-
if (t[0] == '@')
47+
if (values.TryGetValue(_parts[i], out var v))
3448
{
35-
if (r.TryGetValue(parts[i], out var v))
36-
{
37-
if (string.IsNullOrEmpty(v))
38-
throw new Exception($"'{parts[i]}' defined but is empty on url: {route}");
39-
sb.Append(Uri.EscapeDataString(v));
40-
}
41-
else throw new Exception($"No value provided for '{parts[i]}' on url: {route}");
42-
43-
i++;
49+
if (string.IsNullOrEmpty(v))
50+
throw new Exception($"'{_parts[i]}' defined but is empty on url: {_route}");
51+
52+
sb.Append(Uri.EscapeDataString(v));
4453
}
45-
else sb.Append(t);
54+
else throw new Exception($"No value provided for '{_parts[i]}' on url: {_route}");
55+
56+
i++;
4657
}
47-
return sb.ToString();
48-
};
49-
return new UrlLookup(lookup, toString);
58+
else sb.Append(t);
59+
}
60+
return sb.ToString();
5061
}
5162
}
52-
}
63+
}

0 commit comments

Comments
 (0)