Skip to content

Commit

Permalink
Merge pull request #215 from satabin/update/fs2-core-3.1.0
Browse files Browse the repository at this point in the history
Update fs2-core, fs2-io to 3.1.0
  • Loading branch information
satabin authored Aug 9, 2021
2 parents 6af5f7c + f52db02 commit a646536
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
val scala212 = "2.12.13"
val scala213 = "2.13.6"
val scala3 = "3.0.1"
val fs2Version = "3.0.6"
val fs2Version = "3.1.0"
val circeVersion = "0.14.1"
val shapeless2Version = "2.3.7"
val shapeless3Version = "3.0.2"
Expand Down
24 changes: 11 additions & 13 deletions csv/jvm/src/test/scala/fs2/data/csv/CsvParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@ package fs2.data.csv

import io.circe.parser.parse
import fs2._
import fs2.io.file.Files
import fs2.io.file.{Files, Flags, Path}
import cats.effect._
import cats.syntax.all._
import weaver._

import java.nio.file.{Path, Paths}

object CsvParserTest extends SimpleIOSuite {

private val testFileDir: Path = Paths.get("csv/jvm/src/test/resources/csv-spectrum/csvs/")
private val testFileDir: Path = Path("csv/jvm/src/test/resources/csv-spectrum/csvs/")

lazy val allExpected: Stream[IO, (java.nio.file.Path, List[Map[String, String]])] =
lazy val allExpected: Stream[IO, (Path, List[Map[String, String]])] =
Files[IO]
.directoryStream(testFileDir)
.list(testFileDir)
.evalMap { path =>
val name = path.getFileName.toFile.getName.stripSuffix(".csv")
val name = path.fileName.toString.stripSuffix(".csv")
Files[IO]
.readAll(Paths.get(s"csv/jvm/src/test/resources/csv-spectrum/json/$name.json"), 1024)
.through(text.utf8Decode)
.readAll(Path(s"csv/jvm/src/test/resources/csv-spectrum/json/$name.json"), 1024, Flags.Read)
.through(text.utf8.decode)
.compile
.string
.flatMap { rawExpected =>
Expand All @@ -48,11 +46,11 @@ object CsvParserTest extends SimpleIOSuite {

loggedTest("Standard test suite should pass") { log =>
allExpected
.evalTap { case (path, _) => log.info(path.getFileName.toString) }
.evalTap { case (path, _) => log.info(path.fileName.toString) }
.evalMap { case (path, expected) =>
Files[IO]
.readAll(path, 1024)
.through(fs2.text.utf8Decode)
.readAll(path, 1024, Flags.Read)
.through(fs2.text.utf8.decode)
.through(decodeUsingHeaders[CsvRow[String]]())
.compile
.toList
Expand All @@ -65,7 +63,7 @@ object CsvParserTest extends SimpleIOSuite {

loggedTest("Standard test suite files should be encoded and parsed correctly") { log =>
allExpected
.evalTap { case (path, _) => log.info(path.getFileName.toString) }
.evalTap { case (path, _) => log.info(path.fileName.toString) }
.evalMap { case (path, expected) =>
Stream
.emits(expected)
Expand Down
12 changes: 5 additions & 7 deletions documentation/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ A common pattern when using this library to read data from a file is to start by
import cats.effect._

import fs2._
import fs2.io.file.Files

import java.nio.file.Paths
import fs2.io.file.{Files, Flags, Path}

Files[IO]
.readAll(Paths.get("/some/path/to/a/file.data"), 1024)
.readAll(Path("/some/path/to/a/file.data"), 1024, Flags.Read)
// perform your decoding, parsing, and transformation here
.compile
.drain
Expand All @@ -71,10 +69,10 @@ If your file is encoded in **UTF-8**, you can use the [`fs2.text` decoding pipes

```scala mdoc:silent
Files[IO]
.readAll(Paths.get("/some/path/to/a/file.data"), 1024)
.readAll(Path("/some/path/to/a/file.data"), 1024, Flags.Read)
// extra decoding step is required since UTF-8 encodes character input
// up to 4 bytes, which might span several chunks
.through(text.utf8Decode)
.through(text.utf8.decode)
// now that we have a stream of `String`, we can parse
.through(tokens)
.compile
Expand All @@ -90,7 +88,7 @@ If your file is encoded using a single-byte encoding, there is no built-in decod
import fs2.data.text.latin1._

Files[IO]
.readAll(Paths.get("/some/path/to/a/file.data"), 1024)
.readAll(Path("/some/path/to/a/file.data"), 1024, Flags.Read)
// decoding is done by the now in scope `CharLikeChunks[IO, Byte]` instance
.through(tokens)
.compile
Expand Down
8 changes: 3 additions & 5 deletions documentation/docs/json/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,13 @@ Once you got a JSON token stream, selected and transformed what you needed in it
For instance, let's say you want to write the resulting JSON stream to a file in compact form (i.e. with no space or new lines), you can do:

```scala mdoc:compile-only
import fs2.io.file.Files

import java.nio.file.Paths
import fs2.io.file.{Files, Flags, Path}

stream
.through(render.compact)
.through(text.utf8Encode)
.through(text.utf8.encode)
.lift[IO]
.through(Files[IO].writeAll(Paths.get("/some/path/to/file.json")))
.through(Files[IO].writeAll(Path("/some/path/to/file.json"), Flags.Write))
.compile
.drain
```
Expand Down
15 changes: 7 additions & 8 deletions json/jvm/src/test/scala/fs2/data/json/JsonParsertest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ package data
package json

import ast._
import io.file.Files
import io.file.{Files, Flags, Path}

import cats.effect._

import weaver._
import java.nio.file.Paths

sealed trait Expectation
object Expectation {
Expand All @@ -34,24 +33,24 @@ object Expectation {

abstract class JsonParserTest[Json](implicit builder: Builder[Json]) extends SimpleIOSuite {

private val testFileDir = Paths.get("json/jvm/src/test/resources/test-parsing/")
private val testFileDir = Path("json/jvm/src/test/resources/test-parsing/")

test("Standard test suite files should be parsed correctly") {
Files[IO]
.directoryStream(testFileDir)
.list(testFileDir)
.evalMap { path =>
val expectation =
if (path.toFile.getName.startsWith("y_"))
if (path.fileName.startsWith("y_"))
Expectation.Valid
else if (path.toFile.getName.startsWith("n_"))
else if (path.fileName.startsWith("n_"))
Expectation.Invalid
else
Expectation.ImplementationDefined

val contentStream =
Files[IO]
.readAll(path, 1024)
.through(fs2.text.utf8Decode)
.readAll(path, 1024, Flags.Read)
.through(fs2.text.utf8.decode)

contentStream
.through(tokens)
Expand Down
13 changes: 6 additions & 7 deletions xml/jvm/src/test/scala/fs2/data/xml/EventParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ package fs2.data.xml
import cats.effect._

import fs2._
import fs2.io.file.Files
import fs2.io.file.{Files, Flags, Path}

import weaver._
import java.nio.file.Paths

object EventParserTest extends SimpleIOSuite {

val testFileDir = Paths.get("xml/jvm/src/test/resources/xmlconf")
val testFileDir = Path("xml/jvm/src/test/resources/xmlconf")
test("Standard test suite should pass") {
(Files[IO].walk(testFileDir.resolve("xmltest/valid")).filter(_.toFile.getName.endsWith(".xml")) ++
Files[IO].directoryStream(testFileDir.resolve("sun/valid")))
(Files[IO].walk(testFileDir.resolve("xmltest/valid")).filter(_.fileName.endsWith(".xml")) ++
Files[IO].list(testFileDir.resolve("sun/valid")))
.evalMap { path =>
// valid tests
Files[IO]
.readAll(path, 1024)
.through(fs2.text.utf8Decode)
.readAll(path, 1024, Flags.Read)
.through(fs2.text.utf8.decode)
.flatMap(Stream.emits(_))
.through(events)
.compile
Expand Down

0 comments on commit a646536

Please sign in to comment.