-
Notifications
You must be signed in to change notification settings - Fork 39
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 some Refaster rules that avoid nested Publisher
s
#374
Conversation
@AfterTemplate | ||
Flux<T> after(Mono<S> mono, Function<S, ? extends Publisher<T>> function) { | ||
return mono.flatMapMany(function); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of function
being of type Function<S, Mono<T>>
first this rule and then MonoFlatMapToFlux
are applied, which ultimately rewrite this code piece
mono.map(function).flatMapMany(identity());
to
mono.flatMap(function).flux();
/** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */ | ||
static final class FlatMapManyIdentity<S, T> { | ||
@BeforeTemplate | ||
Flux<T> before(Mono<S> mono, Function<S, ? extends Publisher<T>> function) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Admittedly, I couldn't figure out why
Flux<T> before(Mono<S> mono, Function<S, ? extends Publisher<T>> function) { | |
Flux<T> before(Mono<S> mono, Function<S, Publisher<T>> function) { |
alone wouldn't do it. Anyone an idea what I'm missing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the latter case only function that return precisely a Publisher
(so not a subtype) are matched.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Thanks a lot - clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a commit with some suggestions and also tweaked the suggested commit message; PTAL. :)
Tnx!
@@ -615,6 +615,58 @@ Flux<S> after(Flux<T> flux) { | |||
} | |||
} | |||
|
|||
/** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */ | |||
static final class MonoFlatMapIdentity<S, T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally name the patterns after the @AfterTemplate
. So this would be MonoFlatMap
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get this pattern beforehand - noted 📝
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries; we plan to automate this 😄
} | ||
|
||
/** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */ | ||
static final class FlatMapManyIdentity<S, T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W.r.t. the use of generic type parameters: further up we use T
and S
the other way around... perhaps something we should try to "auto-fix" somehow.
/** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */ | ||
static final class FlatMapManyIdentity<S, T> { | ||
@BeforeTemplate | ||
Flux<T> before(Mono<S> mono, Function<S, ? extends Publisher<T>> function) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the latter case only function that return precisely a Publisher
(so not a subtype) are matched.
/** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */ | ||
static final class MonoFlatMapIdentity<S, T> { | ||
@BeforeTemplate | ||
Mono<T> before(Mono<S> mono, Function<S, Mono<T>> function) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This matches more, and is consistent with the relevant parameter types:
Mono<T> before(Mono<S> mono, Function<S, Mono<T>> function) { | |
Mono<T> before(Mono<S> mono, Function<? super S, ? extends Mono<? extends T>> function) { |
ImmutableSet<Flux<String>> testFlatMapManyIdentity() { | ||
return ImmutableSet.of( | ||
Mono.just("foo").map(Mono::just).flatMapMany(identity()), | ||
Mono.just("foo").map(Flux::just).flatMapMany(identity())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One test suffices for this case :)
/** Prefer {@link Flux#concatMap(Function)} over more contrived alternatives. */ | ||
static final class ConcatMapIdentity<S, T> { | ||
@BeforeTemplate | ||
Flux<T> before(Flux<S> flux, Function<S, ? extends Publisher<T>> function) { | ||
return flux.map(function).concatMap(identity()); | ||
} | ||
|
||
@AfterTemplate | ||
Flux<T> after(Flux<S> flux, Function<S, ? extends Publisher<T>> function) { | ||
return flux.concatMap(function); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one can be merged with the existing FluxConcatMap
rule. Likewise for the one below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx - I didn't get this apparently.
Thx @Stephan202 for the good comments. Updated commit message LGTM, that's closer to what I had in mind anyways. |
65c7672
to
193979f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice @werli , you are on a roll! Keep it going 🚀 !
Suggested commit message LGTM!
(Rebased, will merge once 🍏)
Publisher
s
Context
These proposed Refaster rules primarily aim to make Reactor code more readable as nested
Publisher
s are avoided (e.g.Mono<Mono<String>>
).They may result in a minor performance improvement due to the dropped
map()
calls.Summary
I've noticed an occurrence of the following code pattern:
which these changes rewrite to:
Conceptionally, the same applies for:
Mono#flatMap(Function)
Mono#flatMapMap(Function)
Flux#concatMap(Function)
Flux#concatMap(Function, int)
and in theory also for some others (e.g.
Flux#flatMap()
), but these are already re-written by other rules IIUC.Suggested commit message