Skip to content

Commit d4b84e4

Browse files
committed
with operator for routes fix #345
1 parent 62a8d42 commit d4b84e4

8 files changed

Lines changed: 319 additions & 218 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class Issue345 extends ServerFeature {
7+
8+
{
9+
get("/a", req -> req.route().attributes());
10+
11+
with(() -> {
12+
13+
get("/b", req -> req.route().attributes());
14+
15+
get("/c", req -> req.route().attributes());
16+
17+
}).attr("foo", "bar");
18+
19+
get("/d", req -> req.route().attributes());
20+
}
21+
22+
@Test
23+
public void withOperator() throws Exception {
24+
request().get("/a")
25+
.expect("{}");
26+
request().get("/b")
27+
.expect("{foo=bar}");
28+
request().get("/c")
29+
.expect("{foo=bar}");
30+
request().get("/d")
31+
.expect("{}");
32+
}
33+
34+
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
import com.typesafe.config.ConfigValue;
144144
import com.typesafe.config.ConfigValueFactory;
145145

146+
import javaslang.Predicates;
146147
import javaslang.control.Try;
147148

148149
/**
@@ -3124,6 +3125,20 @@ public Route.Definition sse(final String path, final Sse.Handler1 handler) {
31243125
return appendDefinition(new Route.Definition("GET", path, handler)).consumes(MediaType.sse);
31253126
}
31263127

3128+
@Override
3129+
public Route.Collection with(final Runnable callback) {
3130+
// hacky way of doing what we want... but we do simplify developer life
3131+
int size = this.bag.size();
3132+
callback.run();
3133+
// collect latest routes and apply collection attr
3134+
List<Route.Definition> local = this.bag.stream()
3135+
.skip(size)
3136+
.filter(Predicates.instanceOf(Route.Definition.class))
3137+
.map(r -> (Route.Definition) r)
3138+
.collect(Collectors.toList());
3139+
return new Route.Collection(local.toArray(new Route.Definition[local.size()]));
3140+
}
3141+
31273142
/**
31283143
* <h1>Bootstrap</h1>
31293144
* <p>

0 commit comments

Comments
 (0)