Hi people I need to work with a java api. I'm stor...
# announcements
w
Hi people I need to work with a java api. I'm storing info in a remote store and also in memory. The interface for the remote store is that it returns a Mono of the value, but if its not found it returns a Mono with the value null. I'm trying to get the in memory implementation to do the same.
Copy code
override fun load(id: String): Mono<StoredRequest?> {
        val storedRequest: StoredRequest? = memory[id]
        return Mono.just(null);
    }
However it doesn't allow me to return a Mono of null here or even return the value from the backing map. As far as I understand the method signature I've given a null should be allowed. Is there any way to force the java like behaviour?
t
er…. I didn’t think it was possible to create a Mono with a null value. It should be an empty Mono instead
w
Well in my tests against redis it prints null, (which I assume is not the string null)
Copy code
val something = repository.load("Not found").block()
        println(something)
t
Ah - you get
null
there from an empty mono
So just change your code above to be
Mono.empty()
w
Copy code
override fun load(id: String): Mono<StoredRequest?> {
    val storedRequest: StoredRequest? = memory[id]
    return if (storedRequest != null) Mono.just(storedRequest) else Mono.empty()
}
Works thank you. I think I should be able to use let there but I'm making a hash of it. (no pun intended)
Copy code
override fun load(id: String) = memory[id]?.let { Mono.just(it) }?: Mono.empty()
t
Ah, if you want that you can just do
Mono.justOrEmpty(nullableValue)
✔️ 1