Skip to content

Commit 877744c

Browse files
committed
config - allowing backslash in restx configs, allowing multiline property value
This fixes #196
1 parent b4d8650 commit 877744c

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

‎restx-common/src/main/java/restx/common/StdRestxConfig.java‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package restx.common;
22

33
import com.google.common.base.Optional;
4+
import com.google.common.collect.ImmutableList;
45
import com.google.common.collect.ImmutableMap;
56
import com.google.common.io.CharSource;
67
import com.google.common.io.CharStreams;
@@ -24,21 +25,30 @@ public class StdRestxConfig implements RestxConfig {
2425
public static RestxConfig parse(String origin, CharSource charSource) throws IOException {
2526
List<ConfigElement> elements = new ArrayList<>();
2627
StringBuilder doc = new StringBuilder();
27-
int lineCount = 0;
28-
for (String line : charSource.readLines()) {
29-
lineCount++;
28+
ImmutableList<String> lines = charSource.readLines();
29+
30+
for (int lineCount = 0; lineCount < lines.size(); lineCount++) {
31+
String line = lines.get(lineCount);
3032
if (line.startsWith("#")) {
3133
doc.append(line.substring(1).trim()).append("\n");
3234
} else if (!line.trim().isEmpty()) {
33-
int index = line.indexOf('=');
34-
if (index == -1) {
35+
int equalIndex = line.indexOf('=');
36+
if (equalIndex == -1) {
3537
throw new IOException("invalid config " + origin + " at line " + lineCount + ":" +
3638
" line does not contain the equals sign '='");
3739
}
38-
String key = line.substring(0, index).trim();
39-
String value = line.substring(index + 1).trim();
40+
String key = line.substring(0, equalIndex).trim();
41+
StringBuilder value = new StringBuilder();
42+
String lineValue = line.substring(equalIndex + 1).trim();
43+
while(lineValue.endsWith("\\") && lineCount+1 < lines.size()) {
44+
value.append(lineValue.substring(0, lineValue.length() - "\\".length()));
45+
46+
lineCount++;
47+
lineValue = lines.get(lineCount).trim();
48+
}
49+
value.append(lineValue);
4050

41-
elements.add(ConfigElement.of(origin, doc.toString().trim(), key, value));
51+
elements.add(ConfigElement.of(origin, doc.toString().trim(), key, value.toString()));
4252
doc.setLength(0);
4353
}
4454
}

‎restx-common/src/test/java/restx/common/StdRestxConfigTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package restx.common;
22

33
import com.google.common.base.Charsets;
4+
import com.google.common.io.CharSource;
45
import com.google.common.io.Resources;
6+
import org.junit.Assert;
57
import org.junit.Test;
68

9+
import java.io.IOException;
10+
import java.util.Iterator;
11+
712
import static java.util.Arrays.asList;
813
import static org.assertj.core.api.Assertions.assertThat;
914
import static org.assertj.core.groups.Tuple.tuple;
@@ -100,4 +105,24 @@ public void should_parse_properties() throws Exception {
100105
assertThat(config.getElement("key2").get()).isEqualToComparingFieldByField(
101106
ConfigElement.of("restx/common/config.properties", "Doc 2\non 2 lines", "key2", "2"));
102107
}
108+
109+
@Test
110+
public void should_handle_logical_lines() throws IOException {
111+
Exception ex = null;
112+
RestxConfig config = null;
113+
try {
114+
config = StdRestxConfig.parse("test", CharSource.wrap("foo=bar\\\nbaz"));
115+
} catch (Exception e) {
116+
ex = e;
117+
}
118+
119+
Assert.assertNull(ex);
120+
121+
Assert.assertNotNull(config);
122+
Iterator<ConfigElement> configElementIterator = config.elements().iterator();
123+
Assert.assertTrue(configElementIterator.hasNext());
124+
ConfigElement element = configElementIterator.next();
125+
Assert.assertEquals("foo", element.getKey());
126+
Assert.assertEquals("barbaz", element.getValue());
127+
}
103128
}

0 commit comments

Comments
 (0)