Skip to content

Commit

Permalink
ZIO 2.0.0-RC5
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Apr 9, 2022
1 parent 9fe10e6 commit 8d938e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ lazy val zio2 = Project("clipp-zio-2", file("clipp-zio-2")).settings(commonSetti
resolvers +=
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "2.0.0-RC3",
"dev.zio" %% "zio-test" % "2.0.0-RC3" % Test,
"dev.zio" %% "zio-test-sbt" % "2.0.0-RC3" % Test
"dev.zio" %% "zio" % "2.0.0-RC5",
"dev.zio" %% "zio-test" % "2.0.0-RC5" % Test,
"dev.zio" %% "zio-test-sbt" % "2.0.0-RC5" % Test
),

testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.github.vigoo.clipp.errors.CustomParserError
import zio.{CanFail, Console, IsNotIntersection, Runtime, Tag, URIO, ZIO, ZIOAppArgs, ZLayer}

package object zioapi {
type ClippEnv = Console
type ClippEnv = Any
type ClippZIO[+A] = ZIO[ClippEnv, ParserFailure, A]

implicit val clippZio: ClippIO[ClippZIO] = new ClippIO[ClippZIO] {
Expand Down Expand Up @@ -66,22 +66,25 @@ package object zioapi {
zioapi.liftURIO[R, T](description, example)(f)
}

def parametersFromArgs[T: Tag : IsNotIntersection](spec: Parameter.Spec[T]): ZLayer[Console with ZIOAppArgs, ParserFailure, T] =
(for {
args <- ZIO.service[ZIOAppArgs]
result <- Clipp.parseOrFail(args.getArgs, spec)
} yield result).toLayer
def parametersFromArgs[T: Tag : IsNotIntersection](spec: Parameter.Spec[T]): ZLayer[ZIOAppArgs, ParserFailure, T] =
ZLayer {
for {
args <- ZIO.service[ZIOAppArgs]
result <- Clipp.parseOrFail(args.getArgs, spec)
} yield result
}

def effectfulParametersFromArgs[R, T: Tag : IsNotIntersection](createSpec: ZioDSL[R] => Parameter.Spec[T]): ZLayer[Console with ZIOAppArgs with R, ParserFailure, T] = {
def effectfulParametersFromArgs[R, T: Tag : IsNotIntersection](createSpec: ZioDSL[R] => Parameter.Spec[T]): ZLayer[ZIOAppArgs with R, ParserFailure, T] =
ZLayer {
for {
runtime <- ZIO.runtime[R]
args <- ZIO.service[ZIOAppArgs]
spec = createSpec(ZioDSL(runtime))
result <- Clipp.parseOrFail(args.getArgs, spec)
} yield result
}.toLayer
}

implicit class ZLayerOps[R <: Console, T](layer: ZLayer[R, ParserFailure, T]) {
implicit class ZLayerOps[R, T](layer: ZLayer[R, ParserFailure, T]) {
def printUsageInfoOnFailure: ZLayer[R, ParserFailure, T] =
layer.tapError { (parserFailure: ParserFailure) =>
Clipp.displayErrorsAndUsageInfo(parserFailure)
Expand Down
10 changes: 5 additions & 5 deletions clipp-zio-2/src/test/scala/io/github/vigoo/clipp/ZioSpecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import zio.{Console, _}
import zio.test.Assertion._
import zio.test._

object ZioSpecs extends DefaultRunnableSpec {
object ZioSpecs extends ZIOSpecDefault {

def spec = suite("ZIO interface")(
test("successfully parse") {
Expand Down Expand Up @@ -40,11 +40,11 @@ object ZioSpecs extends DefaultRunnableSpec {

test("can provide as layer") {
val spec = flag("Test", 'x')
val config: ZLayer[Console with ZIOAppArgs, ParserFailure, Boolean] = parametersFromArgs(spec).printUsageInfoOnFailure
val config: ZLayer[ZIOAppArgs, ParserFailure, Boolean] = parametersFromArgs(spec).printUsageInfoOnFailure
val test: ZIO[Boolean, Nothing, TestResult] =
parameters[Boolean].map(p => assert(p)(isTrue))

test.provideCustom(ZLayer.succeed(ZIOAppArgs(Chunk("-x"))), config)
test.provide(ZLayer.succeed(ZIOAppArgs(Chunk("-x"))), config)
},

suite("liftEffect")(
Expand All @@ -57,7 +57,7 @@ object ZioSpecs extends DefaultRunnableSpec {
val test: ZIO[String, Nothing, TestResult] =
parameters[String].map(p => assert(p)(equalTo("test")))

test.provideCustom(ZLayer.succeed(ZIOAppArgs(Chunk.empty)), config)
test.provide(ZLayer.succeed(ZIOAppArgs(Chunk.empty)), config)
},
test("failure") {
val config = effectfulParametersFromArgs[Any, String] { p =>
Expand All @@ -66,7 +66,7 @@ object ZioSpecs extends DefaultRunnableSpec {
}
}

assertM(parameters[String].unit.provideCustom(ZLayer.succeed(ZIOAppArgs(Chunk.empty)), config).exit)(fails(
assertM(parameters[String].unit.provide(ZLayer.succeed(ZIOAppArgs(Chunk.empty)), config).exit)(fails(
hasField("errors", _.errors.toList, contains[ParserError](CustomError("failure")))))
}
)
Expand Down

0 comments on commit 8d938e6

Please sign in to comment.