-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add update method to register endpoint #61
Conversation
src/handlers/register.rs
Outdated
let mut tags = registration.tags; | ||
|
||
if let Some(remove_tags) = remove_tags { | ||
for r_tag in remove_tags.iter() { | ||
if let Some(index) = tags.iter().position(|tag| tag.eq(r_tag)) { | ||
tags.remove(index); | ||
} | ||
} | ||
} | ||
|
||
if let Some(append_tags) = append_tags { | ||
for a_tag in append_tags.iter() { | ||
if !tags.contains(a_tag) { | ||
tags.push(a_tag.clone()); | ||
} | ||
} | ||
} |
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.
Would a HashSet be better?
let mut tags = registration.tags; | |
if let Some(remove_tags) = remove_tags { | |
for r_tag in remove_tags.iter() { | |
if let Some(index) = tags.iter().position(|tag| tag.eq(r_tag)) { | |
tags.remove(index); | |
} | |
} | |
} | |
if let Some(append_tags) = append_tags { | |
for a_tag in append_tags.iter() { | |
if !tags.contains(a_tag) { | |
tags.push(a_tag.clone()); | |
} | |
} | |
} | |
let new_tags = registration.tags.into_iter().collect::<HashSet<_>>() | |
.difference(&remove_tags.unwrap_or_else(|| HashSet::new())).collect::<HashSet<_>>() | |
.union(&append_tags.unwrap_or_else(|| HashSet::new())).collect::<HashSet<_>>() | |
.into_iter().collect::<Vec<_>>(); |
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.
This one doesn't compile. A form that actually compiles is this:
let tags = registration
.tags
.into_iter()
.collect::<HashSet<_>>()
.difference(&remove_tags.unwrap_or_else(|| HashSet::new()))
.map(|t| t.clone())
.collect::<HashSet<_>>()
.union(&append_tags.unwrap_or_else(|| HashSet::new()))
.map(|t| t.clone())
.collect::<HashSet<_>>()
.into_iter()
.collect::<Vec<_>>();
This is really verging on write-only code, and I'm not sure there are a lot of computational advantages to this form (especially given the expected size of the collections).
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.
My concern was more over readability/guaranteed set behavior than performance. Thought set operations would be easier to understand and easier to make sure the correct behavior is implemented.
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 think this is a slightly cleaned up version that compiles:
let tags = registration
.tags
.into_iter()
.collect::<HashSet<_>>()
.difference(&remove_tags.unwrap_or_else(|| HashSet::new()))
.cloned()
.collect::<HashSet<_>>()
.union(&append_tags.unwrap_or_else(|| HashSet::new()))
.cloned()
.collect::<Vec<_>>();
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.
Up to you. Just a suggestion
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.
Sure, and I understand that in some way it's more elegant and could be more efficient on bigger collections, but I don't think we'll ever get anything much bigger than 10 items, so I'd rather go with legibility 😀
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.
Again, performance wasn't my concern haha. Set operations are more legible IMO than loops and if statements, and they automatically avoid any edge cases we haven't thought of
src/handlers/register.rs
Outdated
for tag in remove_tags.iter() { | ||
if append_tags.contains(tag) { | ||
return Err(Error::InvalidUpdateRequest); | ||
} | ||
} |
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.
for tag in remove_tags.iter() { | |
if append_tags.contains(tag) { | |
return Err(Error::InvalidUpdateRequest); | |
} | |
} | |
if remove_tags.intersect(&append_tags).count() > 0 { | |
return Err(Error::InvalidUpdateRequest); | |
} |
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.
Found a middle ground 😊
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 think the necessity to collect the iterators between each step feels kinda stupid tho, bad language design 😊
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.
Agree, what you did is much nicer!
d4b23e6
to
5721338
Compare
Description
Implement the update functionality in the
register
endpointResolves #60
How Has This Been Tested?
Unit tests
Due Diligence