Skip to content

Commit

Permalink
use scala 2.13 syntax for cross-compile
Browse files Browse the repository at this point in the history
  • Loading branch information
lbialy committed Jul 16, 2024
1 parent cc1dd26 commit 5eed294
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions core/shared/src/main/scala/org/virtuslab/yaml/YamlDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ object YamlDecoder extends YamlDecoderCompanionCrossCompat {

implicit def forInt: YamlDecoder[Int] = YamlDecoder { case s @ ScalarNode(value, _) =>
val normalizedValue =
if value.startsWith("0o") then value.stripPrefix("0o").prepended('0') else value
if (value.startsWith("0o")) value.stripPrefix("0o").prepended('0') else value

Try(java.lang.Integer.decode(normalizedValue.replaceAll("_", "")).toInt).toEither.left
.map(ConstructError.from(_, "Int", s))
}

implicit def forLong: YamlDecoder[Long] = YamlDecoder { case s @ ScalarNode(value, _) =>
val normalizedValue =
if value.startsWith("0o") then value.stripPrefix("0o").prepended('0') else value
if (value.startsWith("0o")) value.stripPrefix("0o").prepended('0') else value

Try(java.lang.Long.decode(normalizedValue.replaceAll("_", "")).toLong).toEither.left
.map(ConstructError.from(_, "Long", s))
Expand All @@ -105,40 +105,44 @@ object YamlDecoder extends YamlDecoderCompanionCrossCompat {
private val nanRegex = """(\.nan|\.NaN|\.NAN)""".r

implicit def forDouble: YamlDecoder[Double] = YamlDecoder { case s @ ScalarNode(value, _) =>
infinityRegex.findFirstMatchIn(value) match
infinityRegex.findFirstMatchIn(value) match {
case Some(m) =>
Right(m.group(1) match
Right(m.group(1) match {
case "-" => Double.NegativeInfinity
case _ => Double.PositiveInfinity
)
})
case None =>
nanRegex.findFirstMatchIn(value) match
nanRegex.findFirstMatchIn(value) match {
case Some(_) =>
Right(Double.NaN)
case None =>
Try(java.lang.Double.parseDouble(value.replaceAll("_", ""))).toEither.left
.map(ConstructError.from(_, "Double", s))
}
}
}

implicit def forFloat: YamlDecoder[Float] = YamlDecoder { case s @ ScalarNode(value, _) =>
infinityRegex.findFirstMatchIn(value) match
infinityRegex.findFirstMatchIn(value) match {
case Some(m) =>
Right(m.group(1) match
Right(m.group(1) match {
case "-" => Float.NegativeInfinity
case _ => Float.PositiveInfinity
)
})
case None =>
nanRegex.findFirstMatchIn(value) match
nanRegex.findFirstMatchIn(value) match {
case Some(_) =>
Right(Float.NaN)
case None =>
Try(java.lang.Float.parseFloat(value.replaceAll("_", ""))).toEither.left
.map(ConstructError.from(_, "Float", s))
}
}
}

implicit def forShort: YamlDecoder[Short] = YamlDecoder { case s @ ScalarNode(value, _) =>
val normalizedValue =
if value.startsWith("0o") then value.stripPrefix("0o").prepended('0') else value
if (value.startsWith("0o")) value.stripPrefix("0o").prepended('0') else value

Try(java.lang.Short.decode(normalizedValue.replaceAll("_", "")).toShort).toEither.left
.map(ConstructError.from(_, "Short", s))
Expand Down

0 comments on commit 5eed294

Please sign in to comment.