Groovy ConfigSlurper 配置数组

发布于 2024-11-03 18:40:46 字数 409 浏览 3 评论 0原文

我正在尝试创建一个如下所示的配置:

nods = [
    nod {
        test = 1
    },
    nod {
        test = 2
    }
]

然后使用 configSlurper 读取它,但“节点”对象在读取后似乎为空。

这是我的代码:

final ConfigObject data = new ConfigSlurper().parse(new File("config.dat").toURI().toURL())
println  data.nods

和输出:

[null, null]

我做错了什么?

谢谢!

I am trying to create a config that would look something like this:

nods = [
    nod {
        test = 1
    },
    nod {
        test = 2
    }
]

and then use configSlurper to read it but the "node" objects appear to be null after the read.

Here is my code:

final ConfigObject data = new ConfigSlurper().parse(new File("config.dat").toURI().toURL())
println  data.nods

and the output:

[null, null]

What am I doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南汐寒笙箫 2024-11-10 18:40:46

它认为我是这样解决的:

config {
   nods = [
      ['name':'nod1', 'test':true],
      ['name':'nod2', 'test':flase]
   ]
}

然后使用它:

config = new ConfigSlurper().parse(new File("config.groovy").text)
for( i in 0..config.config.nods.size()-1)
    println config.config.nods[i].test

希望这对其他人有帮助!

It think I resolved it this way:

config {
   nods = [
      ['name':'nod1', 'test':true],
      ['name':'nod2', 'test':flase]
   ]
}

And then using it like:

config = new ConfigSlurper().parse(new File("config.groovy").text)
for( i in 0..config.config.nods.size()-1)
    println config.config.nods[i].test

Hope this helps someone else!!

枕梦 2024-11-10 18:40:46

在使用 ConfigSlurper 做此类事情时必须小心。
例如,您的解决方案实际上会产生以下输出:

true
[:]

如果您仔细观察,您会注意到第二个数组值 flase 上有一个拼写错误,而不是 false

以下:

def configObj = new ConfigSlurper().parse("config { nods=[[test:true],[test:false]] }")
configObj.config.nods.each { println it.test }

应该产生正确的结果:

true
false

You have to be careful when using ConfigSlurper when doing this sort of thing.
For example your solution will actually produce the following output:

true
[:]

If you look carefully you will notice that there is a typo on the second array value flase instead of false

The following:

def configObj = new ConfigSlurper().parse("config { nods=[[test:true],[test:false]] }")
configObj.config.nods.each { println it.test }

should produce the correct result:

true
false
初见终念 2024-11-10 18:40:46

我尝试用 ConfigSlurper 解析如下内容:

config {sha=[{from = 123;to = 234},{from = 234;to = 567}]}

数组“sha”与预期相距甚远。
为了将“sha”作为 ConfigObjects 数组获取,我使用了一个助手:

class ClosureScript extends Script {
    Closure closure

    def run() {
        closure.resolveStrategy = Closure.DELEGATE_FIRST
        closure.delegate = this
        closure.call()
    }
}
def item(closure) {
    def eng = new ConfigSlurper()
    def script = new ClosureScript(closure: closure)
    eng.parse(script)
}

这样我就得到了 ConfigObjects 数组:

void testSha() {
    def config = {sha=[item {from = 123;to = 234}, item {from = 234;to = 567}]}
    def xx = item(config)
    assertEquals(123, xx.sha[0].from)
}

I tried to parse with ConfigSlurper something like this:

config {sha=[{from = 123;to = 234},{from = 234;to = 567}]}

The array "sha" was far from what expected.
To get "sha" as an array of ConfigObjects I used a helper:

class ClosureScript extends Script {
    Closure closure

    def run() {
        closure.resolveStrategy = Closure.DELEGATE_FIRST
        closure.delegate = this
        closure.call()
    }
}
def item(closure) {
    def eng = new ConfigSlurper()
    def script = new ClosureScript(closure: closure)
    eng.parse(script)
}

this way I get an array of ConfigObjects:

void testSha() {
    def config = {sha=[item {from = 123;to = 234}, item {from = 234;to = 567}]}
    def xx = item(config)
    assertEquals(123, xx.sha[0].from)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文