-
Notifications
You must be signed in to change notification settings - Fork 77
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
Conversation
🤖 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 -- conventional-commit-lint bot |
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.
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?
google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java
Outdated
Show resolved
Hide resolved
// 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) { |
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 one of these should be commented out as just an example rather than having the code overwrite the first precondition.
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 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
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.
Do you think it would look better as
Storage.BlobTargetOption precondition;
if (storage.get(bucketName, objectName != null) {
precondition = ...;
} else {
precondition = ...;
?
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.
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())
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 @JesseLovelace suggestion here is the most readable. If we wanted to avoid the duplicate call could we could save the object retrieved.
Adds BlobTargetOption.[meta]generationmatch(long) and updates the samples to use it