Reactor - After filter false, call another method

Asked

Viewed 11 times

1

Given the code below:

fun method() {
    return gateway1.getPerson(id)
        .filter { isPersonValid(it) }
        .flatMap { gateway2.isPersonHasAddress(id) }  // Retorna Mono<Boolean>
        .filter { it }
        .switchIfEmpty { Mono.just(false) }
}

private fun isPersonValid(person: Person) {
    // verifica se é valido
}

It is possible after each filter .. if it returns false, to call a method to log something ? Ai after that to switchIfEmpty ?! Remembering that the log that is the example action is different for both, so you wouldn’t necessarily put the call inside the switchIfEmpty

1 answer

0

I believe what you’re looking for is the operator doOnDiscard, designed for scenarios like this:

return gateway1.getPerson(id)
    .filter { isPersonValid(it) }
    .doOnDiscard(Person.class, person -> {
        // somente pessoas que foram descartadas pelo filter
        logPerson(person)
    })
    .flatMap { gateway2.isPersonHasAddress(id) }  // Retorna Mono<Boolean>
    .filter { it }
    .switchIfEmpty { Mono.just(false) }

I used as an example Person.class, but you can substitute for your class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.