I use double to encode boolean value in a configuration file. PureConfig does not find a way to cast it while reading the configuration.
Initial question (see below for edit).
Here is some code to reproduce the behavior.
import com.typesafe.config.ConfigFactory
import pureconfig.ConfigReader
import pureconfig.generic.auto._
object Main {
def main(args: Array[String]): Unit = {
println(pureconfig.loadConfig[BooleanTest](ConfigFactory.parseString("a = 1")))
}
}
case class BooleanTest(a: Boolean)
object ConfigImplicits {
implicit val myBooleanReader: ConfigReader[Boolean] = ConfigReader[Double].map { n => n > 0}
}
Here, I expect my code to print an instance of BooleanTest
.
Instead, I got a ConvertFailure
:
Left(ConfigReaderFailures(ConvertFailure(WrongType(NUMBER,Set(BOOLEAN)),None,a),List()))
One way to fix this, is to add import ConfigImplicits._
just before calling the loadConfig
function.
However, as you can suppose, my code is actually part of a bigger project, and adding the import
in the real project does not fix my error.
Do you have any hint on what can be wrong?
Kind, Alexis.
Edit:
After comments from Thilo it appears logic to add the import
statement.
Below is an updated version of the code which include the import
statement but still produce the same error...
Change the main
function to:
def main(args: Array[String]): Unit = {
println(ConfigUtils.loadConfig[BooleanTest]("a = 1"))
}
And declare a ConfigUtils object as follow:
object ConfigUtils {
def loadConfig[A : ConfigReader](str: String) : ConfigReader.Result[A] = {
import ConfigImplicits._
val config = ConfigFactory.parseString(str)
pureconfig.loadConfig[A](config)
}
}
Run the code and you get the same error as previously:
ConvertFailure(WrongType(NUMBER,Set(BOOLEAN))
Why does pureconfig
not use my implicit myBooleanReader
to parse this configuration?
Kind, Alexis.