Vikas Adyar
Vikas Adyar

Reputation: 148

Retrieve Flux of iterable from an object

I have a Coupon service which gives me the coupon based on id and the return type is Mono. Also I have another service to list down all the employees for an organisation with return type Flux.

I want to create a reactive function to return Flux for a given coupon-id. If the employeeDestinationType is 'NONE' then return empty flux, if it is 'SPECIFIED' then return the set of employee-ids from Coupon object, else if it is 'ALL' make a call to the external service and return the 'ids' from the Flux.

Here is a reference object.

Coupon {
id,                        //Long
employeeDestinationType,   //NONE, SPECIFIED, ALL
employeeList               //Set<Long>
} 

Below are the method signatures.

public Mono<Coupon> findCouponById(long id) {}

public Flux<Employee> listAllEmployees() {}

Upvotes: 0

Views: 2021

Answers (1)

lkatiforis
lkatiforis

Reputation: 6255

You can simply use a flatMapMany to achieve this:

  return findCouponById(id) 
      .flatMapMany(coupon -> {
        if(...) {
          return Flux.empty();
        } else if(...) {
          return Flux.fromIterable(List.of(...));
        } else {
          return externalCall();
        }
      });

Upvotes: 2

Related Questions