From f46859ae3a7634d489d3f5dd937efcf5cfc233b9 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Mon, 28 Nov 2022 16:24:55 +0100 Subject: [PATCH] Introduce some Refaster rules that avoid nested `Publisher`s (#374) --- .../refasterrules/ReactorRules.java | 35 +++++++++++++++++-- .../refasterrules/ReactorRulesTestInput.java | 17 +++++++-- .../refasterrules/ReactorRulesTestOutput.java | 17 +++++++-- 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java index 2d9cd67bf9..d3695bec7f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java @@ -320,7 +320,10 @@ Flux after(Flux flux) { static final class FluxConcatMap { @BeforeTemplate Flux before(Flux flux, Function> function) { - return Refaster.anyOf(flux.flatMap(function, 1), flux.flatMapSequential(function, 1)); + return Refaster.anyOf( + flux.flatMap(function, 1), + flux.flatMapSequential(function, 1), + flux.map(function).concatMap(identity())); } @AfterTemplate @@ -337,7 +340,9 @@ Flux before( Function> function, int prefetch) { return Refaster.anyOf( - flux.flatMap(function, 1, prefetch), flux.flatMapSequential(function, 1, prefetch)); + flux.flatMap(function, 1, prefetch), + flux.flatMapSequential(function, 1, prefetch), + flux.map(function).concatMap(identity(), prefetch)); } @AfterTemplate @@ -639,6 +644,32 @@ Flux after(Flux flux) { } } + /** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */ + static final class MonoFlatMap { + @BeforeTemplate + Mono before(Mono mono, Function> function) { + return mono.map(function).flatMap(identity()); + } + + @AfterTemplate + Mono after(Mono mono, Function> function) { + return mono.flatMap(function); + } + } + + /** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */ + static final class MonoFlatMapMany { + @BeforeTemplate + Flux before(Mono mono, Function> function) { + return mono.map(function).flatMapMany(identity()); + } + + @AfterTemplate + Flux after(Mono mono, Function> function) { + return mono.flatMapMany(function); + } + } + /** * Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional * subscription. diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index 61b258003b..daf40d369f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refasterrules; +import static java.util.function.Function.identity; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; @@ -104,12 +105,16 @@ ImmutableSet> testFluxSwitchIfEmptyOfEmptyPublisher() { ImmutableSet> testFluxConcatMap() { return ImmutableSet.of( - Flux.just(1).flatMap(Mono::just, 1), Flux.just(2).flatMapSequential(Mono::just, 1)); + Flux.just(1).flatMap(Mono::just, 1), + Flux.just(2).flatMapSequential(Mono::just, 1), + Flux.just(3).map(Mono::just).concatMap(identity())); } ImmutableSet> testFluxConcatMapWithPrefetch() { return ImmutableSet.of( - Flux.just(1).flatMap(Mono::just, 1, 3), Flux.just(2).flatMapSequential(Mono::just, 1, 4)); + Flux.just(1).flatMap(Mono::just, 1, 3), + Flux.just(2).flatMapSequential(Mono::just, 1, 4), + Flux.just(3).map(Mono::just).concatMap(identity(), 5)); } Flux testFluxConcatMapIterable() { @@ -207,6 +212,14 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } + Mono testMonoFlatMap() { + return Mono.just("foo").map(Mono::just).flatMap(identity()); + } + + Flux testMonoFlatMapMany() { + return Mono.just("foo").map(Mono::just).flatMapMany(identity()); + } + ImmutableSet> testConcatMapIterableIdentity() { return ImmutableSet.of( Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list)), diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index 55092ba2d5..df6fb70af4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java @@ -108,12 +108,17 @@ ImmutableSet> testFluxSwitchIfEmptyOfEmptyPublisher() { } ImmutableSet> testFluxConcatMap() { - return ImmutableSet.of(Flux.just(1).concatMap(Mono::just), Flux.just(2).concatMap(Mono::just)); + return ImmutableSet.of( + Flux.just(1).concatMap(Mono::just), + Flux.just(2).concatMap(Mono::just), + Flux.just(3).concatMap(Mono::just)); } ImmutableSet> testFluxConcatMapWithPrefetch() { return ImmutableSet.of( - Flux.just(1).concatMap(Mono::just, 3), Flux.just(2).concatMap(Mono::just, 4)); + Flux.just(1).concatMap(Mono::just, 3), + Flux.just(2).concatMap(Mono::just, 4), + Flux.just(3).concatMap(Mono::just, 5)); } Flux testFluxConcatMapIterable() { @@ -206,6 +211,14 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } + Mono testMonoFlatMap() { + return Mono.just("foo").flatMap(Mono::just); + } + + Flux testMonoFlatMapMany() { + return Mono.just("foo").flatMapMany(Mono::just); + } + ImmutableSet> testConcatMapIterableIdentity() { return ImmutableSet.of( Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()),