2012-01-18 360 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