All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.kodeworks.kvarg.util.CfgResolver.scala Maven / Gradle / Ivy

There is a newer version: 0.7
Show newest version
package no.kodeworks.kvarg.util

import java.util.concurrent.TimeUnit

import concurrent.duration._
import com.typesafe.config.Config
import org.apache.commons.lang3.StringUtils
import shapeless.Typeable
import Typeable._
import CfgResolver._

trait CfgResolver {
  def p[T](implicit
           config: Config,
           tt: Typeable[T],
           name: sourcecode.Name): T =
    (tt.describe match {
      case `intTypeableDescribe` => config.getInt(value)
      case `longTypeableDescribe` => config.getLong(value)
      case `doubleTypeableDescribe` => config.getDouble(value)
      case `booleanTypeableDescribe` => config.getBoolean(value)
      case `stringTypeableDescribe` => config.getString(value)
      case `durationTypeableDescribe` => config.getDuration(value, TimeUnit.MILLISECONDS) millis
      case `finiteDurationTypeableDescribe` => p[Duration].toMillis millis
    }).asInstanceOf[T]


  def value(implicit name: sourcecode.Name): String =
    StringUtils.splitByCharacterTypeCamelCase(name.value).map(_.toLowerCase()).mkString(".")
}

object CfgResolver {
  val intTypeableDescribe = intTypeable.describe
  val longTypeableDescribe = longTypeable.describe
  val doubleTypeableDescribe = doubleTypeable.describe
  val booleanTypeableDescribe = booleanTypeable.describe
  val stringTypeableDescribe = implicitly[Typeable[String]].describe
  val durationTypeableDescribe = implicitly[Typeable[Duration]].describe
  val finiteDurationTypeableDescribe = implicitly[Typeable[FiniteDuration]].describe
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy