YAML 预处理器/宏处理器 [英] YAML preprocessor / macro processor
问题描述
是否有一种简单的方法可以将预处理器/宏处理器与 YAML 文件一起使用?(即,我正在考虑与 C 预处理器类似的东西)?
Is there a simple way to use a preproccesor / macro-processor with YAML files? (I.e. I'm thinking of something along the lines of the C preprocessor)?
我们有很多描述各种数据结构的纯文本文件.它们目前采用我们自己的内部格式,并使用内部解析器读取.我想切换到 YAML 文件以利用各种预先存在的库进行读写.
We have a lot of flat text-files that describes various data structures. They're currently in our own in-house format, and are read with an in-house parser. I'd like to switch to YAML files to make use of the various pre-existing libraries for reading and writing.
然而,我们的文件是分层的,因为我们将主文件包含"到子文件中,并使用变量替换生成新的数据结构.
However our files are hierarchical, in that we "include" master files into sub files, and using variable substitution generate new data structures.
作为一个玩具示例,我想要这样的东西:
As a toy example I'd want something like:
country_master.yaml
name: $COUNTRY$
file: C:\data\$COUNTRY$
UK_country.yaml
#define $COUNTRY$ UK
#include <country_master.yaml>
USA_country.yaml
#define $COUNTRY$ USA
#include <country_master.yaml>
然后在预处理之后我们会得到类似的东西:
Then after preprocessing we'd get something like:
name: USA
file: C:\data\USA
C 预处理器不适用于 YAML 注释中使用的 # 字符.此外,理想情况下,我们希望有由预处理器扩展的循环,因此在上面的示例中,我们将创建 UK 和 USA 以及一个循环(我不相信您可以使用 cpp代码>).
The C-preprocessor won't work with the # character used in YAML comments. Also, ideally we'd like to have loops which are expanded by the preprocessor, so in the above example we'd create UK and USA together with a loop (and I don't believe you can loop with cpp
).
有什么想法吗?
推荐答案
# Yamp - YAML Macro-Processor
# https://github.com/birchb1024/yamp
# in master.yaml
defmacro:
name: country
args: [$COUNTRY$]
value:
name: $COUNTRY$
file: C:\data\{{$COUNTRY$}}
---
# in some file
- include: [master.yaml]
# Call with wherever needed:
{ country: USA }
这篇关于YAML 预处理器/宏处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!