mirror of
https://github.com/lightbend/config.git
synced 2025-01-15 14:50:23 +08:00
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:
parent
a29475ba4a
commit
1b257fd426
@ -6,4 +6,9 @@ final class ConfigNodeArray extends ConfigNodeComplexValue {
|
|||||||
ConfigNodeArray(Collection<AbstractConfigNode> children) {
|
ConfigNodeArray(Collection<AbstractConfigNode> children) {
|
||||||
super(children);
|
super(children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ConfigNodeArray newNode(Collection<AbstractConfigNode> nodes) {
|
||||||
|
return new ConfigNodeArray(nodes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,25 +28,25 @@ abstract class ConfigNodeComplexValue extends AbstractConfigNodeValue {
|
|||||||
protected ConfigNodeComplexValue indentText(AbstractConfigNode indentation) {
|
protected ConfigNodeComplexValue indentText(AbstractConfigNode indentation) {
|
||||||
ArrayList<AbstractConfigNode> childrenCopy = new ArrayList<AbstractConfigNode>(children);
|
ArrayList<AbstractConfigNode> childrenCopy = new ArrayList<AbstractConfigNode>(children);
|
||||||
for (int i = 0; i < childrenCopy.size(); i++) {
|
for (int i = 0; i < childrenCopy.size(); i++) {
|
||||||
if (childrenCopy.get(i) instanceof ConfigNodeSingleToken &&
|
AbstractConfigNode child = childrenCopy.get(i);
|
||||||
Tokens.isNewline(((ConfigNodeSingleToken) childrenCopy.get(i)).token())) {
|
if (child instanceof ConfigNodeSingleToken &&
|
||||||
|
Tokens.isNewline(((ConfigNodeSingleToken) child).token())) {
|
||||||
childrenCopy.add(i + 1, indentation);
|
childrenCopy.add(i + 1, indentation);
|
||||||
i++;
|
i++;
|
||||||
} else if (childrenCopy.get(i) instanceof ConfigNodeField) {
|
} else if (child instanceof ConfigNodeField) {
|
||||||
AbstractConfigNode value = ((ConfigNodeField) childrenCopy.get(i)).value();
|
AbstractConfigNode value = ((ConfigNodeField) child).value();
|
||||||
if (value instanceof ConfigNodeComplexValue) {
|
if (value instanceof ConfigNodeComplexValue && !(value instanceof ConfigNodeInclude)) {
|
||||||
childrenCopy.set(i, ((ConfigNodeField) childrenCopy.get(i)).replaceValue(((ConfigNodeComplexValue) value).indentText(indentation)));
|
childrenCopy.set(i, ((ConfigNodeField) child).replaceValue(((ConfigNodeComplexValue) value).indentText(indentation)));
|
||||||
}
|
}
|
||||||
} else if (childrenCopy.get(i) instanceof ConfigNodeComplexValue) {
|
} else if (child instanceof ConfigNodeComplexValue && !(child instanceof ConfigNodeInclude)) {
|
||||||
childrenCopy.set(i, ((ConfigNodeComplexValue) childrenCopy.get(i)).indentText(indentation));
|
childrenCopy.set(i, ((ConfigNodeComplexValue) child).indentText(indentation));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this instanceof ConfigNodeArray) {
|
return newNode(childrenCopy);
|
||||||
return new ConfigNodeArray(childrenCopy);
|
|
||||||
} else if (this instanceof ConfigNodeObject) {
|
|
||||||
return new ConfigNodeObject(childrenCopy);
|
|
||||||
} else {
|
|
||||||
return new ConfigNodeConcatenation(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);
|
||||||
}
|
}
|
||||||
|
@ -6,4 +6,9 @@ final class ConfigNodeConcatenation extends ConfigNodeComplexValue {
|
|||||||
ConfigNodeConcatenation(Collection<AbstractConfigNode> children) {
|
ConfigNodeConcatenation(Collection<AbstractConfigNode> children) {
|
||||||
super(children);
|
super(children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ConfigNodeConcatenation newNode(Collection<AbstractConfigNode> nodes) {
|
||||||
|
return new ConfigNodeConcatenation(nodes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.typesafe.config.impl;
|
package com.typesafe.config.impl;
|
||||||
|
|
||||||
|
import com.typesafe.config.ConfigException;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
final class ConfigNodeInclude extends ConfigNodeComplexValue {
|
final class ConfigNodeInclude extends ConfigNodeComplexValue {
|
||||||
@ -10,6 +12,11 @@ final class ConfigNodeInclude extends ConfigNodeComplexValue {
|
|||||||
this.kind = kind;
|
this.kind = kind;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ConfigNodeInclude newNode(Collection<AbstractConfigNode> nodes) {
|
||||||
|
throw new ConfigException.BugOrBroken("Tried to indent an include node");
|
||||||
|
}
|
||||||
|
|
||||||
protected ConfigIncludeKind kind() {
|
protected ConfigIncludeKind kind() {
|
||||||
return kind;
|
return kind;
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,11 @@ final class ConfigNodeObject extends ConfigNodeComplexValue {
|
|||||||
super(children);
|
super(children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ConfigNodeObject newNode(Collection<AbstractConfigNode> nodes) {
|
||||||
|
return new ConfigNodeObject(nodes);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean hasValue(Path desiredPath) {
|
public boolean hasValue(Path desiredPath) {
|
||||||
for (AbstractConfigNode node : children) {
|
for (AbstractConfigNode node : children) {
|
||||||
if (node instanceof ConfigNodeField) {
|
if (node instanceof ConfigNodeField) {
|
||||||
|
@ -15,6 +15,11 @@ final class ConfigNodeRoot extends ConfigNodeComplexValue {
|
|||||||
this.origin = origin;
|
this.origin = origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ConfigNodeRoot newNode(Collection<AbstractConfigNode> nodes) {
|
||||||
|
throw new ConfigException.BugOrBroken("Tried to indent the root object");
|
||||||
|
}
|
||||||
|
|
||||||
protected ConfigNodeComplexValue value() {
|
protected ConfigNodeComplexValue value() {
|
||||||
for (AbstractConfigNode node : children) {
|
for (AbstractConfigNode node : children) {
|
||||||
if (node instanceof ConfigNodeComplexValue) {
|
if (node instanceof ConfigNodeComplexValue) {
|
||||||
|
@ -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())
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user