-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copied ConfigParse to ConfigBuilder which uses NodeBuilder
- Loading branch information
Yasuharu NAKANO
committed
Apr 2, 2012
1 parent
d482e86
commit 5885e93
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/groovy/org/jggug/javaonetokyo/bof/dsl/ConfigBuilder.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/test/groovy/org/jggug/javaonetokyo/bof/dsl/ConfigBuilderTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |