Skip to content

Commit 7e41355

Browse files
committed
reverse route fix #344
1 parent d97ddec commit 7e41355

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

jooby/src/main/java/org/jooby/Route.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Optional;
32+
import java.util.Set;
3233
import java.util.stream.Collectors;
3334

3435
import org.jooby.internal.AssetProxy;
@@ -41,6 +42,8 @@
4142
import com.google.common.collect.ImmutableList;
4243
import com.google.common.collect.ImmutableMap;
4344
import com.google.common.collect.Lists;
45+
import com.google.inject.Key;
46+
import com.google.inject.TypeLiteral;
4447

4548
/**
4649
* Routes are a key concept in Jooby. Routes are executed in the same order they are defined
@@ -927,6 +930,26 @@ public List<String> vars() {
927930
return cpattern.vars();
928931
}
929932

933+
/**
934+
* Recreate a route path and apply the given variables.
935+
*
936+
* @param vars Path variables.
937+
* @return A route pattern.
938+
*/
939+
public String reverse(final Map<String, Object> vars) {
940+
return cpattern.reverse(vars);
941+
}
942+
943+
/**
944+
* Recreate a route path and apply the given variables.
945+
*
946+
* @param values Path variable values.
947+
* @return A route pattern.
948+
*/
949+
public String reverse(final Object... values) {
950+
return cpattern.reverse(values);
951+
}
952+
930953
/**
931954
* Set route attribute.
932955
*
@@ -1032,7 +1055,7 @@ public String name() {
10321055
*/
10331056
public Definition name(final String name) {
10341057
checkArgument(!Strings.isNullOrEmpty(name), "A route's name is required.");
1035-
this.name = RoutePattern.normalize(name);
1058+
this.name = normalize(name);
10361059
return this;
10371060
}
10381061

@@ -1705,7 +1728,8 @@ public void send(final Result result) throws Exception {
17051728
* The goal of the <code>complete</code> handler is to probably cleanup request object and log
17061729
* responses.
17071730
*
1708-
* Please note that the <code>complete</code> handler is just syntax sugar for {@link Route.Filter}.
1731+
* Please note that the <code>complete</code> handler is just syntax sugar for
1732+
* {@link Route.Filter}.
17091733
* For example, the <code>complete</code> handler was implemented as:
17101734
*
17111735
* <pre>{@code
@@ -1845,6 +1869,10 @@ default void next(final Request req, final Response rsp) throws Throwable {
18451869

18461870
}
18471871

1872+
/** Renderer key. */
1873+
Key<Set<Route.Definition>> KEY = Key.get(new TypeLiteral<Set<Route.Definition>>() {
1874+
});
1875+
18481876
String GET = "GET";
18491877

18501878
String POST = "POST";
@@ -1957,4 +1985,13 @@ default String attr(final String name) {
19571985
return attributes().get(name);
19581986
}
19591987

1988+
/**
1989+
* Normalize a path by removing double or trailing slashes.
1990+
*
1991+
* @param path A path to normalize.
1992+
* @return A normalized path.
1993+
*/
1994+
static String normalize(final String path) {
1995+
return RoutePattern.normalize(path);
1996+
}
19601997
}

jooby/src/test/java/org/jooby/RouteDefinitionTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import java.util.Collections;
77
import java.util.List;
88
import java.util.Optional;
9+
import java.util.function.Function;
910

1011
import org.jooby.Route.Definition;
1112
import org.jooby.internal.RouteImpl;
1213
import org.jooby.test.MockUnit;
1314
import org.junit.Test;
1415

16+
import com.google.common.collect.ImmutableMap;
17+
1518
public class RouteDefinitionTest {
1619

1720
@Test
@@ -281,4 +284,20 @@ public void properties() throws Exception {
281284
assertEquals(MediaType.json, def.consumes().get(0));
282285
assertEquals(MediaType.json, def.produces().get(0));
283286
}
287+
288+
@Test
289+
public void reverse() throws Exception {
290+
Function<String, Route.Definition> route = path -> new Route.Definition("*", path, () -> null);
291+
assertEquals("/1", route.apply("/:id").reverse(1));
292+
293+
assertEquals("/cat/1", route.apply("/:type/:id").reverse("cat", 1));
294+
295+
assertEquals("/cat/5", route.apply("/{type}/{id}").reverse("cat", 5));
296+
297+
assertEquals("/ccat/1",
298+
route.apply("/c{type}/{id}").reverse(ImmutableMap.of("type", "cat", "id", 1)));
299+
300+
assertEquals("/cat/tom", route.apply("/cat/tom").reverse("cat", 1));
301+
}
302+
284303
}
File renamed without changes.

0 commit comments

Comments
 (0)