-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #482 from elifesciences/support-environment-variab…
…le-override added support for environment variable overrides
- Loading branch information
Showing
5 changed files
with
160 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
grobid-core/src/main/java/org/grobid/core/utilities/EnvironmentVariableProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.grobid.core.utilities; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class EnvironmentVariableProperties { | ||
|
||
private final Map<String, String> properties = new HashMap<>(); | ||
|
||
public EnvironmentVariableProperties(String prefix) { | ||
this(System.getenv(), prefix); | ||
} | ||
|
||
public EnvironmentVariableProperties(Map<String, String> environmentVariablesMap, String prefix) { | ||
for (Map.Entry<String, String> entry: environmentVariablesMap.entrySet()) { | ||
if (!entry.getKey().startsWith(prefix)) { | ||
continue; | ||
} | ||
String propertiesKey = getPropertiesKeyForEnvironmentVariableName(entry.getKey()); | ||
this.properties.put(propertiesKey, entry.getValue()); | ||
} | ||
} | ||
|
||
private static String getPropertiesKeyForEnvironmentVariableName(String name) { | ||
return name.replace("__", ".").toLowerCase(); | ||
} | ||
|
||
public Map<String, String> getProperties() { | ||
return this.properties; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
grobid-core/src/test/java/org/grobid/core/utilities/EnvironmentVariablePropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.grobid.core.utilities; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
|
||
public class EnvironmentVariablePropertiesTest { | ||
private Map<String, String> environmentVariables = new HashMap<>(); | ||
|
||
@Test | ||
public void shouldReturnEmptyPropertiesWithEmptyEnvironmentVariables() { | ||
assertEquals( | ||
Collections.emptyMap(), | ||
new EnvironmentVariableProperties( | ||
environmentVariables, | ||
"APP__" | ||
).getProperties() | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmptyPropertiesWithNotMatchingEnvironmentVariables() { | ||
environmentVariables.put("OTHER__ABC", "value1"); | ||
assertEquals( | ||
Collections.emptyMap(), | ||
new EnvironmentVariableProperties( | ||
environmentVariables, | ||
"APP__" | ||
).getProperties() | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAndConvertMatchingEnvironmentVariable() { | ||
environmentVariables.put("APP__ABC", "value1"); | ||
assertEquals( | ||
Collections.singletonMap("app.abc", "value1"), | ||
new EnvironmentVariableProperties( | ||
environmentVariables, | ||
"APP__" | ||
).getProperties() | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAndConvertMatchingNestedEnvironmentVariable() { | ||
environmentVariables.put("APP__ABC__XYZ", "value1"); | ||
assertEquals( | ||
Collections.singletonMap("app.abc.xyz", "value1"), | ||
new EnvironmentVariableProperties( | ||
environmentVariables, | ||
"APP__" | ||
).getProperties() | ||
); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAndConvertMatchingEnvironmentVariableWithUnderscore() { | ||
environmentVariables.put("APP__ABC_XYZ", "value1"); | ||
assertEquals( | ||
Collections.singletonMap("app.abc_xyz", "value1"), | ||
new EnvironmentVariableProperties( | ||
environmentVariables, | ||
"APP__" | ||
).getProperties() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters