Search code examples
typesafe-config

how to read a custom object from com.typesafe.config


Referring to com.typesafe.config library, I notice that while there are methods available to parse a value to Integer, Boolean etc., there aren't methods available to parse a value as a custom object. Am I correct?

https://lightbend.github.io/config/latest/api/com/typesafe/config/Config.html

I want to create a config file with the following values

cookieName: String = "id",
  cookiePath  = "/",
  cookieDomain = None,
  secureCookie  = true,
  httpOnlyCookie = true,
  useFingerprinting = true,
  cookieMaxAge  = None,
  authenticatorIdleTimeout = None,
  authenticatorExpiry: = 12 hours

and parse it into a case class

case class CookieAuthenticatorSettings(
  cookieName: String ,
  cookiePath: String ,
  cookieDomain: Option[String] ,
  secureCookie: Boolean ,
  httpOnlyCookie: Boolean, 
  useFingerprinting: Boolean ,
  cookieMaxAge: Option[FiniteDuration] ,
  authenticatorIdleTimeout: Option[FiniteDuration] ,
  authenticatorExpiry: FiniteDuration 
)

How could I do this? In the following snippet, I see that a method 'as' has been used but when I tried to use it in my code, the compiler couldn't find the 'as' method.

val config = configuration.underlying.as[JcaSignerSettings]("silhouette.oauth1TokenSecretProvider.signer")

The above code is from https://github.com/mohiva/play-silhouette-seed/blob/master/app/modules/SilhouetteModule.scala


Solution

  • you will need to use something like this which leverages the use of Scala implicits. it has a default behavior that sort of figures out what Object what you need as long as the field names in the config matches the ones in your case class, but you can also provide your own reader to map the config fields to your case class.

    i use this library with my own projects and i can say that it works as advertised.