Skip to content

Commit

Permalink
copied ConfigParse to ConfigBuilder which uses NodeBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasuharu NAKANO committed Apr 2, 2012
1 parent d482e86 commit 5885e93
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package src.main.groovy.org.jggug.javaonetokyo.bof.dsl

class ConfigBuilder {

Properties build(Closure dsl) {
def root = new NodeBuilder().config(dsl)
def props = new Properties()
traverse(root.value(), props, [])
return props
}

void traverse(nodes, props, stack) {
nodes.each { node ->
def newStack = stack + node.name()
def value = node.value()
if (value in List) {
traverse(value, props, newStack)
} else {
def key = newStack.join(".")
props[key] = value
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package src.main.groovy.org.jggug.javaonetokyo.bof.dsl

import spock.lang.*

class ConfigBuilderSpec extends Specification {

def builder

def setup() {
builder = new ConfigBuilder()
}

def "入れご構造でドット区切りが表現できる"() {
setup:
def config = {
x {
y {
z "value of z"
}
}
}

when:
def props = builder.build(config)

then:
props."x.y.z" == "value of z"
props.size() == 1
}

def "入れご構造でドット区切りが表現できる。複数キーの場合"() {
setup:
def config = {
x {
a "value of a"
y {
b "value of b"
z "value of z"
}
c "value of c"
}
}

when:
def props = builder.build(config)

then:
props."x.a" == "value of a"
props."x.y.b" == "value of b"
props."x.y.z" == "value of z"
props."x.c" == "value of c"
props.size() == 4
}
}

0 comments on commit 5885e93

Please sign in to comment.