[英]Pretty print for a groovy ConfigObject?
I have this groovy program that creates a groovy configuration file, using a ConfigObject. 我有这个groovy程序,它使用ConfigObject创建一个groovy配置文件。 Once the ConfigObject is set up, it is written to a file using: 设置ConfigObject后,将使用以下命令将其写入文件:
myFile.withWriter {writer -> myConfigObject.writeTo(writer)}
This results in each property of the ConfigObject being written on a single line. 这导致ConfigObject的每个属性都写在一行上。 So for instance a map will be printed as: 因此,例如,一张地图将被打印为:
graphs=[["type":"std", "host":"localhost", "name":"cpurawlinux"], ["type":"std", "host":"localhost", "name":"memory"], ["type":"std", "host":"localhost", "name":"udp"] ... ]
which is quite unreadable if someone has to take a look at it. 如果有人必须看一下,这是很难理解的。 Is there a way to get a more friendly output? 有没有办法获得更友好的输出? Something like that would be great: 这样的事情会很棒:
graphs=[
["type":"std", "host":"localhost", "name":"cpurawlinux"],
["type":"std", "host":"localhost", "name":"memory"],
["type":"std", "host":"localhost", "name":"udp"]
...
]
I know I could create my own writeTo
, but isn't there already something in Groovy for that? 我知道我可以创建自己的writeTo
,但是Groovy中已经不存在此功能吗?
if it helps anyone, i had the same question and wrote this...not pretty (ha), but works: 如果它可以帮助任何人,我也有同样的问题,并写了这个...虽然不漂亮(公顷),但可以工作:
def prettyPrint(properties, level=1, stringBuilder = new StringBuilder()) {
return properties.inject(stringBuilder) { sb, name, value ->
sb.append("\n").append("\t" * level).append(name)
if (!(value instanceof Map) && !(value instanceof List)) {
return sb.append("=").append(value)
} else {
return prettyPrint(properties.getProperty(name), level+1, sb)
}
}
}
Based on mike's answer above: 根据上述迈克的答案:
def prettyPrint
prettyPrint = {obj, level = 0, sb = new StringBuilder() ->
def indent = { lev -> sb.append(" " * lev) }
if(obj instanceof Map){
sb.append("{\n")
obj.each{ name, value ->
if(name.contains('.')) return // skip keys like "a.b.c", which are redundant
indent(level+1).append(name)
(value instanceof Map) ? sb.append(" ") : sb.append(" = ")
prettyPrint(value, level+1, sb)
sb.append("\n")
}
indent(level).append("}")
}
else if(obj instanceof List){
sb.append("[\n")
obj.each{ value ->
indent(level+1)
prettyPrint(value, level+1, sb).append(",")
sb.append("\n")
}
indent(level).append("]")
}
else if(obj instanceof String){
sb.append('"').append(obj).append('"')
}
else {
sb.append(obj)
}
}
For an input like: 对于像这样的输入:
{
grails {
scaffolding {
templates.domainSuffix = "Instance"
}
enable {
native2ascii = true
blah = [ 1, 2, 3 ]
}
mime.disable.accept.header.userAgents = [ "WebKit", "Presto", "Trident" ]
}
}
Produces: 产生:
{
grails {
scaffolding {
templates {
domainSuffix = "Instance"
}
}
enable {
native2ascii = true
blah = [
1,
2,
3,
]
}
mime {
disable {
accept {
header {
userAgents = [
"WebKit",
"Presto",
"Trident",
]
}
}
}
}
}
}
Unfortunately, you'll need to write your own writeTo
as you say. 不幸的是,您需要按照自己的writeTo
编写自己的writeTo
。
If you have a config file with structure like: 如果您有一个结构类似的配置文件:
graphs {
a=["type":"std", "host":"localhost", "name":"cpurawlinux"]
b=["type":"std", "host":"localhost", "name":"memory"]
}
Then writeTo will write it out with structure, but if your config file is just a big old list of things, it will write it out as a big old list 然后writeTo将使用结构将其写出,但是如果您的配置文件只是一个很大的旧列表,它将把它写成一个很大的旧列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.