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:
parent
8b6bd14b18
commit
97182fefb9
|
@ -355,11 +355,11 @@ public class License implements ToXContent {
|
|||
if (version == VERSION_START) {
|
||||
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) {
|
||||
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.ISSUED_TO, issuedTo);
|
||||
builder.field(Fields.ISSUER, issuer);
|
||||
|
|
|
@ -126,10 +126,10 @@ public class RoleDescriptor implements ToXContent {
|
|||
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field(Fields.CLUSTER.getPreferredName(), (Object[]) clusterPrivileges);
|
||||
builder.field(Fields.INDICES.getPreferredName(), (Object[]) indicesPrivileges);
|
||||
builder.array(Fields.CLUSTER.getPreferredName(), clusterPrivileges);
|
||||
builder.array(Fields.INDICES.getPreferredName(), (Object[]) indicesPrivileges);
|
||||
if (runAs != null) {
|
||||
builder.field(Fields.RUN_AS.getPreferredName(), runAs);
|
||||
builder.array(Fields.RUN_AS.getPreferredName(), runAs);
|
||||
}
|
||||
builder.field(Fields.METADATA.getPreferredName(), metadata);
|
||||
return builder.endObject();
|
||||
|
|
|
@ -129,7 +129,7 @@ public class XPackInfoResponse extends ActionResponse {
|
|||
.field("type", type)
|
||||
.field("mode", mode)
|
||||
.field("status", status.label())
|
||||
.dateValueField("expiry_date_in_millis", "expiry_date", expiryDate)
|
||||
.dateField("expiry_date_in_millis", "expiry_date", expiryDate)
|
||||
.endObject();
|
||||
}
|
||||
|
||||
|
|
|
@ -235,7 +235,7 @@ public abstract class WatchRecord implements ToXContent {
|
|||
@Override
|
||||
void innerToXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
if (messages != null) {
|
||||
builder.field(Field.MESSAGES.getPreferredName(), messages);
|
||||
builder.array(Field.MESSAGES.getPreferredName(), messages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ public class HourlySchedule extends CronnableSchedule {
|
|||
if (params.paramAsBoolean("normalize", false) && minutes.length == 1) {
|
||||
builder.field(Parser.MINUTE_FIELD.getPreferredName(), minutes[0]);
|
||||
} else {
|
||||
builder.field(Parser.MINUTE_FIELD.getPreferredName(), minutes);
|
||||
builder.array(Parser.MINUTE_FIELD.getPreferredName(), minutes);
|
||||
}
|
||||
return builder.endObject();
|
||||
}
|
||||
|
|
|
@ -138,8 +138,8 @@ public class DayTimes implements Times {
|
|||
return builder.value(time);
|
||||
}
|
||||
return builder.startObject()
|
||||
.field(HOUR_FIELD.getPreferredName(), hour)
|
||||
.field(MINUTE_FIELD.getPreferredName(), minute)
|
||||
.array(HOUR_FIELD.getPreferredName(), hour)
|
||||
.array(MINUTE_FIELD.getPreferredName(), minute)
|
||||
.endObject();
|
||||
}
|
||||
|
||||
|
|
|
@ -135,7 +135,7 @@ public class MonthTimes implements Times {
|
|||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field(DAY_FIELD.getPreferredName(), days);
|
||||
builder.array(DAY_FIELD.getPreferredName(), days);
|
||||
builder.startArray(TIME_FIELD.getPreferredName());
|
||||
for (DayTimes dayTimes : times) {
|
||||
dayTimes.toXContent(builder, params);
|
||||
|
|
|
@ -124,7 +124,7 @@ public class YearTimes implements Times {
|
|||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject();
|
||||
builder.field(MONTH_FIELD.getPreferredName(), months);
|
||||
builder.field(DAY_FIELD.getPreferredName(), days);
|
||||
builder.array(DAY_FIELD.getPreferredName(), days);
|
||||
builder.startArray(TIME_FIELD.getPreferredName());
|
||||
for (DayTimes dayTimes : times) {
|
||||
dayTimes.toXContent(builder, params);
|
||||
|
|
|
@ -46,7 +46,7 @@ public interface Payload extends ToXContent {
|
|||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
return builder.value(data);
|
||||
return builder.map(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -44,7 +44,7 @@ public class SimpleInputTests extends ESTestCase {
|
|||
data.put("foo", "bar");
|
||||
data.put("baz", new ArrayList<String>());
|
||||
|
||||
XContentBuilder jsonBuilder = jsonBuilder().value(data);
|
||||
XContentBuilder jsonBuilder = jsonBuilder().map(data);
|
||||
InputFactory parser = new SimpleInputFactory(Settings.builder().build());
|
||||
XContentParser xContentParser = JsonXContent.jsonXContent.createParser(jsonBuilder.bytes());
|
||||
xContentParser.nextToken();
|
||||
|
|
|
@ -40,7 +40,7 @@ public class FilterXContentTests extends ESTestCase {
|
|||
data.put("key6", 7.1);
|
||||
data.put("key7", false);
|
||||
|
||||
XContentBuilder builder = jsonBuilder().value(data);
|
||||
XContentBuilder builder = jsonBuilder().map(data);
|
||||
XContentParser parser = XContentHelper.createParser(builder.bytes());
|
||||
|
||||
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();
|
||||
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);
|
||||
Set<String> keys = new HashSet<>(Arrays.asList("leaf1.key2"));
|
||||
|
|
|
@ -26,7 +26,6 @@ import org.elasticsearch.search.SearchRequestParsers;
|
|||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
|
||||
import org.elasticsearch.xpack.XPackPlugin;
|
||||
import org.elasticsearch.xpack.common.text.TextTemplate;
|
||||
import org.elasticsearch.xpack.watcher.actions.ExecutableActions;
|
||||
import org.elasticsearch.xpack.watcher.condition.always.ExecutableAlwaysCondition;
|
||||
|
|
|
@ -78,8 +78,8 @@ public class DailyScheduleTests extends ScheduleTestCase {
|
|||
XContentBuilder builder = jsonBuilder()
|
||||
.startObject()
|
||||
.startObject("at")
|
||||
.field("hour", time.hour())
|
||||
.field("minute", time.minute())
|
||||
.array("hour", time.hour())
|
||||
.array("minute", time.minute())
|
||||
.endObject()
|
||||
.endObject();
|
||||
BytesReference bytes = builder.bytes();
|
||||
|
@ -197,7 +197,7 @@ public class DailyScheduleTests extends ScheduleTestCase {
|
|||
String[] times = invalidDayTimesAsStrings();
|
||||
XContentBuilder builder = jsonBuilder()
|
||||
.startObject()
|
||||
.field("at", times)
|
||||
.array("at", times)
|
||||
.endObject();
|
||||
BytesReference bytes = builder.bytes();
|
||||
XContentParser parser = JsonXContent.jsonXContent.createParser(bytes);
|
||||
|
|
|
@ -84,8 +84,8 @@ public class MonthlyScheduleTests extends ScheduleTestCase {
|
|||
.startObject()
|
||||
.field("on", day)
|
||||
.startObject("at")
|
||||
.field("hour", time.hour())
|
||||
.field("minute", time.minute())
|
||||
.array("hour", time.hour())
|
||||
.array("minute", time.minute())
|
||||
.endObject()
|
||||
.endObject();
|
||||
BytesReference bytes = builder.bytes();
|
||||
|
|
|
@ -82,8 +82,8 @@ public class WeeklyScheduleTests extends ScheduleTestCase {
|
|||
.startObject()
|
||||
.field("on", "mon")
|
||||
.startObject("at")
|
||||
.field("hour", time.hour())
|
||||
.field("minute", time.minute())
|
||||
.array("hour", time.hour())
|
||||
.array("minute", time.minute())
|
||||
.endObject()
|
||||
.endObject();
|
||||
BytesReference bytes = builder.bytes();
|
||||
|
|
|
@ -92,8 +92,8 @@ public class YearlyScheduleTests extends ScheduleTestCase {
|
|||
.field("in", month)
|
||||
.field("on", day)
|
||||
.startObject("at")
|
||||
.field("hour", time.hour())
|
||||
.field("minute", time.minute())
|
||||
.array("hour", time.hour())
|
||||
.array("minute", time.minute())
|
||||
.endObject()
|
||||
.endObject();
|
||||
BytesReference bytes = builder.bytes();
|
||||
|
|
Loading…
Reference in New Issue