Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import dev.voidframework.core.constant.CharConstants;
import dev.voidframework.core.constant.StringConstants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A single route URL.
*
Expand All @@ -11,6 +14,19 @@
*/
public record RouteURL(String url) {

private static final Pattern PATTERN_EXTRACT_SIMPLIFIED_PATH_VARIABLES = Pattern.compile("\\{([a-zA-Z][a-zA-Z0-9]+)}");

/**
* Build a new instance.
*
* @param url The route URL
* @since 1.13.0
*/
public RouteURL {

url = replaceSimplifiedVariable(url);
}

/**
* Creates a new Route URL.
*
Expand Down Expand Up @@ -104,6 +120,23 @@ private static String cleanRoutePath(final String routePath) {
return cleanedRoutePath;
}

/**
* Replace all simplified variable "{varname}" with a regular expression.
*
* @param url URL to transform
* @return URL with all simplified variables replaced
* @since 1.13.0
*/
private String replaceSimplifiedVariable(final String url) {

final Matcher simplifiedPathVarMatcher = PATTERN_EXTRACT_SIMPLIFIED_PATH_VARIABLES.matcher(url);
if (simplifiedPathVarMatcher.find()) {
return simplifiedPathVarMatcher.replaceAll("(?<$1>(.*))"); // "number example input 6"
}

return url;
}

@Override
public String toString() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ void resolveRouteWithRegularExpression() {
Assertions.assertEquals("toto", resolvedRoute.extractedParameterValues().get("accountId"));
}

@Test
void resolveRouteWithSimplifiedVariable() {

// Arrange
final Router router = new DefaultRouter();
final Method methodAccount = ReflectionUtils.resolveMethod("displayAccount", SampleController.class);
router.addRoute(HttpMethod.GET, RouteURL.of("/register/{accountId}"), SampleController.class, methodAccount);

// Act
final ResolvedRoute resolvedRoute = router.resolveRoute(HttpMethod.GET, "/register/toto");

// Assert
Assertions.assertNotNull(resolvedRoute);
Assertions.assertEquals(SampleController.class, resolvedRoute.controllerClassType());
Assertions.assertEquals("displayAccount", resolvedRoute.method().getName());
Assertions.assertNotNull(resolvedRoute.extractedParameterValues());
Assertions.assertEquals(1, resolvedRoute.extractedParameterValues().size());
Assertions.assertTrue(resolvedRoute.extractedParameterValues().containsKey("accountId"));
Assertions.assertEquals("toto", resolvedRoute.extractedParameterValues().get("accountId"));
}

@Test
void reverseUrlWithName() {

Expand Down