-
Notifications
You must be signed in to change notification settings - Fork 44
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
Draft: TxtProperties: new method to get a HashMap #303
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your PR, very appreciated! :)
Some changes I would make, let me know if they are feasible in your opinion
src/service_info.rs
Outdated
@@ -499,6 +499,15 @@ impl TxtProperties { | |||
pub fn get_property_val_str(&self, key: &str) -> Option<&str> { | |||
self.get(key).map(|x| x.val_str()) | |||
} | |||
|
|||
/// Returns a map of properties, where the key is the property key. | |||
pub fn get_property_map_str(&self) -> HashMap<String, String> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we consume the TxtProperties
? Something like:
/// Returns a map of properties, where the key is the property key.
///
/// **This method consumes [`TxtProperties`].**
pub fn into_hashmap(self) -> HashMap<String, String>
One can always obtain an HasMap<&str, &str>
using the current methods provided by TxtProperties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2 commits. The 2nd commit already consumes the TxtProperties
.
src/service_info.rs
Outdated
/// Returns a map of properties, where the key is the property key. | ||
pub fn get_property_map_str(&self) -> HashMap<String, String> { | ||
let mut map = HashMap::new(); | ||
for prop in self.properties.iter() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we consider the comment above, I would use into_iter()
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, it's in the 2nd commit ;-)
src/service_info.rs
Outdated
pub fn get_property_map_str(&self) -> HashMap<String, String> { | ||
let mut map = HashMap::new(); | ||
for prop in self.properties.iter() { | ||
map.insert(prop.key.clone(), prop.val_str().to_string()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we consume, we can use prop
directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, it's in the 2nd commit.
src/service_info.rs
Outdated
( | ||
p.key, | ||
p.val | ||
.map_or_else(String::new, |v| String::from_utf8(v).unwrap_or_default()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would filter out empty String
or wrong ones. What do you think? In this way the HashMap
will have only the correct ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! Will update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last changes and it is fine for me
self.properties | ||
.into_iter() | ||
.filter_map(|p| { | ||
// Skip the property if the value is non-empty and not valid UTF-8. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment seems wrong. We should skip if the value is empty and not valid UTF-8
// Skip the property if the value is non-empty and not valid UTF-8. | ||
let value = p | ||
.val | ||
.map_or(Some(String::new()), |v| String::from_utf8(v).ok())?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use is_some_and instead of map_or
, in this way we do not consider empty String
s
This is to follow up on issue #300 .