circe-yaml icon indicating copy to clipboard operation
circe-yaml copied to clipboard

Option Parameters always returning None

Open fritzwijaya opened this issue 2 years ago • 1 comments

Try to set default value using options to set default value if not set. But, the parsed yaml always returning None whether we set default value or not.

sbt:

libraryDependencies += "io.circe" %% "circe-yaml" % "0.14.1"
libraryDependencies += "io.circe" %% "circe-generic" % "0.14.2"
libraryDependencies += "io.circe" %% "circe-generic-extras" % "0.14.2"

Snippet:

import cats.syntax.either._
import io.circe._
import io.circe.generic.auto._
import io.circe.generic.extras.Configuration
import io.circe.yaml 

case class Config(url: String,
                      user: String,
                      password: String,
                      fetchSize: Option[Integer] = Some(1000))

implicit val customConfig: Configuration = Configuration.default.withDefaults

val sample =
  """
    |url: "some-sample-url"
    |user: "user"
    |password: "secret"
    |""".stripMargin    

val json = yaml.parser.parse(sample)

val foo = json
  .leftMap(err => err: Error)
  .flatMap(_.as[Config])
  .valueOr(throw _)

Change the fetchSize with or without default value is indifferent. Does any config that I am missing here? Thanks

fritzwijaya avatar Aug 29 '22 09:08 fritzwijaya

AFAIK decoders generated with io.circe.generic.auto._ do not take any Configuration from the generic.extras into the account. But if you change the import to io.circe.generic.extras.auto._, the code should work.

shuttie avatar Jul 12 '23 13:07 shuttie