scala 迭代类型安全配置中的字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17598856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:29:06  来源:igfitidea点击:

Iterate over fields in typesafe config

scalatypesafe

提问by Egor Koshelko

I have perks.conf:

我有 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.

但我找不到合适的方法来返回配置中的所有条目。我试过config.root(),但它似乎返回了所有属性,包括系统、akka 和许多其他属性。

回答by Yuri Geinish

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

entrySet倒塌树。如果您只想迭代直接子项,请使用:

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

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

k将是“autoshield”和“immunity”,而不是“autoshield.name”、“autoshield.price”等。

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

这要求您导入scala.collection.JavaConverters._.

回答by 4lex1v

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

例如,您的代码中有以下代码 Settings.scala

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

if you call entrySeton 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:

如果您调用entrySet根配置(不是conf.root(),而是此配置的根对象),它将返回许多垃圾,您需要做的是将所有特权放在 perks.conf 中的某个路径下:

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

and then in the Settings.scalafile get this config:

然后在Settings.scala文件中获取此配置:

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

然后在此配置上调用 entrySet ,您将获得所有条目,而不是来自根对象,而是来自特权。别忘了Typesafe Config是java写的,entrySet返回java.util.Set,所以需要导入scala.collection.JavaConversions._

回答by AlonL

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

getObject给了我一个合格的 json 对象(例如timeout.ms = 5变成{ timeout: { ms: 5 })。

I ended up with:

我结束了:

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

回答by Balaji Tr

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.

这应该有效。但是,如果您的密钥集将以与 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]

回答by wood

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)
}