Skip to content

Commit

Permalink
Merge branch 'main' into zio-sbt-website/update-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegoes authored Jun 12, 2024
2 parents d1852b3 + 3feff56 commit 87b7c32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ object BsonSchemaCodec {
case StandardType.BinaryType => BsonCodec.byteIterable[Chunk].asInstanceOf[BsonCodec[A]]
case StandardType.CharType => BsonCodec.char.asInstanceOf[BsonCodec[A]]
case StandardType.BigIntegerType => BsonCodec.bigInteger.asInstanceOf[BsonCodec[A]]
case StandardType.BigDecimalType => BsonCodec.bigDecimal.asInstanceOf[BsonCodec[A]]
case StandardType.BigDecimalType => BsonCodec.javaBigDecimal.asInstanceOf[BsonCodec[A]]
case StandardType.UUIDType => BsonCodec.uuid.asInstanceOf[BsonCodec[A]]
case StandardType.DayOfWeekType =>
BsonCodec.dayOfWeek.asInstanceOf[BsonCodec[A]] // BsonCodec[java.time.DayOfWeek]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package zio.schema.codec

import scala.reflect.{ ClassTag, classTag }

import org.bson._
import org.bson.codecs.configuration.CodecRegistry
import org.bson.codecs.{ Codec => BCodec, DecoderContext, EncoderContext }
import org.bson.conversions.Bson
import org.bson.io.BasicOutputBuffer
import org.bson.types.ObjectId
import org.bson.types.{ Decimal128, ObjectId }
import org.bson.{ BsonDecimal128, _ }

import zio.bson.BsonBuilder._
import zio.bson._
Expand All @@ -24,6 +24,13 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault {
implicit lazy val codec: BsonCodec[SimpleClass] = BsonSchemaCodec.bsonCodec(schema)
}

case class BigDecimalClass(value: BigDecimal)

object BigDecimalClass {
implicit val schema: Schema[BigDecimalClass] = DeriveSchema.gen
implicit lazy val codec: BsonCodec[BigDecimalClass] = BsonSchemaCodec.bsonCodec(schema)
}

sealed trait Tree

object Tree {
Expand Down Expand Up @@ -79,6 +86,10 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault {
} yield Customer(id, name, age, friends)
}

// Custom generator for BigDecimal values with rounding to ensure exact representation as Decimal128
def genRoundedBigDecimal(scale: Int): Gen[Any, BigDecimal] =
Gen.double.map(d => BigDecimal(d).setScale(scale, BigDecimal.RoundingMode.HALF_UP))

def spec: Spec[TestEnvironment with Scope, Any] = suite("BsonSchemaCodecSpec")(
suite("round trip")(
roundTripTest("SimpleClass")(
Expand Down Expand Up @@ -107,6 +118,12 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault {
Customer.example.invitedFriends.map(_.value.toBsonValue): _*
)
)
),
roundTripTest("BigDecimalClass")(
// 14 decimal places in the assert value below
genRoundedBigDecimal(14).map(BigDecimalClass(_)),
BigDecimalClass(BigDecimal("279.00000000000000")),
doc("value" -> new BsonDecimal128(Decimal128.parse("279.00000000000000")))
)
),
suite("configuration")(
Expand Down

0 comments on commit 87b7c32

Please sign in to comment.