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

Reduce common boilerplate #132

Open
japgolly opened this issue Sep 28, 2022 · 0 comments
Open

Reduce common boilerplate #132

japgolly opened this issue Sep 28, 2022 · 0 comments

Comments

@japgolly
Copy link
Owner

I'm always doing the same thing. Make this easier out-of-the-box:

  def sources: ConfigSources[IO] =
    // Highest pri
    ConfigSource.environment[IO].expandInlineProperties("APP_PROPS").mapKeyQueries(acceptExternalKeyFormat) >
      ConfigSource.propFileOnClasspath[IO]("app.properties", optional = true) >
      ConfigSource.system[IO]
  // Lowest pri

  private def acceptExternalKeyFormat(k: ConfigKey): List[ConfigKey] =
    k :: k.map(_.toUpperCase.replace('.', '_')) :: Nil

  def load: IO[Option[AppConfig]] =
    config
      .withReport
      .run(sources)
      .map { result =>
        result.toEither match {
          case Right((config, report)) =>
            if (config.showUnusedConfig)
              logger.info("Config report\n" + report.full)
            else
              logger.info("Config report\n" + report.sources + "\n\n" + report.used)
            Some(config)
          case Left(err) =>
            logger.error("Invalid config:\n" + err)
            None
        }
      }
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

1 participant