Cleanup and Refactored Templates

Mainly how templates read/write themselves from/to xcontent. Instead of using `text`, use `template`

Original commit: elastic/x-pack-elasticsearch@0d6f317539
This commit is contained in:
uboness 2015-04-23 15:15:46 +02:00
parent ae1d4021c0
commit 345f610bdf
5 changed files with 43 additions and 43 deletions

View File

@ -34,7 +34,7 @@ public class MustacheTemplateEngine extends AbstractComponent implements Templat
Map<String, Object> mergedModel = new HashMap<>(); Map<String, Object> mergedModel = new HashMap<>();
mergedModel.putAll(template.getParams()); mergedModel.putAll(template.getParams());
mergedModel.putAll(model); mergedModel.putAll(model);
ExecutableScript executable = service.executable(MustacheScriptEngineService.NAME, template.getText(), template.getType(), mergedModel); ExecutableScript executable = service.executable(MustacheScriptEngineService.NAME, template.getTemplate(), template.getType(), mergedModel);
Object result = executable.run(); Object result = executable.run();
if (result instanceof BytesReference) { if (result instanceof BytesReference) {
return ((BytesReference) result).toUtf8(); return ((BytesReference) result).toUtf8();

View File

@ -25,22 +25,22 @@ import java.util.Objects;
*/ */
public class Template implements ToXContent { public class Template implements ToXContent {
private final String text; private final String template;
private final @Nullable ScriptType type; private final @Nullable ScriptType type;
private final @Nullable Map<String, Object> params; private final @Nullable Map<String, Object> params;
public Template(String text) { public Template(String template) {
this(text, ScriptType.INLINE, ImmutableMap.<String, Object>of()); this(template, ScriptType.INLINE, ImmutableMap.<String, Object>of());
} }
public Template(String text, ScriptType type, Map<String, Object> params) { public Template(String template, ScriptType type, Map<String, Object> params) {
this.text = text; this.template = template;
this.type = type; this.type = type;
this.params = params; this.params = params;
} }
public String getText() { public String getTemplate() {
return text; return template;
} }
public ScriptType getType() { public ScriptType getType() {
@ -56,23 +56,23 @@ public class Template implements ToXContent {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Template template = (Template) o; Template template = (Template) o;
return Objects.equals(text, template.text) && return Objects.equals(this.template, template.template) &&
Objects.equals(type, template.type) && Objects.equals(type, template.type) &&
Objects.equals(params, template.params); Objects.equals(params, template.params);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(text, type, params); return Objects.hash(template, type, params);
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (type == null && params == null) { if (type == null && params == null) {
return builder.value(text); return builder.value(template);
} }
builder.startObject(); builder.startObject();
builder.field(Field.TEXT.getPreferredName(), text); builder.field(Field.TEMPLATE.getPreferredName(), template);
if (type != null) { if (type != null) {
builder.field(Field.TYPE.getPreferredName(), type.name().toLowerCase(Locale.ROOT)); builder.field(Field.TYPE.getPreferredName(), type.name().toLowerCase(Locale.ROOT));
} }
@ -91,7 +91,7 @@ public class Template implements ToXContent {
throw new ParseException("expected a string value or an object, but found [" + token + "] instead"); throw new ParseException("expected a string value or an object, but found [" + token + "] instead");
} }
String text = null; String template = null;
ScriptType type = ScriptType.INLINE; ScriptType type = ScriptType.INLINE;
Map<String, Object> params = ImmutableMap.of(); Map<String, Object> params = ImmutableMap.of();
@ -99,11 +99,11 @@ public class Template implements ToXContent {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
} else if (Field.TEXT.match(currentFieldName)) { } else if (Field.TEMPLATE.match(currentFieldName)) {
if (token == XContentParser.Token.VALUE_STRING) { if (token == XContentParser.Token.VALUE_STRING) {
text = parser.text(); template = parser.text();
} else { } else {
throw new ParseException("expected a string value for field [" + currentFieldName + "], but found [" + token + "]"); throw new ParseException("expected a string field [{}], but found [{}]", currentFieldName, token);
} }
} else if (Field.TYPE.match(currentFieldName)) { } else if (Field.TYPE.match(currentFieldName)) {
if (token == XContentParser.Token.VALUE_STRING) { if (token == XContentParser.Token.VALUE_STRING) {
@ -111,23 +111,23 @@ public class Template implements ToXContent {
try { try {
type = ScriptType.valueOf(value.toUpperCase(Locale.ROOT)); type = ScriptType.valueOf(value.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
throw new ParseException("unknown template type [" + value + "]"); throw new ParseException("unknown template type [{}]", value);
} }
} }
} else if (Field.PARAMS.match(currentFieldName)) { } else if (Field.PARAMS.match(currentFieldName)) {
if (token == XContentParser.Token.START_OBJECT) { if (token == XContentParser.Token.START_OBJECT) {
params = parser.map(); params = parser.map();
} else { } else {
throw new ParseException("expected an object for field [" + currentFieldName + "], but found [" + token + "]"); throw new ParseException("expected an object for field [{}], but found [{}]", currentFieldName, token);
} }
} else { } else {
throw new ParseException("unexpected field [" + currentFieldName + "]"); throw new ParseException("unexpected field [{}]", currentFieldName);
} }
} }
if (text == null) { if (template == null) {
throw new ParseException("missing required string field [" + Field.TEXT.getPreferredName() + "]"); throw new ParseException("missing required string field [{}]", Field.TEMPLATE.getPreferredName());
} }
return new Template(text, type, params); return new Template(template, type, params);
} }
public static Builder builder(String text) { public static Builder builder(String text) {
@ -136,12 +136,12 @@ public class Template implements ToXContent {
public static class Builder { public static class Builder {
private final String text; private final String template;
private ScriptType type; private ScriptType type;
private HashMap<String, Object> params = new HashMap<>(); private HashMap<String, Object> params = new HashMap<>();
private Builder(String text) { private Builder(String template) {
this.text = text; this.template = template;
} }
public Builder setType(ScriptType type) { public Builder setType(ScriptType type) {
@ -161,23 +161,23 @@ public class Template implements ToXContent {
public Template build() { public Template build() {
type = type != null ? type : ScriptType.INLINE; type = type != null ? type : ScriptType.INLINE;
return new Template(text, type, params); return new Template(template, type, params);
} }
} }
public static class ParseException extends WatcherException { public static class ParseException extends WatcherException {
public ParseException(String msg) { public ParseException(String msg, Object... args) {
super(msg); super(msg, args);
} }
public ParseException(String msg, Throwable cause) { public ParseException(String msg, Throwable cause, Object... args) {
super(msg, cause); super(msg, cause, args);
} }
} }
public interface Field { public interface Field {
ParseField TEXT = new ParseField("text"); ParseField TEMPLATE = new ParseField("template");
ParseField TYPE = new ParseField("type"); ParseField TYPE = new ParseField("type");
ParseField PARAMS = new ParseField("params"); ParseField PARAMS = new ParseField("params");
} }

View File

@ -35,7 +35,7 @@ public class XMustacheTemplateEngine extends AbstractComponent implements Templa
Map<String, Object> mergedModel = new HashMap<>(); Map<String, Object> mergedModel = new HashMap<>();
mergedModel.putAll(template.getParams()); mergedModel.putAll(template.getParams());
mergedModel.putAll(model); mergedModel.putAll(model);
ExecutableScript executable = service.executable(XMustacheScriptEngineService.NAME, template.getText(), template.getType(), mergedModel); ExecutableScript executable = service.executable(XMustacheScriptEngineService.NAME, template.getTemplate(), template.getType(), mergedModel);
Object result = executable.run(); Object result = executable.run();
if (result instanceof BytesReference) { if (result instanceof BytesReference) {
return ((BytesReference) result).toUtf8(); return ((BytesReference) result).toUtf8();

View File

@ -111,13 +111,13 @@ public class EmailActionTests extends ElasticsearchTestCase {
.build(); .build();
if (subject != null) { if (subject != null) {
when(engine.render(subject, expectedModel)).thenReturn(subject.getText()); when(engine.render(subject, expectedModel)).thenReturn(subject.getTemplate());
} }
if (textBody != null) { if (textBody != null) {
when(engine.render(textBody, expectedModel)).thenReturn(textBody.getText()); when(engine.render(textBody, expectedModel)).thenReturn(textBody.getTemplate());
} }
if (htmlBody != null) { if (htmlBody != null) {
when(engine.render(htmlBody, expectedModel)).thenReturn(htmlBody.getText()); when(engine.render(htmlBody, expectedModel)).thenReturn(htmlBody.getTemplate());
} }
EmailAction.Result result = executable.execute("_id", ctx, payload); EmailAction.Result result = executable.execute("_id", ctx, payload);
@ -128,9 +128,9 @@ public class EmailActionTests extends ElasticsearchTestCase {
Email actualEmail = ((EmailAction.Result.Success) result).email(); Email actualEmail = ((EmailAction.Result.Success) result).email();
assertThat(actualEmail.id(), is(wid.value())); assertThat(actualEmail.id(), is(wid.value()));
assertThat(actualEmail, notNullValue()); assertThat(actualEmail, notNullValue());
assertThat(actualEmail.subject(), is(subject == null ? null : subject.getText())); assertThat(actualEmail.subject(), is(subject == null ? null : subject.getTemplate()));
assertThat(actualEmail.textBody(), is(textBody == null ? null : textBody.getText())); assertThat(actualEmail.textBody(), is(textBody == null ? null : textBody.getTemplate()));
assertThat(actualEmail.htmlBody(), is(htmlBody == null ? null : htmlBody.getText())); assertThat(actualEmail.htmlBody(), is(htmlBody == null ? null : htmlBody.getTemplate()));
if (attachPayload) { if (attachPayload) {
assertThat(actualEmail.attachments(), hasKey("data")); assertThat(actualEmail.attachments(), hasKey("data"));
} }
@ -188,21 +188,21 @@ public class EmailActionTests extends ElasticsearchTestCase {
} }
if (subject != null) { if (subject != null) {
if (randomBoolean()) { if (randomBoolean()) {
builder.field("subject", subject.getText()); builder.field("subject", subject.getTemplate());
} else { } else {
builder.field("subject", subject); builder.field("subject", subject);
} }
} }
if (textBody != null) { if (textBody != null) {
if (randomBoolean()) { if (randomBoolean()) {
builder.field("text_body", textBody.getText()); builder.field("text_body", textBody.getTemplate());
} else { } else {
builder.field("text_body", textBody); builder.field("text_body", textBody);
} }
} }
if (htmlBody != null) { if (htmlBody != null) {
if (randomBoolean()) { if (randomBoolean()) {
builder.field("html_body", htmlBody.getText()); builder.field("html_body", htmlBody.getTemplate());
} else { } else {
builder.field("html_body", htmlBody); builder.field("html_body", htmlBody);
} }

View File

@ -90,7 +90,7 @@ public class TemplateTests extends ElasticsearchTestCase {
Template template = new Template("_template", randomScriptType(), ImmutableMap.<String, Object>of("param_key", "param_val")); Template template = new Template("_template", randomScriptType(), ImmutableMap.<String, Object>of("param_key", "param_val"));
XContentBuilder builder = jsonBuilder().startObject() XContentBuilder builder = jsonBuilder().startObject()
.field(randomFrom("text"), template.getText()) .field(randomFrom("template"), template.getTemplate())
.field(randomFrom("type"), template.getType().name()) .field(randomFrom("type"), template.getType().name())
.field(randomFrom("params"), template.getParams()) .field(randomFrom("params"), template.getParams())
.endObject(); .endObject();
@ -130,7 +130,7 @@ public class TemplateTests extends ElasticsearchTestCase {
@Test(expected = Template.ParseException.class) @Test(expected = Template.ParseException.class)
public void testParser_Invalid_UnknownScriptType() throws Exception { public void testParser_Invalid_UnknownScriptType() throws Exception {
XContentBuilder builder = jsonBuilder().startObject() XContentBuilder builder = jsonBuilder().startObject()
.field("text", "_template") .field("template", "_template")
.field("type", "unknown_type") .field("type", "unknown_type")
.startObject("params").endObject() .startObject("params").endObject()
.endObject(); .endObject();