-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Expose Observable[Unit]
instead of Observable[Void]
#1282
Conversation
@deprecated( | ||
"Is no longer needed because of the `ToSingleObservableUnit` implicit class. Scheduled for removal in a major release", | ||
"5.0" | ||
) | ||
def completeWithUnit(): SingleObservable[Unit] = UnitObservable(this) |
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.
Note that I propose to remove this method in the future.
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.
When do you propose to remove it? Prior to 5.0, from the annotation, I gather?
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.
5.0 is the version from which the method is deprecated. Once it's deprecated, we'll be able to remove it in the next major release, which is 6.0.
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.
If we were to release a 4.x version with the deprecation, then we would have been able to delete the method in 5.0. But I am not sure we can do that because we can only deprecate completeWithUnit
when there is an alternative (ToSingleObservableUnit
), and adding that alternative breaks compatibility, which is why the deprecation has to happen in 5.0, and the deletion can only happen after that.
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.
So should the "5.0"
become "6.0"
in the annotation?
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 don't think so, because "5.0" is the value of the since
attribute, not a version in which we promise to remove the method.
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.
Well that makes a lot of sense :)
* | ||
* @param pub A `Publisher` representing a finite stream. | ||
*/ | ||
implicit class ToSingleObservableUnit(pub: => Publisher[Void]) extends SingleObservable[Unit] { |
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.
This class is why we are able to simply replace Observable[Void]
with Observable[Unit]
in the codebase.
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.
implicits are magic and have been changed in Scala 3 in favour of extension methods (probably more akin to Kotlin extension methods).
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.
But for now we are sticking with implicits, right?
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.
yeap 👍
* | ||
* @param pub A `Publisher` representing a finite stream. | ||
*/ | ||
implicit class ToGridFSUploadPublisherUnit(pub: => GridFSUploadPublisher[Void]) extends GridFSUploadPublisher[Unit] { |
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.
This class is why we are able to simply replace GridFSUploadPublisher[Void]
with GridFSUploadPublisher[Unit]
in the codebase.
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.
This doesn't need to be a global implicit so can be added here instead: gridfs/package.scala
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.
Done.
@@ -116,17 +118,41 @@ trait ObservableImplicits { | |||
override def subscribe(observer: Observer[_ >: GridFSFile]): Unit = Mono.from(publisher).subscribe(observer) | |||
} | |||
|
|||
implicit class ToSingleObservableVoid(pub: => Publisher[Void]) extends SingleObservable[Void] { |
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.
ToSingleObservableVoid
is part of public API (https://mongodb.github.io/mongo-java-driver/4.11/apidocs/mongo-scala-driver/org/mongodb/scala/ObservableImplicits$ToSingleObservableVoid.html), but my understanding is that
- There was never a reason for any user to use it explicitly (though I found a single explicit import here https://github.com/hmrc/event-hub/blob/main/app/uk/gov/hmrc/eventhub/service/TransactionHandlerImpl.scala#L19).
- The implicit conversion was convenient for the driver code, and is completely unneeded for any user of the Scala driver API, because it does not expose
Publisher[Void]
. I wonder ifToSingleObservableVoid
is part of public API simply because there is no way to remove it from there, even though users don't need it.
For the aforementioned reasons, it seems to me that removing ToSingleObservableVoid
and introducing ToSingleObservableUnit
is fine (they can't coexist).
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.
Yeap its an implicit class, so using it directly is strange. However, having it in scope makes it to be usable in customers code. Either way this is a breaking change but fixes a bug so makes sense.
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.
One minor change - code move, one test suggestion then LGTM
@@ -116,17 +118,41 @@ trait ObservableImplicits { | |||
override def subscribe(observer: Observer[_ >: GridFSFile]): Unit = Mono.from(publisher).subscribe(observer) | |||
} | |||
|
|||
implicit class ToSingleObservableVoid(pub: => Publisher[Void]) extends SingleObservable[Void] { |
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.
Yeap its an implicit class, so using it directly is strange. However, having it in scope makes it to be usable in customers code. Either way this is a breaking change but fixes a bug so makes sense.
* | ||
* @param pub A `Publisher` representing a finite stream. | ||
*/ | ||
implicit class ToGridFSUploadPublisherUnit(pub: => GridFSUploadPublisher[Void]) extends GridFSUploadPublisher[Unit] { |
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.
This doesn't need to be a global implicit so can be added here instead: gridfs/package.scala
* | ||
* @param pub A `Publisher` representing a finite stream. | ||
*/ | ||
implicit class ToSingleObservableUnit(pub: => Publisher[Void]) extends SingleObservable[Unit] { |
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.
implicits are magic and have been changed in Scala 3 in favour of extension methods (probably more akin to Kotlin extension methods).
|
||
import org.mongodb.scala.{ BaseSpec, Observer, Subscription } | ||
|
||
class UnitObservableSpec extends BaseSpec { |
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.
Lets add a chainability case to ensure this actually fixes the reported issue.
it should "work with for comprehensions" in {
val h = for {
_ <- UnitObservable(TestObservable(1 to 2))
_ <- UnitObservable(TestObservable(20 to 30))
} yield List(1, 2, 3)
val results = ArrayBuffer[Int]()
var completed = false
h.subscribe((s: List[Int]) => results.addAll(s), (t: Throwable) => t, () => completed = true)
results should equal(List(1, 2, 3))
completed should equal(true)
}
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.
Thank you, done.
… test proposed by Ross JAVA-4303
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.
Added a comment to the thread re deprecations
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.
LGTM
This PR is a proof that even a single ape typing behind a keyboard can produce a PR that compiles. Vigilance from reviewers is especially needed here.
JAVA-4303