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<>();
mergedModel.putAll(template.getParams());
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();
if (result instanceof BytesReference) {
return ((BytesReference) result).toUtf8();

View File

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

View File

@ -35,7 +35,7 @@ public class XMustacheTemplateEngine extends AbstractComponent implements Templa
Map<String, Object> mergedModel = new HashMap<>();
mergedModel.putAll(template.getParams());
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();
if (result instanceof BytesReference) {
return ((BytesReference) result).toUtf8();

View File

@ -111,13 +111,13 @@ public class EmailActionTests extends ElasticsearchTestCase {
.build();
if (subject != null) {
when(engine.render(subject, expectedModel)).thenReturn(subject.getText());
when(engine.render(subject, expectedModel)).thenReturn(subject.getTemplate());
}
if (textBody != null) {
when(engine.render(textBody, expectedModel)).thenReturn(textBody.getText());
when(engine.render(textBody, expectedModel)).thenReturn(textBody.getTemplate());
}
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);
@ -128,9 +128,9 @@ public class EmailActionTests extends ElasticsearchTestCase {
Email actualEmail = ((EmailAction.Result.Success) result).email();
assertThat(actualEmail.id(), is(wid.value()));
assertThat(actualEmail, notNullValue());
assertThat(actualEmail.subject(), is(subject == null ? null : subject.getText()));
assertThat(actualEmail.textBody(), is(textBody == null ? null : textBody.getText()));
assertThat(actualEmail.htmlBody(), is(htmlBody == null ? null : htmlBody.getText()));
assertThat(actualEmail.subject(), is(subject == null ? null : subject.getTemplate()));
assertThat(actualEmail.textBody(), is(textBody == null ? null : textBody.getTemplate()));
assertThat(actualEmail.htmlBody(), is(htmlBody == null ? null : htmlBody.getTemplate()));
if (attachPayload) {
assertThat(actualEmail.attachments(), hasKey("data"));
}
@ -188,21 +188,21 @@ public class EmailActionTests extends ElasticsearchTestCase {
}
if (subject != null) {
if (randomBoolean()) {
builder.field("subject", subject.getText());
builder.field("subject", subject.getTemplate());
} else {
builder.field("subject", subject);
}
}
if (textBody != null) {
if (randomBoolean()) {
builder.field("text_body", textBody.getText());
builder.field("text_body", textBody.getTemplate());
} else {
builder.field("text_body", textBody);
}
}
if (htmlBody != null) {
if (randomBoolean()) {
builder.field("html_body", htmlBody.getText());
builder.field("html_body", htmlBody.getTemplate());
} else {
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"));
XContentBuilder builder = jsonBuilder().startObject()
.field(randomFrom("text"), template.getText())
.field(randomFrom("template"), template.getTemplate())
.field(randomFrom("type"), template.getType().name())
.field(randomFrom("params"), template.getParams())
.endObject();
@ -130,7 +130,7 @@ public class TemplateTests extends ElasticsearchTestCase {
@Test(expected = Template.ParseException.class)
public void testParser_Invalid_UnknownScriptType() throws Exception {
XContentBuilder builder = jsonBuilder().startObject()
.field("text", "_template")
.field("template", "_template")
.field("type", "unknown_type")
.startObject("params").endObject()
.endObject();