Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
config - allowing backslash in restx configs, allowing multiline prop…
…erty value

This fixes #196
  • Loading branch information
fcamblor committed Aug 23, 2017
1 parent b4d8650 commit 877744c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
26 changes: 18 additions & 8 deletions restx-common/src/main/java/restx/common/StdRestxConfig.java
@@ -1,6 +1,7 @@
package restx.common;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.CharSource;
import com.google.common.io.CharStreams;
Expand All @@ -24,21 +25,30 @@ public class StdRestxConfig implements RestxConfig {
public static RestxConfig parse(String origin, CharSource charSource) throws IOException {
List<ConfigElement> elements = new ArrayList<>();
StringBuilder doc = new StringBuilder();
int lineCount = 0;
for (String line : charSource.readLines()) {
lineCount++;
ImmutableList<String> lines = charSource.readLines();

for (int lineCount = 0; lineCount < lines.size(); lineCount++) {
String line = lines.get(lineCount);
if (line.startsWith("#")) {
doc.append(line.substring(1).trim()).append("\n");
} else if (!line.trim().isEmpty()) {
int index = line.indexOf('=');
if (index == -1) {
int equalIndex = line.indexOf('=');
if (equalIndex == -1) {
throw new IOException("invalid config " + origin + " at line " + lineCount + ":" +
" line does not contain the equals sign '='");
}
String key = line.substring(0, index).trim();
String value = line.substring(index + 1).trim();
String key = line.substring(0, equalIndex).trim();
StringBuilder value = new StringBuilder();
String lineValue = line.substring(equalIndex + 1).trim();
while(lineValue.endsWith("\\") && lineCount+1 < lines.size()) {
value.append(lineValue.substring(0, lineValue.length() - "\\".length()));

lineCount++;
lineValue = lines.get(lineCount).trim();
}
value.append(lineValue);

elements.add(ConfigElement.of(origin, doc.toString().trim(), key, value));
elements.add(ConfigElement.of(origin, doc.toString().trim(), key, value.toString()));
doc.setLength(0);
}
}
Expand Down
25 changes: 25 additions & 0 deletions restx-common/src/test/java/restx/common/StdRestxConfigTest.java
@@ -1,9 +1,14 @@
package restx.common;

import com.google.common.base.Charsets;
import com.google.common.io.CharSource;
import com.google.common.io.Resources;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.Iterator;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
Expand Down Expand Up @@ -100,4 +105,24 @@ public void should_parse_properties() throws Exception {
assertThat(config.getElement("key2").get()).isEqualToComparingFieldByField(
ConfigElement.of("restx/common/config.properties", "Doc 2\non 2 lines", "key2", "2"));
}

@Test
public void should_handle_logical_lines() throws IOException {
Exception ex = null;
RestxConfig config = null;
try {
config = StdRestxConfig.parse("test", CharSource.wrap("foo=bar\\\nbaz"));
} catch (Exception e) {
ex = e;
}

Assert.assertNull(ex);

Assert.assertNotNull(config);
Iterator<ConfigElement> configElementIterator = config.elements().iterator();
Assert.assertTrue(configElementIterator.hasNext());
ConfigElement element = configElementIterator.next();
Assert.assertEquals("foo", element.getKey());
Assert.assertEquals("barbaz", element.getValue());
}
}

0 comments on commit 877744c

Please sign in to comment.