macwire icon indicating copy to clipboard operation
macwire copied to clipboard

Ref #273 Need help for `Effect Type`'s Injection

Open djx314 opened this issue 2 years ago • 5 comments

@adamw Can't reopen https://github.com/softwaremill/macwire/issues/273 and create a new issue since reproduct the problem. I use macwire to implement a web-app's injection using cats-effect with a simple way. And it seems that it works in any effect type. But some code not works well. Code Link Line 20 and Line 21 can not use macwire since "injection by name" is the same as "injection by varible name" in this injection style. Hoping for help to make these code more friendly.

djx314 avatar Mar 15 '23 18:03 djx314

sbt command

sbt>wire-web-scala/reStart

and input the url in browser.

http://127.0.0.1:8080/api/cats

or

http://127.0.0.1:8080/api/{StringParameter}

djx314 avatar Mar 15 '23 20:03 djx314

Can you create a minimised example, which would show the problem?

adamw avatar Mar 16 '23 10:03 adamw

@adamw Already reproducted. Have a look if there are any other needs. Repo djx314/macwire-issue281 Injection code that need macwire Point Line All code

djx314 avatar Mar 16 '23 19:03 djx314

Run result

sbt:macwire-issue281> run
[info] running macwire.test.app.MainApp
resourceA value:5
resourceB value:2
macwire-issue281 executed successful.
[success] Total time: 1 s, completed 2023-3-17 3:41:23

djx314 avatar Mar 16 '23 19:03 djx314

Provide a quick fix and expect a more reasonable approach. Code link

class AppInjection {
  lazy val namedInstance: Named = Named("macwire-issue281")

  private case class ServiceBImpl(test: PrintString) {
    def build(resourceB: DBResource): ServiceB = ServiceB(resourceB = resourceB, test = test)
  }

  def serviceCResource[F[_]]: Resource[F, ServiceC] = wire[DBResourceA].resource.flatMap(dbA =>
    wire[DBResourceB].resource.flatMap(dbB =>
      wire[PrintStringResource].resource.map { printlnString =>
        lazy val serviceA: ServiceA         = ServiceA(resourceA = dbA)           // Injection by name.
        lazy val serviceBImpl: ServiceBImpl = wire[ServiceBImpl]
        lazy val serviceB: ServiceB         = serviceBImpl.build(resourceB = dbB) // Injection by name and injection by type.
        wire[ServiceC]
      }
    )
  )
}

djx314 avatar Mar 17 '23 07:03 djx314