Skip to content

Commit

Permalink
More useful collection default values (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Jan 6, 2025
1 parent c0a2ec7 commit c49b7bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 8 additions & 2 deletions tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,16 @@ object DefaultValueSpec extends ZIOSpecDefault {
),
suite("Sequence")(
test("chunk") {
assert(Schema.chunk[Int].defaultValue)(isRight(equalTo(Chunk(0))))
assert(Schema.chunk[Int].defaultValue)(isRight(equalTo(Chunk())))
},
test("list") {
assert(Schema.list[Int].defaultValue)(isRight(equalTo(List(0))))
assert(Schema.list[Int].defaultValue)(isRight(equalTo(List())))
},
test("nonEmptyChunk") {
assert(Schema.nonEmptyChunk[Int].defaultValue)(isLeft)
},
test("nonEmptyMap") {
assert(Schema.nonEmptyMap[Int, String].defaultValue)(isLeft)
}
),
suite("Enumeration")(
Expand Down
16 changes: 5 additions & 11 deletions zio-schema/shared/src/main/scala/zio/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,7 @@ object Schema extends SchemaPlatformSpecific with SchemaEquality {
override def annotate(annotation: Any): Sequence[Col, Elem, I] =
copy(annotations = (annotations :+ annotation).distinct)

override def defaultValue: scala.util.Either[String, Col] =
elementSchema.defaultValue.map(fromChunk.compose(Chunk(_)))
override def defaultValue: scala.util.Either[String, Col] = Right(empty)

override def makeAccessors(b: AccessorBuilder): b.Traversal[Col, Elem] = b.makeTraversal(self, elementSchema)

Expand Down Expand Up @@ -830,10 +829,7 @@ object Schema extends SchemaPlatformSpecific with SchemaEquality {
override def annotate(annotation: Any): Map[K, V] = copy(annotations = (annotations :+ annotation).distinct)

override def defaultValue: scala.util.Either[String, scala.collection.immutable.Map[K, V]] =
keySchema.defaultValue.flatMap(
defaultKey =>
valueSchema.defaultValue.map(defaultValue => scala.collection.immutable.Map(defaultKey -> defaultValue))
)
Right(empty)

override val fromChunk: Chunk[(K, V)] => scala.collection.immutable.Map[K, V] = _.toMap

Expand All @@ -858,9 +854,7 @@ object Schema extends SchemaPlatformSpecific with SchemaEquality {
copy(annotations = (annotations :+ annotation).distinct)

override def defaultValue: scala.Either[String, prelude.NonEmptyMap[K, V]] =
keySchema.defaultValue.flatMap(
defaultKey => valueSchema.defaultValue.map(defaultValue => prelude.NonEmptyMap(defaultKey -> defaultValue))
)
Left("NonEmptyMap has no default value")

override def fromChunk: Chunk[(K, V)] => prelude.NonEmptyMap[K, V] =
chunk => fromChunkOption(chunk).getOrElse(throw new IllegalArgumentException("NonEmptyMap cannot be empty"))
Expand Down Expand Up @@ -901,7 +895,7 @@ object Schema extends SchemaPlatformSpecific with SchemaEquality {
copy(annotations = (annotations :+ annotation).distinct)

override def defaultValue: scala.util.Either[String, Col] =
elementSchema.defaultValue.map(fromChunk.compose(Chunk(_)))
Left(s"$identity cannot be empty")

override def makeAccessors(b: AccessorBuilder): b.Traversal[Col, Elm] = b.makeTraversal(self, elementSchema)

Expand All @@ -922,7 +916,7 @@ object Schema extends SchemaPlatformSpecific with SchemaEquality {
copy(annotations = (annotations :+ annotation).distinct)

override def defaultValue: scala.util.Either[String, scala.collection.immutable.Set[A]] =
elementSchema.defaultValue.map(scala.collection.immutable.Set(_))
Right(empty)

override val fromChunk: Chunk[A] => scala.collection.immutable.Set[A] = _.toSet

Expand Down

0 comments on commit c49b7bf

Please sign in to comment.