1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-11 09:49:29 +00:00

Add "name" attribute to route macro (#1934)

This commit is contained in:
Richard Chien 2021-03-04 20:38:47 +08:00 committed by GitHub
parent 14b249b804
commit fc6f974617
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View file

@ -2,8 +2,10 @@
## Unreleased - 2021-xx-xx
* Preserve doc comments when using route macros. [#2022]
* Add `name` attribute to `route` macro. [#1934]
[#2022]: https://github.com/actix/actix-web/pull/2022
[#1934]: https://github.com/actix/actix-web/pull/1934
## 0.5.0-beta.1 - 2021-02-10

View file

@ -71,6 +71,7 @@ mod route;
///
/// # Attributes
/// - `"path"` - Raw literal string with path for which to register handler.
/// - `name="resource_name"` - Specifies resource name for the handler. If not set, the function name of handler is used.
/// - `method="HTTP_METHOD"` - Registers HTTP method to provide guard for. Upper-case string, "GET", "POST" for example.
/// - `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`
/// - `wrap="Middleware"` - Registers a resource middleware.
@ -116,6 +117,7 @@ Creates route handler with `actix_web::guard::", stringify!($variant), "`.
# Attributes
- `"path"` - Raw literal string with path for which to register handler.
- `name="resource_name"` - Specifies resource name for the handler. If not set, the function name of handler is used.
- `guard="function_name"` - Registers function as guard using `actix_web::guard::fn_guard`.
- `wrap="Middleware"` - Registers a resource middleware.

View file

@ -78,6 +78,7 @@ impl TryFrom<&syn::LitStr> for MethodType {
struct Args {
path: syn::LitStr,
resource_name: Option<syn::LitStr>,
guards: Vec<Ident>,
wrappers: Vec<syn::Type>,
methods: HashSet<MethodType>,
@ -86,6 +87,7 @@ struct Args {
impl Args {
fn new(args: AttributeArgs, method: Option<MethodType>) -> syn::Result<Self> {
let mut path = None;
let mut resource_name = None;
let mut guards = Vec::new();
let mut wrappers = Vec::new();
let mut methods = HashSet::new();
@ -109,7 +111,16 @@ impl Args {
}
},
NestedMeta::Meta(syn::Meta::NameValue(nv)) => {
if nv.path.is_ident("guard") {
if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = nv.lit {
resource_name = Some(lit);
} else {
return Err(syn::Error::new_spanned(
nv.lit,
"Attribute name expects literal string!",
));
}
} else if nv.path.is_ident("guard") {
if let syn::Lit::Str(lit) = nv.lit {
guards.push(Ident::new(&lit.value(), Span::call_site()));
} else {
@ -164,6 +175,7 @@ impl Args {
}
Ok(Args {
path: path.unwrap(),
resource_name,
guards,
wrappers,
methods,
@ -276,6 +288,7 @@ impl ToTokens for Route {
args:
Args {
path,
resource_name,
guards,
wrappers,
methods,
@ -283,7 +296,9 @@ impl ToTokens for Route {
resource_type,
doc_attributes,
} = self;
let resource_name = name.to_string();
let resource_name = resource_name
.as_ref()
.map_or_else(|| name.to_string(), |n| n.value());
let method_guards = {
let mut others = methods.iter();
// unwrapping since length is checked to be at least one

View file

@ -83,6 +83,13 @@ async fn route_test() -> impl Responder {
HttpResponse::Ok()
}
#[get("/custom_resource_name", name = "custom")]
async fn custom_resource_name_test<'a>(req: actix_web::HttpRequest) -> impl Responder {
assert!(req.url_for_static("custom").is_ok());
assert!(req.url_for_static("custom_resource_name_test").is_err());
HttpResponse::Ok()
}
pub struct ChangeStatusCode;
impl<S, B> Transform<S, ServiceRequest> for ChangeStatusCode
@ -174,6 +181,7 @@ async fn test_body() {
.service(patch_test)
.service(test_handler)
.service(route_test)
.service(custom_resource_name_test)
});
let request = srv.request(http::Method::GET, srv.url("/test"));
let response = request.send().await.unwrap();
@ -228,6 +236,10 @@ async fn test_body() {
let request = srv.request(http::Method::PATCH, srv.url("/multi"));
let response = request.send().await.unwrap();
assert!(!response.status().is_success());
let request = srv.request(http::Method::GET, srv.url("/custom_resource_name"));
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}
#[actix_rt::test]