Merge pull request #2018 from nosqlbench/dfisher-atfile-env-defaults

At file environment variables with default values
This commit is contained in:
Jonathan Shook 2024-09-11 15:21:01 -05:00 committed by GitHub
commit 490ea9bd07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 1 deletions

View File

@ -217,15 +217,20 @@ public class NBEnvironment {
* @return The interpolated value, after substitutions, or null if any lookup failed
*/
public Optional<String> interpolate(String word, Map<String,String> supplemental) {
Pattern envpattern = Pattern.compile("(\\$(?<env1>[a-zA-Z_][A-Za-z0-9_.]+)|\\$\\{(?<env2>[^}]+)\\})");
Pattern envpattern = Pattern.compile("\\$(?<env1>[a-zA-Z_][A-Za-z0-9_.]+)|\\$\\{(?<env2>[a-zA-Z_][A-Za-z0-9_.]*)(:\\-(?<default>[a-zA-Z0-9_./\\- ]+))?\\}");
Matcher matcher = envpattern.matcher(word);
StringBuilder sb = new StringBuilder();
while (matcher.find()) {
String envvar = matcher.group("env1");
String defaultValue = null;
if (envvar == null) {
envvar = matcher.group("env2");
defaultValue = matcher.group("default");
}
String value = peek(envvar,supplemental);
if (value == null) {
value = defaultValue;
}
if (value == null) {
if (logger != null) {
logger.debug("no value found for '" + envvar + "', returning Optional.empty() for '" + word + "'");

View File

@ -97,6 +97,12 @@ class NBAtFileTest {
assertThat(strings).containsExactly("My value environment");
}
@Test
public void testAtfileEnvironmentVariableDefault() {
LinkedList<String> strings = NBAtFile.includeAt("@src/test/resources/atfiles/environment_variable_default.yaml");
assertThat(strings).containsExactly("My default value environment");
}
@Test
public void testAtfileMissingEnvironmentVariable() {
assertThrows(RuntimeException.class, () -> NBAtFile.includeAt("@src/test/resources/atfiles/environment_variable_missing.yaml:>:"));

View File

@ -0,0 +1 @@
- My ${MISSING_ENV_VAR:-default value} environment