Address PR feedback and add new test

Cleanup the indentText() method on ConfigNodeComplexValue as per
PR feedback. Add a test for indentation when inserting a value
with an include statement.
This commit is contained in:
Preben Ingvaldsen 2015-04-01 09:54:34 -07:00
parent a29475ba4a
commit 1b257fd426
7 changed files with 51 additions and 15 deletions

View File

@ -6,4 +6,9 @@ final class ConfigNodeArray extends ConfigNodeComplexValue {
ConfigNodeArray(Collection<AbstractConfigNode> children) {
super(children);
}
@Override
protected ConfigNodeArray newNode(Collection<AbstractConfigNode> nodes) {
return new ConfigNodeArray(nodes);
}
}

View File

@ -28,25 +28,25 @@ abstract class ConfigNodeComplexValue extends AbstractConfigNodeValue {
protected ConfigNodeComplexValue indentText(AbstractConfigNode indentation) {
ArrayList<AbstractConfigNode> childrenCopy = new ArrayList<AbstractConfigNode>(children);
for (int i = 0; i < childrenCopy.size(); i++) {
if (childrenCopy.get(i) instanceof ConfigNodeSingleToken &&
Tokens.isNewline(((ConfigNodeSingleToken) childrenCopy.get(i)).token())) {
AbstractConfigNode child = childrenCopy.get(i);
if (child instanceof ConfigNodeSingleToken &&
Tokens.isNewline(((ConfigNodeSingleToken) child).token())) {
childrenCopy.add(i + 1, indentation);
i++;
} else if (childrenCopy.get(i) instanceof ConfigNodeField) {
AbstractConfigNode value = ((ConfigNodeField) childrenCopy.get(i)).value();
if (value instanceof ConfigNodeComplexValue) {
childrenCopy.set(i, ((ConfigNodeField) childrenCopy.get(i)).replaceValue(((ConfigNodeComplexValue) value).indentText(indentation)));
} else if (child instanceof ConfigNodeField) {
AbstractConfigNode value = ((ConfigNodeField) child).value();
if (value instanceof ConfigNodeComplexValue && !(value instanceof ConfigNodeInclude)) {
childrenCopy.set(i, ((ConfigNodeField) child).replaceValue(((ConfigNodeComplexValue) value).indentText(indentation)));
}
} else if (childrenCopy.get(i) instanceof ConfigNodeComplexValue) {
childrenCopy.set(i, ((ConfigNodeComplexValue) childrenCopy.get(i)).indentText(indentation));
} else if (child instanceof ConfigNodeComplexValue && !(child instanceof ConfigNodeInclude)) {
childrenCopy.set(i, ((ConfigNodeComplexValue) child).indentText(indentation));
}
}
if (this instanceof ConfigNodeArray) {
return new ConfigNodeArray(childrenCopy);
} else if (this instanceof ConfigNodeObject) {
return new ConfigNodeObject(childrenCopy);
} else {
return new ConfigNodeConcatenation(childrenCopy);
}
return newNode(childrenCopy);
}
// This method will just call into the object's constructor, but it's needed
// for use in the indentText() method so we can avoid a gross if/else statement
// checking the type of this
abstract ConfigNodeComplexValue newNode(Collection<AbstractConfigNode> nodes);
}

View File

@ -6,4 +6,9 @@ final class ConfigNodeConcatenation extends ConfigNodeComplexValue {
ConfigNodeConcatenation(Collection<AbstractConfigNode> children) {
super(children);
}
@Override
protected ConfigNodeConcatenation newNode(Collection<AbstractConfigNode> nodes) {
return new ConfigNodeConcatenation(nodes);
}
}

View File

@ -1,5 +1,7 @@
package com.typesafe.config.impl;
import com.typesafe.config.ConfigException;
import java.util.Collection;
final class ConfigNodeInclude extends ConfigNodeComplexValue {
@ -10,6 +12,11 @@ final class ConfigNodeInclude extends ConfigNodeComplexValue {
this.kind = kind;
}
@Override
protected ConfigNodeInclude newNode(Collection<AbstractConfigNode> nodes) {
throw new ConfigException.BugOrBroken("Tried to indent an include node");
}
protected ConfigIncludeKind kind() {
return kind;
}

View File

@ -10,6 +10,11 @@ final class ConfigNodeObject extends ConfigNodeComplexValue {
super(children);
}
@Override
protected ConfigNodeObject newNode(Collection<AbstractConfigNode> nodes) {
return new ConfigNodeObject(nodes);
}
public boolean hasValue(Path desiredPath) {
for (AbstractConfigNode node : children) {
if (node instanceof ConfigNodeField) {

View File

@ -15,6 +15,11 @@ final class ConfigNodeRoot extends ConfigNodeComplexValue {
this.origin = origin;
}
@Override
protected ConfigNodeRoot newNode(Collection<AbstractConfigNode> nodes) {
throw new ConfigException.BugOrBroken("Tried to indent the root object");
}
protected ConfigNodeComplexValue value() {
for (AbstractConfigNode node : children) {
if (node instanceof ConfigNodeComplexValue) {

View File

@ -426,4 +426,13 @@ class ConfigDocumentTest extends TestUtils {
assertEquals("a {\n b {\n f : 10\n c : {\n d:e\n }\n }\n}", configDocument.setValue("a.b.c", "{\n d:e\n}").render())
}
@Test
def configDocumentIndentationValueWithIncludeTest {
val origText = "a {\n b {\n c : 22\n }\n}"
val configDocument = ConfigDocumentFactory.parseString(origText)
assertEquals("a {\n b {\n c : 22\n d : {\n include \"foo\"\n e:f\n }\n }\n}",
configDocument.setValue("a.b.d", "{\n include \"foo\"\n e:f\n}").render())
}
}