diff --git a/engine-core/src/main/java/io/nosqlbench/engine/core/script/ScriptParams.java b/engine-core/src/main/java/io/nosqlbench/engine/core/script/ScriptParams.java index 2830626a5..e435490cd 100644 --- a/engine-core/src/main/java/io/nosqlbench/engine/core/script/ScriptParams.java +++ b/engine-core/src/main/java/io/nosqlbench/engine/core/script/ScriptParams.java @@ -18,36 +18,56 @@ package io.nosqlbench.engine.core.script; import ch.qos.logback.classic.Logger; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import io.nosqlbench.nb.api.errors.BasicError; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; +/** + * Wrap the script parameters in a type which allows for easy manipulation + */ public class ScriptParams extends HashMap { private static final Logger logger = (Logger) LoggerFactory.getLogger(ScriptParams.class); + private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); - public Map withOverrides(Map overrides) { + public Map withOverrides(Map overrides) { if (overrides == null) { logger.warn("A null map was provided to withOverrides. This could be a bug in your script."); overrides=Map.of(); } checkForNulls("params", "calling withOverrides", this); checkForNulls("overrides", "calling withOverrides", overrides); - HashMap result = new HashMap<>(); + ScriptParams result = new ScriptParams(); result.putAll(this); - result.putAll(overrides); + + for (Entry overrideEntry : overrides.entrySet()) { + String oKey = overrideEntry.getKey(); + String oVal = overrideEntry.getValue(); + if (oVal.toUpperCase().endsWith("UNDEF")) { + String removed = result.remove(oKey); + logger.trace("Removed key '" + oKey + "': '" + removed + "' from script params because it was "+ oVal + " in overrides"); + } else { + String was = result.get(oKey); + was = (was==null ? "NULL" : was); + result.put(oKey,oVal); + logger.trace("Overrode key '" + oKey + "': from '" + was + " to " + oVal); + } + } return result; } - public Map withDefaults(Map defaults) { + public Map withDefaults(Map defaults) { if (defaults == null) { logger.warn("A null map was provided to withDefaults. This could be a bug in your script."); defaults=Map.of(); } - HashMap result = new HashMap<>(); + ScriptParams result = new ScriptParams(); checkForNulls("params", "calling withDefaults", this); checkForNulls("defaults", "calling withDefaults", defaults); result.putAll(defaults); @@ -78,6 +98,11 @@ public class ScriptParams extends HashMap { logger.info(mapdetail); } + @Override + public String toString() { + return gson.toJson(this,Map.class).toString(); + } + private static String valueOf(Object o) { if (o == null) { return "NULL"; diff --git a/nb/src/test/java/io/nosqlbench/engine/core/script/AsyncScriptIntegrationTests.java b/nb/src/test/java/io/nosqlbench/engine/core/script/AsyncScriptIntegrationTests.java index 24c96a820..e7a46e521 100644 --- a/nb/src/test/java/io/nosqlbench/engine/core/script/AsyncScriptIntegrationTests.java +++ b/nb/src/test/java/io/nosqlbench/engine/core/script/AsyncScriptIntegrationTests.java @@ -161,6 +161,13 @@ public class AsyncScriptIntegrationTests { assertThat(scenarioResult.getIOLog()).contains("params.get(\"four\") [defaulted-four-niner]='niner'"); } + @Test + public void testScriptParamsUndefVariableWithOverride() { + ScenarioResult scenarioResult = runScenario("undef_param", "one", "two", "three", "four"); + assertThat(scenarioResult.getIOLog()).contains("before: params.get(\"three\"):four"); + assertThat(scenarioResult.getIOLog()).contains("after: params.get(\"three\"):null"); + } + @Test public void testExtensionHistoStatsLogger() throws IOException { ScenarioResult scenarioResult = runScenario("extension_histostatslogger"); diff --git a/nb/src/test/resources/scripts/async/undef_param.js b/nb/src/test/resources/scripts/async/undef_param.js new file mode 100644 index 000000000..5ba310d1e --- /dev/null +++ b/nb/src/test/resources/scripts/async/undef_param.js @@ -0,0 +1,16 @@ +print("params from command line:"); +print(params); +print('before: params.get("three"):' + params.get("three")); +print('before: params.three:' + params.get("three")); + +var overrides = { + 'three': "undef", +}; + +print("params.three after overriding with three:UNDEF"); +params = params.withOverrides({'three':'UNDEF'}); +print(params); +print('after: params.get("three"):' + params.get("three")); +print('after: params.three:' + params.get("three")); + +