Skip to content

Conversation

@pmeredit
Copy link
Contributor

@pmeredit pmeredit commented Mar 6, 2024

No description provided.


Ok(())
}

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 tried to make this return an Iterator to avoid allocating Vecs, but I'm not sure how to handle errors internally without collecting to <Result<Vec<_>>.

Copy link
Contributor Author

@pmeredit pmeredit Mar 7, 2024

Choose a reason for hiding this comment

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

Ok, I found a solution, and a deeper understanding of impl return. I should have remembered it's existential not universal (for good reason).

Copy link
Contributor

Choose a reason for hiding this comment

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

Now that you've solved it, I think Result<Vec<&'a str>> would actually be better :) That'll eagerly validate the list of hosts rather than only erroring if the iteration happens to hit an invalid value, and the overhead of Vec might not even be more than the overhead of Box<dyn ..> depending on list length and Iterator dynamic dispatch size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is true for short lists. This is sort of optimizing for large lists of allowed hosts specified by users, which is probably pretty unlikely.

}

fn get_allowed_hosts<'a>(
mechanism_properties: &'a Option<Document>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Rust convention is to always accept options as concrete types, i.e. Option<&'a Document>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, thanks!


Ok(())
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Now that you've solved it, I think Result<Vec<&'a str>> would actually be better :) That'll eagerly validate the list of hosts rather than only erroring if the iteration happens to hit an invalid value, and the overhead of Vec might not even be more than the overhead of Box<dyn ..> depending on list length and Iterator dynamic dispatch size.

@pmeredit pmeredit requested a review from abr-egn March 7, 2024 17:20
Copy link
Contributor

@abr-egn abr-egn left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

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

impl looks good! I just have a few minor style suggestions, no need for re-review

// "127.0.0.1",
// "::1",
// ]
if has_mechanism_properties {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggest using and_then (flat map) here instead of using has_mechanism_properties and unwrapping:

if let Some(allowed_hosts) = credential
    .mechanism_properties
    .as_ref()
    .and_then(|p| p.get("ALLOWED_HOSTS"))
{
    // check for array
}

.mechanism_properties
.as_ref()
.map_or(false, |p| p.contains_key("PROVIDER_NAME"));
let has_mechanism_properties = credential.mechanism_properties.is_some();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this change can be reverted with the suggestion below -- it's nice to avoid unwraps (even if they're safe) when map works instead

if mechanism_properties.is_none() {
return Ok(Vec::from(DEFAULT_ALLOWED_HOSTS));
}
if let Some(allowed_hosts) = mechanism_properties.as_ref().unwrap().get("ALLOWED_HOSTS") {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: suggest flat mapping here as well, i.e.

if let Some(allowed_hosts) =
    mechanism_properties.and_then(|p| p.get_array("ALLOWED_HOSTS").ok()) {
    // iterate
} else {
    // default
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow, I totally missed get_array, that is so nice

@pmeredit pmeredit merged commit bf178cc into mongodb:main Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants