Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

feat: add update method to register endpoint #61

Merged
merged 2 commits into from
Jul 13, 2023

Conversation

xav
Copy link
Contributor

@xav xav commented Jul 12, 2023

Description

Implement the update functionality in the register endpoint
Resolves #60

How Has This Been Tested?

Unit tests

Due Diligence

  • Breaking change
  • Requires a documentation update
  • Requires a e2e/integration test update

@xav xav requested review from arein, heilhead and devceline July 12, 2023 12:59
@xav xav linked an issue Jul 12, 2023 that may be closed by this pull request
src/handlers/register.rs Show resolved Hide resolved
Comment on lines 103 to 119
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());
}
}
}
Copy link
Member

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?

Suggested change
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<_>>();

Copy link
Contributor Author

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).

Copy link
Member

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.

Copy link
Member

@chris13524 chris13524 Jul 12, 2023

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<_>>();

Copy link
Member

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

Copy link
Contributor Author

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 😀

Copy link
Member

@chris13524 chris13524 Jul 12, 2023

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

Comment on lines 91 to 95
for tag in remove_tags.iter() {
if append_tags.contains(tag) {
return Err(Error::InvalidUpdateRequest);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a middle ground 😊

Copy link
Contributor Author

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 😊

Copy link
Member

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!

@xav xav force-pushed the 60-add-put-handler-for-register branch from d4b23e6 to 5721338 Compare July 13, 2023 05:35
@xav xav added this pull request to the merge queue Jul 13, 2023
Merged via the queue into main with commit ac132bc Jul 13, 2023
@xav xav deleted the 60-add-put-handler-for-register branch July 13, 2023 05:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add modification functionality to POST handler for register
2 participants