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

fix: add missing preconditions and update samples #1753

Merged
merged 9 commits into from
Dec 16, 2022
Merged

Conversation

JesseLovelace
Copy link
Contributor

Adds BlobTargetOption.[meta]generationmatch(long) and updates the samples to use it

@JesseLovelace JesseLovelace requested a review from a team as a code owner November 1, 2022 20:11
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: storage Issues related to the googleapis/java-storage API. samples Issues that are directly related to samples. labels Nov 1, 2022
@JesseLovelace JesseLovelace changed the title (fix) Add missing preconditions and update samples fix: add missing preconditions and update samples Nov 1, 2022
@conventional-commit-lint-gcf
Copy link

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@gcf-owl-bot gcf-owl-bot bot requested a review from a team as a code owner November 1, 2022 20:14
Copy link
Collaborator

@BenWhitehead BenWhitehead left a comment

Choose a reason for hiding this comment

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

Thanks for adding this needed overload! One spelling that needs to be fixed.

Do we also want to add the corresponding [meta]generationNotMatch overloads as well?

// Storage.BlobTargetOption precondition = Storage.BlobTargetOption.generationMatch();
// precondition. This will cause the request to fail if the existing object's generation
// changes before the request runs.
if (storage.get(bucketName, targetObjectName) != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think one of these should be commented out as just an example rather than having the code overwrite the first precondition.

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 this makes more sense, because it will actually behave appropriately based on whether the object already exists or not. If it doesn't exist, the original precondition isn't overwritten

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think it would look better as

Storage.BlobTargetOption precondition;
if (storage.get(bucketName, objectName != null) {
    precondition = ...;
} else {
    precondition = ...;

?

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternative that avoids the duplicate storage.get call:

Storage.BlobTargetOption precondition = 
  Optional.ofNullable(storage.get(bucketName, targetObjectName))
    .map(Storage::getGeneration)
    .map(Storage.BlobTargetOption::generationMatch)
    .orElse(Storage.BlobTargetOption.doesNotExist())

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think @JesseLovelace suggestion here is the most readable. If we wanted to avoid the duplicate call could we could save the object retrieved.

@BenWhitehead BenWhitehead merged commit 96beca2 into main Dec 16, 2022
@BenWhitehead BenWhitehead deleted the preconfix branch December 16, 2022 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/java-storage API. samples Issues that are directly related to samples. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants