Skip to content

Commit

Permalink
Sealing the SelectorExt trait
Browse files Browse the repository at this point in the history
- fmt fixes

Signed-off-by: Danil-Grigorev <[email protected]>
  • Loading branch information
Danil-Grigorev committed Jul 19, 2024
1 parent 5c52593 commit 3f83904
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
20 changes: 15 additions & 5 deletions kube-core/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ pub struct ParseExpressionError(pub String);
// local type aliases
type Expressions = Vec<Expression>;

mod private {
pub trait Sealed {}
impl<R: super::ResourceExt> Sealed for R {}
}

/// Extensions to [`ResourceExt`](crate::ResourceExt)
/// Helper methods for resource selection based on provided Selector
pub trait SelectorExt {
pub trait SelectorExt: private::Sealed {
fn selector_map(&self) -> &BTreeMap<String, String>;

/// Perform a match on the resource using Matcher trait
Expand Down Expand Up @@ -272,7 +277,7 @@ impl FromIterator<(&'static str, &'static str)> for Selector {
/// ```
/// use kube_core::{Selector, Expression};
///
/// let sel: Selector = Some(("foo", "bar")).into_iter().collect();
/// let sel: Selector = Some("foo", "bar")).into_iter().collect();
/// let equal: Selector = Expression::Equal("foo".into(), "bar".into()).into();
/// assert_eq!(sel, equal)
/// ```
Expand Down Expand Up @@ -323,11 +328,15 @@ impl TryFrom<LabelSelectorRequirement> for Expression {
match requirement.operator.as_str() {
"In" => match values {
Some(values) => Ok(Expression::In(key, values)),
None => Err(ParseExpressionError("Expected values for In operator, got none".into())),
None => Err(ParseExpressionError(
"Expected values for In operator, got none".into(),

Check warning on line 332 in kube-core/src/labels.rs

View check run for this annotation

Codecov / codecov/patch

kube-core/src/labels.rs#L331-L332

Added lines #L331 - L332 were not covered by tests
)),
},
"NotIn" => match values {
Some(values) => Ok(Expression::NotIn(key, values)),
None => Err(ParseExpressionError("Expected values for In operator, got none".into())),
None => Err(ParseExpressionError(
"Expected values for In operator, got none".into(),

Check warning on line 338 in kube-core/src/labels.rs

View check run for this annotation

Codecov / codecov/patch

kube-core/src/labels.rs#L337-L338

Added lines #L337 - L338 were not covered by tests
)),
},
"Exists" => Ok(Expression::Exists(key)),
"DoesNotExist" => Ok(Expression::DoesNotExist(key)),
Expand Down Expand Up @@ -622,7 +631,8 @@ mod tests {
]),
match_labels: Some([("foo".into(), "bar".into())].into()),
}
.try_into().unwrap();
.try_into()
.unwrap();
assert!(selector.matches(&[("foo".into(), "bar".into())].into()));
assert!(!selector.matches(&Default::default()));
}
Expand Down
2 changes: 1 addition & 1 deletion kube-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub use resource::{
pub mod response;
pub use response::Status;

pub use labels::{Expression, Matcher, Selector, SelectorExt, ParseExpressionError};
pub use labels::{Expression, Matcher, ParseExpressionError, Selector, SelectorExt};

#[cfg_attr(docsrs, doc(cfg(feature = "schema")))]
#[cfg(feature = "schema")]
Expand Down
22 changes: 21 additions & 1 deletion kube-core/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,9 @@ where
}
#[cfg(test)]
mod test {
use super::{DeleteParams, PatchParams};
use crate::{params::WatchParams, Expression, Selector};

use super::{DeleteParams, ListParams, PatchParams};
#[test]
fn delete_param_serialize() {
let mut dp = DeleteParams::default();
Expand Down Expand Up @@ -919,6 +921,24 @@ mod test {
let urlstr = qp.finish();
assert_eq!(String::from("some/resource?&fieldValidation=Strict"), urlstr);
}

#[test]
fn list_params_serialize() {
let selector: Selector =
Expression::In("env".into(), ["development".into(), "sandbox".into()].into()).into();
let lp = ListParams::default().labels_from(&selector);
let labels = lp.label_selector.unwrap();
assert_eq!(labels, "env in (development,sandbox)");
}

#[test]
fn watch_params_serialize() {
let selector: Selector =
Expression::In("env".into(), ["development".into(), "sandbox".into()].into()).into();
let wp = WatchParams::default().labels_from(&selector);
let labels = wp.label_selector.unwrap();
assert_eq!(labels, "env in (development,sandbox)");
}
}

/// Preconditions must be fulfilled before an operation (update, delete, etc.) is carried out.
Expand Down

0 comments on commit 3f83904

Please sign in to comment.