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

refactor: Change Deref target of KeyPrefix and KeyName to str #725

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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");
NickLarsenNZ marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}