【发布时间】:2019-08-11 00:11:30
【问题描述】:
我有以下 YAML 文件:
site:
title: My blog
domain: example.com
author1:
name: bob
url: /author/bob
author2:
name: jane
url: /author/jane
header_links:
about:
title: About
url: about.html
contact:
title: Contact Us
url: contactus.html
js_deps:
- cashjs
- jets
products:
product1:
name: Prod One
price: 10
product2:
name: Prod Two
price: 20
我想要一个 Bash、Python 或 AWK 函数或脚本,它可以将上面的 YAML 文件作为输入 ($1),然后生成然后执行以下代码(或者确切地说等效):
unset site_title
unset site_domain
unset site_author1
unset site_author2
unset site_header_links
unset site_header_links_about
unset site_header_links_contact
unset js_deps
site_title="My blog"
site_domain="example.com"
declare -A site_author1
declare -A site_author2
site_author1=(
[name]="bob"
[url]="/author/bob"
)
site_author2=(
[name]="jane"
[url]="/author/jane"
)
declare -A site_header_links_about
declare -A site_header_links_contact
site_header_links_about=(
[name]="About"
[url]="about.html"
)
site_header_links_contact=(
[name]="Contact Us"
[url]="contact.html"
)
site_header_links=(site_header_links_about site_header_links_contact)
js_deps=(cashjs jets)
unset products
unset product1
unset product2
declare -A product1
declare -A product2
product1=(
[name]="Prod One"
[price]=10
)
product2=(
[name]="Prod Two"
[price]=20
)
products=(product1 product2)
所以,逻辑是:
遍历 YAML,并在最后(底部)级别创建带有字符串值的下划线连接变量名称,except,其中应尽可能将数据创建为关联数组或索引数组。 .. 此外,创建的任何关联数组都应按名称列出,在索引数组中。
所以,换句话说:
只要最后一级数据可以转换为关联数组,那么它应该是 (
foo.bar.hash=>${foo_bar_hash[@]}只要最后一级数据可以转换为索引数组,那么它应该是 (
foo.bar.list=>${foo_bar_list[@]}每个 assoc 数组都应按名称列在一个索引数组中,该数组以其在 yaml 数据中的父级命名(参见示例中的
products)否则,只需创建一个下划线连接的 var 名称并将值保存为字符串 (
foo.bar.string=>${foo_bar_string}
...我需要这个特定的 Bash 数据结构的原因是我正在使用一个需要它的基于 Bash 的模板系统。
一旦有了我需要的功能,我就可以在我的模板中轻松使用 YAML 数据,如下所示:
{{site_title}}
...
{{#foreach link in site_header_links}}
<a href="{{link.url}}">{{link.name}}</a>
{{/foreach}}
...
{{#js_deps}}
{{.}}
{{/js_deps}}
...
{{#foreach item in products}}
{{item.name}}
{{item.price}}
{{/foreach}}
我尝试了什么:
这与我之前提出的一个问题完全相关:
这太接近了,但我还需要一个 site_header_links 的关联数组才能正常生成 .. 它失败了,因为 site_header_links 嵌套太深。
我仍然希望在解决方案中使用https://github.com/azohra/yaml.sh,因为它也可以为模板系统提供简单的把手样式lookup rip-off :)
编辑:
非常清楚:解决方案不能使用pip、virtualenv,或任何其他需要单独安装的外部部门——它必须是一个独立的脚本/函数(如@ 987654323@ 是),它可以存在于 CMS 项目目录中......或者我不需要在这里......
...
希望,一个很好的评论答案可能会帮助我避免回到这里;)
【问题讨论】:
-
我不太清楚我尝试了什么,什么不起作用 - 我在 OP 中声明我以前的帖子对我不起作用,因为“我需要一个
site_header_links的关联数组生成的也很好..它失败了,因为site_header_links嵌套太深”我尝试了很多东西,但都没有工作——只是破解了以前的解决方案,但无济于事。我不认为它很宽泛——我只想要 90% 的基于 shell 的 YAML 解析器所做的事情。要创建_串联变量——except 我想要最后一级的索引/关联数组(以及索引数组中按名称列出的关联数组).. -
我已经通过链接到另一个问题来总结我已经尝试过的内容,这是我在这里的旅程的一部分,并包含指向我试图破解的所有库的链接,如以及最接近的解决方案是什么,以及为什么它仍然不能完全满足我的需求...我认为用我遇到的所有 很多很多 失败向我的帖子发送垃圾邮件是不合适的,当我链接到的库比我得到的更接近时.. :/
-
为什么输出中有变量
product1而不是products_product1?哪里没有products_product1_name变量?你如何决定哪个级别的 get 是一个关联数组,哪个用下划线命名?为什么会有site_header_links_contact?但是没有site=([title]="My blog")array?为什么是site_title变量而不是site数组? -
我不需要
products_product1_name,因为我将拥有product1[name],可以像{{foreach product in products}}一样访问(在我的模板系统中)..这就是为什么..我不需要包含的变量数组中已经存在的东西 .. 可以 在底层创建数组的地方,它们应该是(而不是你本来拥有的连接变量)......没有site数组,因为,作为众所周知,Bash 不做多维对象,也不在底层..... -
所以换句话说,我想要底层的数组,因为 Bash 可以做到,但它不能制作其他级别的多维数组,所以按名称列出数组是一个 hack ...一旦数据结构到位,我的模板系统就可以迭代事物就好像它们是 2 级(或更多)深 ..
标签: arrays bash yaml associative-array