Updated the plumbing for scripts to accept compile time parameters.

Closes elastic/elasticsearch#1155

Original commit: elastic/x-pack-elasticsearch@a7fd92f052
This commit is contained in:
Jack Conradson 2015-12-16 12:38:21 -08:00
parent 3529231b5d
commit 9370ea99d0
5 changed files with 13 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import org.elasticsearch.watcher.shield.ShieldIntegration;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.support.init.InitializingService;
import java.util.Collections;
import java.util.Map;
/**
@ -46,7 +47,7 @@ public class ScriptServiceProxy implements InitializingService.Initializable {
}
public CompiledScript compile(org.elasticsearch.script.Script script) {
return service.compile(script, WatcherScriptContext.CTX, contextAndHeaderHolder);
return service.compile(script, WatcherScriptContext.CTX, contextAndHeaderHolder, Collections.emptyMap());
}
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> vars) {
@ -55,7 +56,7 @@ public class ScriptServiceProxy implements InitializingService.Initializable {
public ExecutableScript executable(org.elasticsearch.script.Script script) {
return service.executable(script, WatcherScriptContext.CTX, contextAndHeaderHolder);
return service.executable(script, WatcherScriptContext.CTX, contextAndHeaderHolder, Collections.emptyMap());
}
public static final ScriptContext.Plugin INSTANCE = new ScriptContext.Plugin("elasticsearch-watcher", "watch");

View File

@ -50,7 +50,7 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S
* @return a compiled template object for later execution.
* */
@Override
public Object compile(String template) {
public Object compile(String template, Map<String, String> params) {
/** Factory to generate Mustache objects from. */
XContentType xContentType = detectContentType(template);
template = trimContentType(template);

View File

@ -56,7 +56,7 @@ public class SleepScriptEngine implements ScriptEngineService {
}
@Override
public Object compile(String script) {
public Object compile(String script, Map<String, String> params) {
return script;
}

View File

@ -16,6 +16,7 @@ import org.junit.Before;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -36,7 +37,7 @@ public class XMustacheScriptEngineTests extends ESTestCase {
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}";
Map<String, Object> vars = new HashMap<>();
vars.put("boost_val", "0.3");
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template, Collections.emptyMap()));
BytesReference o = (BytesReference) engine.executable(compiledScript, vars).run();
assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}",
@ -48,7 +49,7 @@ public class XMustacheScriptEngineTests extends ESTestCase {
Map<String, Object> vars = new HashMap<>();
vars.put("boost_val", "0.3");
vars.put("body_val", "\"quick brown\"");
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template, Collections.emptyMap()));
BytesReference o = (BytesReference) engine.executable(compiledScript, vars).run();
assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}",
@ -73,7 +74,7 @@ public class XMustacheScriptEngineTests extends ESTestCase {
vars.put("test_var1", var1Writer.toString());
vars.put("test_var2", var2Writer.toString());
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template, Collections.emptyMap()));
BytesReference o = (BytesReference) engine.executable(compiledScript, vars).run();
String s1 = o.toUtf8();
String s2 = prefix + " " + var1Writer.toString() + " " + var2Writer.toString();

View File

@ -46,7 +46,7 @@ public class XMustacheTests extends ESTestCase {
public void testArrayAccess() throws Exception {
String template = "{{data.0}} {{data.1}}";
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template, Collections.emptyMap()));
Map<String, Object> vars = new HashMap<>();
Object data = randomFrom(
new String[] { "foo", "bar" },
@ -72,7 +72,7 @@ public class XMustacheTests extends ESTestCase {
public void testArrayInArrayAccess() throws Exception {
String template = "{{data.0.0}} {{data.0.1}}";
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template, Collections.emptyMap()));
Map<String, Object> vars = new HashMap<>();
Object data = randomFrom(
new String[][] { new String[] { "foo", "bar" }},
@ -89,7 +89,7 @@ public class XMustacheTests extends ESTestCase {
public void testMapInArrayAccess() throws Exception {
String template = "{{data.0.key}} {{data.1.key}}";
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template, Collections.emptyMap()));
Map<String, Object> vars = new HashMap<>();
Object data = randomFrom(
new Map[] { singletonMap("key", "foo"), singletonMap("key", "bar") },
@ -156,7 +156,7 @@ public class XMustacheTests extends ESTestCase {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("data", unescaped.toString());
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template, Collections.emptyMap()));
Object output = engine.executable(mustache, dataMap).run();
assertThat(output, notNullValue());