Skip to content
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

Add Api::get_opt for better existence handling #809

Merged
merged 3 commits into from
Feb 11, 2022

Conversation

nightkr
Copy link
Member

@nightkr nightkr commented Feb 7, 2022

Motivation

Fixes #806

Solution

Adds a new method Api::try_get, as described in #806 (called get_opt there).

I think an Entry API is still worthwhile, but that still requires this as groundwork.

Fixes kube-rs#806

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
@nightkr nightkr added the api Api abstraction related label Feb 7, 2022
@nightkr nightkr requested a review from clux February 7, 2022 09:53
@nightkr nightkr self-assigned this Feb 7, 2022
@codecov-commenter
Copy link

codecov-commenter commented Feb 7, 2022

Codecov Report

Merging #809 (f65228b) into master (f39c147) will increase coverage by 0.00%.
The diff coverage is 81.81%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #809   +/-   ##
=======================================
  Coverage   70.04%   70.05%           
=======================================
  Files          58       58           
  Lines        3973     3984   +11     
=======================================
+ Hits         2783     2791    +8     
- Misses       1190     1193    +3     
Impacted Files Coverage Δ
kube-client/src/api/core_methods.rs 84.44% <60.00%> (-3.06%) ⬇️
kube/src/lib.rs 88.37% <100.00%> (+0.56%) ⬆️
kube-runtime/src/wait.rs 68.00% <0.00%> (-2.00%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f39c147...f65228b. Read the comment docs.

@nightkr nightkr requested a review from a team February 7, 2022 10:29
This was referenced Feb 7, 2022
Comment on lines 50 to 55
pub async fn try_get(&self, name: &str) -> Result<Option<K>> {
match self.get(name).await {
Ok(obj) => Ok(Some(obj)),
Err(Error::Api(ErrorResponse { reason, .. })) if &reason == "NotFound" => Ok(None),
Err(err) => Err(err),
}
Copy link
Member

@clux clux Feb 7, 2022

Choose a reason for hiding this comment

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

I think the logic and tests for this are all good. I am only on the fence with the name here as it's a bit clashing with the normal expectations of the try_ prefix. The other alternatives; get_optional and get_opt does feel a bit more awkward though.

I kind of feel like this method should be made into .get as that is more rust-standard, and maybe relegate the old getter to .get_raw or .get_unchecked, even if this is a breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

I agree that this is awkward, but if we change .get to special case 404, isn't that also awkward as an abstraction for HTTP GET?

I can't think of a good name either, but we might want to avoid try_ if possible.

Copy link
Member

Choose a reason for hiding this comment

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

How about get_or_none?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, upon reflection, it's probably not wise to change .get. A get_or_none works, although it kind of implies the existence with get_or which feels more like an Entry thing.

Maybe .get_ok? Result::ok converts from Result to Option, which is similar (but not quite the same), but perhaps signals an option here more than the result?

Copy link
Member Author

Choose a reason for hiding this comment

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

Personally, I don't like get_or_none or get_ok since they imply a mechanical transformation rather than what conditions we're separating out.

Choose a reason for hiding this comment

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

True, although those functions also return Options so the signature is at least consistent.

Copy link
Member Author

@nightkr nightkr Feb 10, 2022

Choose a reason for hiding this comment

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

As does slice::get. We're a relative outlier in not using Option consistently. They use x[i] for "asserting get", which we might technically be able to get away with too, but I think it would end up pretty awkward in terms of both usage and implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, to my Rust taste find feels like a perfect match for intended behavior

Copy link
Member

Choose a reason for hiding this comment

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

The signature for find technically matches, but behaviourally feels weird to me as well. It's a lookup on name, not a predicate search. Would personally prefer to stay within the conventions of db/hash table lookups with some variant of .get* (this also makes it appear right next to .get in docs.rs - which i think also help alleviate confusions).

Out of all of these suggestions, and given there are legit concerns here with every other suggestion (mismatching expectations / long names / convention breaks), maybe we should reconsider pivoting back onto get_opt and fix what it means more with documentation. E.g. add a line to the original get to cross-link:

/// NB: this returns an error type when the named object does note exist, if you want to catch this error more easily, consider [`Api::get_opt`].

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, renamed it to get_opt and added a docs note to get

@clux clux added this to the 0.69.0 milestone Feb 7, 2022
@nightkr nightkr added the changelog-add changelog added category for prs label Feb 8, 2022
nightkr and others added 2 commits February 11, 2022 09:32
In accordance with feedback in the PR.

Signed-off-by: Teo Klestrup Röijezon <teo@nullable.se>
@nightkr nightkr requested review from clux and kazk February 11, 2022 09:56
@nightkr nightkr merged commit 6f42b80 into kube-rs:master Feb 11, 2022
@clux clux changed the title Implement Api::try_get Add Api::get_opt for better existence handling Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Api abstraction related changelog-add changelog added category for prs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional Api::get
6 participants