Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/src/main/java/com/typesafe/config/impl/ConfigNodeField.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ protected Token separator() {
return null;
}

protected int newlineCountBeforeValue() {
int newlineCount = 0;
for (AbstractConfigNode child : children) {
if (child instanceof AbstractConfigNodeValue)
return newlineCount;

if (child instanceof ConfigNodeSingleToken
&& Tokens.isNewline(((ConfigNodeSingleToken) child).token())) {
newlineCount++;
}
}
return newlineCount;
}

protected List<String> comments() {
List<String> comments = new ArrayList<String>();
for (AbstractConfigNode child : children) {
Expand Down
16 changes: 11 additions & 5 deletions config/src/main/java/com/typesafe/config/impl/ConfigParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ private AbstractConfigValue parseValue(AbstractConfigNodeValue n, List<String> c
return v;
}

private void advanceLineNumberBeforeValue(ConfigNodeField field) {
lineNumber += field.newlineCountBeforeValue();
}

private static AbstractConfigObject createValueUnderPath(Path path,
AbstractConfigValue value) {
// for path foo.bar, we are creating
Expand Down Expand Up @@ -237,13 +241,14 @@ private AbstractConfigObject parseObject(ConfigNodeObject n) {
parseInclude(values, (ConfigNodeInclude)node);
lastWasNewline = false;
} else if (node instanceof ConfigNodeField) {
ConfigNodeField field = (ConfigNodeField) node;
lastWasNewline = false;
Path path = ((ConfigNodeField) node).path().value();
comments.addAll(((ConfigNodeField) node).comments());
Path path = field.path().value();
comments.addAll(field.comments());

// path must be on-stack while we parse the value
pathStack.push(path);
if (((ConfigNodeField) node).separator() == Tokens.PLUS_EQUALS) {
if (field.separator() == Tokens.PLUS_EQUALS) {
// we really should make this work, but for now throwing
// an exception is better than producing an incorrect
// result. See
Expand All @@ -262,12 +267,13 @@ private AbstractConfigObject parseObject(ConfigNodeObject n) {
AbstractConfigNodeValue valueNode;
AbstractConfigValue newValue;

valueNode = ((ConfigNodeField) node).value();
valueNode = field.value();
advanceLineNumberBeforeValue(field);

// comments from the key token go to the value token
newValue = parseValue(valueNode, comments);

if (((ConfigNodeField) node).separator() == Tokens.PLUS_EQUALS) {
if (field.separator() == Tokens.PLUS_EQUALS) {
arrayCount -= 1;

List<AbstractConfigValue> concat = new ArrayList<AbstractConfigValue>(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,21 @@ class ConfParserTest extends TestUtils {
assertEquals("bar", conf.getString("foo"))
}

@Test
def valuesAfterSeparatorNewlineHaveCorrectOriginLine() {
val scalar = parseConfig("a=\n42")
assertEquals(2, scalar.getValue("a").origin().lineNumber())

val objectAfterEquals = parseConfig("a=\n{\n b=1\n}")
assertEquals(2, objectAfterEquals.getObject("a").origin().lineNumber())

val objectAfterColon = parseConfig("a:\n{\n b=1\n}")
assertEquals(2, objectAfterColon.getObject("a").origin().lineNumber())

val arrayAfterEquals = parseConfig("a=\n[\n 1\n]")
assertEquals(2, arrayAfterEquals.getList("a").origin().lineNumber())
}

@Test
def acceptMultiPeriodNumericPath() {
val conf1 = ConfigFactory.parseString("0.1.2.3=foobar1")
Expand Down