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

Implement Uni.onItem().ifNotNull() #111

Merged
merged 3 commits into from
Apr 17, 2020
Merged

Implement Uni.onItem().ifNotNull() #111

merged 3 commits into from
Apr 17, 2020

Conversation

cescoffier
Copy link
Contributor

@cescoffier cescoffier commented Apr 17, 2020

This group checks if the item is not null. If the item is null default results are provided (null, empty Multi...)

This PR also contains various cosmetic fixes.

Related to #109

@cescoffier cescoffier added this to the 0.5.0 milestone Apr 17, 2020
@cescoffier cescoffier added the enhancement New feature or request label Apr 17, 2020
@cescoffier cescoffier linked an issue Apr 17, 2020 that may be closed by this pull request
@cescoffier cescoffier merged commit 9728e88 into master Apr 17, 2020
@cescoffier cescoffier deleted the features/if-not-null branch April 17, 2020 12:53
.onItem().ifNotNull().apply(String::toUpperCase)
.onItem().ifNull().continueWith("yolo!")
.await().indefinitely();
assertThat(r).isEqualTo("yolo!");
Copy link
Collaborator

Choose a reason for hiding this comment

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

I must be not grokking as I don't get how this would pass.

uni has null and therefore will return yolo! on subscription.

So I expected that r would return YOLO! and not yolo!.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The uni produces null, so ifNotNull().apply(String::toUpperCase) is not called, and null is propagated downstream. As a consequence .onItem().ifNull().continueWith("yolo!") is called, yolo! is emitted.

Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

But if you switch the 2 lines, it will become YOLO!, right?

(Because it's a pipeline, even though it really doesn't look like one.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, so are you saying onItem().ifNull() in the uni is never called because the previous line already consumed the onItem()?

If so, how would that line ever get executed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Each line is a stage, so there is an item emitted on every line.

Uni.createFrom().item(() -> null); // emits `null`
  .onItem().ifNotNull().apply(String::toUpperCase) // skipped because the item is null, so emits (another) `null`
  .onItem().ifNull().continueWith("yolo!") // called because the received item is `null`, and emits "yolol!"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe my confusion is that the code is actually duplicated? In that the same onItem() calls are added to the same uni which means it's a single chain of 4 possible outcomes, as opposed to 2 chains with 2 possible outcomes each.

Am I reading that right now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, yes the code is duplicated, as the first one (does nothing - no one subscribes) is just there for the snippet inclusion in the doc.

The second one is actually checking that it does the right thing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If I'm understanding, you could remove lines 33 and 34 and just do uni.await() right, as the onItem() entries are still in the Uni?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, every line returns a new Uni. So if line 27 we affect a new variable, and reuse it, then yes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider adding a onItem().ifNotNull() group
3 participants