-
Notifications
You must be signed in to change notification settings - Fork 325
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
Reduce the amount of boilerplate needed to declare partials in the routes #108
Comments
I think it's very likely that all named segments (or, wildcards) are used in the endpoint. So it would be nice if we provide a way to extract all of those at once, and access each of them via method call or indexing. async fn handler(segments: Segments) -> Response {
let number = segments.get::<usize>("number");
// ...
} We need to think how to propagate parse errors though. Maybe returning |
That would mean a possible situation like this could occur... async fn handler(segments: Segments) -> Response {
let number = segments.get::<usize>("num");
// handler only requires "num" which "/{foo}/{num}" satisfies
}
// ...
app.at("/{foo}/{num}").get(handler); ...and since no errors get thrown, this wouldn't get surfaced immediately. Maybe |
@petejodo I don't think we have to error on non-exhaustive use of segments. As of now it's totally okay to partially use named segments. However it would be great if we get a log message when some segments are not used! |
Writing What about the actix way of getting the args? use actix_web::{App, Path, Result, http};
/// extract path info from "/users/{userid}/{friend}" url
/// {userid} - - deserializes to a u32
/// {friend} - deserializes to a String
fn index(info: Path<(u32, String)>) -> Result<String> {
Ok(format!("Welcome {}! {}", info.1, info.0))
} |
I propose that this could be solved by submitting the name of the segment as additional information to the async fn display_number(nr: Named<i32>) -> String {
format!("Segment number: {}", nr.0)
}
async fn two_segments(from: Named<String>, to: Named<String>) {
format!("Message from {} to {}", &from, &to)M
}
fn main() {
let mut app = tide::App::new(());
app.at("/{num}").get_with(display_number, SegmentName("num".into()));
// Or extracting multiple segments at once by providing a tuple as second argument.
app.at("/msg/{from}/{to}").get_width(two_segments,
(SegmentName("from".into()), SegmentName("to".into())));
app.serve();
} Since this relies only on runtime values, it would not be unimaginable that the |
This is resolved in #156, which takes a radically different approach to extraction. |
When playing with Tide, I found that NamedPath is not friendly to use compare to Rocket.
It seems tedious when working with parameters if we don't using macro to make API easier to reason about.
Rocket has a convention the named segment's name are the handler's parameter name
I know there is an
id
and is an int. It is simple.https://github.com/rust-net-web/tide/pull/32/files
Most of time named path is simple type. Consider following case :
A restful url for a comment api
I have to duplicate and declare a type for every single named path.
By current design:
app.at("/posts/<num>/<num>
is not going to work properly(lastnum
wins)Maybe we need to give a NamedPath a name from routing rule but not a type's name. A probably solution might be using const generics to give a type a
name
but const generics is not implemented.
Another solution might be using macros, but according to this post: https://rust-lang-nursery.github.io/wg-net/2018/10/16/tide-routing.html
Any more ideas to make bridge route rule to endpoint declaration easier?
The text was updated successfully, but these errors were encountered: