Update test script engines to use generic compile method (elastic/x-pack-elasticsearch#1546)
This is the xpack side of https://github.com/elastic/elasticsearch/pull/24873 Original commit: elastic/x-pack-elasticsearch@1779afa6bc
This commit is contained in:
parent
3b58334efb
commit
aae7cf0b0f
|
@ -39,11 +39,13 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object compile(String name, String script, Map<String, String> params) {
|
public <T> T compile(String name, String script, ScriptContext<T> context, Map<String, String> params) {
|
||||||
if (script.contains("{{") && script.contains("}}")) {
|
if (script.contains("{{") && script.contains("}}")) {
|
||||||
throw new IllegalArgumentException("Fix your test to not rely on mustache");
|
throw new IllegalArgumentException("Fix your test to not rely on mustache");
|
||||||
}
|
}
|
||||||
// We always return the script's source as it is
|
if (context.instanceClazz.equals(ExecutableScript.class) == false) {
|
||||||
return new MockCompiledScript(name, params, script, null);
|
throw new IllegalArgumentException("mock mustache only understands template scripts, not [" + context.name + "]");
|
||||||
|
}
|
||||||
|
return context.compiledClazz.cast((ExecutableScript.Compiled) vars -> new MockExecutableScript(vars, p -> script));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,12 @@
|
||||||
package org.elasticsearch.xpack.monitoring.test;
|
package org.elasticsearch.xpack.monitoring.test;
|
||||||
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.script.ExecutableScript;
|
||||||
import org.elasticsearch.script.MockScriptEngine;
|
import org.elasticsearch.script.MockScriptEngine;
|
||||||
import org.elasticsearch.script.MockScriptPlugin;
|
import org.elasticsearch.script.MockScriptPlugin;
|
||||||
|
import org.elasticsearch.script.ScriptContext;
|
||||||
import org.elasticsearch.script.ScriptEngine;
|
import org.elasticsearch.script.ScriptEngine;
|
||||||
|
import org.elasticsearch.script.SearchScript;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -39,8 +42,12 @@ public class MockPainlessScriptEngine extends MockScriptEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object compile(String name, String script, Map<String, String> params) {
|
public <T> T compile(String name, String script, ScriptContext<T> context, Map<String, String> params) {
|
||||||
// We always return the script's source as it is
|
if (context.instanceClazz.equals(ExecutableScript.class)) {
|
||||||
return new MockCompiledScript(name, params, script, null);
|
return context.compiledClazz.cast((ExecutableScript.Compiled) vars -> new MockExecutableScript(vars, p -> script));
|
||||||
|
} else if (context.instanceClazz.equals(SearchScript.class)) {
|
||||||
|
return context.compiledClazz.cast((SearchScript.Compiled) (vars, lookup) -> new MockSearchScript(lookup, vars, p -> script));
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("mock painless does not know how to handle context [" + context.name + "]");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue