Skip to content
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

Adding app_data to ServiceConfig #1758

Merged
merged 9 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Added
* Implement `exclude_regex` for Logger middleware. [#1723]
* Add request-local data extractor `web::ReqData`. [#1748]
* Add `app_data` to `ServiceConfig`. [#1757]

### Changed
* Print non-configured `Data<T>` type when attempting extraction. [#1743]
Expand Down
44 changes: 44 additions & 0 deletions actix-http/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ impl Extensions {
pub fn clear(&mut self) {
self.map.clear();
}

/// Extends self with the items from another `Extensions`.
pub fn extend(&mut self, other: Extensions) {
self.map.extend(other.map);
}

/// Returns `true` if no extension is registered
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
}

impl fmt::Debug for Extensions {
Expand Down Expand Up @@ -88,19 +98,23 @@ mod tests {
fn test_clear() {
let mut map = Extensions::new();

assert!(map.is_empty());

map.insert::<i8>(8);
map.insert::<i16>(16);
map.insert::<i32>(32);

assert!(map.contains::<i8>());
assert!(map.contains::<i16>());
assert!(map.contains::<i32>());
assert!(!map.is_empty());

map.clear();

assert!(!map.contains::<i8>());
assert!(!map.contains::<i16>());
assert!(!map.contains::<i32>());
assert!(map.is_empty());

map.insert::<i8>(10);
assert_eq!(*map.get::<i8>().unwrap(), 10);
Expand Down Expand Up @@ -178,4 +192,34 @@ mod tests {
assert_eq!(extensions.get::<bool>(), None);
assert_eq!(extensions.get(), Some(&MyType(10)));
}

#[test]
fn test_extend() {
#[derive(Debug, PartialEq)]
struct MyType(i32);

let mut extensions = Extensions::new();

extensions.insert(5i32);
extensions.insert(MyType(10));

let mut other = Extensions::new();

other.insert(15i32);
other.insert(20u8);

extensions.extend(other);

assert_eq!(extensions.get(), Some(&15i32));
assert_eq!(extensions.get_mut(), Some(&mut 15i32));

assert_eq!(extensions.remove::<i32>(), Some(15i32));
assert!(extensions.get::<i32>().is_none());

assert_eq!(extensions.get::<bool>(), None);
assert_eq!(extensions.get(), Some(&MyType(10)));

assert_eq!(extensions.get(), Some(&20u8));
assert_eq!(extensions.get_mut(), Some(&mut 20u8));
}
}
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ where
self.data.extend(cfg.data);
self.services.extend(cfg.services);
self.external.extend(cfg.external);
self.extensions.extend(cfg.extensions);
self
}

Expand Down
23 changes: 18 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub struct ServiceConfig {
pub(crate) services: Vec<Box<dyn AppServiceFactory>>,
pub(crate) data: Vec<Box<dyn DataFactory>>,
pub(crate) external: Vec<ResourceDef>,
pub(crate) extensions: Extensions,
}

impl ServiceConfig {
Expand All @@ -186,6 +187,7 @@ impl ServiceConfig {
services: Vec::new(),
data: Vec::new(),
external: Vec::new(),
extensions: Extensions::new(),
}
}

Expand All @@ -198,6 +200,14 @@ impl ServiceConfig {
self
}

/// Set arbitrary data item.
///
/// This is same as `App::data()` method.
pub fn app_data<U: 'static>(&mut self, ext: U) -> &mut Self {
self.extensions.insert(ext);
self
}

/// Configure route for a specific path.
///
/// This is same as `App::route()` method.
Expand Down Expand Up @@ -254,13 +264,16 @@ mod tests {
async fn test_data() {
let cfg = |cfg: &mut ServiceConfig| {
cfg.data(10usize);
cfg.app_data(15u8);
};

let mut srv =
init_service(App::new().configure(cfg).service(
web::resource("/").to(|_: web::Data<usize>| HttpResponse::Ok()),
))
.await;
let mut srv = init_service(App::new().configure(cfg).service(
web::resource("/").to(|_: web::Data<usize>, req: HttpRequest| {
assert_eq!(*req.app_data::<u8>().unwrap(), 15u8);
HttpResponse::Ok()
}),
))
.await;
let req = TestRequest::default().to_request();
let resp = srv.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
Expand Down
6 changes: 6 additions & 0 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ where

self.data = Some(data);
}

if !cfg.extensions.is_empty() {
let mut data = self.data.unwrap_or_else(Extensions::new);
data.extend(cfg.extensions);
self.data = Some(data);
}
augustocdias marked this conversation as resolved.
Show resolved Hide resolved
self
}

Expand Down