Skip to content

Commit

Permalink
Support for zio layers
Browse files Browse the repository at this point in the history
  • Loading branch information
danilbykov committed Feb 22, 2021
1 parent 585ca33 commit 3083772
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fork := true
libraryDependencies ++= List(
scalaOrganization.value % "scala-compiler" % scalaVersion.value % "provided",
"com.chuusai" %% "shapeless" % "2.3.3" % "test",
"dev.zio" %% "zio" % "1.0.4" % "test"
)

libraryDependencies += "org.specs2" %% "specs2-core" % "4.5.1" % Test
Expand Down
28 changes: 27 additions & 1 deletion src/main/scala/splain/format/Formatters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ trait Formatters { self: Analyzer =>
tpes.filterNot(t => ignoredTypes.exists(_ =:= t))
}

object DeepRefined {
def unapply(tpe: Type): Option[(List[Type], Scope)] =
tpe match {
case TypeRef(pre, sym, List(RefinedType(parents, decls)))
if decls.isEmpty && pre.typeSymbol.fullName == "zio" && sym.fullName == "zio.Has" =>
val sanitized = sanitizeParents(parents)
if (sanitized.length == 1) {
Some((List(TypeRef(pre, sym, sanitized.headOption.toList)), decls))
} else {
None
}
case Refined(types, scope) =>
if (scope.isEmpty) {
val subtypes = types.map(_.dealias).flatMap {
case DeepRefined(types, _) => types
case tpe => List(tpe)
}
Some((subtypes, scope))
} else {
Some((types, scope))
}
case _ =>
None
}
}

def formatDecl: Symbol => Formatted = {
case DeclSymbol(n, t) =>
Decl(n, t)
Expand Down Expand Up @@ -239,7 +265,7 @@ trait Formatters { self: Analyzer =>

def diff(left: Type, right: Type, top: Boolean): Option[Formatted] =
(left, right) match {
case (Refined(leftParents, leftDecls), Refined(rightParents, rightDecls)) =>
case (DeepRefined(leftParents, leftDecls), DeepRefined(rightParents, rightDecls)) =>
val parents = matchTypes(sanitizeParents(leftParents), sanitizeParents(rightParents)).sorted
val decls = matchDecls(leftDecls.toList, rightDecls.toList).sorted
Some(RefinedForm(parents, decls))
Expand Down
1 change: 1 addition & 0 deletions src/test/scala/splain/ErrorsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class ErrorsSpec extends SpecBase {
single types in function ${checkError("single-fn")}
single types with free symbol ${checkError("single-free")}
witness value types ${checkError("witness-value")}
zio test ${checkError("zlayer")}
"""

// def is = s2"""
Expand Down
20 changes: 20 additions & 0 deletions tests/zlayer/code.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import zio._

object layers {

trait Service1
trait Service2
trait Service3
trait Service4

val service1 = ZLayer.succeed(new Service1 {})

val service2 = ZLayer.succeed(new Service2 {})

val service3 = ZLayer.fromService { (_: Service1) => new Service3 {} }

val service4 = ZLayer.succeed(new Service4 {})

val services: ULayer[Has[Service1] with Has[Service2] with Has[Service3] with Has[Service4]] =
service1 ++ service2 >+> service3// ++ service4
}
2 changes: 2 additions & 0 deletions tests/zlayer/error
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type mismatch;
ZLayer[, Nothing, <none>|Has[Service4] with Has[Service1] with Has[Service2] with Has[Service3]]

0 comments on commit 3083772

Please sign in to comment.