Search in sources :

Example 21 with CompiledScript

use of org.elasticsearch.script.CompiledScript in project elasticsearch by elastic.

the class MustacheTests method testArrayAccess.

public void testArrayAccess() throws Exception {
    String template = "{{data.0}} {{data.1}}";
    CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
    Map<String, Object> vars = new HashMap<>();
    Object data = randomFrom(new String[] { "foo", "bar" }, Arrays.asList("foo", "bar"));
    vars.put("data", data);
    Object output = engine.executable(mustache, vars).run();
    assertThat(output, notNullValue());
    assertThat(output, instanceOf(BytesReference.class));
    BytesReference bytes = (BytesReference) output;
    assertThat(bytes.utf8ToString(), equalTo("foo bar"));
    // Sets can come out in any order
    Set<String> setData = new HashSet<>();
    setData.add("foo");
    setData.add("bar");
    vars.put("data", setData);
    output = engine.executable(mustache, vars).run();
    assertThat(output, notNullValue());
    assertThat(output, instanceOf(BytesReference.class));
    bytes = (BytesReference) output;
    assertThat(bytes.utf8ToString(), both(containsString("foo")).and(containsString("bar")));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) HashMap(java.util.HashMap) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) HashSet(java.util.HashSet)

Example 22 with CompiledScript

use of org.elasticsearch.script.CompiledScript in project elasticsearch by elastic.

the class MustacheTests method testMapInArrayAccess.

public void testMapInArrayAccess() throws Exception {
    String template = "{{data.0.key}} {{data.1.key}}";
    CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
    Map<String, Object> vars = new HashMap<>();
    Object data = randomFrom(new Object[] { singletonMap("key", "foo"), singletonMap("key", "bar") }, Arrays.asList(singletonMap("key", "foo"), singletonMap("key", "bar")));
    vars.put("data", data);
    Object output = engine.executable(mustache, vars).run();
    assertThat(output, notNullValue());
    assertThat(output, instanceOf(BytesReference.class));
    BytesReference bytes = (BytesReference) output;
    assertThat(bytes.utf8ToString(), equalTo("foo bar"));
    // HashSet iteration order isn't fixed
    Set<Object> setData = new HashSet<>();
    setData.add(singletonMap("key", "foo"));
    setData.add(singletonMap("key", "bar"));
    vars.put("data", setData);
    output = engine.executable(mustache, vars).run();
    assertThat(output, notNullValue());
    assertThat(output, instanceOf(BytesReference.class));
    bytes = (BytesReference) output;
    assertThat(bytes.utf8ToString(), both(containsString("foo")).and(containsString("bar")));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) HashMap(java.util.HashMap) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) HashSet(java.util.HashSet)

Aggregations

CompiledScript (org.elasticsearch.script.CompiledScript)22 ExecutableScript (org.elasticsearch.script.ExecutableScript)15 HashMap (java.util.HashMap)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)10 Script (org.elasticsearch.script.Script)7 Map (java.util.Map)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)5 Mustache (com.github.mustachejava.Mustache)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)3 IndexSettings (org.elasticsearch.index.IndexSettings)3 ScriptContext (org.elasticsearch.script.ScriptContext)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Function (java.util.function.Function)2