All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ciris.decoders.PrimitiveConfigDecoders.scala Maven / Gradle / Ivy

There is a newer version: 0.12.1
Show newest version
package ciris.decoders

import ciris.ConfigDecoder

trait PrimitiveConfigDecoders {
  implicit val booleanConfigDecoder: ConfigDecoder[String, Boolean] =
    ConfigDecoder.catchNonFatal("Boolean") { s =>
      s.toLowerCase match {
        case "yes" | "on" => true
        case "no" | "off" => false
        case sLowerCase   => sLowerCase.toBoolean
      }
    }

  implicit val byteConfigDecoder: ConfigDecoder[String, Byte] =
    ConfigDecoder.catchNonFatal("Byte")(_.toByte)

  implicit val charConfigDecoder: ConfigDecoder[String, Char] =
    ConfigDecoder.fromOption("Char") { value =>
      if (value.length == 1) Some(value.head) else None
    }

  implicit val doubleConfigDecoder: ConfigDecoder[String, Double] =
    ConfigDecoder.catchNonFatal("Double") {
      case s if s.lastOption.exists(_ == '%') => s.init.toDouble / 100d
      case s => s.toDouble
    }

  implicit val floatConfigDecoder: ConfigDecoder[String, Float] =
    ConfigDecoder.catchNonFatal("Float") {
      case s if s.lastOption.exists(_ == '%') => s.init.toFloat / 100f
      case s => s.toFloat
    }

  implicit val intConfigDecoder: ConfigDecoder[String, Int] =
    ConfigDecoder.catchNonFatal("Int")(_.toInt)

  implicit val longConfigDecoder: ConfigDecoder[String, Long] =
    ConfigDecoder.catchNonFatal("Long")(_.toLong)

  implicit val shortConfigDecoder: ConfigDecoder[String, Short] =
    ConfigDecoder.catchNonFatal("Short")(_.toShort)

  implicit def identityConfigDecoder[A]: ConfigDecoder[A, A] =
    ConfigDecoder.identity
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy