Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the codec for big decimal (#663) #687

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading