Skip to content

Commit

Permalink
Upgrade to ZIO 2.0.0-RC6 (#245)
Browse files Browse the repository at this point in the history
* Upgrade to ZIO 2.0.0-RC6

* Convert error to string

* Fix schema derivation test

* Migrate test

* Switch to ZPipeline.utf8Decode

* Fix JSON decode

* fix and format

Co-authored-by: Daniel Vigovszky <[email protected]>
  • Loading branch information
denisgarci and vigoo authored May 25, 2022
1 parent 09f39e6 commit d92d5f7
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 74 deletions.
8 changes: 4 additions & 4 deletions project/BuildHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ object BuildHelper {
val Scala213: String = versions("2.13")
val ScalaDotty: String = "3.1.0" //versions.getOrElse("3.0", versions("3.1"))

val zioVersion = "2.0.0-RC5"
val zioJsonVersion = "0.3.0-RC7"
val zioPreludeVersion = "1.0.0-RC13"
val zioOpticsVersion = "2.0.0-RC4"
val zioVersion = "2.0.0-RC6"
val zioJsonVersion = "0.3.0-RC8"
val zioPreludeVersion = "1.0.0-RC14"
val zioOpticsVersion = "0.2.0-RC4"
val silencerVersion = "1.7.8"

private val testDeps = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object AccessorBuilderSpec extends ZIOSpecDefault {

private val builder: TestAccessorBuilder = new TestAccessorBuilder

override def spec: ZSpec[Environment, Any] = suite("AccessorBuilder")(
override def spec: Spec[Environment, Any] = suite("AccessorBuilder")(
test("fail") {
assert(Schema.fail("error").makeAccessors(builder).asInstanceOf[Unit])(isUnit)
},
Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import zio.Chunk
import zio.schema.CaseSet.caseOf
import zio.schema.Schema.{ Lazy, Primitive }
import zio.test.Assertion._
import zio.test.{ ZIOSpecDefault, ZSpec, assert }
import zio.test.{ Spec, ZIOSpecDefault, assert }

object DefaultValueSpec extends ZIOSpecDefault {
// Record Tests
Expand All @@ -32,7 +32,7 @@ object DefaultValueSpec extends ZIOSpecDefault {
implicit lazy val schema: Schema[Status] = DeriveSchema.gen[Status]
}

def spec: ZSpec[Environment, Any] = suite("Default Value Spec")(
def spec: Spec[Environment, Any] = suite("Default Value Spec")(
suite("Primitive")(
test("UnitType default value") {
assert(Primitive(StandardType.UnitType).defaultValue)(isRight(equalTo(())))
Expand Down
2 changes: 1 addition & 1 deletion tests/shared/src/test/scala/zio/schema/DiffSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import zio.{ Chunk, URIO, ZIO }

object DiffSpec extends ZIOSpecDefault with DefaultJavaTimeSchemas {

def spec: ZSpec[Environment, Any] = suite("DiffSpec")(
def spec: Spec[Environment, Any] = suite("DiffSpec")(
suite("identity law")(
suite("standard types")(
test("Int")(diffIdentityLaw[Int]),
Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/zio/schema/DynamicValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio.test.{ Sized, TestConfig, _ }

object DynamicValueSpec extends ZIOSpecDefault {

def spec: ZSpec[Environment, Any] =
def spec: Spec[Environment, Any] =
suite("DynamicValueSpec")(
suite("Primitives")(primitiveTests: _*),
test("round-trips Records") {
Expand Down Expand Up @@ -85,7 +85,7 @@ object DynamicValueSpec extends ZIOSpecDefault {
}
)

val primitiveTests: List[ZSpec[Sized with TestConfig, Nothing]] = schemasAndGens.map {
val primitiveTests: List[Spec[Sized with TestConfig, Nothing]] = schemasAndGens.map {
case SchemaTest(name, standardType, gen) =>
test(s"round-trips $name") {
dynamicValueLaw(gen, Primitive(standardType, Chunk.empty))
Expand Down
7 changes: 3 additions & 4 deletions tests/shared/src/test/scala/zio/schema/MigrationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import scala.collection.immutable.ListMap
import zio._
import zio.schema.ast._
import zio.schema.syntax._
import zio.test.AssertionM.Render.param
import zio.test._

object MigrationSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Any] = suite("Migration Spec")(
override def spec: Spec[Environment, Any] = suite("Migration Spec")(
suite("Derivation")(
suite("Value")(
test("change type") {
Expand Down Expand Up @@ -257,13 +256,13 @@ object MigrationSpec extends ZIOSpecDefault {
.getOrElse(false)

def transformsValueTo[A: Schema](value: A, expected: DynamicValue): Assertion[Migration] =
Assertion.assertion("transformsValueTo")(param(value), param(expected)) { transform =>
Assertion.assertion("transformsValueTo") { transform =>
val transformed = transform.migrate(value.dynamic)
transformed == Right(expected)
}

def failsToTransform[A: Schema](value: A): Assertion[Migration] =
Assertion.assertion("failsToTransform")(param(value)) { transform =>
Assertion.assertion("failsToTransform") { transform =>
transform.migrate(value.dynamic).isLeft
}

Expand Down
6 changes: 3 additions & 3 deletions tests/shared/src/test/scala/zio/schema/OrderingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import zio.{ Chunk, URIO }

object OrderingSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Any] =
override def spec: Spec[Environment, Any] =
suite("schemas should generate correct orderings")(
suite("primitives")(primitiveOrderingTests: _*),
suite("structures")(structureTestCases.map(structureOrderingTest): _*),
Expand Down Expand Up @@ -56,7 +56,7 @@ object OrderingSpec extends ZIOSpecDefault {
)
)

val primitiveOrderingTests: List[ZSpec[Sized with TestConfig, Nothing]] =
val primitiveOrderingTests: List[Spec[Sized with TestConfig, Nothing]] =
schemasAndGens.map {
case SchemaTest(name, schema, gen) =>
test(s"$name") {
Expand Down Expand Up @@ -89,7 +89,7 @@ object OrderingSpec extends ZIOSpecDefault {
StructureTestCase("enumN", genAnyOrderedPairEnum)
)

def structureOrderingTest(t: StructureTestCase): ZSpec[TestConfig with Sized, Nothing] =
def structureOrderingTest(t: StructureTestCase): Spec[TestConfig with Sized, Nothing] =
test(t.name)(check(t.increasingPairGen)(_ match {
case (schema, l, r) =>
assert(schema.ordering.compare(l, r))(isLessThan(0))
Expand Down
2 changes: 1 addition & 1 deletion tests/shared/src/test/scala/zio/schema/SchemaAstSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio.test._

object SchemaAstSpec extends ZIOSpecDefault {

def spec: ZSpec[Environment, Any] = suite("SchemaAst")(
def spec: Spec[Environment, Any] = suite("SchemaAst")(
suite("from schema")(
test("primitive") {
check(SchemaGen.anyPrimitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio.test._
object SchemaMigrationSpec extends ZIOSpecDefault {
import SchemaAssertions._

override def spec: ZSpec[TestEnvironment, Any] = suite("Schema Migration Spec")(
override def spec: Spec[TestEnvironment, Any] = suite("Schema Migration Spec")(
suite("case class")(
suite("isomorphisms")(isomorphismTests: _*),
test("delete field recursively") {
Expand Down Expand Up @@ -87,7 +87,7 @@ object SchemaMigrationSpec extends ZIOSpecDefault {
)
)

val isomorphismTests: List[ZSpec[TestEnvironment with Sized with TestConfig, Nothing]] =
val isomorphismTests: List[Spec[TestEnvironment with Sized with TestConfig, Nothing]] =
List(
test("DogFood <-> CatFood")(
isomorphismLaw[TestEnvironment, PetFood.DogFood, PetFood.CatFood](
Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/zio/schema/SchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import scala.collection.immutable.ListMap
import zio.Chunk
import zio.schema.CaseSet._
import zio.test.Assertion._
import zio.test.{ ZSpec, _ }
import zio.test._

object SchemaSpec extends ZIOSpecDefault {

def spec: ZSpec[Environment, Any] = suite("Schema Spec")(
def spec: Spec[Environment, Any] = suite("Schema Spec")(
suite("Should have valid equals")(
test("primitive") {
assert(schemaUnit)(equalTo(schemaUnit))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import zio.test._

object NodePathSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Any] = suite("NodePath")(
override def spec: Spec[Environment, Any] = suite("NodePath")(
suite("relativeTo")(
test("compute relative subpath") {
val path = NodePath.root / "foo" / "bar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ object DeriveSchemaSpec extends ZIOSpecDefault {

}

override def spec: ZSpec[Environment, Any] = suite("DeriveSchemaSpec")(
override def spec: Spec[Environment, Any] = suite("DeriveSchemaSpec")(
suite("Derivation")(
test("correctly derives case class") {
assert(Schema[User].toString)(not(containsString("null")) && not(equalTo("$Lazy$")))
Expand Down Expand Up @@ -320,10 +320,12 @@ object DeriveSchemaSpec extends ZIOSpecDefault {
assert(b0)(isRight(equalTo(b)))
},
test("correctly derives recursive Enum with type parameters") {
assert(DeriveSchema.gen[Tree[Recursive]])(anything)
val derived: Schema[Tree[Recursive]] = DeriveSchema.gen[Tree[Recursive]]
assert(derived)(anything)
},
test("correctly derives recursive Enum with multiple type parameters") {
assert(DeriveSchema.gen[RBTree[String, Int]])(anything)
val derived: Schema[RBTree[String, Int]] = DeriveSchema.gen[RBTree[String, Int]]
assert(derived)(anything)
},
test("correctly derives recursive Enum") {
assert(Schema[RecursiveEnum].toString)(not(containsString("null")) && not(equalTo("$Lazy$")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import zio.schema.Schema.EitherSchema
import zio.schema.ast.SchemaAst
import zio.schema.{ StandardType, _ }
import zio.stream.ZPipeline
import zio.{ Chunk, ChunkBuilder, ZIO }
import zio.{ Chunk, ChunkBuilder, NonEmptyChunk, ZIO }

object JsonCodec extends Codec {

Expand All @@ -23,9 +23,13 @@ object JsonCodec extends Codec {
)

override def decoder[A](schema: Schema[A]): ZPipeline[Any, String, Byte, A] =
ZPipeline.utfDecode >>> ZPipeline.mapZIO(
(s: String) => ZIO.fromEither(Decoder.decode(schema, s))
)
ZPipeline.fromChannel(ZPipeline.utfDecode.channel.mapError(_.toString)) >>>
ZPipeline.groupAdjacentBy[String, Unit](_ => ()) >>>
ZPipeline.map[(Unit, NonEmptyChunk[String]), String] {
case (_, fragments) => fragments.mkString
} >>> ZPipeline.mapZIO { (s: String) =>
ZIO.fromEither(Decoder.decode(schema, s))
}

override def encode[A](schema: Schema[A]): A => Chunk[Byte] = Encoder.encode(schema, _)

Expand Down Expand Up @@ -85,7 +89,8 @@ object JsonCodec extends Codec {
object Encoder {

import Codecs._
import JsonEncoder.{ bump, pad }
import JsonEncoder.bump
import JsonEncoder.pad
import ProductEncoder._

private[codec] val CHARSET = StandardCharsets.UTF_8
Expand Down Expand Up @@ -491,7 +496,8 @@ object JsonCodec extends Codec {

//scalafmt: { maxColumn = 400, optIn.configStyleArguments = false }
private[codec] object ProductEncoder {
import JsonEncoder.{ bump, pad }
import JsonEncoder.bump
import JsonEncoder.pad

private[codec] def caseClassEncoder[Z](fields: (Schema.Field[_], Z => Any)*): JsonEncoder[Z] = { (a: Z, indent: Option[Int], out: Write) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import zio.test._

object JsonCodecSpec extends ZIOSpecDefault {

def spec: ZSpec[TestEnvironment, Any] =
def spec: Spec[TestEnvironment, Any] =
suite("JsonCodec Spec")(
encoderSuite,
decoderSuite,
Expand Down Expand Up @@ -681,7 +681,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
.succeed(value)
.via(JsonCodec.encoder(schema))
.runCollect
assertM(stream)(equalTo(chunk))
assertZIO(stream)(equalTo(chunk))
}

private def assertEncodesJson[A](schema: Schema[A], value: A, json: String) = {
Expand All @@ -690,15 +690,15 @@ object JsonCodecSpec extends ZIOSpecDefault {
.via(JsonCodec.encoder(schema))
.runCollect
.map(chunk => new String(chunk.toArray))
assertM(stream)(equalTo(json))
assertZIO(stream)(equalTo(json))
}

private def assertEncodesJson[A](schema: Schema[A], value: A)(implicit enc: JsonEncoder[A]) = {
val stream = ZStream
.succeed(value)
.via(JsonCodec.encoder(schema))
.runCollect
assertM(stream)(equalTo(jsonEncoded(value)))
assertZIO(stream)(equalTo(jsonEncoded(value)))
}

private def assertDecodesToError[A](schema: Schema[A], json: CharSequence, errors: List[JsonError]) = {
Expand All @@ -707,12 +707,12 @@ object JsonCodecSpec extends ZIOSpecDefault {
.via(JsonCodec.decoder(schema))
.catchAll(ZStream.succeed[String](_))
.runHead
assertM(stream)(isSome(equalTo(JsonError.render(errors))))
assertZIO(stream)(isSome(equalTo(JsonError.render(errors))))
}

private def assertDecodes[A](schema: Schema[A], value: A, chunk: Chunk[Byte]) = {
val result = ZStream.fromChunk(chunk).via(JsonCodec.decoder(schema)).runCollect
assertM(result)(equalTo(Chunk(value)))
assertZIO(result)(equalTo(Chunk(value)))
}

private def assertEncodesThenDecodes[A](schema: Schema[A], value: A, print: Boolean = false) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import zio.test._

object LensSpec extends ZIOSpecDefault {

def spec: ZSpec[Environment, Any] = suite("LensSpec")(
def spec: Spec[Environment, Any] = suite("LensSpec")(
suite("constructors")(
test("first")(lensLaws(Gen.int.zip(Gen.int), Gen.int)(Lens.first)),
test("second")(lensLaws(Gen.int.zip(Gen.int), Gen.int)(Lens.second)),
Expand Down
Loading

0 comments on commit d92d5f7

Please sign in to comment.