使用 YAML 标签来表示类型
我不太明白如何使用应用程序特定的 YAML 标签,也许是因为我对它们的期望使用完全是错误的。我使用 YAML 作为配置文件,并希望使用标签为我的配置加载器提供一个提示,告诉它应该将数据解析成什么数据类型 - 应用程序特定的数据类型。
我也在使用 libyaml 和 C。
所以我试图做一些类似的事情...
shapes:
square: "0,4,8,16"
circle: "5,10"
在我的应用程序中,我想使用标签作为提示,这样我就可以将 square 的值加载到我的 square 数据结构中,并将值加载到我的 square 数据结构中将圆放入我的圆数据结构中(这些值在本例中没有任何意义)。
所以我目前正在做:
shapes:
square: !square "0,4,8,16"
circle: !circle "5,10"
当我传递标量“0,4,8,16”时,Libyaml 将提供“!square”标签。使用此标签为我的加载程序提供如何处理标量的提示是否有效?
因为它确实对我有用,所以我更好奇它是否合适。如果不是,我该如何让这变得更合适。
谢谢。
I don't quite understand how to use application specific YAML tags, and maybe its because my desired use of them is purely wrong. I am using YAML for a configuration file and was hoping to use tags to provide my configuration loader with a hint as to what datatype it should parse the data into - application specific datatypes.
I'm also using libyaml with C.
So I'm trying to do something like...
shapes:
square: "0,4,8,16"
circle: "5,10"
In my app I'd like to use tags as hints so I can load the values of square into my square data structure, and the values of circle into my circle data structure (these values mean nothing in this example).
So I'm currently doing:
shapes:
square: !square "0,4,8,16"
circle: !circle "5,10"
Libyaml will provide a tag of "!square" when I'm passed the scalar "0,4,8,16". Is it valid to use this tag to provide my loader with a hint of how to process the scalar?
Since it does work for me, I'm more curious to know if its proper. And if not, how would I go about making this more proper.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这是一个古老的问题,但无论如何我之前见过 !int 等在 yaml 文件中使用,所以我去查找规范 Yaml 1.2 Spec # 标签
根据文档,您对标签的预期用法对于应用程序特定标签来说确实是正确的。
I know that this is an ancient question, but anyway I've seen !int, etc being used in yaml files before so I went to look up the specs at Yaml 1.2 Spec # Tags
As per the document, it does look like your intended usage of tags is correct for application specific tag.