Bring Watcher inline with es script changes
This change allows watcher to compile against the latest es core scripting changes. Instantiate compiled scripts and use those in tests. Original commit: elastic/x-pack-elasticsearch@edd46a089f
This commit is contained in:
parent
4cf56aef12
commit
53a5aa373b
|
@ -61,7 +61,7 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S
|
|||
* Execute a compiled template object (as retrieved from the compile method)
|
||||
* and fill potential place holders with the variables given.
|
||||
*
|
||||
* @param template
|
||||
* @param compiledScript
|
||||
* compiled template object.
|
||||
* @param vars
|
||||
* map of variables to use during substitution.
|
||||
|
@ -69,10 +69,10 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S
|
|||
* @return the processed string with all given variables substitued.
|
||||
* */
|
||||
@Override
|
||||
public Object execute(Object template, Map<String, Object> vars) {
|
||||
public Object execute(CompiledScript compiledScript, Map<String, Object> vars) {
|
||||
BytesStreamOutput result = new BytesStreamOutput();
|
||||
UTF8StreamWriter writer = utf8StreamWriter().setOutput(result);
|
||||
((Mustache) template).execute(writer, vars);
|
||||
((Mustache) compiledScript.compiled()).execute(writer, vars);
|
||||
try {
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
|
@ -103,13 +103,13 @@ public class XMustacheScriptEngineService extends AbstractComponent implements S
|
|||
}
|
||||
|
||||
@Override
|
||||
public ExecutableScript executable(Object mustache,
|
||||
public ExecutableScript executable(CompiledScript compiledScript,
|
||||
@Nullable Map<String, Object> vars) {
|
||||
return new MustacheExecutableScript((Mustache) mustache, vars);
|
||||
return new MustacheExecutableScript((Mustache) compiledScript.compiled(), vars);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SearchScript search(Object compiledScript, SearchLookup lookup,
|
||||
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup,
|
||||
@Nullable Map<String, Object> vars) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ package org.elasticsearch.watcher.support.template.xmustache;
|
|||
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.script.CompiledScript;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -37,7 +39,8 @@ public class XMustacheScriptEngineTests extends ElasticsearchTestCase {
|
|||
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" + "}}, \"negative_boost\": {{boost_val}} } }}";
|
||||
Map<String, Object> vars = new HashMap<>();
|
||||
vars.put("boost_val", "0.3");
|
||||
BytesReference o = (BytesReference) engine.execute(engine.compile(template), vars);
|
||||
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
|
||||
BytesReference o = (BytesReference) engine.execute(compiledScript, vars);
|
||||
assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
|
||||
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}",
|
||||
new String(o.toBytes(), Charset.forName("UTF-8")));
|
||||
|
@ -48,7 +51,8 @@ public class XMustacheScriptEngineTests extends ElasticsearchTestCase {
|
|||
Map<String, Object> vars = new HashMap<>();
|
||||
vars.put("boost_val", "0.3");
|
||||
vars.put("body_val", "\"quick brown\"");
|
||||
BytesReference o = (BytesReference) engine.execute(engine.compile(template), vars);
|
||||
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
|
||||
BytesReference o = (BytesReference) engine.execute(compiledScript, vars);
|
||||
assertEquals("GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
|
||||
+ "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}",
|
||||
new String(o.toBytes(), Charset.forName("UTF-8")));
|
||||
|
@ -73,7 +77,8 @@ public class XMustacheScriptEngineTests extends ElasticsearchTestCase {
|
|||
|
||||
vars.put("test_var1", var1Writer.toString());
|
||||
vars.put("test_var2", var2Writer.toString());
|
||||
BytesReference o = (BytesReference) engine.execute(engine.compile(template), vars);
|
||||
CompiledScript compiledScript = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
|
||||
BytesReference o = (BytesReference) engine.execute(compiledScript, vars);
|
||||
String s1 = o.toUtf8();
|
||||
String s2 = prefix + " " + var1Writer.toString() + " " + var2Writer.toString();
|
||||
assertEquals(s1, s2);
|
||||
|
|
|
@ -13,7 +13,9 @@ import com.google.common.collect.ImmutableMap;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.script.CompiledScript;
|
||||
import org.elasticsearch.script.ScriptEngineService;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -40,7 +42,7 @@ public class XMustacheTests extends ElasticsearchTestCase {
|
|||
@Test
|
||||
public void testArrayAccess() throws Exception {
|
||||
String template = "{{data.0}} {{data.1}}";
|
||||
Object mustache = engine.compile(template);
|
||||
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
|
||||
Map<String, Object> vars = new HashMap<>();
|
||||
Object data = randomFrom(
|
||||
new String[] { "foo", "bar" },
|
||||
|
@ -57,7 +59,7 @@ public class XMustacheTests extends ElasticsearchTestCase {
|
|||
@Test
|
||||
public void testArrayInArrayAccess() throws Exception {
|
||||
String template = "{{data.0.0}} {{data.0.1}}";
|
||||
Object mustache = engine.compile(template);
|
||||
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
|
||||
Map<String, Object> vars = new HashMap<>();
|
||||
Object data = randomFrom(
|
||||
new String[][] { new String[] {"foo", "bar" }},
|
||||
|
@ -75,7 +77,7 @@ public class XMustacheTests extends ElasticsearchTestCase {
|
|||
@Test
|
||||
public void testMapInArrayAccess() throws Exception {
|
||||
String template = "{{data.0.key}} {{data.1.key}}";
|
||||
Object mustache = engine.compile(template);
|
||||
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
|
||||
Map<String, Object> vars = new HashMap<>();
|
||||
Object data = randomFrom(
|
||||
new Map[] { ImmutableMap.<String, Object>of("key", "foo"), ImmutableMap.<String, Object>of("key", "bar") },
|
||||
|
@ -132,7 +134,7 @@ public class XMustacheTests extends ElasticsearchTestCase {
|
|||
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
dataMap.put("data", unescaped.toString());
|
||||
Object mustache = engine.compile(template);
|
||||
CompiledScript mustache = new CompiledScript(ScriptService.ScriptType.INLINE, "inline", "mustache", engine.compile(template));
|
||||
Object output = engine.execute(mustache, dataMap);
|
||||
|
||||
assertThat(output, notNullValue());
|
||||
|
|
Loading…
Reference in New Issue