Scala Typesafe Config Printing file path, keys and values

Related searches

Trying to find a very simple and clean way to just print the filepath and keys along with values that are present inside my application.conf file when using typesafe config library in scala. I have found many examples that almost do what I need but can't figure out how to properly filter down to the keys only in file. For example, I tried below variations but it prints too many values along with what I need such as akka etc which I'm not sure where it even gets from.

  val keys = propReader.getConfig().entrySet().map(_.getKey)
  val values = propReader.getConfig().entrySet().map(_.getValue)
  for (key <- keys) if (key.contains("mykey"))
    for (value <- values) println(key, value)

Also tried but also prints too much

  private def listingPathsAndKeys(config: Config) {
    import scala.collection.JavaConversions._
    def display(codeAsString: String)(body: => Any) {
      println(body)
    }
    display("config.root().keySet()") {
      config.root().render(ConfigRenderOptions.concise())

    }
  }
  listingPathsAndKeys(propReader.getConfig())

this gives me the root level keys that ideally I would like to filter to a specific root in my file and just print the keys/values:

config.root().keySet()

This answer is based on Typesafe Config 1.3.1

To get the path of your config file first get a subtree existing in your application.conf (check @fcat's answer) returning the Config object and then use the origin() method on it. You will get the ConfigOrigin object with the url field that points to your application.conf:

config.getConfig("sample-configuration").origin()

In other words - this will return the origin of the configuration subtree, which in your case is application.conf file. However, in some cases ConfigOrigin.url can be null.

The code you presented does not contain any information on how you actually load the configuration. From what you described I assume it is ConfigFactory.load() in which case the library uses a bunch of defaults that load different configs present in the classpath. That is why you get some keys not existing in your application.conf. And since ConfigFactory.load() actually returns a merged Config from what it can find in the classpath, it is impossible to get its "merged" origin.

Scala Typesafe Config Printing file path, keys and values, and clean way to just print the filepath and keys along with values that are present inside my application.conf file when using typesafe config library in scala. Best way would be to use a .conf file and the ConfigFactory instead of having to do all the file parsing by yourself: import java.io.File import com.typesafe.config.{ Config, ConfigFactory } // this can be set into the JVM environment variables, you can easily find it on google val configPath =

I don't know how to print the path of application.conf in use. However, I can recommend a good approach for printing key-value pairs:

1. In your application.confenclose your parameters in a specified namespace.

application.conf:

 sample-configuration = {
    key1 = "value1"
    key2 = "value2"
 }

2. In your application load the configuration and print only the parameters in the specified namespace:

val config: Config = ConfigFactory.load()
val sampleConfig: Config = config.getConfig("sample-configuration")
println(sampleConfig)

lightbend/config: configuration library for JVM languages , configuration library for JVM languages using HOCON files - lightbend/config. Factor out common values; Inheritance; Optional system or env variable overrides Guice integration; Java (yep!) wrappers for the Java library; Scala wrappers for the keys do not have any syntax (they are just strings, not path expressions). Apart from loading your config through a java.nio.file.Path you can also use the following resources: String; java.io.File; scala.io.Source; java.net.URI; java.net.URL; com.typesafe.config.Config; If that's not enough it's not so difficult provide your implementation of the ConfigLoader type class and make it available in scope.

Was trying to do the same thing to figure out which value is from which config file. An alternative way is to write a script that appends the current file path to each of the value, then dump out the merged config file and you can tell which file the value is from.

Scala Examples of com.typesafe.config.ConfigFactory, Project: daml Author: digital-asset File: LedgerClientConfigTest.scala License: Apache getSeconds shouldEqual 60 config.ssl shouldBe None } "parse the expected values out of the mock config" in { val toList } def groupNames: Set[ String] = groupFilters.keys.toSet } foldLeft(ConfigFactory.empty()) { case (r, path ) => r. The following examples show how to use com.typesafe.config.Config.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to produce more good examples.

Typesafe Config & HOCON in examples, I loved their configuration syntax, the HOCON - Human-Optimized Config Object Notation. No matter what programming language are you using – Java, Scala, Config vs ConfigObject, Keys vs Paths and immutability aspects. All of my examples uses the same HOCON file as a configuration source:� There are several ways of reading configuration files in Scala including the java properties way with the deprecated Configgy and quite some more. One way which is easy and comes in handy is with the Typesafe Config project which is also used in Akka.

Configuring Spark applications with Typesafe Config, Lightbend's config library allows to use configuration files in in JVM languages, including Spark applications written in Scala. No possibility of managing optional arguments and default values The Spark code is printed here: are accessed using typed getter methods and the path of the parameter. Typesafe Config (little scala wrapper) Little scala extension to Typesafe Config. Usage build.sbt libraryDependencies += "com.github.andyglow" %% "typesafe-config-scala" % ${LATEST_VERSION} % Compile

Hence, this is not only another Typesafe's config wrapper. However, if you are already using Typesafe's config library and/or just prefer HOCON syntax for your configuration, there's an adapter that will convert a Typesafe Config object into this config's AST. See this example. Configuration Syntax. The syntax expected by this library is a JSON

Comments
  • If you provide your input and expected output, I can better help. And the other configurations come from the libraries you include in your build.sbt. When the program runs, all configurations are merged, therefore you see all of them when you print
  • thanks. just want file path it is read from since we work with multi-module project with different .conf files. so output I'm looking for would be something like: <path_to_application.conf_file> {key:value} from file without showing any of the merged library files. Just looking for what is defined in application.conf to verify that the app is reading the correct one.