Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Jun 28, 2023
1 parent 1233cb8 commit df00591
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions web/vibe/web/internal/rest/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -215,30 +215,9 @@ import std.traits : hasUDA;
/** Returns an array with routes grouped by path pattern
*/
static if (__VERSION__ < 2099)
auto getRoutesGroupedByPattern() @trusted { return getRoutesGroupedByPatternImpl(); }
auto getRoutesGroupedByPattern() @trusted { return getRoutesGroupedByPatternImpl(routes); }
else
auto getRoutesGroupedByPattern() { return getRoutesGroupedByPatternImpl(); }

private auto getRoutesGroupedByPatternImpl()
{
import std.algorithm : map, sort, filter, any;
import std.array : array;
import std.typecons : tuple;
// since /foo/:bar and /foo/:baz are the same route, we first normalize
// the patterns (by replacing each param with just ':'). after that, we
// sort and groupBy, in order to group related routes
return routes[]
.dup // just to silence scope warnings later in the chain
.map!(route => tuple(route,
route.fullPathParts
.map!(part => part.isParameter ? ":" : part.text)
.array) // can probably remove the array here if we rewrite the comparison functions (in sort and in the foreach) to work on ranges
)
.array
.sort!((a,b) => a[1] < b[1])
.groupBy
.map!(group => group.map!(tuple => tuple[0]));
}
auto getRoutesGroupedByPattern() { return getRoutesGroupedByPatternImpl(routes); }

private static StaticRoute[routeCount] computeStaticRoutes()
{
Expand Down Expand Up @@ -566,6 +545,28 @@ private bool extractPathParts(ref PathPart[] parts, string pattern)
return has_placeholders;
}

private static auto getRoutesGroupedByPatternImpl(scope Route[] routes)
{
import std.algorithm : map, sort, filter, any;
import std.array : array;
import std.typecons : tuple;
// since /foo/:bar and /foo/:baz are the same route, we first normalize
// the patterns (by replacing each param with just ':'). after that, we
// sort and groupBy, in order to group related routes
return routes[]
.dup // just to silence scope warnings later in the chain
.map!(route => tuple(route,
route.fullPathParts
.map!(part => part.isParameter ? ":" : part.text)
.array) // can probably remove the array here if we rewrite the comparison functions (in sort and in the foreach) to work on ranges
)
.array
.sort!((a,b) => a[1] < b[1])
.groupBy
.map!(group => group.map!(tuple => tuple[0]));
}


unittest {
interface IDUMMY { void test(int dummy); }
class DUMMY : IDUMMY { void test(int) {} }
Expand Down

0 comments on commit df00591

Please sign in to comment.