Skip to content

Instantly share code, notes, and snippets.

@keirlawson
Created March 25, 2022 22:24
Show Gist options
  • Save keirlawson/0a08db3cd515c9850f300a2dd1646c05 to your computer and use it in GitHub Desktop.
Save keirlawson/0a08db3cd515c9850f300a2dd1646c05 to your computer and use it in GitHub Desktop.
JSON test file from Scalacheck Arbitrary/Circe Encoder
def streamGen[A](gen: Gen[A]): Stream[IO, A] = Stream.repeatEval(IO(gen.sample)).flatMap(Stream.fromOption(_))
def generateTestFile[A : Arbitrary : Encoder](file: fs2.io.file.Path, items: Long): IO[Unit] = {
val values = streamGen(Arbitrary.arbitrary[A])
val jsons = values.map(Encoder[A].apply).take(items)
val lines = Stream("[\n") ++ jsons.map(_.toString()).intersperse(",\n") ++ Stream("]")
lines
.through(text.utf8.encode)
.through(Files[IO].writeAll(file))
.compile
.drain
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment