Skip to content

Commit

Permalink
Add tagging example
Browse files Browse the repository at this point in the history
  • Loading branch information
mbore committed Oct 16, 2021
1 parent 4b70ca2 commit 2bb2a00
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ lazy val macrosAkkaTests = projectMatrix
lazy val macrosAutoCatsTests = projectMatrix
.in(file("macrosAutoCatsTests"))
.settings(testSettings)
.settings(libraryDependencies ++= Seq(scalatest, catsEffect))
.settings(libraryDependencies ++= Seq(scalatest, catsEffect, tagging))
.dependsOn(macrosAutoCats, testUtil)
.jvmPlatform(scalaVersions = scala2)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import com.softwaremill.macwire.auto.catssupport._

import cats.effect._

trait A
class AA(i: Int) extends A

class B(val a: A)

object Test {

val theAA: AA = new AA(1)
val theB: B = {
import cats.effect.unsafe.implicits.global

val resource = autowire[B](theAA)
resource.allocated.unsafeRunSync()._1
}

}

require(Test.theB.a == Test.theAA)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cats.effect._
import com.softwaremill.tagging._
import com.softwaremill.macwire.auto.catssupport._

class Cache {
val state = scala.collection.mutable.HashMap.empty[String, String]
}

trait First
trait Second

class A(val cache: Cache)
class B(val cache: Cache)
class C(val cache: Cache)
class D(val cache: Cache, val a: A, val b: B)
class E(val c: C, val d: D)

object Test {

val cache1 = (new Cache()).taggedWith[First]
val cache2 = (new Cache()).taggedWith[Second]

val theE = {
import cats.effect.unsafe.implicits.global

val resource = autowire[E](
cache1,
cache2,
((c: Cache @@ First) => new A(c)),
((c: Cache @@ First) => new B(c)),
((c: Cache @@ Second) => new C(c)),
((c: Cache @@ First) => new D(c, _, _))
)
resource.allocated.unsafeRunSync()._1
}

}

require(Test.theE.d.a.cache == Test.cache1)
require(Test.theE.d.b.cache == Test.cache1)
require(Test.theE.d.cache == Test.cache1)
require(Test.theE.c.cache == Test.cache2)

0 comments on commit 2bb2a00

Please sign in to comment.