Add `no_cache` for `cache_control` attribute #1051

This commit is contained in:
Sunli 2022-09-02 15:09:44 +08:00
parent 4d0451d675
commit 69d8c3e26e
5 changed files with 135 additions and 20 deletions

View File

@ -17,6 +17,7 @@ use crate::validators::Validators;
pub struct CacheControl { pub struct CacheControl {
public: bool, public: bool,
private: bool, private: bool,
pub no_cache: bool,
pub max_age: usize, pub max_age: usize,
} }
@ -25,6 +26,7 @@ impl Default for CacheControl {
Self { Self {
public: true, public: true,
private: false, private: false,
no_cache: false,
max_age: 0, max_age: 0,
} }
} }

View File

@ -189,7 +189,11 @@ pub fn generate(
}; };
let cache_control = { let cache_control = {
let public = method_args.cache_control.is_public(); let public = method_args.cache_control.is_public();
let max_age = method_args.cache_control.max_age; let max_age = if method_args.cache_control.no_cache {
-1
} else {
method_args.cache_control.max_age as i32
};
quote! { quote! {
#crate_name::CacheControl { #crate_name::CacheControl {
public: #public, public: #public,

View File

@ -335,7 +335,11 @@ pub fn generate(
}; };
let cache_control = { let cache_control = {
let public = method_args.cache_control.is_public(); let public = method_args.cache_control.is_public();
let max_age = method_args.cache_control.max_age; let max_age = if method_args.cache_control.no_cache {
-1
} else {
method_args.cache_control.max_age as i32
};
quote! { quote! {
#crate_name::CacheControl { #crate_name::CacheControl {
public: #public, public: #public,
@ -583,7 +587,11 @@ pub fn generate(
let cache_control = { let cache_control = {
let public = object_args.cache_control.is_public(); let public = object_args.cache_control.is_public();
let max_age = object_args.cache_control.max_age; let max_age = if object_args.cache_control.no_cache {
-1
} else {
object_args.cache_control.max_age as i32
};
quote! { quote! {
#crate_name::CacheControl { #crate_name::CacheControl {
public: #public, public: #public,

View File

@ -161,7 +161,11 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
let cache_control = { let cache_control = {
let public = field.cache_control.is_public(); let public = field.cache_control.is_public();
let max_age = field.cache_control.max_age; let max_age = if field.cache_control.no_cache {
-1
} else {
field.cache_control.max_age as i32
};
quote! { quote! {
#crate_name::CacheControl { #crate_name::CacheControl {
public: #public, public: #public,
@ -275,7 +279,11 @@ pub fn generate(object_args: &args::SimpleObject) -> GeneratorResult<TokenStream
let cache_control = { let cache_control = {
let public = object_args.cache_control.is_public(); let public = object_args.cache_control.is_public();
let max_age = object_args.cache_control.max_age; let max_age = if object_args.cache_control.no_cache {
-1
} else {
object_args.cache_control.max_age as i32
};
quote! { quote! {
#crate_name::CacheControl { #crate_name::CacheControl {
public: #public, public: #public,

View File

@ -1,4 +1,4 @@
/// Cache control values /// Cache control value
/// ///
/// # Examples /// # Examples
/// ///
@ -18,6 +18,11 @@
/// async fn value2(&self) -> i32 { /// async fn value2(&self) -> i32 {
/// 0 /// 0
/// } /// }
///
/// #[graphql(cache_control(no_cache))]
/// async fn value3(&self) -> i32 {
/// 0
/// }
/// } /// }
/// ///
/// # tokio::runtime::Runtime::new().unwrap().block_on(async { /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
@ -34,6 +39,7 @@
/// max_age: 30 /// max_age: 30
/// } /// }
/// ); /// );
///
/// assert_eq!( /// assert_eq!(
/// schema /// schema
/// .execute("{ value2 }") /// .execute("{ value2 }")
@ -46,6 +52,7 @@
/// max_age: 60 /// max_age: 60
/// } /// }
/// ); /// );
///
/// assert_eq!( /// assert_eq!(
/// schema /// schema
/// .execute("{ value1 value2 }") /// .execute("{ value1 value2 }")
@ -58,6 +65,19 @@
/// max_age: 30 /// max_age: 30
/// } /// }
/// ); /// );
///
/// assert_eq!(
/// schema
/// .execute("{ value1 value2 value3 }")
/// .await
/// .into_result()
/// .unwrap()
/// .cache_control,
/// CacheControl {
/// public: false,
/// max_age: -1
/// }
/// );
/// # }); /// # });
/// ``` /// ```
#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
@ -65,8 +85,8 @@ pub struct CacheControl {
/// Scope is public, default is true. /// Scope is public, default is true.
pub public: bool, pub public: bool,
/// Cache max age, default is 0. /// Cache max age, `-1` represent `no-cache`, default is 0.
pub max_age: usize, pub max_age: i32,
} }
impl Default for CacheControl { impl Default for CacheControl {
@ -82,12 +102,23 @@ impl CacheControl {
/// Get 'Cache-Control' header value. /// Get 'Cache-Control' header value.
#[must_use] #[must_use]
pub fn value(&self) -> Option<String> { pub fn value(&self) -> Option<String> {
if self.max_age > 0 { let mut value = if self.max_age > 0 {
Some(format!( format!("max-age={}", self.max_age)
"max-age={}{}", } else if self.max_age == -1 {
self.max_age, "no-cache".to_string()
if self.public { "" } else { ", private" } } else {
)) String::new()
};
if !self.public {
if !value.is_empty() {
value += ", ";
}
value += "private";
}
if !value.is_empty() {
Some(value)
} else { } else {
None None
} }
@ -99,13 +130,75 @@ impl CacheControl {
pub(crate) fn merge(self, other: &CacheControl) -> CacheControl { pub(crate) fn merge(self, other: &CacheControl) -> CacheControl {
CacheControl { CacheControl {
public: self.public && other.public, public: self.public && other.public,
max_age: if self.max_age == 0 { max_age: match (self.max_age, other.max_age) {
other.max_age (-1, _) => -1,
} else if other.max_age == 0 { (_, -1) => -1,
self.max_age (a, 0) => a,
} else { (0, b) => b,
self.max_age.min(other.max_age) (a, b) => a.min(b),
}, },
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn to_value() {
assert_eq!(
CacheControl {
public: true,
max_age: 0,
}
.value(),
None
);
assert_eq!(
CacheControl {
public: false,
max_age: 0,
}
.value(),
Some("private".to_string())
);
assert_eq!(
CacheControl {
public: false,
max_age: 10,
}
.value(),
Some("max-age=10, private".to_string())
);
assert_eq!(
CacheControl {
public: true,
max_age: 10,
}
.value(),
Some("max-age=10".to_string())
);
assert_eq!(
CacheControl {
public: true,
max_age: -1,
}
.value(),
Some("no-cache".to_string())
);
assert_eq!(
CacheControl {
public: false,
max_age: -1,
}
.value(),
Some("no-cache, private".to_string())
);
}
}