[英]How do I get an unwrapped key in Typesafe Config?
測試用例:
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"Typesafe Config" should "allow me to see my escaped key" in {
val entries = ConfigFactory.parseString("""
"quoted.key.1" = 5
"quoted.key.2" = 6""").entrySet
entries.head.getKey === "quoted.key.1"
}
}
此測試失敗,因為密鑰實際上是"quoted.key.1"
,而不是quoted.key.1
。 是否有建議的方法來解開這個或我是否必須手動查找周圍的引號並每次刪除它們?
請閱讀API文檔中的“路徑,鍵和Config與ConfigObject”: http : //typesafehub.github.io/config/latest/api/com/typesafe/config/Config.html,並在README中: https://github.com/typesafehub/config#understanding-config-and-configobject
(歡迎提出改進這些文檔的建議。)
條目集(和Config)中的鍵是路徑表達式。 這些是需要解析的字符串。 ConfigUtil中有一個解析方法,請參閱http://typesafehub.github.io/config/latest/api/com/typesafe/config/ConfigUtil.html#splitPath%28java.lang.String%29
僅僅刪除引號是不行的,解析比這更復雜。 幸運的是,您可以使用ConfigUtil.splitPath
方法。
因此,在根級別迭代鍵的兩種方法類似於,首先使用Config:
Config config = ... ;
for (Map.Entry<String, ConfigValue> entry: config.entrySet()) {
String[] keys = ConfigUtil.splitPath(entry.getKey());
System.out.println("Root key = " + keys[0]);
}
然后使用ConfigObject:
Config config = ... ;
for (Map.Entry<String, ConfigValue> entry: config.root().entrySet()) {
System.out.println("Root key = " + entry.getKey());
}
我沒有嘗試編譯上面的例子,所以請原諒任何愚蠢的語法錯誤。
如果您的配置只包含一個級別(沒有嵌套對象),則上述兩種迭代方式是相同的; 但是如果你有嵌套值,它們就不一樣了,因為迭代Config
將為你提供所有葉子值,而迭代ConfigObject
( config.root()
)將為你提供root的所有直接子config.root()
,即使這些直接子節點本身就是對象。
說你有:
foo {
bar {
baz = 10
}
}
如果您將其作為Config
進行迭代,您將得到一個條目,其路徑為foo.bar.baz
為鍵,值為10
。 如果您將其作為ConfigObject
進行迭代,那么您將擁有一個具有密鑰foo
條目,該值將是一個對象,該對象將包含密鑰bar
。 當將其作為Config
迭代時,你可以將splitPath
與foo.bar.baz
,你將得到一個包含三個字符串foo
, bar
和baz
的數組。
要將Config
轉換為ConfigObject
使用root()
方法並使用toConfig()
方法將ConfigObject
轉換為Config
。 所以config.root().toConfig() == config
。
此外,上面的配置文件可以等效地寫為:
foo.bar.baz = 10
但如果寫成:
"foo.bar.baz" = 10
因為在第一種情況下,您具有嵌套對象,而在第二種情況下,您有一個在鍵名稱中具有句點的對象。 這是由於報價。
如果你用引號寫"foo.bar.baz"
,那么當迭代Config
,你會得到引用的路徑,而splitPath()
會返回一個元素foo.bar.baz
的數組。 迭代ConfigObject
您將擁有一個對象,該對象包含一個以foo.bar.baz
為鍵,值為10
的條目。 包含的密鑰.
或者其他特殊字符必須引用,因此它們被解釋為單個鍵而不是路徑。
為了使您的測試用例通過,您可以使用splitPath執行此操作:
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"Typesafe Config" should "allow me to see my escaped key" in {
val entries = ConfigFactory.parseString("""
"quoted.key.1" = 5
"quoted.key.2" = 6""").entrySet
// the ordering of entrySet is not guaranteed so this may
// still fail because it gets quoted.key.2 instead
ConfigUtil.splitPath(entries.head.getKey).head === "quoted.key.1"
}
}
您也可以使用ConfigObject
執行此操作:
import org.specs2.mutable._
class HelloWorldSpec extends Specification {
"Typesafe Config" should "allow me to see my escaped key" in {
val entries = ConfigFactory.parseString("""
"quoted.key.1" = 5
"quoted.key.2" = 6""").root.entrySet // note ".root." here
// the ordering of entrySet is not guaranteed so this may
// still fail because it gets quoted.key.2 instead
// no need to splitPath because ConfigObject has keys not paths
entries.head.getKey === "quoted.key.1"
}
}
在java中,首先將整個ConfigObject
取消扭曲到一個hashmap,然后你可以使用quoted.key.1
(不包含引號)作為獲取正確值的鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.