|
@@ -19,13 +19,21 @@
|
|
|
|
|
|
package org.elasticsearch.ingest;
|
|
|
|
|
|
+import org.elasticsearch.script.Script;
|
|
|
+import org.elasticsearch.script.ScriptService;
|
|
|
+import org.elasticsearch.script.ScriptType;
|
|
|
+import org.elasticsearch.script.TemplateScript;
|
|
|
+
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
+import static org.elasticsearch.script.Script.DEFAULT_TEMPLATE_LANG;
|
|
|
+
|
|
|
/**
|
|
|
* Holds a value. If the value is requested a copy is made and optionally template snippets are resolved too.
|
|
|
*/
|
|
@@ -41,13 +49,14 @@ public interface ValueSource {
|
|
|
*/
|
|
|
Object copyAndResolve(Map<String, Object> model);
|
|
|
|
|
|
- static ValueSource wrap(Object value, TemplateService templateService) {
|
|
|
+ static ValueSource wrap(Object value, ScriptService scriptService) {
|
|
|
+
|
|
|
if (value instanceof Map) {
|
|
|
@SuppressWarnings("unchecked")
|
|
|
Map<Object, Object> mapValue = (Map) value;
|
|
|
Map<ValueSource, ValueSource> valueTypeMap = new HashMap<>(mapValue.size());
|
|
|
for (Map.Entry<Object, Object> entry : mapValue.entrySet()) {
|
|
|
- valueTypeMap.put(wrap(entry.getKey(), templateService), wrap(entry.getValue(), templateService));
|
|
|
+ valueTypeMap.put(wrap(entry.getKey(), scriptService), wrap(entry.getValue(), scriptService));
|
|
|
}
|
|
|
return new MapValue(valueTypeMap);
|
|
|
} else if (value instanceof List) {
|
|
@@ -55,7 +64,7 @@ public interface ValueSource {
|
|
|
List<Object> listValue = (List) value;
|
|
|
List<ValueSource> valueSourceList = new ArrayList<>(listValue.size());
|
|
|
for (Object item : listValue) {
|
|
|
- valueSourceList.add(wrap(item, templateService));
|
|
|
+ valueSourceList.add(wrap(item, scriptService));
|
|
|
}
|
|
|
return new ListValue(valueSourceList);
|
|
|
} else if (value == null || value instanceof Number || value instanceof Boolean) {
|
|
@@ -63,7 +72,15 @@ public interface ValueSource {
|
|
|
} else if (value instanceof byte[]) {
|
|
|
return new ByteValue((byte[]) value);
|
|
|
} else if (value instanceof String) {
|
|
|
- return new TemplatedValue(templateService.compile((String) value));
|
|
|
+ // This check is here because the DEFAULT_TEMPLATE_LANG(mustache) is not
|
|
|
+ // installed for use by REST tests. `value` will not be
|
|
|
+ // modified if templating is not available
|
|
|
+ if (scriptService.isLangSupported(DEFAULT_TEMPLATE_LANG)) {
|
|
|
+ Script script = new Script(ScriptType.INLINE, DEFAULT_TEMPLATE_LANG, (String) value, Collections.emptyMap());
|
|
|
+ return new TemplatedValue(scriptService.compile(script, TemplateScript.CONTEXT));
|
|
|
+ } else {
|
|
|
+ return new ObjectValue(value);
|
|
|
+ }
|
|
|
} else {
|
|
|
throw new IllegalArgumentException("unexpected value type [" + value.getClass() + "]");
|
|
|
}
|
|
@@ -194,15 +211,15 @@ public interface ValueSource {
|
|
|
|
|
|
final class TemplatedValue implements ValueSource {
|
|
|
|
|
|
- private final TemplateService.Template template;
|
|
|
+ private final TemplateScript.Factory template;
|
|
|
|
|
|
- TemplatedValue(TemplateService.Template template) {
|
|
|
+ TemplatedValue(TemplateScript.Factory template) {
|
|
|
this.template = template;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object copyAndResolve(Map<String, Object> model) {
|
|
|
- return template.execute(model);
|
|
|
+ return template.newInstance(model).execute();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -211,12 +228,12 @@ public interface ValueSource {
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
TemplatedValue templatedValue = (TemplatedValue) o;
|
|
|
- return Objects.equals(template.getKey(), templatedValue.template.getKey());
|
|
|
+ return Objects.equals(template, templatedValue.template);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int hashCode() {
|
|
|
- return Objects.hashCode(template.getKey());
|
|
|
+ return Objects.hashCode(template);
|
|
|
}
|
|
|
}
|
|
|
|