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

Expose Observable[Unit] instead of Observable[Void] #1282

Merged
merged 3 commits into from Jan 5, 2024

Conversation

stIncMale
Copy link
Member

@stIncMale stIncMale commented Dec 15, 2023

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

Comment on lines +473 to 477
@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)
Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

@stIncMale stIncMale Jan 4, 2024

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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] {
Copy link
Member Author

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.

Copy link
Member

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).

Copy link
Member Author

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?

Copy link
Member

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] {
Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

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] {
Copy link
Member Author

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

  1. 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).
  2. 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 if ToSingleObservableVoid 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).

Copy link
Member

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.

Copy link
Member

@rozza rozza left a 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] {
Copy link
Member

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] {
Copy link
Member

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] {
Copy link
Member

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 {
Copy link
Member

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)
  }

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you, done.

@stIncMale stIncMale requested a review from rozza January 4, 2024 18:17
@stIncMale stIncMale requested a review from jyemin January 4, 2024 22:45
Copy link
Contributor

@jyemin jyemin left a 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

@jyemin jyemin self-requested a review January 4, 2024 23:06
Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

LGTM

@stIncMale stIncMale merged commit e78a2dc into mongodb:master Jan 5, 2024
60 checks passed
@stIncMale stIncMale deleted the JAVA-4303 branch January 5, 2024 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants