Skip to content

Commit

Permalink
Support variable interpolation for string configurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Apr 13, 2018
1 parent 6be57c7 commit 4832125
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
6 changes: 6 additions & 0 deletions config/pom.xml
Expand Up @@ -47,6 +47,12 @@
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -147,6 +147,11 @@ private static class InterpolatingYamlFactory extends YAMLFactory {
protected YAMLParser _createParser(InputStream in, IOContext ctxt) throws IOException {
return new InterpolatingYamlParser(ctxt, _getBufferRecycler(), _parserFeatures, _yamlParserFeatures, _objectCodec, _createReader(in, null, ctxt));
}

@Override
protected YAMLParser _createParser(Reader r, IOContext ctxt) throws IOException {
return new InterpolatingYamlParser(ctxt, _getBufferRecycler(), _parserFeatures, _yamlParserFeatures, _objectCodec, r);
}
}

private static class InterpolatingYamlParser extends YAMLParser {
Expand Down Expand Up @@ -175,20 +180,28 @@ public String getValueAsString(String defaultValue) throws IOException {
}

private String interpolateString(String value) {
value = interpolate(value, sysPattern, name -> System.getProperty(name, ""));
value = interpolate(value, sysPattern, name -> System.getProperty(name));
value = interpolate(value, envPattern, name -> System.getenv(name));
return value;
}

private String interpolate(String value, Pattern pattern, Function<String, String> supplier) {
if (value == null) {
return null;
}

Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
String name = matcher.group(1);
String replace = supplier.apply(name);
String group = matcher.group(0);
if (group.equals(value)) {
return replace;
}
if (replace == null) {
replace = "null";
replace = "";
}
Pattern subPattern = Pattern.compile(Pattern.quote(matcher.group(0)));
Pattern subPattern = Pattern.compile(Pattern.quote(group));
value = subPattern.matcher(value).replaceAll(replace);
}
return value;
Expand Down
Expand Up @@ -17,10 +17,13 @@

import io.atomix.core.AtomixConfig;
import io.atomix.core.config.ConfigProvider;
import io.atomix.protocols.raft.partition.RaftPartitionGroupConfig;
import org.apache.commons.io.IOUtils;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -51,7 +54,7 @@ public void testYaml() throws Exception {

@Test
@Ignore
public void testEnv() throws Exception {
public void testEnvFile() throws Exception {
ConfigProvider provider = new JacksonConfigProvider();
File file = new File(getClass().getClassLoader().getResource("env.yaml").getFile());
assertTrue(provider.isConfigFile(file));
Expand All @@ -61,11 +64,31 @@ public void testEnv() throws Exception {

@Test
@Ignore
public void testSystemProperty() throws Exception {
public void testEnvString() throws Exception {
ConfigProvider provider = new JacksonConfigProvider();
File file = new File(getClass().getClassLoader().getResource("env.yaml").getFile());
AtomixConfig config = provider.load(IOUtils.toString(file.toURI(), StandardCharsets.UTF_8), AtomixConfig.class);
assertEquals("test", config.getPartitionGroups().iterator().next().getName());
assertEquals(3, ((RaftPartitionGroupConfig) config.getPartitionGroups().iterator().next()).getPartitions());
}

@Test
@Ignore
public void testSystemPropertyFile() throws Exception {
ConfigProvider provider = new JacksonConfigProvider();
File file = new File(getClass().getClassLoader().getResource("sys.yaml").getFile());
assertTrue(provider.isConfigFile(file));
AtomixConfig config = provider.load(file, AtomixConfig.class);
assertEquals("test", config.getPartitionGroups().iterator().next().getName());
}

@Test
@Ignore
public void testSystemPropertyString() throws Exception {
ConfigProvider provider = new JacksonConfigProvider();
File file = new File(getClass().getClassLoader().getResource("sys.yaml").getFile());
assertTrue(provider.isConfigFile(file));
AtomixConfig config = provider.load(IOUtils.toString(file.toURI(), StandardCharsets.UTF_8), AtomixConfig.class);
assertEquals("test", config.getPartitionGroups().iterator().next().getName());
}
}
3 changes: 2 additions & 1 deletion config/src/test/resources/env.yaml
Expand Up @@ -4,4 +4,5 @@ primitive-types:
- io.atomix.core.map.ConsistentMapType
partition-groups:
- name: ${env:GROUP_NAME}
type: raft
type: raft
partitions: ${env:NUM_PARTITIONS}

0 comments on commit 4832125

Please sign in to comment.