com.typesafe.config.ConfigObject Scala Examples

The following examples show how to use com.typesafe.config.ConfigObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: EnumCoproductHint.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.generic

import com.typesafe.config.{ ConfigObject, ConfigValue, ConfigValueType }
import pureconfig._
import pureconfig.error._
import pureconfig.generic.CoproductHint.Use
import pureconfig.generic.error.{ CoproductHintException, NoValidCoproductOptionFound }
import pureconfig.syntax._


  protected def fieldValue(name: String): String = name.toLowerCase

  def from(cursor: ConfigCursor, options: Seq[String]): ConfigReader.Result[CoproductHint.Action] =
    cursor.asString.right.flatMap { str =>
      options.find(str == fieldValue(_)) match {
        case Some(opt) => Right(Use(cursor, opt))
        case None => cursor.failed[CoproductHint.Action](NoValidCoproductOptionFound(cursor.value, Seq.empty))
      }
    }

  def to(value: ConfigValue, name: String): ConfigValue =
    value match {
      case co: ConfigObject if co.isEmpty => fieldValue(name).toConfig
      case _: ConfigObject =>
        throw CoproductHintException(NonEmptyObjectFound(name))
      case cv =>
        throw CoproductHintException(WrongType(cv.valueType, Set(ConfigValueType.OBJECT)))
    }
} 
Example 2
Source File: MetricsReportingManager.scala    From service-container   with Apache License 2.0 5 votes vote down vote up
package com.github.vonnagy.service.container.metrics.reporting

import java.util.concurrent.TimeUnit

import akka.ConfigurationException
import akka.actor.{Actor, ActorSystem, Props}
import com.github.vonnagy.service.container.health.{GetHealth, HealthInfo, HealthState, RegisteredHealthCheckActor}
import com.github.vonnagy.service.container.log.ActorLoggingAdapter
import com.github.vonnagy.service.container.metrics.Metrics
import com.typesafe.config.{Config, ConfigObject}

import scala.collection.JavaConverters._
import scala.concurrent.duration.FiniteDuration

object MetricsReportingManager {
  def props(): Props =
    Props(classOf[MetricsReportingManager])
}


  private[reporting] def stopReporters(): Unit = {
    reporters.foreach(_.stop)
    reporters = Seq.empty[ScheduledReporter]
  }

  private def checkHealth(): HealthInfo = {
    if (reporters.length == 0) {
      HealthInfo("metrics-reporting", HealthState.OK, s"The system is currently not managing any metrics reporters")
    }
    else {
      val x = for {
        reporter <- reporters
      } yield {
        reporter.getClass.getName
      }

      HealthInfo("metrics-reporting", HealthState.OK, s"The system is currently managing ${reporters.length} metrics reporters", Some(x))
    }
  }
} 
Example 3
Source File: package.scala    From sbt-node   with MIT License 5 votes vote down vote up
//     Project: sbt-node
//      Module:
// Description:
package de.surfice.sbtnpm

import sbt._
import Keys._
import Cache._
import com.typesafe.config.{Config, ConfigFactory, ConfigObject}

package object utils {
  def fileWithScalaJSStageSuffix(dir: File, filePrefix: String, stage: Scoped, fileSuffix: String): File =
    dir / (filePrefix + stage.key.toString.dropRight(2).toLowerCase() + fileSuffix)


  implicit final class RichConfig(val config: Config) extends AnyVal {
    import collection.JavaConverters._
    def getStringMap(path: String): Map[String,String] =  config.withOnlyPath(path).entrySet().asScala
      .map(p => keySuffix(path,p.getKey) -> config.getString(p.getKey))
      .toMap

    def getConfigMap(path: String): Map[String,Config] =
      config.getObject(path).asScala.map(p => p._1 -> p._2).map {
        case (key,obj:ConfigObject) => (key,obj.toConfig)
      }.toMap

    private def stripQuotes(key: String): String = key.replaceAll("\"","")
    private def keySuffix(path: String, key: String): String = stripQuotes(key).stripPrefix(path+".")
  }
} 
Example 4
Source File: MySqlConnectionConfig.scala    From vinyldns   with Apache License 2.0 5 votes vote down vote up
package vinyldns.mysql

import com.typesafe.config.ConfigObject
import pureconfig.ConfigReader

object MySqlConnectionConfig {

  import scala.collection.JavaConverters._

  val mySqlPropertiesReader: ConfigReader[Map[String, AnyRef]] = {
    ConfigReader[Option[ConfigObject]].map(_.map(_.unwrapped().asScala.toMap).getOrElse(Map()))
  }
}

final case class MySqlConnectionConfig(
    name: String,
    driver: String,
    migrationUrl: String,
    url: String,
    user: String,
    password: String,
    migrationSchemaTable: Option[String],
    // Optional settings, will use Hikari defaults if unset
    // see https://github.com/brettwooldridge/HikariCP#frequently-used
    connectionTimeoutMillis: Option[Long],
    idleTimeout: Option[Long],
    maxLifetime: Option[Long],
    maximumPoolSize: Option[Int],
    minimumIdle: Option[Int],
    registerMbeans: Boolean = false,
    // MySql performance settings
    // see https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
    mySqlProperties: Map[String, AnyRef] = Map()
) 
Example 5
Source File: EmailNotifierConfig.scala    From vinyldns   with Apache License 2.0 5 votes vote down vote up
package vinyldns.api.notifier.email

import scala.collection.JavaConverters._
import javax.mail.Address
import javax.mail.internet.InternetAddress
import pureconfig.ConfigReader
import scala.util.Try
import pureconfig.error.CannotConvert
import java.util.Properties
import com.typesafe.config.{ConfigObject, ConfigValue}
import com.typesafe.config.ConfigValueType

object EmailNotifierConfig {

  implicit val smtpPropertiesReader: ConfigReader[Properties] = {
    ConfigReader[ConfigObject].map { config =>
      val props = new Properties()

      def convertToProperties(baseKey: String, config: ConfigObject): Unit =
        config.keySet().asScala.foreach {
          case key =>
            config.get(key) match {
              case value: ConfigObject =>
                convertToProperties(s"${baseKey}.${key}", value)
              case value: ConfigValue if value.valueType != ConfigValueType.NULL =>
                props.put(s"${baseKey}.${key}", value.unwrapped())
              case _ =>
            }
        }

      convertToProperties("mail.smtp", config)
      props
    }
  }

  implicit val addressReader: ConfigReader[Address] = ConfigReader[String].emap { s =>
    Try(new InternetAddress(s)).toEither.left.map { exc =>
      CannotConvert(s, "InternetAddress", exc.getMessage)
    }
  }

}

case class EmailNotifierConfig(from: Address, smtp: Properties = new Properties()) 
Example 6
Source File: GlobalDatabaseManager.scala    From graphcool-framework   with Apache License 2.0 5 votes vote down vote up
package cool.graph.shared.database

import com.typesafe.config.{Config, ConfigObject}
import cool.graph.shared.models.Region.Region
import cool.graph.shared.models.{Project, ProjectDatabase, Region}
import slick.jdbc.MySQLProfile.api._
import slick.jdbc.MySQLProfile.backend.DatabaseDef

object InternalAndProjectDbs {
  def apply(internal: InternalDatabase, client: Databases): InternalAndProjectDbs = {
    InternalAndProjectDbs(internal, Some(client))
  }
}
case class InternalAndProjectDbs(internal: InternalDatabase, client: Option[Databases] = None)
case class Databases(master: DatabaseDef, readOnly: DatabaseDef)
case class InternalDatabase(databaseDef: DatabaseDef)


case class ProjectDatabaseRef(region: Region, name: String)

case class GlobalDatabaseManager(currentRegion: Region, databases: Map[ProjectDatabaseRef, Databases]) {

  def getDbForProject(project: Project): Databases = getDbForProjectDatabase(project.projectDatabase)

  def getDbForProjectDatabase(projectDatabase: ProjectDatabase): Databases = {
    val projectDbRef = ProjectDatabaseRef(projectDatabase.region, projectDatabase.name)
    databases.get(projectDbRef) match {
      case None =>
        sys.error(s"This service is not configured to access Client Db with name [${projectDbRef.name}] in region '${projectDbRef.region}'")
      case Some(db) => db
    }
  }
}

object GlobalDatabaseManager {
  val singleConfigRoot    = "clientDatabases"
  val allConfigRoot       = "allClientDatabases"
  val awsRegionConfigProp = "awsRegion"

  def initializeForSingleRegion(config: Config): GlobalDatabaseManager = {
    import scala.collection.JavaConversions._
    config.resolve()
    val currentRegion = Region.withName(config.getString(awsRegionConfigProp))

    val databasesMap = for {
      (dbName, _) <- config.getObject(singleConfigRoot)
    } yield {
      val readOnlyPath    = s"$singleConfigRoot.$dbName.readonly"
      val masterDb        = Database.forConfig(s"$singleConfigRoot.$dbName.master", config)
      lazy val readOnlyDb = Database.forConfig(readOnlyPath, config)

      val dbs = Databases(
        master = masterDb,
        readOnly = if (config.hasPath(readOnlyPath)) readOnlyDb else masterDb
      )

      ProjectDatabaseRef(currentRegion, dbName) -> dbs
    }

    GlobalDatabaseManager(currentRegion = currentRegion, databases = databasesMap.toMap)
  }

  def initializeForMultipleRegions(config: Config): GlobalDatabaseManager = {
    import scala.collection.JavaConversions._

    val currentRegion = Region.withName(config.getString(awsRegionConfigProp))

    val databasesMap = for {
      (regionName, regionValue) <- config.getObject(allConfigRoot)
      (dbName, _)               <- regionValue.asInstanceOf[ConfigObject]
    } yield {
      val readOnlyPath    = s"$allConfigRoot.$regionName.$dbName.readonly"
      val masterDb        = Database.forConfig(s"$allConfigRoot.$regionName.$dbName.master", config)
      lazy val readOnlyDb = Database.forConfig(readOnlyPath, config)

      val dbs = Databases(
        master = masterDb,
        readOnly = if (config.hasPath(readOnlyPath)) readOnlyDb else masterDb
      )

      ProjectDatabaseRef(Region.withName(regionName), dbName) -> dbs
    }

    GlobalDatabaseManager(currentRegion = currentRegion, databases = databasesMap.toMap)
  }
} 
Example 7
Source File: CoproductConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import com.typesafe.config.{ ConfigFactory, ConfigObject, ConfigValueFactory }
import org.scalacheck.{ Arbitrary, Gen }
import pureconfig.error._
import pureconfig.generic.auto._

class CoproductConvertersSuite extends BaseSuite {

  behavior of "ConfigConvert"

  val genBirdConfig: Gen[BirdConfig] = Arbitrary.arbBool.arbitrary.map(BirdConfig.apply)
  val genCatConfig: Gen[CatConfig] = Arbitrary.arbInt.arbitrary.map(CatConfig.apply)
  val genDogConfig: Gen[DogConfig] = Arbitrary.arbInt.arbitrary.map(DogConfig.apply)
  val genAnimalConfig: Gen[AnimalConfig] = Gen.oneOf(genBirdConfig, genCatConfig, genDogConfig)
  implicit val arbAnimalConfig = Arbitrary(genAnimalConfig)

  checkArbitrary[AnimalConfig]

  it should "read disambiguation information on sealed families by default" in {
    val conf = ConfigFactory.parseString("{ type = dog-config, age = 2 }")
    ConfigConvert[AnimalConfig].from(conf.root()) shouldEqual Right(DogConfig(2))
  }

  it should "write disambiguation information on sealed families by default" in {
    val conf = ConfigConvert[AnimalConfig].to(DogConfig(2))
    conf shouldBe a[ConfigObject]
    conf.asInstanceOf[ConfigObject].get("type") shouldEqual ConfigValueFactory.fromAnyRef("dog-config")
  }

  it should "return a proper ConfigReaderFailure if the hint field in a coproduct is missing" in {
    val conf = ConfigFactory.parseString("{ can-fly = true }")
    ConfigConvert[AnimalConfig].from(conf.root()) should failWithType[KeyNotFound]
  }

  it should "return a proper ConfigReaderFailure when a coproduct config is missing" in {
    case class AnimalCage(animal: AnimalConfig)
    ConfigConvert[AnimalCage].from(ConfigFactory.empty().root()) should failWithType[KeyNotFound]
  }
} 
Example 8
Source File: ConfigReaderSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig

import com.typesafe.config.{ ConfigFactory, ConfigObject, ConfigOriginFactory, ConfigParseOptions, ConfigValue, ConfigValueFactory }
import org.scalacheck.{ Arbitrary, Gen }
import pureconfig.error._

class ConfigReaderSuite extends BaseSuite {
  implicit override val generatorDrivenConfig = PropertyCheckConfiguration(minSuccessful = 100)

  val intReader = ConfigReader[Int]
  val strReader = ConfigReader[String]

  def intSummedReader(n: Int) = new ConfigReader[Int] {
    def from(cur: ConfigCursor) = intReader.from(cur).right.map(_ + n)
  }

  // generate configs that always read correctly as strings, but not always as integers
  val genConfig: Gen[ConfigValue] =
    Gen.frequency(80 -> Gen.chooseNum(Int.MinValue, Int.MaxValue), 20 -> Gen.alphaStr)
      .map(ConfigValueFactory.fromAnyRef)

  val genFailureReason: Gen[FailureReason] =
    Gen.const(UnknownKey(""))

  implicit val arbConfig = Arbitrary(genConfig)
  implicit val arbFailureReason = Arbitrary(genFailureReason)

  behavior of "ConfigReader"

  it should "have a correct map method" in forAll { (conf: ConfigValue, f: Int => String) =>
    intReader.map(f).from(conf) shouldEqual intReader.from(conf).right.map(f)
  }

  it should "have a map method that wraps exceptions in a ConfigReaderFailure" in {
    val throwable = new Exception("Exception message.")
    val cr = ConfigReader[Int].map({ _ => throw throwable })
    cr.from(ConfigValueFactory.fromAnyRef(1)) should failWith(ExceptionThrown(throwable))
  }

  it should "have a correct emap method" in forAll { (conf: ConfigValue, f: Int => Either[FailureReason, String]) =>
    def getReason[A](failures: ConfigReaderFailures): FailureReason = failures match {
      case ConfigReaderFailures(ConvertFailure(reason, _, _)) => reason
      case _ => throw new Exception(s"Unexpected value: $failures")
    }
    intReader.emap(f).from(conf).left.map(getReason) shouldEqual
      intReader.from(conf).left.map(getReason).right.flatMap(f)
  }

  it should "have a correct flatMap method" in forAll { conf: ConfigValue =>
    val g: Int => ConfigReader[Int] = intSummedReader
    intReader.flatMap(g).from(conf) shouldEqual intReader.from(conf).right.flatMap(g(_).from(conf))
  }

  it should "have a correct zip method" in forAll { conf: ConfigValue =>
    def zip[A, B](r1: ConfigReader[A], r2: ConfigReader[B]): ConfigReader.Result[(A, B)] = {
      (r1.from(conf), r2.from(conf)) match {
        case (Right(a), Right(b)) => Right((a, b))
        case (Left(fa), Right(_)) => Left(fa)
        case (Right(_), Left(fb)) => Left(fb)
        case (Left(fa), Left(fb)) => Left(fa ++ fb)
      }
    }

    intReader.zip(strReader).from(conf) shouldEqual zip(intReader, strReader)
    strReader.zip(intReader).from(conf) shouldEqual zip(strReader, intReader)
    intReader.zip(intReader).from(conf) shouldEqual zip(intReader, intReader)
    strReader.zip(strReader).from(conf) shouldEqual zip(strReader, strReader)
  }

  it should "have a correct orElse method" in forAll { conf: ConfigValue =>
    def orElse[AA, A <: AA, B <: AA](r1: ConfigReader[A], r2: ConfigReader[B]): ConfigReader.Result[AA] = {
      (r1.from(conf), r2.from(conf)) match {
        case (Right(a), _) => Right(a)
        case (Left(_), Right(b)) => Right(b)
        case (Left(fa), Left(fb)) => Left(fa ++ fb)
      }
    }

    // results are explicitly typed so that we also test the resulting type of `orElse`
    intReader.orElse(strReader).from(conf) shouldEqual orElse[Any, Int, String](intReader, strReader)
    strReader.orElse(intReader).from(conf) shouldEqual orElse[Any, String, Int](strReader, intReader)
    intReader.orElse(intReader).from(conf) shouldEqual orElse[Int, Int, Int](intReader, intReader)
    strReader.orElse(strReader).from(conf) shouldEqual orElse[String, String, String](strReader, strReader)
  }

  it should "have a correct contramapConfig method" in forAll { conf: ConfigValue =>
    val wrappedConf = conf.atKey("value").root()
    val unwrap = { cv: ConfigValue => cv.asInstanceOf[ConfigObject].get("value") }

    intReader.contramapConfig(unwrap).from(wrappedConf) shouldEqual intReader.from(conf)
  }
} 
Example 9
Source File: MapShapedWriter.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.generic

import scala.collection.JavaConverters._

import com.typesafe.config.{ ConfigFactory, ConfigObject, ConfigValue }
import pureconfig._
import shapeless._
import shapeless.labelled.FieldType


private[generic] trait MapShapedWriter[Original, Repr] extends ConfigWriter[Repr]

object MapShapedWriter {

  implicit def labelledHNilWriter[Original]: MapShapedWriter[Original, HNil] = new MapShapedWriter[Original, HNil] {
    override def to(t: HNil): ConfigValue = ConfigFactory.parseMap(Map().asJava).root()
  }

  final implicit def labelledHConsWriter[Original, K <: Symbol, H, T <: HList](
    implicit
    key: Witness.Aux[K],
    hConfigWriter: Derivation[Lazy[ConfigWriter[H]]],
    tConfigWriter: Lazy[MapShapedWriter[Original, T]],
    hint: ProductHint[Original]): MapShapedWriter[Original, FieldType[K, H] :: T] =
    new MapShapedWriter[Original, FieldType[K, H] :: T] {
      override def to(t: FieldType[K, H] :: T): ConfigValue = {
        val rem = tConfigWriter.value.to(t.tail)
        val valueOpt = hConfigWriter.value.value match {
          case tc: WritesMissingKeys[H @unchecked] =>
            tc.toOpt(t.head)
          case w =>
            Some(w.to(t.head))
        }
        val kv = hint.to(valueOpt, key.value.name)

        // TODO check that all keys are unique
        kv.fold(rem) {
          case (k, v) =>
            rem.asInstanceOf[ConfigObject].withValue(k, v)
        }
      }
    }
} 
Example 10
Source File: ConfigSupport.scala    From hydra   with Apache License 2.0 5 votes vote down vote up
package hydra.common.config

import java.util.Properties
import java.util.concurrent.TimeUnit

import cats.implicits._
import com.typesafe.config.{Config, ConfigFactory, ConfigObject}

import scala.concurrent.duration.FiniteDuration
import scala.language.implicitConversions


trait ConfigSupport extends ConfigComponent {

  private val defaultConfig = ConfigFactory.load()

  val applicationName: String = defaultConfig.getString("application.name")

  val rootConfig: Config = defaultConfig

  val applicationConfig: Config = rootConfig.getConfig(applicationName)

}

object ConfigSupport {

  import scala.collection.JavaConverters._

  implicit def toMap(cfg: ConfigObject): Map[String, Object] = {
    cfg.toConfig
      .entrySet()
      .asScala
      .map({ entry => entry.getKey -> entry.getValue.unwrapped() })(
        collection.breakOut
      )
  }

  implicit def toMap(cfg: Config): Map[String, Object] = {
    cfg
      .entrySet()
      .asScala
      .map({ entry => entry.getKey -> entry.getValue.unwrapped() })(
        collection.breakOut
      )
  }

  implicit def toProps(map: Map[String, AnyRef]): Properties = {
    map.foldLeft(new Properties) {
      case (a, (k, v)) =>
        a.put(k, v)
        a
    }
  }

  implicit class ConfigImplicits(config: Config) {
    def getDurationOpt(path: String): Option[FiniteDuration] =
      getOptional(path, config.getDuration).map(d => FiniteDuration(d.toNanos, TimeUnit.NANOSECONDS))

    def getStringOpt(path: String): Option[String] =
      getOptional(path, config.getString)

    def getConfigOpt(path: String): Option[Config] =
      getOptional(path, config.getConfig)

    def getIntOpt(path: String): Option[Int] =
      getOptional(path, config.getInt)

    def getBooleanOpt(path: String): Option[Boolean] =
      getOptional(path, config.getBoolean)

    def getStringListOpt(path: String): Option[List[String]] =
      getOptional(path, config.getStringList).map(_.asScala.toList)

    private def getOptional[A](path: String, method: String => A): Option[A] = {
      if (config.hasPath(path)) {
        method(path).some
      } else {
        none
      }
    }
  }

} 
Example 11
Source File: CoproductConvertersSuite.scala    From pureconfig   with Mozilla Public License 2.0 5 votes vote down vote up
package pureconfig.module.magnolia

import scala.language.higherKinds

import com.typesafe.config.{ ConfigFactory, ConfigObject, ConfigValueFactory }
import org.scalacheck.{ Arbitrary, Gen }
import pureconfig._
import pureconfig.error._
import pureconfig.module.magnolia.auto.reader._
import pureconfig.module.magnolia.auto.writer._

class CoproductConvertersSuite extends BaseSuite {

  behavior of "ConfigConvert"

  val genBirdConfig: Gen[BirdConfig] = Arbitrary.arbBool.arbitrary.map(BirdConfig.apply)
  val genCatConfig: Gen[CatConfig] = Arbitrary.arbInt.arbitrary.map(CatConfig.apply)
  val genDogConfig: Gen[DogConfig] = Arbitrary.arbInt.arbitrary.map(DogConfig.apply)
  val genAnimalConfig: Gen[AnimalConfig] = Gen.oneOf(genBirdConfig, genCatConfig, genDogConfig)
  implicit val arbAnimalConfig = Arbitrary(genAnimalConfig)

  checkArbitrary[AnimalConfig]

  it should "read disambiguation information on sealed families by default" in {
    val conf = ConfigFactory.parseString("{ type = dog-config, age = 2 }")
    ConfigConvert[AnimalConfig].from(conf.root()) shouldEqual Right(DogConfig(2))
  }

  it should "write disambiguation information on sealed families by default" in {
    val conf = ConfigConvert[AnimalConfig].to(DogConfig(2))
    conf shouldBe a[ConfigObject]
    conf.asInstanceOf[ConfigObject].get("type") shouldEqual ConfigValueFactory.fromAnyRef("dog-config")
  }

  it should "return a proper ConfigReaderFailure if the hint field in a coproduct is missing" in {
    val conf = ConfigFactory.parseString("{ can-fly = true }")
    ConfigConvert[AnimalConfig].from(conf.root()) should failWithType[KeyNotFound]
  }

  it should "return a proper ConfigReaderFailure when a coproduct config is missing" in {
    case class AnimalCage(animal: AnimalConfig)
    ConfigConvert[AnimalCage].from(ConfigFactory.empty().root()) should failWithType[KeyNotFound]
  }
} 
Example 12
Source File: TemplateConfig.scala    From cluster-broccoli   with Apache License 2.0 5 votes vote down vote up
package de.frosner.broccoli.templates

import com.typesafe.config.{ConfigObject, ConfigValue}
import de.frosner.broccoli.models.{ParameterType, ParameterValue}
import pureconfig.ConfigReader
import pureconfig.error.{ConfigReaderFailures, ThrowableFailure}

import scala.collection.JavaConversions._
import scala.util.{Failure, Success, Try}

object TemplateConfig {

  implicit val configReader: ConfigReader[TemplateInfo] = new ConfigReader[TemplateInfo] {
    override def from(configOrig: ConfigValue): Either[ConfigReaderFailures, TemplateInfo] =
      Try {
        val confObj = configOrig.asInstanceOf[ConfigObject]
        val config = confObj.toConfig
        val description = Try(config.getString("description")).toOption
        val parameters = config
          .getObject("parameters")
          .map {
            case (paramName, paramConfig) =>
              val paramValueObj = paramConfig.asInstanceOf[ConfigObject].toConfig
              val maybeName = Try(paramValueObj.getString("name")).toOption
              val maybeSecret = Try(paramValueObj.getBoolean("secret")).toOption
              // Don't wrap the call as we want it to fail in case the wrong type or no type is supplied
              val paramType = ParameterType.withName(paramValueObj.getString("type"))
              val maybeOrderIndex = Try(paramValueObj.getInt("order-index")).toOption
              val maybeDefault = Try(paramValueObj.getValue("default")).toOption.map { paramValueConf =>
                ParameterValue.fromConfigValue(
                  paramName,
                  paramType,
                  paramValueConf
                ) match {
                  case Success(paramDefault) => paramDefault
                  case Failure(ex)           => throw ex
                }
              }
              (paramName, Parameter(maybeName, maybeDefault, maybeSecret, paramType, maybeOrderIndex))
          }
          .toMap
        TemplateInfo(description, parameters)
      } match {
        case Success(e)  => Right(e)
        case Failure(ex) =>
          // TODO: Improve this error throwing.
          Left(ConfigReaderFailures(ThrowableFailure(ex, None, "")))
      }
  }

  final case class TemplateInfo(description: Option[String], parameters: Map[String, Parameter])

  final case class Parameter(name: Option[String],
                             default: Option[ParameterValue],
                             secret: Option[Boolean],
                             `type`: ParameterType,
                             orderIndex: Option[Int])

} 
Example 13
Source File: Configurator.scala    From spark-bench   with Apache License 2.0 5 votes vote down vote up
package com.ibm.sparktc.sparkbench.cli

import com.ibm.sparktc.sparkbench.utils.SaveModes
import com.ibm.sparktc.sparkbench.utils.TypesafeAccessories.configToMapStringSeqAny

import scala.collection.JavaConverters._
import com.ibm.sparktc.sparkbench.workload.{MultiSuiteRunConfig, Suite}
import com.typesafe.config.{Config, ConfigFactory, ConfigObject}

import scala.util.Try

object Configurator {

  def apply(str: String): Seq[MultiSuiteRunConfig] = {
    val unescaped = StringContext.treatEscapes(str)
    val config: Config = ConfigFactory.parseString(unescaped)
    val sparkBenchConfig = config.getObject("spark-bench").toConfig
    val multiSuiteRunConfig: Seq[MultiSuiteRunConfig] = parseSparkBenchRunConfig(sparkBenchConfig)
    multiSuiteRunConfig
  }

  private def parseSparkBenchRunConfig(config: Config): Seq[MultiSuiteRunConfig] = {
    val sparkContextConfs = getConfigListByName("spark-submit-config", config)
    val workloadConfs = sparkContextConfs.map { sparkContextConf => {
        MultiSuiteRunConfig(
          suitesParallel = Try(sparkContextConf.getBoolean("suites-parallel")).getOrElse(false),
          enableHive = Try(sparkContextConf.getBoolean("enable-hive")).getOrElse(false),
          suites = getConfigListByName("workload-suites", sparkContextConf).map(parseSuite)
        )
      }
    }
    workloadConfs
  }

  private def getConfigListByName(name: String, config: Config): List[Config] = {
    val workloadObjs: Iterable[ConfigObject] = config.getObjectList(name).asScala
    workloadObjs.map(_.toConfig).toList
  }

  def parseSuite(config: Config): Suite = {
    val descr: Option[String] = Try(config.getString("descr")).toOption
    val parallel: Boolean = Try(config.getBoolean("parallel")).getOrElse(false)
    val repeat: Int = Try(config.getInt("repeat")).getOrElse(1)
    val output: Option[String] = Try(config.getString("benchmark-output")).toOption
    val saveMode: String = Try(config.getString("save-mode")).getOrElse(SaveModes.error)
    val workloads: Seq[Map[String, Seq[Any]]]  = getConfigListByName("workloads", config).map(configToMapStringSeqAny)

    Suite.build(
      workloads,
      descr,
      repeat,
      parallel,
      saveMode,
      output
    )
  }
} 
Example 14
Source File: ServiceNameMapper.scala    From lagom   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.lagom.internal.client

import akka.discovery.Lookup
import com.typesafe.config.Config
import com.typesafe.config.ConfigObject
import com.typesafe.config.ConfigValueType
import org.slf4j.LoggerFactory

import scala.collection.JavaConverters._

private[lagom] class ServiceNameMapper(config: Config) {
  private val logger = LoggerFactory.getLogger(this.getClass)

  private val defaultPortName     = readConfigValue(config, "defaults.port-name").toOption
  private val defaultPortProtocol = readConfigValue(config, "defaults.port-protocol").toOption
  private val defaultScheme       = readConfigValue(config, "defaults.scheme").toOption

  private sealed trait ConfigValue {
    def toOption =
      this match {
        case NonEmpty(v) => Some(v)
        case _           => None
      }
  }
  private object ConfigValue {
    def apply(value: String) =
      if (value.trim.isEmpty) Empty
      else NonEmpty(value.trim)
  }
  private case object Undefined              extends ConfigValue
  private case object Empty                  extends ConfigValue
  private case class NonEmpty(value: String) extends ConfigValue

  private def readConfigValue(config: Config, name: String): ConfigValue =
    if (config.hasPathOrNull(name)) {
      if (config.getIsNull(name)) Empty
      else ConfigValue(config.getString(name))
    } else Undefined

  
  private def readOptionalConfigValue(config: Config, name: String, defaultValue: Option[String]): Option[String] =
    readConfigValue(config, name) match {
      case Undefined => defaultValue
      // this is the case the user explicitly set the scheme to empty string
      case Empty           => None
      case NonEmpty(value) => Option(value)
    }

  private val serviceLookupMapping: Map[String, ServiceLookup] =
    config
      .getObject("service-name-mappings")
      .entrySet()
      .asScala
      .map { entry =>
        if (entry.getValue.valueType != ConfigValueType.OBJECT) {
          throw new IllegalArgumentException(
            s"Illegal value type in service-name-mappings: ${entry.getKey} - ${entry.getValue.valueType}"
          )
        }
        val configEntry = entry.getValue.asInstanceOf[ConfigObject].toConfig

        // read config values for portName, portProtocol and scheme
        // when not explicitly overwritten by used, uses default values
        val portName     = readOptionalConfigValue(configEntry, "port-name", defaultPortName)
        val portProtocol = readOptionalConfigValue(configEntry, "port-protocol", defaultPortProtocol)
        val scheme       = readOptionalConfigValue(configEntry, "scheme", defaultScheme)

        val lookup: Lookup =
          readConfigValue(configEntry, "lookup").toOption
            .map(name => parseSrv(name, portName, portProtocol))
            .getOrElse(Lookup(entry.getKey, portName, portProtocol))

        entry.getKey -> ServiceLookup(lookup, scheme)
      }
      .toMap

  private def parseSrv(name: String, portName: Option[String], portProtocol: Option[String]) =
    if (Lookup.isValidSrv(name)) Lookup.parseSrv(name)
    else Lookup(name, portName, portProtocol)

  private[lagom] def mapLookupQuery(name: String): ServiceLookup = {
    val serviceLookup = serviceLookupMapping.getOrElse(
      name,
      ServiceLookup(parseSrv(name, defaultPortName, defaultPortProtocol), defaultScheme)
    )
    logger.debug("Lookup service '{}', mapped to {}", name: Any, serviceLookup: Any)
    serviceLookup
  }
}

private[lagom] case class ServiceLookup(lookup: Lookup, scheme: Option[String]) 
Example 15
Source File: Main.scala    From perf_tester   with Apache License 2.0 5 votes vote down vote up
package org.preftester

import java.io.File
import java.nio.file.{Files, Paths}

import com.typesafe.config.{ConfigFactory, ConfigObject, ConfigParseOptions}
import org.perftester.results.renderer.TextRenderer
import org.perftester.results.{ResultReader, RunResult}

import scala.collection.JavaConverters._
import scala.sys.process.Process
import scala.util.{Random, Try}

object Main extends App {
  val baseDir = Paths.get(args.headOption.getOrElse("."))

  case class Configuration(
                            reference: String,
                            baseScalaVersion: String,
                            buildLocally: Boolean,
                            jvmOptions: String,
                            scalaOptions: String
                          ){
    val scalaVersion = if(buildLocally) s"$baseScalaVersion-$reference-SNAPSHOT" else reference
  }

  val config = ConfigFactory.parseFile(
    baseDir.resolve("benchmark.conf").toFile,
    ConfigParseOptions.defaults().setAllowMissing(false)
  )

  val benchmarks = config.getObject("benchmarks").asScala.map {
    case (name, obj: ConfigObject) =>
      def read(name: String, default: String) = Try(obj.toConfig.getString(name)).getOrElse(default)

      name -> Configuration(
        reference = read("reference", name),
        baseScalaVersion = read("baseScalaVersion", "2.12.4"),
        buildLocally = read("buildLocally", "false").toBoolean,
        jvmOptions = read("jvmOptions", ""),
        scalaOptions = read("scalaOptions", "")
      )
  }.toSeq

  val iterations = config.getInt("iterations")
  val N = config.getInt("N")
  val M = config.getInt("M")

  val results = (1 to iterations).foldLeft(Map.empty[String, Vector[RunResult]]){
    case (all, i) =>
      Random.shuffle(benchmarks).foldLeft(all){
        case (all, (name, benchmark)) =>
          val location = baseDir.resolve(benchmark.scalaVersion)
          val cmd = Seq(s"./run.sh", ".", N, M, benchmark.scalaOptions).map(_.toString)
          println(s"## Run $i for $name")
          val env = if(benchmark.jvmOptions.isEmpty) Nil else Seq("_JAVA_OPTIONS" -> benchmark.jvmOptions)
          val output = Process(cmd, location.toFile, env:_*).!!
          println(output)
          val resultsDir = location.resolve("output").resolve("profile.txt")
          if (Files.exists(resultsDir)){
            val result = ResultReader.readResults(name, resultsDir, N)
            val previous = all.getOrElse(name, Vector.empty)
            all + (name -> (previous :+ result))
          } else all

      }
  }
  results.foreach{ case (name, results) =>
    println(s"########## Result for $name ##########")
    TextRenderer.outputTextResults(iterations, results)
  }
} 
Example 16
Source File: ServiceNameMapper.scala    From lagom-akka-discovery-service-locator   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.lagom.internal.client

import akka.discovery.Lookup
import com.typesafe.config.Config
import com.typesafe.config.ConfigObject
import com.typesafe.config.ConfigValueType
import org.slf4j.LoggerFactory

import scala.collection.JavaConverters._

private[lagom] class ServiceNameMapper(config: Config) {
  private val logger = LoggerFactory.getLogger(this.getClass)

  private val defaultPortName = readConfigValue(config, "defaults.port-name").toOption
  private val defaultPortProtocol = readConfigValue(config, "defaults.port-protocol").toOption
  private val defaultScheme = readConfigValue(config, "defaults.scheme").toOption

  sealed private trait ConfigValue {
    def toOption =
      this match {
        case NonEmpty(v) => Some(v)
        case _           => None
      }
  }
  private object ConfigValue {
    def apply(value: String) =
      if (value.trim.isEmpty) Empty
      else NonEmpty(value.trim)
  }
  private case object Undefined extends ConfigValue
  private case object Empty extends ConfigValue
  private case class NonEmpty(value: String) extends ConfigValue

  private def readConfigValue(config: Config, name: String) =
    if (config.hasPathOrNull(name)) {
      if (config.getIsNull(name)) Empty
      else ConfigValue(config.getString(name))
    } else Undefined

  private val serviceLookupMapping: Map[String, ServiceLookup] =
    config
      .getObject("service-name-mappings")
      .entrySet()
      .asScala
      .map { entry =>
        if (entry.getValue.valueType != ConfigValueType.OBJECT) {
          throw new IllegalArgumentException(
            s"Illegal value type in service-name-mappings: ${entry.getKey} - ${entry.getValue.valueType}")
        }
        val configEntry = entry.getValue.asInstanceOf[ConfigObject].toConfig

        val lookup: Lookup =
          readConfigValue(configEntry, "lookup").toOption
            .map(parseSrv)
            .getOrElse(Lookup(entry.getKey, defaultPortName, defaultPortProtocol))

        // if the user didn't explicitly set a value, use the default scheme,
        // otherwise honour user settings.
        val scheme =
          readConfigValue(configEntry, "scheme") match {
            case Undefined => defaultScheme
            // this is the case the user explicitly set the scheme to empty string
            case Empty           => None
            case NonEmpty(value) => Option(value)
          }

        entry.getKey -> ServiceLookup(lookup, scheme)
      }
      .toMap

  private def parseSrv(name: String) =
    if (Lookup.isValidSrv(name)) Lookup.parseSrv(name)
    else Lookup(name, defaultPortName, defaultPortProtocol)

  private[lagom] def mapLookupQuery(name: String): ServiceLookup = {
    val serviceLookup = serviceLookupMapping.getOrElse(name, ServiceLookup(parseSrv(name), defaultScheme))
    logger.debug("Lookup service '{}', mapped to {}", name: Any, serviceLookup: Any)
    serviceLookup
  }
}

private[lagom] case class ServiceLookup(lookup: Lookup, scheme: Option[String]) 
Example 17
Source File: JsonConfig.scala    From Cortex   with GNU Affero General Public License v3.0 5 votes vote down vote up
package org.thp.cortex.util

import com.typesafe.config.ConfigValueType.{BOOLEAN, NULL, NUMBER, STRING}
import com.typesafe.config.{ConfigList, ConfigObject, ConfigValue}
import play.api.Configuration
import play.api.libs.json._

import scala.collection.JavaConverters._

object JsonConfig {
  implicit val configValueWrites: Writes[ConfigValue] = Writes(
    (value: ConfigValue) ⇒
      value match {
        case v: ConfigObject             ⇒ configWrites.writes(Configuration(v.toConfig))
        case v: ConfigList               ⇒ JsArray(v.asScala.map(x ⇒ configValueWrites.writes(x)))
        case v if v.valueType == NUMBER  ⇒ JsNumber(BigDecimal(v.unwrapped.asInstanceOf[Number].toString))
        case v if v.valueType == BOOLEAN ⇒ JsBoolean(v.unwrapped.asInstanceOf[Boolean])
        case v if v.valueType == NULL    ⇒ JsNull
        case v if v.valueType == STRING  ⇒ JsString(v.unwrapped.asInstanceOf[String])
      }
  )

  implicit def configWrites = OWrites { (cfg: Configuration) ⇒
    JsObject(cfg.subKeys.map(key ⇒ key → configValueWrites.writes(cfg.underlying.getValue(key))).toSeq)
  }
}