Clean up XContentBuilder in X-Pack

This commit cleans most of the methods of XContentBuilder so that:
    - Jackson's convenience methods are used instead of our custom ones (ie field(String,long) now uses Jackson's writeNumberField(String, long) instead of calling writeField(String) then writeNumber(long))
    - null checks are added for all field names and values
    - methods are grouped by type in the class source
    - methods have the same parameters names
    - duplicated methods like field(String, String...) and array(String, String...) are removed
    - varargs methods now have the "array" name to reflect that it builds arrays
    - unused methods like field(String,BigDecimal) are removed
    - all methods now follow the execution path: field(String,?) -> field(String) then value(?), and value(?) -> writeSomething() method. Methods to build arrays also follow the same execution path.

Original commit: elastic/x-pack-elasticsearch@d83f3aa6e2
This commit is contained in:
Tanguy Leroux 2016-09-05 16:56:06 +02:00
parent 8b6bd14b18
commit 97182fefb9
16 changed files with 25 additions and 26 deletions

View File

@ -355,11 +355,11 @@ public class License implements ToXContent {
if (version == VERSION_START) { if (version == VERSION_START) {
builder.field(Fields.SUBSCRIPTION_TYPE, subscriptionType); builder.field(Fields.SUBSCRIPTION_TYPE, subscriptionType);
} }
builder.dateValueField(Fields.ISSUE_DATE_IN_MILLIS, Fields.ISSUE_DATE, issueDate); builder.dateField(Fields.ISSUE_DATE_IN_MILLIS, Fields.ISSUE_DATE, issueDate);
if (version == VERSION_START) { if (version == VERSION_START) {
builder.field(Fields.FEATURE, feature); builder.field(Fields.FEATURE, feature);
} }
builder.dateValueField(Fields.EXPIRY_DATE_IN_MILLIS, Fields.EXPIRY_DATE, expiryDate); builder.dateField(Fields.EXPIRY_DATE_IN_MILLIS, Fields.EXPIRY_DATE, expiryDate);
builder.field(Fields.MAX_NODES, maxNodes); builder.field(Fields.MAX_NODES, maxNodes);
builder.field(Fields.ISSUED_TO, issuedTo); builder.field(Fields.ISSUED_TO, issuedTo);
builder.field(Fields.ISSUER, issuer); builder.field(Fields.ISSUER, issuer);

View File

@ -126,10 +126,10 @@ public class RoleDescriptor implements ToXContent {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();
builder.field(Fields.CLUSTER.getPreferredName(), (Object[]) clusterPrivileges); builder.array(Fields.CLUSTER.getPreferredName(), clusterPrivileges);
builder.field(Fields.INDICES.getPreferredName(), (Object[]) indicesPrivileges); builder.array(Fields.INDICES.getPreferredName(), (Object[]) indicesPrivileges);
if (runAs != null) { if (runAs != null) {
builder.field(Fields.RUN_AS.getPreferredName(), runAs); builder.array(Fields.RUN_AS.getPreferredName(), runAs);
} }
builder.field(Fields.METADATA.getPreferredName(), metadata); builder.field(Fields.METADATA.getPreferredName(), metadata);
return builder.endObject(); return builder.endObject();

View File

@ -129,7 +129,7 @@ public class XPackInfoResponse extends ActionResponse {
.field("type", type) .field("type", type)
.field("mode", mode) .field("mode", mode)
.field("status", status.label()) .field("status", status.label())
.dateValueField("expiry_date_in_millis", "expiry_date", expiryDate) .dateField("expiry_date_in_millis", "expiry_date", expiryDate)
.endObject(); .endObject();
} }

View File

@ -235,7 +235,7 @@ public abstract class WatchRecord implements ToXContent {
@Override @Override
void innerToXContent(XContentBuilder builder, Params params) throws IOException { void innerToXContent(XContentBuilder builder, Params params) throws IOException {
if (messages != null) { if (messages != null) {
builder.field(Field.MESSAGES.getPreferredName(), messages); builder.array(Field.MESSAGES.getPreferredName(), messages);
} }
} }
} }

View File

@ -56,7 +56,7 @@ public class HourlySchedule extends CronnableSchedule {
if (params.paramAsBoolean("normalize", false) && minutes.length == 1) { if (params.paramAsBoolean("normalize", false) && minutes.length == 1) {
builder.field(Parser.MINUTE_FIELD.getPreferredName(), minutes[0]); builder.field(Parser.MINUTE_FIELD.getPreferredName(), minutes[0]);
} else { } else {
builder.field(Parser.MINUTE_FIELD.getPreferredName(), minutes); builder.array(Parser.MINUTE_FIELD.getPreferredName(), minutes);
} }
return builder.endObject(); return builder.endObject();
} }

View File

@ -138,8 +138,8 @@ public class DayTimes implements Times {
return builder.value(time); return builder.value(time);
} }
return builder.startObject() return builder.startObject()
.field(HOUR_FIELD.getPreferredName(), hour) .array(HOUR_FIELD.getPreferredName(), hour)
.field(MINUTE_FIELD.getPreferredName(), minute) .array(MINUTE_FIELD.getPreferredName(), minute)
.endObject(); .endObject();
} }

View File

@ -135,7 +135,7 @@ public class MonthTimes implements Times {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();
builder.field(DAY_FIELD.getPreferredName(), days); builder.array(DAY_FIELD.getPreferredName(), days);
builder.startArray(TIME_FIELD.getPreferredName()); builder.startArray(TIME_FIELD.getPreferredName());
for (DayTimes dayTimes : times) { for (DayTimes dayTimes : times) {
dayTimes.toXContent(builder, params); dayTimes.toXContent(builder, params);

View File

@ -124,7 +124,7 @@ public class YearTimes implements Times {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); builder.startObject();
builder.field(MONTH_FIELD.getPreferredName(), months); builder.field(MONTH_FIELD.getPreferredName(), months);
builder.field(DAY_FIELD.getPreferredName(), days); builder.array(DAY_FIELD.getPreferredName(), days);
builder.startArray(TIME_FIELD.getPreferredName()); builder.startArray(TIME_FIELD.getPreferredName());
for (DayTimes dayTimes : times) { for (DayTimes dayTimes : times) {
dayTimes.toXContent(builder, params); dayTimes.toXContent(builder, params);

View File

@ -46,7 +46,7 @@ public interface Payload extends ToXContent {
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.value(data); return builder.map(data);
} }
@Override @Override

View File

@ -44,7 +44,7 @@ public class SimpleInputTests extends ESTestCase {
data.put("foo", "bar"); data.put("foo", "bar");
data.put("baz", new ArrayList<String>()); data.put("baz", new ArrayList<String>());
XContentBuilder jsonBuilder = jsonBuilder().value(data); XContentBuilder jsonBuilder = jsonBuilder().map(data);
InputFactory parser = new SimpleInputFactory(Settings.builder().build()); InputFactory parser = new SimpleInputFactory(Settings.builder().build());
XContentParser xContentParser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes()); XContentParser xContentParser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes());
xContentParser.nextToken(); xContentParser.nextToken();

View File

@ -40,7 +40,7 @@ public class FilterXContentTests extends ESTestCase {
data.put("key6", 7.1); data.put("key6", 7.1);
data.put("key7", false); data.put("key7", false);
XContentBuilder builder = jsonBuilder().value(data); XContentBuilder builder = jsonBuilder().map(data);
XContentParser parser = XContentHelper.createParser(builder.bytes()); XContentParser parser = XContentHelper.createParser(builder.bytes());
Set<String> keys = new HashSet<>(); Set<String> keys = new HashSet<>();
@ -66,7 +66,7 @@ public class FilterXContentTests extends ESTestCase {
Map<Object, Object> innerMap = MapBuilder.newMapBuilder().put("key1", "value1").put("key2", "value2").map(); Map<Object, Object> innerMap = MapBuilder.newMapBuilder().put("key1", "value1").put("key2", "value2").map();
data.put("leaf3", MapBuilder.newMapBuilder().put("key1", "value1").put("key2", innerMap).map()); data.put("leaf3", MapBuilder.newMapBuilder().put("key1", "value1").put("key2", innerMap).map());
BytesReference bytes = jsonBuilder().value(data).bytes(); BytesReference bytes = jsonBuilder().map(data).bytes();
XContentParser parser = XContentHelper.createParser(bytes); XContentParser parser = XContentHelper.createParser(bytes);
Set<String> keys = new HashSet<>(Arrays.asList("leaf1.key2")); Set<String> keys = new HashSet<>(Arrays.asList("leaf1.key2"));

View File

@ -26,7 +26,6 @@ import org.elasticsearch.search.SearchRequestParsers;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.common.text.TextTemplate; import org.elasticsearch.xpack.common.text.TextTemplate;
import org.elasticsearch.xpack.watcher.actions.ExecutableActions; import org.elasticsearch.xpack.watcher.actions.ExecutableActions;
import org.elasticsearch.xpack.watcher.condition.always.ExecutableAlwaysCondition; import org.elasticsearch.xpack.watcher.condition.always.ExecutableAlwaysCondition;

View File

@ -78,8 +78,8 @@ public class DailyScheduleTests extends ScheduleTestCase {
XContentBuilder builder = jsonBuilder() XContentBuilder builder = jsonBuilder()
.startObject() .startObject()
.startObject("at") .startObject("at")
.field("hour", time.hour()) .array("hour", time.hour())
.field("minute", time.minute()) .array("minute", time.minute())
.endObject() .endObject()
.endObject(); .endObject();
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();
@ -197,7 +197,7 @@ public class DailyScheduleTests extends ScheduleTestCase {
String[] times = invalidDayTimesAsStrings(); String[] times = invalidDayTimesAsStrings();
XContentBuilder builder = jsonBuilder() XContentBuilder builder = jsonBuilder()
.startObject() .startObject()
.field("at", times) .array("at", times)
.endObject(); .endObject();
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes); XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);

View File

@ -84,8 +84,8 @@ public class MonthlyScheduleTests extends ScheduleTestCase {
.startObject() .startObject()
.field("on", day) .field("on", day)
.startObject("at") .startObject("at")
.field("hour", time.hour()) .array("hour", time.hour())
.field("minute", time.minute()) .array("minute", time.minute())
.endObject() .endObject()
.endObject(); .endObject();
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();

View File

@ -82,8 +82,8 @@ public class WeeklyScheduleTests extends ScheduleTestCase {
.startObject() .startObject()
.field("on", "mon") .field("on", "mon")
.startObject("at") .startObject("at")
.field("hour", time.hour()) .array("hour", time.hour())
.field("minute", time.minute()) .array("minute", time.minute())
.endObject() .endObject()
.endObject(); .endObject();
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();

View File

@ -92,8 +92,8 @@ public class YearlyScheduleTests extends ScheduleTestCase {
.field("in", month) .field("in", month)
.field("on", day) .field("on", day)
.startObject("at") .startObject("at")
.field("hour", time.hour()) .array("hour", time.hour())
.field("minute", time.minute()) .array("minute", time.minute())
.endObject() .endObject()
.endObject(); .endObject();
BytesReference bytes = builder.bytes(); BytesReference bytes = builder.bytes();