我是ConfigObject的新手。我打算用它来存储分析工作的结果。但偶尔我可能需要添加新的配置字段,并且我很难将变量添加到ConfigObject。所以我决定添加一张地图。但是没有正确格式化,导致回读时出错。我不知道该怎么办..
config.groovy file
-------------------
"serverX" {
var {
low=-12.89
maybe_low=1.65
maybe_high=40.45
high=55
}
}
rest of Script
----------------------
ConfigObject conf = new ConfigSlurper("development").parse(new File("config.groovy").toURI().toURL());
System.out.println(conf);
// New object to add
def var_stuff = ['low' : 0] + ['maybe_low' : 0] + ['maybe_high' : 0] + ['high' : 0]
def var_object = ['var' : var_stuff]
def final_var_object = ['ServerY' : var_object]
def new_config_object = final_var_object as ConfigObject
conf.merge(new_config_object)
def file1 = new File('newconfig.groovy')
file1.withWriter('UTF-8') { writer ->
conf.writeTo(writer)
}
但结果却是
serverX {
var {
low=-12.89
maybe_low=1.65
maybe_high=40.45
high=55
}
}
serverY=["var":["low":0, "maybe_low":0, "maybe_high":0, "high":0]]
我确实考虑过以下
ConfigObject conf_new = new ConfigObject();
conf_new."ServerY".tukey.low = 0
conf_new."ServerY".tukey.maybe_low = 0
conf_new."ServerY".tukey.maybe_high = 0
conf.merge(conf_new)
哪个效果很好。但由于“ServerY”对我来说是一个变量,我不能写这样的手册。
任何提示?
编辑:2016年3月5日 根据Emmanuel的建议,我尝试了以下
def server = 'ServerY'
def more = [
(server): [
var: [
low: 0,
maybe_low: 0,
maybe_high: 0,
high: 0
]
]
]
conf.putAll(more)
def obj1_temp = conf as Map
System.out.println(obj1_temp)
/* which looks like this:
{ServerX={var={low=-12.89, maybe_low=1.65, maybe_high=40.45, high=55}}, ServerY={var={low=0, maybe_low=0, maybe_high=0, high=0}}}
*/
def file1 = new File('app/tasks/newconfig.groovy')
file1.withWriter('UTF-8') { writer ->
conf.writeTo(writer)
}
然而,文件中的输出仍然与以前类似: - (
ServerX {
var {
low=-12.89
maybe_low=1.65
maybe_high=40.45
high=55
}
}
ServerY=["var":["low":0, "maybe_low":0, "maybe_high":0, "high":0]]
在回读时无法正确解析。
答案 0 :(得分:1)
您可以使用putMap()
将Map
条目添加到ConfigObject
。
import groovy.util.ConfigSlurper
def content = '''
"serverX" {
var {
low=-12.89
maybe_low=1.65
maybe_high=40.45
high=55
}
}
'''
def conf = new ConfigSlurper("development").parse(content) // Using a String for demonstration purposes
def server = 'ServerY'
def more = [
(server): [
var: [
low: 0,
maybe_low: 0,
maybe_high: 0,
high: 0
]
]
]
conf.putAll(more)
// A test showing it works.
assert conf as Map == [
serverX:[
var:[
low:-12.89,
maybe_low:1.65,
maybe_high:40.45,
high:55]
],
ServerY:[
var:[
low:0,
maybe_low:0,
maybe_high:0,
high:0
]
]
]
所以,putAll()
没有像我预期的那样工作。这是解决方案:
import groovy.util.ConfigSlurper
import groovy.util.ConfigObject
def content = '''
"serverX" {
var {
low=-12.89
maybe_low=1.65
maybe_high=40.45
high=55
}
}
'''
def mergeConfig = { configObject, server, map ->
configObject.merge new ConfigObject().with {
def var = getProperty(server).getProperty('var')
map.each { key, value ->
var.put(key, value)
}
delegate
}
configObject
}
def conf = new ConfigSlurper("development").parse(content) // Using a String for demonstration purposes
conf = mergeConfig(conf, 'ServerY', [low: 0, maybe_low: 0, maybe_high: 0, high: 0])
您现在可以连续致电mergeConfig()
以合并配置。在这个例子中,mergeConfig()
是一个Closure,但它也可以作为一种方法。