[英]Using Groovy 3 YamlBuilder with Yaml that contains a hyphen
I'm trying to write a Groovy 3 script that uses yamlbuilder to write a yaml file.我正在尝试编写一个使用 yamlbuilder 编写 yaml 文件的 Groovy 3 脚本。 I have it working on almost everything apart from;
除了以下内容,我几乎可以处理所有事情;
execution:
set-props:
url:http://myhouse.net
port:8000
How do I write a map that allows the use of a hyphen in the name?如何编写允许在名称中使用连字符的映射? Following my previous work I foolishly tried;
继我之前的工作之后,我愚蠢地尝试了;
def setprops=[:]
setprops=(["url":"http://myhouse.net","port":"8000"])
execution.set-props=setprops
Which gives me an error 'The LHS of an assignment should be a variable or a field'.这给了我一个错误“赋值的 LHS 应该是一个变量或一个字段”。
If I just use execution.setprops then it works fine, but of course the resulting yaml from yaml(execution) is invalid.如果我只使用 execution.setprops 那么它工作正常,但当然 yaml(execution) 产生的 yaml 是无效的。
I think if the set-props was aa key/value pair then it could go into quote and everything would be good.我认为如果 set-props 是一个键/值对,那么它可以引用,一切都会好起来的。 But because it is part of the structure I don't know what needs to be done.
但是因为它是结构的一部分,我不知道需要做什么。
You can use strings as "methods" and the builder will create your intermediate structures from them:您可以使用字符串作为“方法”,构建器将从它们创建您的中间结构:
import groovy.yaml.YamlBuilder
def b = new YamlBuilder()
b.execution {
"set-props"(
url: "..."
)
}
println b
Or to continue on your example: You can create the whole map and use is as argument, where you want to have that content.或者继续您的示例:您可以创建整个地图并使用 is 作为参数,您希望在其中包含该内容。
def setprops=["set-props": [url:"..."]]
b.execution(setprops)
Both result in:两者都会导致:
---
execution:
set-props:
url: "..."
Note that the first version nests via passed closures and then passes in the map.请注意,第一个版本通过传递的闭包嵌套,然后传入映射。 The second bit just passes a nested map.
第二位只是传递一个嵌套映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.