Skip to content

Commit

Permalink
sphere-mongo-core, sphere-util compiles with both Scala 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
benko-ct committed Feb 22, 2024
1 parent ff4171a commit 6aab3f9
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ trait DefaultMongoFormats {

implicit def optionFormat[@specialized A](implicit f: MongoFormat[A]): MongoFormat[Option[A]] =
new MongoFormat[Option[A]] {
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
override def toMongoValue(a: Option[A]) = a match {
case Some(aa) => f.toMongoValue(aa)
case None => MongoNothing
Expand Down Expand Up @@ -78,7 +78,7 @@ trait DefaultMongoFormats {

implicit def vecFormat[@specialized A](implicit f: MongoFormat[A]): MongoFormat[Vector[A]] =
new MongoFormat[Vector[A]] {
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
override def toMongoValue(a: Vector[A]) = {
val m = new BasicBSONList()
if (a.nonEmpty) m.addAll(a.map(f.toMongoValue(_).asInstanceOf[AnyRef]).asJavaCollection)
Expand All @@ -103,7 +103,7 @@ trait DefaultMongoFormats {

implicit def listFormat[@specialized A](implicit f: MongoFormat[A]): MongoFormat[List[A]] =
new MongoFormat[List[A]] {
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
override def toMongoValue(a: List[A]) = {
val m = new BasicBSONList()
if (a.nonEmpty) m.addAll(a.map(f.toMongoValue(_).asInstanceOf[AnyRef]).asJavaCollection)
Expand All @@ -128,7 +128,7 @@ trait DefaultMongoFormats {

implicit def setFormat[@specialized A](implicit f: MongoFormat[A]): MongoFormat[Set[A]] =
new MongoFormat[Set[A]] {
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
override def toMongoValue(a: Set[A]) = {
val m = new BasicBSONList()
if (a.nonEmpty) m.addAll(a.map(f.toMongoValue(_).asInstanceOf[AnyRef]).asJavaCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.bson.BSONObject
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

class BaseMoneyMongoFormatTest extends AnyWordSpec with Matchers {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.scalatest.matchers.must.Matchers
import org.scalatest.wordspec.AnyWordSpec
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

object DefaultMongoFormatsTest {
case class User(name: String)
Expand Down Expand Up @@ -126,7 +126,7 @@ class DefaultMongoFormatsTest
}

"support Java Locale" in {
Locale.getAvailableLocales.filter(_.toLanguageTag != LangTag.UNDEFINED).foreach { l: Locale =>
Locale.getAvailableLocales.filter(_.toLanguageTag != LangTag.UNDEFINED).foreach { l =>
localeFormat.fromMongoValue(localeFormat.toMongoValue(l)).toLanguageTag must be(
l.toLanguageTag)
}
Expand Down
2 changes: 1 addition & 1 deletion util/dependencies.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ libraryDependencies ++= Seq(
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.4",
"joda-time" % "joda-time" % "2.12.7",
"org.joda" % "joda-convert" % "2.2.3",
"org.typelevel" %% "cats-core" % "2.10.0",
("org.typelevel" % "cats-core" % "2.10.0").cross(CrossVersion.binary),
"org.json4s" %% "json4s-scalap" % "4.0.7"
)
7 changes: 4 additions & 3 deletions util/src/main/scala/Money.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ object BaseMoney {
def requireSameCurrency(m1: BaseMoney, m2: BaseMoney): Unit =
require(m1.currency eq m2.currency, s"${m1.currency} != ${m2.currency}")

def toScalaRoundingMode(mode: java.math.RoundingMode): RoundingMode =
BigDecimal.RoundingMode(mode.ordinal())
def toScalaRoundingMode(mode: java.math.RoundingMode): RoundingMode.Value =
BigDecimal.RoundingMode(mode.ordinal)

implicit def baseMoneyMonoid(implicit c: Currency, mode: RoundingMode): Monoid[BaseMoney] =
new Monoid[BaseMoney] {
Expand All @@ -90,7 +90,7 @@ object BaseMoney {
* @param currency
* The currency of the amount.
*/
case class Money private (centAmount: Long, currency: Currency)
case class Money private[util] (centAmount: Long, currency: Currency)
extends BaseMoney
with Ordered[Money] {
import Money._
Expand Down Expand Up @@ -263,6 +263,7 @@ object Money {
}

def apply(amount: BigDecimal, currency: Currency): Money = {
println("this is called")
require(
amount.scale == currency.getDefaultFractionDigits,
"The scale of the given amount does not match the scale of the provided currency." +
Expand Down
2 changes: 1 addition & 1 deletion util/src/test/scala/DomainObjectsGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.util.Currency

import org.scalacheck.Gen

import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

object DomainObjectsGen {

Expand Down
3 changes: 2 additions & 1 deletion util/src/test/scala/HighPrecisionMoneySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import org.scalatest.matchers.must.Matchers

import scala.collection.mutable.ArrayBuffer
import scala.language.postfixOps
import scala.math.BigDecimal

class HighPrecisionMoneySpec extends AnyFunSpec with Matchers with ScalaCheckDrivenPropertyChecks {
import HighPrecisionMoney.ImplicitsString._
import HighPrecisionMoney.ImplicitsStringPrecise._

implicit val defaultRoundingMode = BigDecimal.RoundingMode.HALF_EVEN
implicit val defaultRoundingMode: BigDecimal.RoundingMode.Value = BigDecimal.RoundingMode.HALF_EVEN

val Euro: Currency = Currency.getInstance("EUR")

Expand Down
3 changes: 2 additions & 1 deletion util/src/test/scala/MoneySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import org.scalatest.matchers.must.Matchers
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks

import scala.language.postfixOps
import scala.math.BigDecimal

class MoneySpec extends AnyFunSpec with Matchers with ScalaCheckDrivenPropertyChecks {
import Money.ImplicitsDecimal._
import Money._

implicit val mode = BigDecimal.RoundingMode.UNNECESSARY
implicit val mode: BigDecimal.RoundingMode.Value = BigDecimal.RoundingMode.UNNECESSARY

def euroCents(cents: Long): Money = EUR(0).withCentAmount(cents)

Expand Down

0 comments on commit 6aab3f9

Please sign in to comment.