Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ private ConfigNodeComplexValue parseObject(boolean hadOpenCurly) {
boolean afterComma = false;
Path lastPath = null;
boolean lastInsideEquals = false;
ConfigOrigin objectOrigin = baseOrigin.withLineNumber(lineNumber);
ArrayList<AbstractConfigNode> objectNodes = new ArrayList<AbstractConfigNode>();
ArrayList<AbstractConfigNode> keyValueNodes;
HashMap<String, Boolean> keys = new HashMap<String, Boolean>();
Expand Down Expand Up @@ -539,11 +540,12 @@ private ConfigNodeComplexValue parseObject(boolean hadOpenCurly) {
}
}

return new ConfigNodeObject(objectNodes);
return new ConfigNodeObject(objectNodes, objectOrigin);
}

private ConfigNodeComplexValue parseArray() {
ArrayList<AbstractConfigNode> children = new ArrayList<AbstractConfigNode>();
ConfigOrigin arrayOrigin = baseOrigin.withLineNumber(lineNumber);
children.add(new ConfigNodeSingleToken(Tokens.OPEN_SQUARE));
// invoked just after the OPEN_SQUARE
Token t;
Expand All @@ -557,7 +559,7 @@ private ConfigNodeComplexValue parseArray() {
// special-case the first element
if (t == Tokens.CLOSE_SQUARE) {
children.add(new ConfigNodeSingleToken(t));
return new ConfigNodeArray(children);
return new ConfigNodeArray(children, arrayOrigin);
} else if (Tokens.isValue(t) || t == Tokens.OPEN_CURLY
|| t == Tokens.OPEN_SQUARE || Tokens.isUnquotedText(t)
|| Tokens.isSubstitution(t)) {
Expand All @@ -581,7 +583,7 @@ private ConfigNodeComplexValue parseArray() {
t = nextTokenCollectingWhitespace(children);
if (t == Tokens.CLOSE_SQUARE) {
children.add(new ConfigNodeSingleToken(t));
return new ConfigNodeArray(children);
return new ConfigNodeArray(children, arrayOrigin);
} else {
throw parseError("List should have ended with ] or had a comma, instead had token: "
+ t
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.typesafe.config.impl;

import com.typesafe.config.ConfigOrigin;

import java.util.Collection;

final class ConfigNodeArray extends ConfigNodeComplexValue {
ConfigNodeArray(Collection<AbstractConfigNode> children) {
super(children);
}

ConfigNodeArray(Collection<AbstractConfigNode> children, ConfigOrigin origin) {
super(children, origin);
}

@Override
protected ConfigNodeArray newNode(Collection<AbstractConfigNode> nodes) {
return new ConfigNodeArray(nodes);
return new ConfigNodeArray(nodes, origin());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
*/
package com.typesafe.config.impl;

import com.typesafe.config.ConfigOrigin;

import java.util.*;

abstract class ConfigNodeComplexValue extends AbstractConfigNodeValue {
final protected ArrayList<AbstractConfigNode> children;
final private ConfigOrigin origin;

ConfigNodeComplexValue(Collection<AbstractConfigNode> children) {
this(children, null);
}

ConfigNodeComplexValue(Collection<AbstractConfigNode> children, ConfigOrigin origin) {
this.children = new ArrayList<AbstractConfigNode>(children);
this.origin = origin;
}

final public Collection<AbstractConfigNode> children() {
return children;
}

final public ConfigOrigin origin() {
return origin;
}

@Override
protected Collection<Token> tokens() {
ArrayList<Token> tokens = new ArrayList<Token>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.typesafe.config.impl;

import com.typesafe.config.ConfigOrigin;
import com.typesafe.config.ConfigSyntax;

import java.util.ArrayList;
Expand All @@ -10,9 +11,13 @@ final class ConfigNodeObject extends ConfigNodeComplexValue {
super(children);
}

ConfigNodeObject(Collection<AbstractConfigNode> children, ConfigOrigin origin) {
super(children, origin);
}

@Override
protected ConfigNodeObject newNode(Collection<AbstractConfigNode> nodes) {
return new ConfigNodeObject(nodes);
return new ConfigNodeObject(nodes, origin());
}

public boolean hasValue(Path desiredPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class ConfigNodeRoot extends ConfigNodeComplexValue {
final private ConfigOrigin origin;

ConfigNodeRoot(Collection<AbstractConfigNode> children, ConfigOrigin origin) {
super(children);
super(children, origin);
this.origin = origin;
}

Expand Down
15 changes: 13 additions & 2 deletions config/src/main/java/com/typesafe/config/impl/ConfigParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ private SimpleConfigOrigin lineOrigin() {
return ((SimpleConfigOrigin) baseOrigin).withLineNumber(lineNumber);
}

// prefer origin captured at tokenize time (should be correct even when
// following a multiline string); falls back to the line counter for
// nodes built without origin.
private SimpleConfigOrigin nodeOrigin(ConfigNodeComplexValue n) {
ConfigOrigin origin = n.origin();
if (origin != null)
return (SimpleConfigOrigin) origin;
else
return lineOrigin();
}

private ConfigException parseError(String message) {
return parseError(message, null);
}
Expand Down Expand Up @@ -220,7 +231,7 @@ private void parseInclude(Map<String, AbstractConfigValue> values, ConfigNodeInc

private AbstractConfigObject parseObject(ConfigNodeObject n) {
Map<String, AbstractConfigValue> values = new HashMap<String, AbstractConfigValue>();
SimpleConfigOrigin objectOrigin = lineOrigin();
SimpleConfigOrigin objectOrigin = nodeOrigin(n);
boolean lastWasNewline = false;

ArrayList<AbstractConfigNode> nodes = new ArrayList<AbstractConfigNode>(n.children());
Expand Down Expand Up @@ -357,7 +368,7 @@ private AbstractConfigObject parseObject(ConfigNodeObject n) {
private SimpleConfigList parseArray(ConfigNodeArray n) {
arrayCount += 1;

SimpleConfigOrigin arrayOrigin = lineOrigin();
SimpleConfigOrigin arrayOrigin = nodeOrigin(n);
List<AbstractConfigValue> values = new ArrayList<AbstractConfigValue>();

boolean lastWasNewLine = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,42 @@ class ConfParserTest extends TestUtils {
assertEquals(2, arrayAfterEquals.getList("a").origin().lineNumber())
}

@Test
def valuesAfterMultilineStringHaveCorrectOriginLine() {
val tripleQuote = "\"\"\""
val conf = parseConfig(Seq(
"transformations {",
" trim-xml-leading-whitespace = " + tripleQuote,
" stringTransformation {",
" println(\"Original message: '$it'\")",
" it.trimStart { it.isWhitespace() || it == '\\uFEFF' }.also { println(\"Trimmed leading whitespace: '${it}'\") }",
" }",
" " + tripleQuote,
"}",
"",
"pipelines {",
" my-pipeline {",
" from {",
" type = GENERATOR",
" count = 1",
" message = \"foo\"",
" }",
" error-strategy = SHUTDOWN",
" processing {",
" transformation = trim-xml-leading-whitespace",
" xml-to-json = { attribute-prefix = \"@\" }",
" }",
" to {",
" type = LOGGER",
" }",
" }",
"}"
).mkString("\n"))

assertEquals(19, conf.getValue("pipelines.my-pipeline.processing.transformation").origin().lineNumber())
assertEquals(20, conf.getObject("pipelines.my-pipeline.processing.xml-to-json").origin().lineNumber())
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are touching parseArray as well, add a test covering that.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the problem I identified also affects arrays? Interesting.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johanandren added/passes in 68ff133


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