From 2352521dfa34eb91b0d3d47929fd082364c317a0 Mon Sep 17 00:00:00 2001 From: Dave Fisher Date: Wed, 11 Sep 2024 11:38:04 -0700 Subject: [PATCH] Atfile environment variables with default values --- .../java/io/nosqlbench/nb/api/system/NBEnvironment.java | 7 ++++++- .../io/nosqlbench/engine/cli/atfiles/NBAtFileTest.java | 6 ++++++ .../resources/atfiles/environment_variable_default.yaml | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 nb-engine/nb-engine-cli/src/test/resources/atfiles/environment_variable_default.yaml diff --git a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/system/NBEnvironment.java b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/system/NBEnvironment.java index f44557289..092430fe3 100644 --- a/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/system/NBEnvironment.java +++ b/nb-apis/nb-api/src/main/java/io/nosqlbench/nb/api/system/NBEnvironment.java @@ -217,15 +217,20 @@ public class NBEnvironment { * @return The interpolated value, after substitutions, or null if any lookup failed */ public Optional interpolate(String word, Map supplemental) { - Pattern envpattern = Pattern.compile("(\\$(?[a-zA-Z_][A-Za-z0-9_.]+)|\\$\\{(?[^}]+)\\})"); + Pattern envpattern = Pattern.compile("\\$(?[a-zA-Z_][A-Za-z0-9_.]+)|\\$\\{(?[a-zA-Z_][A-Za-z0-9_.]*)(:(?[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 + "'"); diff --git a/nb-engine/nb-engine-cli/src/test/java/io/nosqlbench/engine/cli/atfiles/NBAtFileTest.java b/nb-engine/nb-engine-cli/src/test/java/io/nosqlbench/engine/cli/atfiles/NBAtFileTest.java index 5979472d7..55feeb374 100644 --- a/nb-engine/nb-engine-cli/src/test/java/io/nosqlbench/engine/cli/atfiles/NBAtFileTest.java +++ b/nb-engine/nb-engine-cli/src/test/java/io/nosqlbench/engine/cli/atfiles/NBAtFileTest.java @@ -97,6 +97,12 @@ class NBAtFileTest { assertThat(strings).containsExactly("My value environment"); } + @Test + public void testAtfileEnvironmentVariableDefault() { + LinkedList 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:>:")); diff --git a/nb-engine/nb-engine-cli/src/test/resources/atfiles/environment_variable_default.yaml b/nb-engine/nb-engine-cli/src/test/resources/atfiles/environment_variable_default.yaml new file mode 100644 index 000000000..46127c618 --- /dev/null +++ b/nb-engine/nb-engine-cli/src/test/resources/atfiles/environment_variable_default.yaml @@ -0,0 +1 @@ +- My ${MISSING_ENV_VAR:default value} environment