use of org.opensearch.script.ScriptContext in project OpenSearch by opensearch-project.
the class MustacheScriptEngine method compile.
/**
* Compile a template string to (in this case) a Mustache object than can
* later be re-used for execution to fill in missing parameter values.
*
* @param templateSource a string representing the template to compile.
* @return a compiled template object for later execution.
*/
@Override
public <T> T compile(String templateName, String templateSource, ScriptContext<T> context, Map<String, String> options) {
if (context.instanceClazz.equals(TemplateScript.class) == false) {
throw new IllegalArgumentException("mustache engine does not know how to handle context [" + context.name + "]");
}
final MustacheFactory factory = createMustacheFactory(options);
Reader reader = new StringReader(templateSource);
try {
Mustache template = factory.compile(reader, "query-template");
TemplateScript.Factory compiled = params -> new MustacheExecutableScript(template, params);
return context.factoryClazz.cast(compiled);
} catch (MustacheException ex) {
throw new ScriptException(ex.getMessage(), ex, Collections.emptyList(), templateSource, NAME);
}
}
Aggregations