make script params work with graaljs

This commit is contained in:
Jonathan Shook 2020-08-06 09:50:19 -05:00
parent a533901349
commit e2505f717a

View File

@ -21,32 +21,48 @@ import ch.qos.logback.classic.Logger;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import io.nosqlbench.nb.api.errors.BasicError; import io.nosqlbench.nb.api.errors.BasicError;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.Proxy;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Flow;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* Wrap the script parameters in a type which allows for easy manipulation * Wrap the script parameters in a type which allows for easy manipulation
*/ */
public class ScriptParams extends HashMap<String, String> { public class ScriptParams extends HashMap<String, String> implements ProxyObject {
private static final Logger logger = (Logger) LoggerFactory.getLogger(ScriptParams.class); private static final Logger logger = (Logger) LoggerFactory.getLogger(ScriptParams.class);
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public Map<String,String> withOverrides(Map<String, String> overrides) { public ScriptParams withOverrides(Object overrides) {
Map<String, String> map;
if (overrides instanceof Map) {
map = (Map) overrides;
} else if (overrides instanceof Value) {
Value v = (Value) overrides;
map = v.as(Map.class);
} else {
throw new RuntimeException("Unrecognized overrides type: " + overrides.getClass().getCanonicalName());
}
if (overrides == null) { if (overrides == null) {
logger.warn("A null map was provided to withOverrides. This could be a bug in your script."); logger.warn("A null map was provided to withOverrides. This could be a bug in your script.");
overrides = Map.of(); overrides = Map.of();
} }
checkForNulls("params", "calling withOverrides", this); checkForNulls("params", "calling withOverrides", this);
checkForNulls("overrides", "calling withOverrides", overrides); checkForNulls("overrides", "calling withOverrides", map);
ScriptParams result = new ScriptParams(); ScriptParams result = new ScriptParams();
result.putAll(this); result.putAll(this);
for (Entry<String, String> overrideEntry : overrides.entrySet()) { for (Entry<String, String> overrideEntry : map.entrySet()) {
String oKey = overrideEntry.getKey(); String oKey = overrideEntry.getKey();
String oVal = overrideEntry.getValue(); String oVal = overrideEntry.getValue();
if (oVal.toUpperCase().endsWith("UNDEF")) { if (oVal.toUpperCase().endsWith("UNDEF")) {
@ -59,20 +75,32 @@ public class ScriptParams extends HashMap<String, String> {
logger.trace("Overrode key '" + oKey + "': from '" + was + " to " + oVal); logger.trace("Overrode key '" + oKey + "': from '" + was + " to " + oVal);
} }
} }
return result; ScriptParams p = new ScriptParams();
p.putAll(result);
return p;
} }
public Map<String,String> withDefaults(Map<String, String> defaults) { public ScriptParams withDefaults(Object defaults) {
if (defaults == null) { Map<String, String> map;
if (defaults instanceof Map) {
map = (Map) defaults;
} else if (defaults instanceof Value) {
map = ((Value) defaults).as(Map.class);
} else {
throw new RuntimeException("Unrecognized type for defaults: " + defaults.getClass().getCanonicalName());
}
if (map == null) {
logger.warn("A null map was provided to withDefaults. This could be a bug in your script."); logger.warn("A null map was provided to withDefaults. This could be a bug in your script.");
defaults=Map.of(); map = Map.of();
} }
ScriptParams result = new ScriptParams(); ScriptParams result = new ScriptParams();
checkForNulls("params", "calling withDefaults", this); checkForNulls("params", "calling withDefaults", this);
checkForNulls("defaults", "calling withDefaults", defaults); checkForNulls("defaults", "calling withDefaults", map);
result.putAll(defaults); result.putAll(map);
result.putAll(this); result.putAll(this);
return result; ScriptParams p = new ScriptParams();
p.putAll(result);
return p;
} }
private static void checkForNulls(String name, String action, Map<String, String> map) { private static void checkForNulls(String name, String action, Map<String, String> map) {
@ -110,4 +138,50 @@ public class ScriptParams extends HashMap<String, String> {
return String.valueOf(o); return String.valueOf(o);
} }
//region ProxyObject methods
@Override
public Object getMember(String key) {
if (key.equals("withDefaults")) {
return (Function<Object, Object>) this::withDefaults;
}
if (key.equals("withOverrides")) {
return (Function<Object, Object>) this::withOverrides;
}
return super.get(key);
}
@Override
public boolean containsKey(Object key) {
return
key.equals("withOverrides")
|| key.equals("withDefaults")
|| super.containsKey(key);
}
@Override
public Object getMemberKeys() {
ArrayList<String> memberKeys = new ArrayList<>(super.keySet());
memberKeys.add("withDefaults");
memberKeys.add("withOverride");
return memberKeys;
}
@Override
public boolean hasMember(String key) {
if (super.containsKey(key)) {
return true;
}
if (key.equals("withOverrides") || key.equals("withDefaults")) {
return true;
}
return false;
}
@Override
public void putMember(String key, Value value) {
super.put(key, value.asString());
}
//endregion
} }