2012-01-18 186 views
13

我想使用YAML創建應用程序中使用的所有存儲過程的名單以及它們從何處被調用。我設想了類似於下面的內容,但我認爲YAML不允許多層嵌套。YAML中的多級嵌套

access_log: 
    stored_proc: getsomething 
    uses: 
     usedin: some->bread->crumb 
     usedin: something else here 
    stored_proc: anothersp 
    uses: 
     usedin: blahblah 

reporting: 
    stored_proc: reportingsp 
    uses: 
     usedin: breadcrumb 

有沒有辦法在YAML中做到這一點,如果沒有,還有什麼其他的選擇?

回答

13

這就是我在YAML中爲perl腳本配置文件所使用的嵌套級別。 This YAML Tutorial可能是你如何處理你想要的Ruby結構的一個很好的參考。

我認爲你的問題是試圖混合類型。我建議修改如下:

reporting: 
    stored_procs: 
    reportingsp 
     uses: 
     usedin: breadcrumb 
    secondProc 
     uses: 
     usedin: something_else 
+0

嗯該教程不顯示深度嵌套。如果我在我的ruby腳本中加載上述結構化的yaml,它會在加載yaml文件時給我一個錯誤。 – Anthony 2012-01-18 15:38:06

+0

我絕對使用了多層次的嵌套。我注意到,我沒有做的是,你有一些東西混合在一起。例如stored_proc帶有一個值並且嵌套在下面。這可能是問題。 – Ilion 2012-01-18 15:43:38

+0

是的,這是問題。我想深層嵌套,所以一切都對齊。我想這不可能與yaml – Anthony 2012-01-18 15:45:15

13

正如@Ilion指出,你不能指着這兩個字符串和對象的屬性;你需要一個數組,或者給你的stored_proc名稱添加一個標籤。此外,當你真正想要的是一個數組時,你仍然使用相同的名字繼續運行你的密鑰。這裏有一個簡單的例子,證明了它的工作原理:

MY_YAML = " 
access_log: 
    - 
    name: getsomething 
    uses: 
     - some->bread 
     - something else here 
    - 
    name: anothersp 
    uses: 
     - blahblah" 

require 'yaml' 
require 'pp' 
pp YAML.load(MY_YAML) 
#=> {"access_log"=>[ 
#=> {"name"=>"get something", "uses"=>["some->bread", "something else here"]}, 
#=> {"name"=>"anothersp", "uses"=>["blahblah"]} 
#=> ]} 
-3
--- 
access_log: 
    - stored_proc: getsomething  
    - uses:  
    - usedin: some->bread->crumb  
    - usedin: something else here 
    - stored_proc: anothersp  
    - uses:  
    - usedin: blahblah 
reporting: 
    - stored_proc: reportingsp  
    - uses:  
    - usedin: breadcrumb 
+0

爲什麼這會消失,所以我們都可以學習? – 2015-08-30 13:55:43

+4

@Brian通過用包含單個元素字典的單元素列表替換每個字典元素,規避了無法使用同一名稱的多個字典鍵。它使結構非常深,非常奇特。嘗試將其粘貼到http://www.yamllint.com/或其他地方,您會更清楚地看到結構的真實性。 – Godsmith 2015-10-16 13:25:19