I created two versions of my service. First one uses Futures, other one uses ZIO as an effect.
I have a simple method which use Future as a result effect:
def get(id: String)(implicit executionContext: ExecutionContext): Future[Data]
I also has some other version which uses ZIO[SomeEnv, SomeError, Data]:
def get(id: String): ZIO[SomeEnv, SomeError, Data]
Now, I need to create some kind of adapter which will return data from one version or another one:
def returnDataFromServiceVersion(version: Int) : ??? = {
if(version == 1) futureService.get(...)
else zioService.get(...)
}
The problem here is with returned type. I do not know how to convert ZIO into future or Future into ZIO to have common return type. I tried use ZIO.fromFuture{...} or toFuture() but it did not help.
My question is - how to create this returnDataFromServiceVersion method to use it with both services? I need to have common return type here.
Or maybe there is another way to solve this problem?
You have to decide whether your function returns Future or ZIO and this cannot depend on a runtime value like version in your snippet (unless of course you define the return type as AnyRef - not very useful).
If the rest of your program is based on Futures, but you would like to start introducing ZIO in some services, you can do that by executing your effects yourself using runtime.unsafeRun(effect):
val runtime = Runtime.default
def returnDataFromServiceVersion(version: Int): Future[Data] = {
runtime.unsafeRunToFuture(
zioService.get(...)
)
}
For more details refer to Running Effects in the official ZIO docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With