-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Look up path parameters by name #20
Comments
Yeah there is no such method because the Here is a utility method one could use to get a parameter by name: static Optional<CharSequence> paramValueByName(final Router.Result<?> result, final String paramName) {
for (int i = 0; i < result.params(); i++) {
if (result.paramName(i).equals(paramName)) {
return Optional.of(result.paramValue(i));
}
}
return Optional.empty();
} Use it like this: final Optional<CharSequence> user = paramValueByName(result, "user"); Arguably we could add this utility method to |
Yeah i'm less concerned with absolute maximal efficiency and more with the usage side. Considering routes tend to have relatively few route params I don't think you need a map. The linear scan approach is probably just fine. The reason I am on this is I just made this library https://github.com/bowbahdoe/jdk-httpserver-rutrouter void main() {
var router = RutRouter.builder()
.get("/hello/<name>", e -> {
e.getResponseHeaders().put("Content-Type", List.of("text/html"));
e.sendResponseHeaders(200, 0);
var c = RutRouter.result(e);
try (var body = e.getResponseBody()) {
body.write(("<h1>Hiya " + c.paramValueDecoded(0) + " " + c.query() + "</h1>").getBytes(StandardCharsets.UTF_8));
}
})
.notFoundHandler(e -> {
e.getResponseHeaders().put("Content-Type", List.of("text/html"));
e.sendResponseHeaders(404, 0);
try (var body = e.getResponseBody()) {
body.write("<h1>Not Found</h1>".getBytes(StandardCharsets.UTF_8));
}
})
.build();
var server = HttpServer.create(new InetSocketAddress(8783), 0);
server.createContext("/", router);
server.start();
} As you can see from the example, i'd prefer to use |
Yeah, linear scan is fine and more efficient than a map for most cases. Sure, we can add |
Okay opened #21 - we can bikeshed there |
Maybe i'm missing something, but i don't see an easy way to get the value for a path parameter by name instead of by index
The text was updated successfully, but these errors were encountered: