Iterate over fields in typesafe config

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._.


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._

Tags:

Scala

Typesafe