Skip to content
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

Option Parameters always returning None #316

Open
fritzwijaya opened this issue Aug 29, 2022 · 1 comment
Open

Option Parameters always returning None #316

fritzwijaya opened this issue Aug 29, 2022 · 1 comment

Comments

@fritzwijaya
Copy link

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

@shuttie
Copy link

shuttie commented Jul 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants