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

Introduce {Mono,Flux}OnErrorComplete Refaster rules #273

Merged
merged 3 commits into from
Oct 7, 2022

Conversation

chamil-prabodha
Copy link
Contributor

Add a new refaster template rule that rewrites .onErrorResume(e -> Mono.empty()) to .onErrorComplete(). This is applied to .onErrorResume(e -> Flux.empty()) as well.

@rickie rickie changed the title PSM-1627 Create refaster template to rewrite .onErrorResume(e -> Mono.empty()) to .onErrorComplete() Introduce Refaster template to rewrite .onErrorResume(e -> Mono.empty()) to .onErrorComplete() Oct 3, 2022
@rickie rickie added this to the 0.4.0 milestone Oct 3, 2022
Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Congrats on the first EPS PR @chamil-prabodha ! 🚀

Added a commit and left some comments.

Suggested commit message:

Prefer `{Mono,Flux}#onErrorComplete` over `{Mono,Flux}#onErrorResume(Function)` (#273)

Alternative:

Prefer `{Mono,Flux}#onErrorComplete` over `onErrorResume` with `{Mono,Flux#empty}` (#273)

One thing:

  • Usually we sort lexicographical but I feel Mono and Flux are a bit like min and max, which means we order like proposed.

@@ -271,6 +271,32 @@ Flux<S> after(Flux<T> flux) {
}
}

/** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives */
static final class FluxOnErrorComplete<T> {
Copy link
Member

Choose a reason for hiding this comment

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

Although the added templates are sorted lexicographical, I see that within this class we usually list the Mono before the Flux, so I'm swapping them around.

@@ -271,6 +271,32 @@ Flux<S> after(Flux<T> flux) {
}
}

/** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives */
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives */
/** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives. */

Javadoc should end with a dot 😄.,

@@ -95,6 +95,14 @@ Flux<Number> testFluxCast() {
return Flux.just(1).map(Number.class::cast);
}

Flux<Object> testFluxOnErrorComplete() {
return Flux.error(new IllegalStateException()).onErrorResume(e -> Flux.empty());
Copy link
Member

Choose a reason for hiding this comment

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

We can simplify the example and as a result we don't need to use Flux<Object>, we can go with Flux<Integer>.

}
}

/** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives */
Copy link
Member

Choose a reason for hiding this comment

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

I was doubting to make the description more specific. However, it's now the same as the Javadoc of surrounding templates so decided to leave it as is.

chamil-prabodha and others added 3 commits October 6, 2022 14:21
…o.empty())` to `.onErrorComplete()`

Add a new refaster template rule that rewrites `.onErrorResume(e -> Mono.empty())` to `.onErrorComplete()`. This is applied to `.onErrorResume(e -> Flux.empty())` as well.
Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

Tnx for the PR! Rebased and added a commit.

Alternative suggested commit message:

Introduce `{Mono,Flux}OnErrorComplete` Refaster rules (#273)

Comment on lines 274 to 298
/** Prefer {@link Mono#onErrorComplete()} over more contrived alternatives. */
static final class MonoOnErrorComplete<T> {
@BeforeTemplate
Mono<T> before(Mono<T> mono) {
return mono.onErrorResume(e -> Mono.empty());
}

@AfterTemplate
Mono<T> after(Mono<T> mono) {
return mono.onErrorComplete();
}
}

/** Prefer {@link Flux#onErrorComplete()} over more contrived alternatives. */
static final class FluxOnErrorComplete<T> {
@BeforeTemplate
Flux<T> before(Flux<T> flux) {
return flux.onErrorResume(e -> Flux.empty());
}

@AfterTemplate
Flux<T> after(Flux<T> flux) {
return flux.onErrorComplete();
}
}
Copy link
Member

Choose a reason for hiding this comment

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

There are also templates (such as EqualityTemplates.Negation) in which we combine @BeforeTemplates with incompatible types. We could do the same here. It feels kinda hacky, so I think it's good to keep this code as-is, but that does raise questions about the other templates. (Something to revisit once we add a check that validates the "sanity" of a Refaster rule.)

static final class FluxOnErrorComplete<T> {
@BeforeTemplate
Flux<T> before(Flux<T> flux) {
return flux.onErrorResume(e -> Flux.empty());
Copy link
Member

Choose a reason for hiding this comment

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

We can also add a case for Mono.empty() here.

@@ -301,7 +301,7 @@ Flux<T> after(Flux<T> flux) {
static final class PublisherProbeEmpty<T> {
@BeforeTemplate
PublisherProbe<T> before() {
return Refaster.anyOf(PublisherProbe.of(Mono.empty()), PublisherProbe.of(Flux.empty()));
Copy link
Member

Choose a reason for hiding this comment

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

Aiii, not sure how I misses this 😓.

@rickie rickie changed the title Introduce Refaster template to rewrite .onErrorResume(e -> Mono.empty()) to .onErrorComplete() Introduce {Mono,Flux}OnErrorComplete Refaster rules Oct 7, 2022
@rickie rickie merged commit bd73243 into master Oct 7, 2022
@rickie rickie deleted the chamil-prabodha/PSM-1627 branch October 7, 2022 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants