Pergunta

I have perks.conf:

autoshield {
    name="autoshield"
    price=2
    description="autoshield description"
}
immunity {
    name="immunity"
    price=2
    description="autoshield description"
}
premium {
    name="premium"
    price=2
    description="premium description"
}
starter {
    name="starter"
    price=2
    description="starter description"
}
jetpack {
    name="jetpack"
    price=2
    description="jetpack description"
}

And I want to iterate over perks in my application something like this:

val conf: Config = ConfigFactory.load("perks.conf")
val entries = conf.getEntries()
for (entry <- entries) yield {
  Perk(entry.getString("name"), entry.getInt("price"), entry.getString("description"))
}

But I can't find appropriate method which returns all entries from config. I tried config.root(), but it seems it returns all properties including system, akka and a lot of other properties.

Foi útil?

Solução 2

For example you have the following code in your Settings.scala

val conf = ConfigFactory.load("perks.conf")

if you call entrySet on the root config (not conf.root(), but the root object of this config) it will returns many garbage, what you need to do is to place all your perks under some path in the perks.conf:

perks {
  autoshield {
    name="autoshield"
    price=2
    description="autoshield description"
  }
  immunity {
    name="immunity"
    price=2
    description="autoshield description"
  }
}

and then in the Settings.scala file get this config:

val conf = ConfigFactory.load("perks.conf").getConfig("perks")

and then call entrySet on this config and you'll get all the entries not from the root object, but from the perks. Don't forget that Typesafe Config is written in java and entrySet returns java.util.Set, so you need to import scala.collection.JavaConversions._

Outras dicas

entrySet collapses the tree. If you want to iterate over immediate children only, use:

conf.getObject("perks").asScala.foreach({ case (k, v) => ... })

k will be "autoshield" and "immunity", but not "autoshield.name", "autoshield.price" etc.

This requires that you import scala.collection.JavaConverters._.

val common = allConfig.getConfig("column.audit")
   val commonList = common.root().keySet()
      commonList.iterator().foreach( x => { 
      println("Value is :: " + x) 
    }
   )

This should work. But if your keyset is will print in a different order than app.conf.

e.g.:

> cat application.conf

`column {
  audit {
    load_ts = "current_timestamp",
    load_file_nm = "current_filename",
    load_id = "load_id"
  }`

the script above will print this:

Value is :: [load_id, load_ts, load_file_nm]

getObject has given me a qualified json object (e.g timeout.ms = 5 becomes { timeout: { ms: 5 }).

I ended up with:

conf.getConfig(baseKey).entrySet().foreach { entry =>
   println(s"${entry.getKey} = ${entry.getValue.unwrapped().toString}")
}

To anyone who may need it:

val sysProperties = System.getProperties
val allConfig = ConfigFactory.load("perks.conf")
val appConfig = allConfig.entrySet().filter { entry =>
  !sysProperties.containsKey(entry.getKey)
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top