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

Add Refaster rule for Flux#concat for a single Mono or Flux #20

Merged
merged 3 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,32 @@ Flux<S> after(Mono<T> mono) {
}
}

/** Don't use {@link Flux#concat(Publisher)} with only a single {@link Mono}. */
abstract static class FluxConcatOfOneMono<T> {
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 use static final here instead of abstract. abstract for templates is only when we need to use the Refaster @Placeholder.

Copy link
Member

Choose a reason for hiding this comment

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

Additionally, the convention for naming the templates is almost exactly describing the beforetemplate content. So it seems that we can even drop OfOne.
I'll push something :)

Copy link
Member Author

Choose a reason for hiding this comment

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

🧑‍🎓, thanks @rickie !

Copy link
Member

Choose a reason for hiding this comment

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

I don't know why I described it precisely the other way around here.. My bad 😅

@BeforeTemplate
Flux<T> before(Mono<T> mono) {
return Flux.concat(mono);
}

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

/** Don't use {@link Flux#concat(Publisher)} with only a single {@link Flux}. */
abstract static class FluxConcatOfOneFlux<T> {
@BeforeTemplate
Flux<T> before(Flux<T> flux) {
return Flux.concat(flux);
}

@AfterTemplate
Flux<T> after(Flux<T> flux) {
return flux;
}
}

/**
* Prefer a collection using {@link MoreCollectors#toOptional()} over more contrived alternatives.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMapMany(s -> Mono.just(s + s));
}

Flux<String> testFluxConcatOfOneMono() {
return Flux.concat(Mono.just("foo"));
}

Flux<String> testFluxConcatOfOneFlux() {
return Flux.concat(Flux.just("foo", "bar"));
werli marked this conversation as resolved.
Show resolved Hide resolved
}

ImmutableSet<Mono<Optional<String>>> testMonoCollectToOptional() {
return ImmutableSet.of(
Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux();
}

Flux<String> testFluxConcatOfOneMono() {
return Mono.just("foo").flux();
}

Flux<String> testFluxConcatOfOneFlux() {
return Flux.just("foo", "bar");
}

ImmutableSet<Mono<Optional<String>>> testMonoCollectToOptional() {
return ImmutableSet.of(
Mono.just("foo").flux().collect(toOptional()),
Expand Down