Skip to content

Commit

Permalink
fixing broken default currency test
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrapyre committed May 10, 2024
1 parent 1cb746a commit 76e7b6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ object DefaultValueSpec extends ZIOSpecDefault {
)
},
test("Currency default value") {
java.util.Locale
.setDefault(java.util.Locale.US) //This is a workaround for the default locale not being set in the JVM in Github actions as of May, 2024.
assert(Primitive(StandardType.CurrencyType).defaultValue)(
isRight(isSubtype[java.util.Currency](anything))
)
Expand Down
16 changes: 13 additions & 3 deletions zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,20 @@ object StandardType {
override def tag: String = Tags.CURRENCY
override def defaultValue: Either[String, java.util.Currency] =
try {
Right(java.util.Currency.getInstance(java.util.Locale.getDefault()))
val currency = java.util.Currency.getInstance(java.util.Locale.getDefault())
if (currency == null) {
Left(
"Could not get default currency. In most cases, this is because a currency could not be determined from the provided locale (e.g. when the locale is set to Antarctica). Please inspect the default locale to ensure that it is properly set and try again."
)
} else {
Right(currency)
}

} catch {
case _: NullPointerException => throw new Exception("Could not get default currency. Default locale was null.")
case ex: Throwable => throw new Exception(s"Could not get default currency. ${ex.getMessage}")
case ex: Throwable =>
Left(
s"Could not get default currency. In most cases, this is because the default locale was not set on the JVM. If this is the case, running java.util.Locale.setDefault() before getting the default currency should fix this. Error message: ${ex.getMessage}."
)
}

override def compare(x: java.util.Currency, y: java.util.Currency): Int =
Expand Down

0 comments on commit 76e7b6c

Please sign in to comment.