Groovy 生成器支持
我如何使用 groovy 构建器支持构建上述模式
emp = empFileFactory.root()
{
emp(id: '3', value: '1')
emp(id:'24')
{
emp(id: '1', value: '2')
emp(id: '6', value: '7')
emp(id: '7', value: '1')
}
emp(id: '25')
{
emp(id: '1', value: '1')
emp(id: '6', value: '7')
}
}
我正在尝试在 groovy 中构建上述结构有人可以解释一下我如何实现这一点
How can i build the above pattern using groovy builder support
emp = empFileFactory.root()
{
emp(id: '3', value: '1')
emp(id:'24')
{
emp(id: '1', value: '2')
emp(id: '6', value: '7')
emp(id: '7', value: '1')
}
emp(id: '25')
{
emp(id: '1', value: '1')
emp(id: '6', value: '7')
}
}
i'm trying to build the above strucutre in groovy can some one explain how could i acieve this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以做这样的事情(这没有错误处理,只是为我不希望被调用的方法返回 null):
然后,如果我们用你所需的构建器代码调用它,如下所示:
我们可以打印出 '像这样的树
,看看我们得到了我们要求的结构:
You can do something like this (this has no error handling, and just returns null for methods that I don't expect to be called):
Then, if we call this with your required builder code like so:
We can print out the 'tree' like so
and see we get the structure we asked for:
您想要实现扩展
BuilderSupport
类,这很容易做到。这里有一个非常好的教程。
您需要实现一些方法,但命名应该非常不言自明:
createNode
创建一个节点(每个节点都有一个名称和可选属性和/或一个值)setParent
code> 将一个节点指定为另一个节点的父节点就这样。
You want to implement extend the
BuilderSupport
class, which is pretty easy to do. There's a pretty nice tutorial here.You need to implement a few methods, but the naming should be pretty self-explanatory:
createNode
creates a node (each node has a name and optional attributes and/or a value)setParent
assigns a node as another nodes parentThat's pretty much it.