-
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Add Key::shortened_to_valid_length helper function
#1260
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
Open
sbernauer
wants to merge
11
commits into
main
Choose a base branch
from
feat/ensure-max-length-pub
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0d683e3
feat(v2): Make `ResourceNames::ensure_max_length` public
sbernauer b7f0f0b
changelog
sbernauer fb2914b
Merge remote-tracking branch 'origin/main' into feat/ensure-max-lengt…
sbernauer eca4b00
Rework according to feedback
sbernauer 9c5b35d
changelog
sbernauer f0a76b3
feat: Also handle UTF-8
sbernauer b2d9da3
changelog
sbernauer a9cbd52
fix: Don't shorten prefix; Make prefix optional
sbernauer 0c8b4fc
Rename fn to ensure_max_string_length
sbernauer 32fb08e
Update docs
sbernauer 03d8966
fix: Remove failing test for has length, which is now checked
sbernauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
crates/stackable-operator/src/utils/length_enforcement.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| use sha2::{Digest, Sha256}; | ||
|
|
||
| /// Ensures that the given input does not exceed the given maximum length. | ||
| /// If required, the input is truncated and a hex encoded hash is appended with a dash. | ||
| /// | ||
| /// It is recommended to only use ASCII characters, but this function also handles UTF-8: Multi-byte | ||
| /// characters are never split up, so the result can be shorter than the maximum length. | ||
| /// | ||
| /// If the truncation does not leave any character then only the hash is returned. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if `max_length_bytes < 1 /* character */ + 1 /* dash */ + hash_length`. | ||
| pub fn ensure_max_length( | ||
|
siegfriedweber marked this conversation as resolved.
Outdated
|
||
| original: impl Into<String>, | ||
| max_length_bytes: usize, | ||
| hash_length: usize, | ||
| ) -> String { | ||
| assert!(max_length_bytes >= 1 /* character */ + 1 /* dash */ + hash_length); | ||
|
|
||
| let original = original.into(); | ||
| if original.len() <= max_length_bytes { | ||
| return original; | ||
| } | ||
| if hash_length == 0 { | ||
| return truncate_at_char_boundary(original, max_length_bytes); | ||
| } | ||
|
|
||
| let mut hash = format!("{:x}", Sha256::digest(original.as_bytes())); | ||
| hash.truncate(hash_length); | ||
|
|
||
| // The result is `<name>-<hash>`, so the name must not occupy the bytes which are reserved | ||
| // for the hash. | ||
| let mut name = truncate_at_char_boundary(original, max_length_bytes - hash_length); | ||
|
|
||
| // Remove one more character to make room for the dash. | ||
| let removed_char = name.pop(); | ||
|
|
||
| if name.is_empty() { | ||
| return hash; | ||
| } | ||
|
|
||
| // A dash at the end of the name is reused as the separator. If the removed character was a | ||
| // dash itself then both dashes belong to the name and are kept. | ||
| if !name.ends_with('-') || removed_char == Some('-') { | ||
| name.push('-'); | ||
| } | ||
|
|
||
| format!("{name}{hash}") | ||
| } | ||
|
|
||
| /// Truncates the given input to at most `max_length_bytes` bytes. | ||
| /// | ||
| /// The input is only truncated at a character boundary, so a multi-byte character is never split | ||
| /// up but dropped entirely. | ||
| fn truncate_at_char_boundary(mut input: String, max_length_bytes: usize) -> String { | ||
| input.truncate(input.floor_char_boundary(max_length_bytes)); | ||
| input | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_ensure_max_length() { | ||
| // empty resource name, no hash length | ||
| assert_eq!(String::new(), ensure_max_length(String::new(), 2, 0)); | ||
|
|
||
| // resource_name.len() <= max_length | ||
| assert_eq!( | ||
| "abcdef".to_owned(), | ||
| ensure_max_length("abcdef".to_owned(), 6, 4) | ||
| ); | ||
|
|
||
| // hash_length == 0 | ||
| assert_eq!( | ||
| "abcdef".to_owned(), | ||
| ensure_max_length("abcdefg".to_owned(), 6, 0) | ||
| ); | ||
|
|
||
| // hash appended with dash | ||
| assert_eq!( | ||
| "a-7d1a".to_owned(), | ||
| ensure_max_length("abcdefg".to_owned(), 6, 4) | ||
| ); | ||
|
|
||
| // hash appended without an extra dash | ||
| assert_eq!( | ||
| "ab-a1b1".to_owned(), | ||
| ensure_max_length("ab-defgh".to_owned(), 7, 4) | ||
| ); | ||
|
|
||
| // hash appended without an extra dash | ||
| // In this case, the result is one character shorter than the maximum length. | ||
| assert_eq!( | ||
| "a-3951".to_owned(), | ||
| ensure_max_length("a-cdefgh".to_owned(), 7, 4) | ||
| ); | ||
|
|
||
| // hash appended without an extra dash | ||
| // The two dashes in the given resource name are intentionally kept. | ||
| assert_eq!( | ||
| "a--f7a0".to_owned(), | ||
| ensure_max_length("a--defgh".to_owned(), 7, 4) | ||
| ); | ||
|
|
||
| // A hash_length longer than the produced hash string may not produce the desired result. | ||
| // Just use sensible values! | ||
| assert_eq!( | ||
| "aaaaaaaaa-d476ce01c3787bcab054a2cf48d6af6dd303a0eb549e21a74125132f79d90c36".to_owned(), | ||
| ensure_max_length("a".repeat(1011), 1010, 1000) | ||
| ); | ||
| } | ||
|
|
||
| /// The maximum length is measured in bytes, so multi-byte characters must not be split up by | ||
| /// the truncation. This can make the result shorter than the maximum length. | ||
| #[test] | ||
| fn test_ensure_max_length_with_multi_byte_characters() { | ||
| // The two byte characters fit exactly into the maximum length. | ||
| assert_eq!("äöü".to_owned(), ensure_max_length("äöü".to_owned(), 6, 4)); | ||
|
|
||
| // Truncating after 5 bytes would split up the "ü", so it is dropped entirely. | ||
| assert_eq!("äö".to_owned(), ensure_max_length("äöü".to_owned(), 5, 0)); | ||
|
|
||
| // The 5 bytes reserved for the name only fit "äö", of which the "ö" is then replaced by | ||
| // the dash, so the result is two bytes shorter than the maximum length. | ||
| assert_eq!( | ||
| "ä-e109".to_owned(), | ||
| ensure_max_length("äöüäöü".to_owned(), 9, 4) | ||
| ); | ||
|
|
||
| // hash appended with dash, three byte characters | ||
| assert_eq!( | ||
| "日-9efa".to_owned(), | ||
| ensure_max_length("日本語日本語".to_owned(), 10, 4) | ||
| ); | ||
|
|
||
| // hash appended with dash, four byte characters | ||
| assert_eq!( | ||
| "🚀-a13c".to_owned(), | ||
| ensure_max_length("🚀🚀🚀🚀".to_owned(), 13, 4) | ||
| ); | ||
|
|
||
| // The trailing dash of the truncated name is replaced by the dash which separates the | ||
| // hash. | ||
| assert_eq!( | ||
| "aä-f726".to_owned(), | ||
| ensure_max_length("aä-öüb".to_owned(), 8, 4) | ||
| ); | ||
|
|
||
| // The truncated name is "aä-ö", so the "ö" is dropped and the existing dash is reused. | ||
| assert_eq!( | ||
| "aä-ae0c".to_owned(), | ||
| ensure_max_length("aä-öüäöü".to_owned(), 10, 4) | ||
| ); | ||
|
|
||
| // The truncation does not leave any character, so only the hash is returned. | ||
| assert_eq!( | ||
| "d24d".to_owned(), | ||
| ensure_max_length("🚀🚀🚀".to_owned(), 6, 4) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.