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 34d1932 commit bf9239e
Show file tree
Hide file tree
Showing 26 changed files with 115 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import io.github.vigoo.desert.codecs._
import io.github.vigoo.desert.BinarySerialization._
import io.github.vigoo.desert.{BinaryCodec, FieldAdded}
import org.openjdk.jmh.annotations._
import zio.BootstrapRuntime

import scala.util.Random

@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
class GenericComplexSerializationBenchmark extends BootstrapRuntime {
class GenericComplexSerializationBenchmark {
import GenericComplexSerializationBenchmark._

var testDocument: Root = _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.github.vigoo.desert._
import io.github.vigoo.desert.codecs._
import io.github.vigoo.desert.syntax._
import org.openjdk.jmh.annotations._
import zio.BootstrapRuntime

import scala.util.Random

Expand All @@ -16,7 +15,7 @@ import scala.util.Random
@State(Scope.Benchmark)
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
class JavaStreamBinaryInputBenchmark extends BootstrapRuntime {
class JavaStreamBinaryInputBenchmark {

val inputByte: Array[Byte] = serializeToArray(100.toByte).toOption.get
val inputShort: Array[Byte] = serializeToArray(100.toShort).toOption.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.util.concurrent.TimeUnit

import io.github.vigoo.desert.{BinaryOutput, JavaStreamBinaryOutput}
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Level, Measurement, Mode, OperationsPerInvocation, OutputTimeUnit, Scope, Setup, State, Warmup}
import zio.BootstrapRuntime

import scala.util.Random

Expand All @@ -14,7 +13,7 @@ import scala.util.Random
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 200, timeUnit = TimeUnit.MILLISECONDS)
class JavaStreamBinaryOutputBenchmark extends BootstrapRuntime {
class JavaStreamBinaryOutputBenchmark {

val array: Array[Byte] = Random.nextBytes(4096)
var output: BinaryOutput = _
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import java.util.zip.Deflater

import io.github.vigoo.desert.BinaryCodec
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Measurement, Mode, OutputTimeUnit, Param, Scope, Setup, State, Warmup}
import zio.BootstrapRuntime
import io.github.vigoo.desert.syntax._

import scala.io.Source
Expand All @@ -16,7 +15,7 @@ import scala.io.Source
@State(Scope.Benchmark)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
class LargeCompressedArraySerializationBenchmark extends BootstrapRuntime {
class LargeCompressedArraySerializationBenchmark {
import LargeCompressedArraySerializationBenchmark._

var testData: TestData = _
Expand Down
16 changes: 8 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ lazy val commonSettings = Seq(
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),

libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "1.0.13" % "test",
"dev.zio" %% "zio-test" % "1.0.13" % "test",
"dev.zio" %% "zio-test-sbt" % "1.0.13" % "test",
"dev.zio" %% "zio-test-magnolia" % "1.0.13" % "test",
"dev.zio" %% "zio" % "2.0.0-RC5" % Test,
"dev.zio" %% "zio-test" % "2.0.0-RC5" % Test,
"dev.zio" %% "zio-test-sbt" % "2.0.0-RC5" % Test,
"dev.zio" %% "zio-test-magnolia" % "2.0.0-RC5" % Test,
),

coverageEnabled in(Test, compile) := true,
Expand Down Expand Up @@ -73,7 +73,7 @@ lazy val core = CrossProject("desert-core", file("desert-core"))(JVMPlatform, JS
description := "A Scala binary serialization library",
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
"dev.zio" %% "zio-prelude" % "1.0.0-RC6",
"dev.zio" %% "zio-prelude" % "1.0.0-RC13",
"com.chuusai" %% "shapeless" % "2.3.9",
),
)
Expand Down Expand Up @@ -121,7 +121,7 @@ lazy val zio = CrossProject("desert-zio", file("desert-zio"))(JVMPlatform, JSPla
.settings(
description := "ZIO API and codecs for desert",
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "1.0.13"
"dev.zio" %% "zio" % "2.0.0-RC5"
)
)
.jsSettings(coverageEnabled := false)
Expand All @@ -133,8 +133,8 @@ lazy val benchmarks = project.in(file("benchmarks"))
publishArtifact := false,
coverageEnabled := false,
libraryDependencies ++= Seq(
"dev.zio" %% "zio-test" % "1.0.13",
"dev.zio" %% "zio-test-magnolia" % "1.0.13",
"dev.zio" %% "zio-test" % "2.0.0-RC5",
"dev.zio" %% "zio-test-magnolia" % "2.0.0-RC5",
)
)
.enablePlugins(JmhPlugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,44 @@ import akka.actor.{Actor => UntypedActor, ActorRef => UntypedActorRef, ActorSyst
import io.github.vigoo.desert.DesertException
import io.github.vigoo.desert.akka.codecs._
import io.github.vigoo.desert.syntax._
import zio.console.Console
import zio.test.Assertion._
import zio.test._
import zio.test.environment.TestEnvironment
import zio.{Task, ZIO, ZManaged, console}
import zio._

object AkkaCodecsSpec extends DefaultRunnableSpec {
object AkkaCodecsSpec extends ZIOSpecDefault {

override def spec: ZSpec[TestEnvironment, Any] =
override def spec =
suite("Akka serialization codecs")(
testM("correctly serializes untyped actor references")(
untypedActorSystem.use { system =>
test("correctly serializes untyped actor references")(
untypedActorSystem.flatMap { system =>
implicit val extendedSystem: UntypedExtendedActorSystem = system.asInstanceOf[UntypedExtendedActorSystem]
for {
actor1 <- Task(system.actorOf(UntypedProps(classOf[TestUntypedActor])))
actor1 <- ZIO.attempt(system.actorOf(UntypedProps(classOf[TestUntypedActor])))
serialized <- ZIO.fromEither(serializeToArray(actor1)).mapError(new DesertException(_))
actor2 <- ZIO.fromEither(deserializeFromArray[UntypedActorRef](serialized)).mapError(new DesertException(_))
} yield assert(actor1)(equalTo(actor2))
}
),
testM("correctly serializes typed actor references")(
typedActorSystem.use { implicit system =>
test("correctly serializes typed actor references")(
typedActorSystem.flatMap { implicit system =>
for {
actor1 <- Task(system.toClassic.spawnAnonymous(testActorBehavior))
actor1 <- ZIO.attempt(system.toClassic.spawnAnonymous(testActorBehavior))
serialized <- ZIO.fromEither(serializeToArray(actor1)).mapError(new DesertException(_))
actor2 <- ZIO.fromEither(deserializeFromArray[ActorRef[String]](serialized)).mapError(new DesertException(_))
} yield assert(actor1)(equalTo(actor2))
}
)
)

private def untypedActorSystem: ZManaged[TestEnvironment, Throwable, UntypedActorSystem] =
ZManaged.make(Task(UntypedActorSystem("test")))(
private def untypedActorSystem: ZIO[Scope, Throwable, UntypedActorSystem] =
ZIO.acquireRelease(ZIO.attempt(UntypedActorSystem("test")))(
system => ZIO.fromFuture(_ => system.terminate()).unit.catchAll(logFatalError))

private def typedActorSystem: ZManaged[TestEnvironment, Throwable, ActorSystem[_]] =
private def typedActorSystem: ZIO[Scope, Throwable, ActorSystem[_]] =
untypedActorSystem.map(_.toTyped)

private def logFatalError(reason: Throwable): ZIO[Console, Nothing, Unit] =
console.putStrLn(s"Fatal actor termination error: ${reason.getMessage}").orDie
private def logFatalError(reason: Throwable): ZIO[Any, Nothing, Unit] =
Console.printLine(s"Fatal actor termination error: ${reason.getMessage}").orDie

class TestUntypedActor extends UntypedActor {
override def receive: Receive = ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import io.github.vigoo.desert.codecs._
import zio.ZIO
import zio.test.Assertion._
import zio.test._
import zio.test.environment._

object IOWrapperSpec extends DefaultRunnableSpec {
object IOWrapperSpec extends ZIOSpecDefault {
override def spec: ZSpec[TestEnvironment, Throwable] =
suite("Cats effect syntax")(
testM("is an IO wrapper around the core functionality") {
ZIO.effect {
test("is an IO wrapper around the core functionality") {
ZIO.attempt {
val test = for {
bytes <- serializeToArray[IO, String]("Hello world")
deser <- deserializeFromArray[IO, String](bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import io.github.vigoo.desert.SerializationProperties
import io.github.vigoo.desert.cats.codecs._
import io.github.vigoo.desert.codecs._
import zio.test._
import zio.test.environment.TestEnvironment

import scala.collection.immutable.{SortedMap, SortedSet}

object CatsCollectionSerializationSpec extends DefaultRunnableSpec with SerializationProperties {
object CatsCollectionSerializationSpec extends ZIOSpecDefault with SerializationProperties {
override def spec: ZSpec[TestEnvironment, Any] =
suite("Cats collections can be serialized and read back")(
testM("validated")(canBeSerialized(Gen.either(Gen.anyInt, Gen.anyString).map(Validated.fromEither))),
testM("non-empty list")(canBeSerialized(Gen.listOf1(Gen.anyString).map(NonEmptyList.fromList))),
testM("non-empty set")(canBeSerialized(Gen.setOf1(Gen.anyString).map(set => NonEmptySet.fromSet(SortedSet.from(set))))),
testM("non-empty map")(canBeSerialized(Gen.mapOf1(Gen.anyString, Gen.anyInt).map(map => NonEmptyMap.fromMap(SortedMap.from[String, Int](map))))),
test("validated")(canBeSerialized(Gen.either(Gen.int, Gen.string).map(Validated.fromEither))),
test("non-empty list")(canBeSerialized(Gen.listOf1(Gen.string).map(NonEmptyList.fromList))),
test("non-empty set")(canBeSerialized(Gen.setOf1(Gen.string).map(set => NonEmptySet.fromSet(SortedSet.from(set))))),
test("non-empty map")(canBeSerialized(Gen.mapOf1(Gen.string, Gen.int).map(map => NonEmptyMap.fromMap(SortedMap.from[String, Int](map))))),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ object BinaryDeserializer {
trait BinaryCodec[T] extends BinarySerializer[T] with BinaryDeserializer[T]

object BinaryCodec {
def apply[T: BinaryCodec]: BinaryCodec[T] = implicitly[BinaryCodec[T]]

implicit def from[T](serializer: BinarySerializer[T], deserializer: BinaryDeserializer[T]): BinaryCodec[T] = new BinaryCodec[T] {
override def deserialize(): Deser[T] = deserializer.deserialize()
override def serialize(value: T): Ser[Unit] = serializer.serialize(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import _root_.zio.prelude.fx._
import scala.util.Try

trait BinaryDeserializerOps {
final def getInput: Deser[BinaryInput] = ZPure.access(_.input)
final def getInputTypeRegistry: Deser[TypeRegistry] = ZPure.access(_.typeRegistry)
final def getInput: Deser[BinaryInput] = ZPure.serviceWith(_.input)
final def getInputTypeRegistry: Deser[TypeRegistry] = ZPure.serviceWith(_.typeRegistry)
final def getDeserializerState: Deser[SerializerState] = ZPure.get
final def setDeserializerState(state: SerializerState): Deser[Unit] = ZPure.set(state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ import io.github.vigoo.desert.BinarySerializer.SerializationEnv
trait BinarySerialization {
def serialize[T : BinarySerializer](value: T, output: BinaryOutput, typeRegistry: TypeRegistry = TypeRegistry.empty): Either[DesertFailure, Unit] =
write[T](value)
.provide(SerializationEnv(output, typeRegistry))
.provideService(SerializationEnv(output, typeRegistry))
.either
.runResult(SerializerState.initial)

def serializeUnknown(value: Any, output: BinaryOutput, typeRegistry: TypeRegistry = TypeRegistry.empty): Either[DesertFailure, Unit] =
writeUnknown(value)
.provide(SerializationEnv(output, typeRegistry))
.provideService(SerializationEnv(output, typeRegistry))
.either
.runResult(SerializerState.initial)

def deserialize[T: BinaryDeserializer](input: BinaryInput, typeRegistry: TypeRegistry = TypeRegistry.empty): Either[DesertFailure, T] =
read[T]()
.provide(DeserializationEnv(input, typeRegistry))
.provideService(DeserializationEnv(input, typeRegistry))
.either
.runResult(SerializerState.initial)

def deserializeUnknown(input: BinaryInput, typeRegistry: TypeRegistry = TypeRegistry.empty): Either[DesertFailure, Any] =
readUnknown()
.provide(DeserializationEnv(input, typeRegistry))
.provideService(DeserializationEnv(input, typeRegistry))
.either
.runResult(SerializerState.initial)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import _root_.zio.prelude.fx._
import scala.util.Try

trait BinarySerializerOps {
final def getOutput: Ser[BinaryOutput] = ZPure.access(_.output)
final def getOutputTypeRegistry: Ser[TypeRegistry] = ZPure.access(_.typeRegistry)
final def getOutput: Ser[BinaryOutput] = ZPure.serviceWith(_.output)
final def getOutputTypeRegistry: Ser[TypeRegistry] = ZPure.serviceWith(_.typeRegistry)
final def getSerializerState: Ser[SerializerState] = ZPure.get
final def setSerializerState(state: SerializerState): Ser[Unit] = ZPure.set(state)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ class GenericBinaryCodec(evolutionSteps: Vector[Evolution]) extends GenericDeriv
)
finalState <- Ser.fromEither(
serializer.value.serialize(taggedTransients.tag(genericValue))
.provide(chunkedOutput)
.provideService(chunkedOutput)
.getState
.map(_._1)
.either
Expand All @@ -635,7 +635,7 @@ class GenericBinaryCodec(evolutionSteps: Vector[Evolution]) extends GenericDeriv
readConstructorName = None,
transientFields)
result <- Deser.fromEither(deserializer.value.deserialize()
.provide(chunkedInput)
.provideService(chunkedInput)
.getState
.either
.runResult(initialState))
Expand Down Expand Up @@ -850,7 +850,7 @@ object GenericBinaryCodec {
type ChunkedSer[T] = ZPure[Nothing, ChunkedSerState, ChunkedSerState, ChunkedOutput, DesertFailure, T]

object ChunkedSerOps {
final def getChunkedOutput: ChunkedSer[ChunkedOutput] = ZPure.environment
final def getChunkedOutput: ChunkedSer[ChunkedOutput] = ZPure.service[ChunkedSerState, ChunkedOutput]
final def getChunkedState: ChunkedSer[ChunkedSerState] = ZPure.get
final def setChunkedState(newState: ChunkedSerState): ChunkedSer[Unit] = ZPure.set(newState)

Expand All @@ -863,7 +863,7 @@ object GenericBinaryCodec {
for {
chunkedState <- getChunkedState
runResult <- fromEither(value
.provide(SerializationEnv(output, chunkedState.typeRegistry))
.provideService(SerializationEnv(output, chunkedState.typeRegistry))
.getState
.either
.runResult(chunkedState.serializerState))
Expand All @@ -876,15 +876,15 @@ object GenericBinaryCodec {
state.lastIndexPerChunk.get(chunk) match {
case Some(lastIndex) =>
val newIndex: Byte = (lastIndex + 1).toByte
(state.copy(
((), state.copy(
lastIndexPerChunk = state.lastIndexPerChunk.updated(chunk, newIndex),
fieldIndices = state.fieldIndices + (fieldName -> FieldPosition(chunk, newIndex))
), ())
))
case None =>
(state.copy(
((), state.copy(
lastIndexPerChunk = state.lastIndexPerChunk + (chunk -> 0),
fieldIndices = state.fieldIndices + (fieldName -> FieldPosition(chunk, 0))
), ())
))
}
}
}
Expand Down Expand Up @@ -914,7 +914,7 @@ object GenericBinaryCodec {
type ChunkedDeser[T] = ZPure[Nothing, ChunkedSerState, ChunkedSerState, ChunkedInput, DesertFailure, T]

object ChunkedDeserOps {
final def getChunkedInput: ChunkedDeser[ChunkedInput] = ZPure.environment
final def getChunkedInput: ChunkedDeser[ChunkedInput] = ZPure.service
final def getChunkedState: ChunkedDeser[ChunkedSerState] = ZPure.get
final def setChunkedState(newState: ChunkedSerState): ChunkedDeser[Unit] = ZPure.set(newState)

Expand All @@ -926,7 +926,7 @@ object GenericBinaryCodec {
for {
chunkedState <- getChunkedState
runResult <- fromEither(value
.provide(DeserializationEnv(input, chunkedState.typeRegistry))
.provideService(DeserializationEnv(input, chunkedState.typeRegistry))
.getState
.either
.runResult(chunkedState.serializerState))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ object codecs {
// ZIO prelude specific codecs

implicit def nonEmptyChunkCodec[A: BinaryCodec]: BinaryCodec[NonEmptyChunk[A]] = {
val inner = iterableCodec[A, Chunk[A]]
val inner = iterableCodec[A, Chunk[A]](BinaryCodec[A], Chunk.iterableFactory)
BinaryCodec.from(
inner.contramap(_.toChunk),
() => inner.deserialize().flatMap { chunk =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ import io.github.vigoo.desert.codecs._
import zio.NonEmptyChunk
import zio.prelude.{NonEmptyList, Validation, ZSet}
import zio.test._
import zio.test.environment.TestEnvironment

import scala.util.{Failure, Success, Try}

object CollectionSerializationSpec extends DefaultRunnableSpec with SerializationProperties {
object CollectionSerializationSpec extends ZIOSpecDefault with SerializationProperties {
override def spec: ZSpec[TestEnvironment, Any] =
suite("Collections can be serialized and read back")(
testM("array of strings")(canBeSerialized(Gen.listOf(Gen.anyString).map(_.toArray))),
testM("array of ints")(canBeSerialized(Gen.listOf(Gen.anyInt).map(_.toArray))),
testM("list of strings")(canBeSerialized(Gen.listOf(Gen.anyString))),
testM("vector of strings")(canBeSerialized(Gen.vectorOf(Gen.anyString))),
testM("set of strings")(canBeSerialized(Gen.setOf(Gen.anyString))),
testM("string -> int map")(canBeSerialized(Gen.mapOf(Gen.anyString, Gen.anyInt))),
testM("option")(canBeSerialized(Gen.option(Gen.anyString))),
testM("either")(canBeSerialized(Gen.either(Gen.anyInt, Gen.anyString))),
testM("validation")(canBeSerialized(Gen.either(Gen.listOf1(Gen.anyInt).map(l => NonEmptyChunk.fromIterable(l.head, l.tail)), Gen.anyString).map(Validation.fromEither))),
testM("non-empty list")(canBeSerialized(Gen.listOf1(Gen.anyString).map(l => NonEmptyList.fromIterable(l.head, l.tail)))),
testM("zset")(canBeSerialized(Gen.mapOf(Gen.anyString, Gen.anyInt).map(ZSet.fromMap))),
test("array of strings")(canBeSerialized(Gen.listOf(Gen.string).map(_.toArray))),
test("array of ints")(canBeSerialized(Gen.listOf(Gen.int).map(_.toArray))),
test("list of strings")(canBeSerialized(Gen.listOf(Gen.string))),
test("vector of strings")(canBeSerialized(Gen.vectorOf(Gen.string))),
test("set of strings")(canBeSerialized(Gen.setOf(Gen.string))),
test("string -> int map")(canBeSerialized(Gen.mapOf(Gen.string, Gen.int))),
test("option")(canBeSerialized(Gen.option(Gen.string))),
test("either")(canBeSerialized(Gen.either(Gen.int, Gen.string))),
test("validation")(canBeSerialized(Gen.either(Gen.listOf1(Gen.int).map(l => NonEmptyChunk.fromIterable(l.head, l.tail)), Gen.string).map(Validation.fromEither))),
test("non-empty list")(canBeSerialized(Gen.listOf1(Gen.string).map(l => NonEmptyList.fromIterable(l.head, l.tail)))),
test("zset")(canBeSerialized(Gen.mapOf(Gen.string, Gen.int).map(ZSet.fromMap))),

testM("try")(canBeSerialized(Gen.either(Gen.throwable, Gen.anyString).map(_.toTry), Some({ source: Try[String] =>
test("try")(canBeSerialized(Gen.either(Gen.throwable, Gen.string).map(_.toTry), Some({ source: Try[String] =>
import Assertion._

source match {
Expand Down
Loading

0 comments on commit bf9239e

Please sign in to comment.