Skip to content

Commit

Permalink
If available, use annotation default values over schema default values (
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Jan 12, 2024
1 parent 830017e commit 477f991
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ _ZIO Schema_ is used by a growing number of ZIO libraries, including [ZIO Flow](
In order to use this library, we need to add the following lines in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-schema" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-avro" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-bson" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-json" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-msg-pack" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-protobuf" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-thrift" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-zio-test" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema" % "0.4.17"
libraryDependencies += "dev.zio" %% "zio-schema-avro" % "0.4.17"
libraryDependencies += "dev.zio" %% "zio-schema-bson" % "0.4.17"
libraryDependencies += "dev.zio" %% "zio-schema-json" % "0.4.17"
libraryDependencies += "dev.zio" %% "zio-schema-msg-pack" % "0.4.17"
libraryDependencies += "dev.zio" %% "zio-schema-protobuf" % "0.4.17"
libraryDependencies += "dev.zio" %% "zio-schema-thrift" % "0.4.17"
libraryDependencies += "dev.zio" %% "zio-schema-zio-test" % "0.4.17"

// Required for the automatic generic derivation of schemas
libraryDependencies += "dev.zio" %% "zio-schema-derivation" % "0.4.16"
libraryDependencies += "dev.zio" %% "zio-schema-derivation" % "0.4.17"
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value % "provided"
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ object DefaultValueSpec extends ZIOSpecDefault {
(s: Status) => s.isInstanceOf[Pending.type]
)
)
assert(schema.defaultValue)(isRight(equalTo(Failed(0, "", None, ""))))
assert(schema.defaultValue)(isRight(equalTo(Failed(0, "", None, "oops"))))
}
),
suite("EitherSchema")(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ import zio.{ Chunk, Scope }
.fields(0)
.annotations
assertTrue(
annotations
.exists(a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Int]].value == 42)
annotations.exists { a =>
a.isInstanceOf[fieldDefaultValue[_]] &&
a.asInstanceOf[fieldDefaultValue[Int]].value == 42
},
capturedSchema.schema.defaultValue == Right(RecordWithDefaultValue(42, 52))
)
},
test("use case class default values of generic class") {
Expand All @@ -180,12 +183,13 @@ import zio.{ Chunk, Scope }
.asInstanceOf[Schema.Record[GenericRecordWithDefaultValue[Int]]]
.fields(0)
.annotations
assertTrue {
assertTrue(
annotations.exists { a =>
a.isInstanceOf[fieldDefaultValue[_]] &&
a.asInstanceOf[fieldDefaultValue[Option[Int]]].value == None
}
}
},
capturedSchema.schema.defaultValue == Right(GenericRecordWithDefaultValue[Int](None, 52))
)
},
test("prefer field annotations over case class default values") {
val capturedSchema = Derive.derive[CapturedSchema, RecordWithDefaultValue](schemaCapturer)
Expand All @@ -195,7 +199,8 @@ import zio.{ Chunk, Scope }
.annotations
assertTrue(
annotations
.exists(a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Int]].value == 52)
.exists(a => a.isInstanceOf[fieldDefaultValue[_]] && a.asInstanceOf[fieldDefaultValue[Int]].value == 52),
capturedSchema.schema.defaultValue == Right(RecordWithDefaultValue(42, 52))
)
}
),
Expand Down
10 changes: 7 additions & 3 deletions zio-schema/shared/src/main/scala/zio/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.time.temporal.ChronoUnit
import scala.annotation.tailrec
import scala.collection.immutable.ListMap

import zio.schema.annotation.fieldDefaultValue
import zio.schema.internal.SourceLocation
import zio.schema.meta._
import zio.schema.validation._
Expand Down Expand Up @@ -424,9 +425,12 @@ object Schema extends SchemaEquality {

def defaultValue: scala.util.Either[String, R] =
Unsafe.unsafe { implicit unsafe =>
self.fields
.map(_.schema.defaultValue)
.foldLeft[scala.util.Either[String, Chunk[R]]](Right(Chunk.empty)) {
self.fields.map { field =>
field.annotations.collectFirst { case fieldDefaultValue(value) => value } match {
case Some(value) => Right(value)
case None => field.schema.defaultValue
}
}.foldLeft[scala.util.Either[String, Chunk[R]]](Right(Chunk.empty)) {
case (e @ Left(_), _) => e
case (_, Left(e)) => Left[String, Chunk[R]](e)
case (Right(values), Right(value)) => Right[String, Chunk[R]](values :+ value.asInstanceOf[R])
Expand Down

0 comments on commit 477f991

Please sign in to comment.