Skip to content

Commit

Permalink
refactor: Change Deref target of KeyPrefix and KeyName to str (#725)
Browse files Browse the repository at this point in the history
* Change Deref target of KeyPrefix and KeyName to str

* Update changelog

* Update CHANGELOG.md

Co-authored-by: Sebastian Bernauer <sebastian.bernauer@stackable.de>

* Add KeyPrefix and KeyName deref tests

---------

Co-authored-by: Sebastian Bernauer <sebastian.bernauer@stackable.de>
  • Loading branch information
Techassi and sbernauer authored Jan 24, 2024
1 parent 7774823 commit 487f608
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ All notable changes to this project will be documented in this file.

[#724]: https://github.com/stackabletech/operator-rs/pull/724

### Changed

- Change Deref target of `KeyPrefix` and `KeyName` from `String` to `str` ([#725]).

[#725]: https://github.com/stackabletech/operator-rs/pull/725

## [0.62.0] - 2024-01-19

### Added
Expand Down
39 changes: 34 additions & 5 deletions src/kvp/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl FromStr for KeyPrefix {
}

impl Deref for KeyPrefix {
type Target = String;
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -247,7 +247,7 @@ where
T: AsRef<str>,
{
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
self.deref() == other.as_ref()
}
}

Expand Down Expand Up @@ -311,7 +311,7 @@ impl FromStr for KeyName {
}

impl Deref for KeyName {
type Target = String;
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -329,15 +329,18 @@ where
T: AsRef<str>,
{
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
self.deref() == other.as_ref()
}
}

#[cfg(test)]
mod test {
use super::*;
use rstest::rstest;

use crate::kvp::Label;

use super::*;

#[test]
fn key_with_prefix() {
let key = Key::from_str("stackable.tech/vendor").unwrap();
Expand Down Expand Up @@ -399,4 +402,30 @@ mod test {
let err = KeyName::from_str(&input).unwrap_err();
assert_eq!(err, error);
}

#[rstest]
#[case("app.kubernetes.io/name", true)]
#[case("name", false)]
fn key_prefix_deref(#[case] key: &str, #[case] expected: bool) {
let label = Label::try_from((key, "zookeeper")).unwrap();

let is_valid = label
.key()
.prefix()
.is_some_and(|prefix| *prefix == "app.kubernetes.io");

assert_eq!(is_valid, expected)
}

#[rstest]
#[case("app.kubernetes.io/name", true)]
#[case("app.kubernetes.io/foo", false)]
#[case("name", true)]
#[case("foo", false)]
fn key_name_deref(#[case] key: &str, #[case] expected: bool) {
let label = Label::try_from((key, "zookeeper")).unwrap();
let is_valid = *label.key().name() == "name";

assert_eq!(is_valid, expected);
}
}

0 comments on commit 487f608

Please sign in to comment.