-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
circe-config will fail on Boolean values written as "on/true/false/off" #12
Comments
The problem is that The only solution I can think of is to create a wrapper "boolean' type which allows to use an implicit convertion to translate from string to boolean as Typesafe config does. import scala.language.implicitConversions
import org.scalatest.{ FlatSpec, Matchers }
import com.typesafe.config.ConfigFactory
import io.circe._
import io.circe.generic.auto._
import io.circe.config.syntax._
object ConfigBooleanSpec {
case class ConfigBoolean(value: Boolean)
object ConfigBoolean {
implicit def toBoolean(b: ConfigBoolean): Boolean = b.value
}
implicit val configBooleanDecoder: Decoder[ConfigBoolean] = {
val truthful = Set("true", "yes", "on")
Decoder.decodeString.map(s => ConfigBoolean(truthful(s)))
}
case class ServerConfig(host: String, port: Int, enabled: ConfigBoolean)
}
class ConfigBooleanSpec extends FlatSpec with Matchers {
import ConfigBooleanSpec._
"custom boolean" should "parse and decode from string" in {
val config = ConfigFactory.parseString(
"""
host = localhost
port = 8080
enabled = on
""")
val Right(ServerConfig(_, _, enabled)) = config.as[ServerConfig]
assert(enabled.value)
assert(enabled)
}
} |
It would be good to document this though so if you are up for making a PR that would be great else let's keep this issue open until that gets fixed. |
Sorry for the late answer, I think better solution would be to delay conversion to Maybe something could be done via |
I agree that would be optimal but not sure it is possible to do this sort of lazy conversion. |
circe/circe-config#12 (#871) * Fix #869 workaround circe-config Boolean codec issue in logstage config circe/circe-config#12 * Fixes repro at https://github.com/DmytroOrlov/logstage-resolve * RenderingOptions codec
It may be intended, but right now circe-config is inconsistent with original config for Boolean values written as strings:
It is possible to work around it, but it is not very convenient when passing config values via ENV_VARIABLES in K8s -- it disallows unquoted strings. :(
The text was updated successfully, but these errors were encountered: