Skip to content

Commit

Permalink
Upgrade ZIO to 2.0.0-RC4 + resurrect word count example (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
William Harvey authored Apr 3, 2022
1 parent d45450a commit 7d87012
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ inThisBuild(
addCommandAlias("fmt", "all scalafmtSbt scalafmt test:scalafmt")
addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck")

val zioVersion = "2.0.0-RC3"
val zioVersion = "2.0.0-RC4"

lazy val root = project
.in(file("."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.nio.file.{Path => JPath}
import zio.cli.{Args, CliApp, Command, Exists, HelpDoc, Options}
import zio.cli.HelpDoc.Span.text

//import zio._
import zio.ZIOAppDefault
import zio.Console.printLine

Expand Down
45 changes: 25 additions & 20 deletions examples/shared/src/main/scala/zio/cli/examples/WcApp.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package zio.cli.examples
/*

import java.nio.file.Path

import zio.cli.HelpDoc.Span.text
import zio.cli._
import zio.stream.{ZSink, ZStream}
import zio.{URIO, ZIO}
import zio.{ Console, ZIOAppDefault }
import zio.stream.{ZPipeline, ZSink, ZStream}
import zio._
import zio.Console.printLine

object WcApp extends ZIOAppDefault {
Expand All @@ -33,8 +30,8 @@ object WcApp extends ZIOAppDefault {

val wc: Command[(WcOptions, ::[Path])] = Command("wc", options, args)

val execute: (WcOptions, ::[Path]) => URIO[Console, Unit] = {
def printResult(res: List[WcResult]): ZIO[Console, Nothing, Unit] = {
val execute: (WcOptions, ::[Path]) => UIO[Unit] = {
def printResult(res: List[WcResult]): UIO[Unit] = {
def wcTotal(results: List[WcResult]) = {
def optSum(acc: WcResult, elem: WcResult, extract: WcResult => Option[Long]): Option[Long] =
extract(acc).flatMap(a => extract(elem).map(_ + a))
Expand All @@ -55,31 +52,38 @@ object WcApp extends ZIOAppDefault {
s"${opt(res.countLines)} ${opt(res.countWords)} ${opt(res.countChar)} ${opt(res.countBytes)} ${res.fileName}"
}

ZIO.foreachDiscard(res)(r => printLine(format(r)).!) *> ZIO.when(res.length > 1)(printLine(format(wcTotal(res))).!).ignore
ZIO.foreachDiscard(res)(r => printLine(format(r)).!) *> ZIO
.when(res.length > 1)(printLine(format(wcTotal(res))).!)
.ignore
}

(opts, paths) => {
zio.Console.printLine(s"executing wc with args: $opts $paths").! *> ???
ZIO.foreachPar[Any, Throwable, Path, WcResult, List](paths)({ path =>
zio.Console.printLine(s"executing wc with args: $opts $paths").! *>
ZIO
.foreachPar[Any, Throwable, Path, WcResult, List](paths)({ path =>
def option(bool: Boolean, sink: ZSink[Any, Nothing, Byte, Byte, Long])
: ZSink[Any, Nothing, Byte, Byte, Option[Long]] =
if (bool) sink.map(Some(_)) else ZSink.succeed[Byte, Option[Long]](None)
if (bool) sink.map(Some(_)) else ZSink.succeed(None)

val byteCount = option(opts.bytes, ZSink.count)
val lineCount = option(opts.lines, ZTransducer.utfDecode >>> ZTransducer.splitLines >>> ZSink.count)
val lineCount = option(opts.lines, ZPipeline.utfDecode >>> ZPipeline.splitLines >>> ZSink.count)
val wordCount =
option(opts.words, ZTransducer.utfDecode.mapChunks(_.flatMap(_.split("\\s+"))) >>> ZSink.count)
option(
opts.words,
ZPipeline.utfDecode >>> ZPipeline.mapChunks((_: Chunk[String]).flatMap(_.split("\\s+"))) >>> ZSink.count
)
val charCount =
option(opts.char, ZTransducer.utfDecode >>> ZSink.foldLeft[String, Long](0L)((s, e) => s + e.length))
option(opts.char, ZPipeline.utfDecode >>> ZSink.foldLeft[String, Long](0L)((s, e) => s + e.length))

val zippedSinks: ZSink[Any, Nothing, Byte, Byte, (Option[Long], Option[Long], Option[Long], Option[Long])] =
(byteCount <&> lineCount <&> wordCount <&> charCount).map(t => (t._1._1._1, t._1._1._2, t._1._2, t._2))
(byteCount <&> lineCount <&> wordCount <&> charCount)

ZStream
.fromFile(path.toFile)
.run(zippedSinks)
.map(t => WcResult(path.getFileName.toString, t._1, t._2, t._3, t._4))
}).withParallelism(4)
})
.withParallelism(4)
.orDie
.flatMap(res => printResult(res))
}
Expand All @@ -92,8 +96,9 @@ object WcApp extends ZIOAppDefault {
wc
)(execute.tupled)

override def run(args: List[String]) = wcApp.run(args)
override def run =
for {
args <- ZIOAppArgs.getArgs
_ <- wcApp.run(args.toList)
} yield ()
}
*/
9 changes: 4 additions & 5 deletions zio-cli/shared/src/main/scala/zio/cli/CliApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import zio.cli.BuiltInOption._
import zio.cli.completion.{Completion, CompletionScript}

import scala.annotation.tailrec
import zio.{Console, System}
import zio.Console.printLine
import zio.System.envs

Expand All @@ -18,7 +17,7 @@ import zio.System.envs
* requires environment `R`, and may fail with a value of type `E`.
*/
sealed trait CliApp[-R, +E, Model] {
def run(args: List[String]): ZIO[R with Console with System, Nothing, ExitCode]
def run(args: List[String]): ZIO[R, Nothing, ExitCode]

def config(newConfig: CliConfig): CliApp[R, E, Model]

Expand Down Expand Up @@ -54,7 +53,7 @@ object CliApp {
) extends CliApp[R, E, Model] { self =>
def config(newConfig: CliConfig): CliApp[R, E, Model] = copy(config = newConfig)

def executeBuiltIn(builtInOption: BuiltInOption): RIO[Console with System, Unit] =
def executeBuiltIn(builtInOption: BuiltInOption): Task[Unit] =
builtInOption match {
case ShowHelp(helpDoc) =>
val fancyName =
Expand Down Expand Up @@ -87,7 +86,7 @@ object CliApp {
def footer(newFooter: HelpDoc): CliApp[R, E, Model] =
copy(footer = self.footer + newFooter)

def printDocs(helpDoc: HelpDoc): URIO[Console, Unit] =
def printDocs(helpDoc: HelpDoc): UIO[Unit] =
printLine(helpDoc.toPlaintext(80)).!

// prepend a first argument in case the CliApp's command is expected to consume it
Expand All @@ -100,7 +99,7 @@ object CliApp {
case Command.Subcommands(parent, _) => prefix(parent)
}

def run(args: List[String]): ZIO[R with Console with System, Nothing, ExitCode] =
def run(args: List[String]): ZIO[R, Nothing, ExitCode] =
command
.parse(prefix(command) ++ args, config)
.foldZIO(
Expand Down
12 changes: 6 additions & 6 deletions zio-cli/shared/src/test/scala/zio/cli/PrimTypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ object PrimTypeSpec extends ZIOSpecDefault {
simplePrimTypeSuite(PrimType.Period, anyPeriod, "Period")
)

def simplePrimTypeSuite[G, P[G] <: PrimType[G]](primType: P[G], gen: Gen[Random, G], primeTypeName: String) =
def simplePrimTypeSuite[G, P[G] <: PrimType[G]](primType: P[G], gen: Gen[Any, G], primeTypeName: String) =
suite(s"$primeTypeName Suite")(
test(s"validate returns proper $primeTypeName representation") {
check(gen) { i =>
Expand All @@ -153,7 +153,7 @@ object PrimTypeSpec extends ZIOSpecDefault {
}
)

def randomizeCharCases(s: String): ZIO[Random, Nothing, String] =
def randomizeCharCases(s: String): UIO[String] =
ZIO.foreach(s.toList)(c => Random.nextBoolean.map(b => if (b) c.toUpper else c.toLower)).map(_.mkString)

def mockFileSystem(
Expand All @@ -172,13 +172,13 @@ object PrimTypeSpec extends ZIOSpecDefault {
override def isRegularFile(path: JPath): UIO[Boolean] = ZIO.succeed(pathIsRegularFile)
}

val anyTrueBooleanString: Gen[Random, String] =
val anyTrueBooleanString: Gen[Any, String] =
Gen.fromIterable(List("true", "1", "y", "yes", "on")).mapZIO(randomizeCharCases)
val anyFalseBooleanString: Gen[Random, String] =
val anyFalseBooleanString: Gen[Any, String] =
Gen.fromIterable(List("false", "0", "n", "no", "off")).mapZIO(randomizeCharCases)

val anyBigInt: Gen[Random, BigInt] = Gen.long(0, Long.MaxValue).map(BigInt(_))
val anyBoolean: Gen[Random, Boolean] =
val anyBigInt: Gen[Any, BigInt] = Gen.long(0, Long.MaxValue).map(BigInt(_))
val anyBoolean: Gen[Any, Boolean] =
Gen.fromIterable(List(true, false))
val anyInstant = Gen.instant.map(_.atZone(ZoneOffset.UTC))
val anyPeriod = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.nio.file.{Files => JFiles, Path => JPath}

import scala.language.postfixOps

object CompletionSpec extends DefaultRunnableSpec {
object CompletionSpec extends ZIOSpecDefault {

/**
* A micro potpourri of functions borrowed (in simplified form) from `zio-nio`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import zio.test.Assertion._
import zio.test._
import zio.cli._

object RegularLanguageSpec extends DefaultRunnableSpec {
object RegularLanguageSpec extends ZIOSpecDefault {
def spec = suite("RegularLanguage Spec")(
suite("Toplevel Command Completion Spec")(
suite("Empty language")(
Expand Down

0 comments on commit 7d87012

Please sign in to comment.