Skip to content

Commit c41df74

Browse files
Move more XContent parsers that are only used in tests to test codebase (#105801)
Just like a couple times before, moving a couple more of the test only parsers to the test codebase to save code-size etc.
1 parent a8188f8 commit c41df74

File tree

46 files changed

+639
-713
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+639
-713
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStats.java

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.ingest.geoip.GeoIpDownloader;
1515
import org.elasticsearch.tasks.Task;
16-
import org.elasticsearch.xcontent.ConstructingObjectParser;
1716
import org.elasticsearch.xcontent.ParseField;
1817
import org.elasticsearch.xcontent.XContentBuilder;
19-
import org.elasticsearch.xcontent.XContentParser;
2018

2119
import java.io.IOException;
2220
import java.util.Objects;
@@ -25,26 +23,12 @@ public class GeoIpDownloaderStats implements Task.Status {
2523

2624
public static final GeoIpDownloaderStats EMPTY = new GeoIpDownloaderStats(0, 0, 0, 0, 0, 0);
2725

28-
public static final ConstructingObjectParser<GeoIpDownloaderStats, Void> PARSER = new ConstructingObjectParser<>(
29-
"geoip_downloader_stats",
30-
a -> new GeoIpDownloaderStats((int) a[0], (int) a[1], (long) a[2], (int) a[3], (int) a[4], a[5] == null ? 0 : (int) a[5])
31-
);
32-
33-
private static final ParseField SUCCESSFUL_DOWNLOADS = new ParseField("successful_downloads");
34-
private static final ParseField FAILED_DOWNLOADS = new ParseField("failed_downloads");
35-
private static final ParseField TOTAL_DOWNLOAD_TIME = new ParseField("total_download_time");
36-
private static final ParseField DATABASES_COUNT = new ParseField("databases_count");
37-
private static final ParseField SKIPPED_DOWNLOADS = new ParseField("skipped_updates");
38-
private static final ParseField EXPIRED_DATABASES = new ParseField("expired_databases");
39-
40-
static {
41-
PARSER.declareInt(ConstructingObjectParser.constructorArg(), SUCCESSFUL_DOWNLOADS);
42-
PARSER.declareInt(ConstructingObjectParser.constructorArg(), FAILED_DOWNLOADS);
43-
PARSER.declareLong(ConstructingObjectParser.constructorArg(), TOTAL_DOWNLOAD_TIME);
44-
PARSER.declareInt(ConstructingObjectParser.constructorArg(), DATABASES_COUNT);
45-
PARSER.declareInt(ConstructingObjectParser.constructorArg(), SKIPPED_DOWNLOADS);
46-
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), EXPIRED_DATABASES);
47-
}
26+
static final ParseField SUCCESSFUL_DOWNLOADS = new ParseField("successful_downloads");
27+
static final ParseField FAILED_DOWNLOADS = new ParseField("failed_downloads");
28+
static final ParseField TOTAL_DOWNLOAD_TIME = new ParseField("total_download_time");
29+
static final ParseField DATABASES_COUNT = new ParseField("databases_count");
30+
static final ParseField SKIPPED_DOWNLOADS = new ParseField("skipped_updates");
31+
static final ParseField EXPIRED_DATABASES = new ParseField("expired_databases");
4832

4933
private final int successfulDownloads;
5034
private final int failedDownloads;
@@ -62,7 +46,7 @@ public GeoIpDownloaderStats(StreamInput in) throws IOException {
6246
expiredDatabases = in.readVInt();
6347
}
6448

65-
private GeoIpDownloaderStats(
49+
GeoIpDownloaderStats(
6650
int successfulDownloads,
6751
int failedDownloads,
6852
long totalDownloadTime,
@@ -170,10 +154,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
170154
return builder;
171155
}
172156

173-
public static GeoIpDownloaderStats fromXContent(XContentParser parser) throws IOException {
174-
return PARSER.parse(parser, null);
175-
}
176-
177157
@Override
178158
public void writeTo(StreamOutput out) throws IOException {
179159
out.writeVInt(successfulDownloads);

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/stats/GeoIpDownloaderStatsSerializingTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,30 @@
1010

1111
import org.elasticsearch.common.io.stream.Writeable;
1212
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
13+
import org.elasticsearch.xcontent.ConstructingObjectParser;
1314
import org.elasticsearch.xcontent.XContentParser;
1415

1516
import java.io.IOException;
1617

1718
public class GeoIpDownloaderStatsSerializingTests extends AbstractXContentSerializingTestCase<GeoIpDownloaderStats> {
1819

20+
private static final ConstructingObjectParser<GeoIpDownloaderStats, Void> PARSER = new ConstructingObjectParser<>(
21+
"geoip_downloader_stats",
22+
a -> new GeoIpDownloaderStats((int) a[0], (int) a[1], (long) a[2], (int) a[3], (int) a[4], a[5] == null ? 0 : (int) a[5])
23+
);
24+
25+
static {
26+
PARSER.declareInt(ConstructingObjectParser.constructorArg(), GeoIpDownloaderStats.SUCCESSFUL_DOWNLOADS);
27+
PARSER.declareInt(ConstructingObjectParser.constructorArg(), GeoIpDownloaderStats.FAILED_DOWNLOADS);
28+
PARSER.declareLong(ConstructingObjectParser.constructorArg(), GeoIpDownloaderStats.TOTAL_DOWNLOAD_TIME);
29+
PARSER.declareInt(ConstructingObjectParser.constructorArg(), GeoIpDownloaderStats.DATABASES_COUNT);
30+
PARSER.declareInt(ConstructingObjectParser.constructorArg(), GeoIpDownloaderStats.SKIPPED_DOWNLOADS);
31+
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), GeoIpDownloaderStats.EXPIRED_DATABASES);
32+
}
33+
1934
@Override
2035
protected GeoIpDownloaderStats doParseInstance(XContentParser parser) throws IOException {
21-
return GeoIpDownloaderStats.fromXContent(parser);
36+
return PARSER.parse(parser, null);
2237
}
2338

2439
@Override

server/src/main/java/org/elasticsearch/action/DocWriteResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
309309
* {@link DocWriteResponse} objects. It always parses the current token, updates the given parsing context accordingly
310310
* if needed and then immediately returns.
311311
*/
312-
protected static void parseInnerToXContent(XContentParser parser, Builder context) throws IOException {
312+
public static void parseInnerToXContent(XContentParser parser, Builder context) throws IOException {
313313
XContentParser.Token token = parser.currentToken();
314314
ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
315315

server/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/list/ListTasksResponse.java

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
1616
import org.elasticsearch.cluster.node.DiscoveryNodes;
1717
import org.elasticsearch.common.Strings;
18-
import org.elasticsearch.common.TriFunction;
1918
import org.elasticsearch.common.collect.Iterators;
2019
import org.elasticsearch.common.io.stream.StreamInput;
2120
import org.elasticsearch.common.io.stream.StreamOutput;
2221
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2322
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
2423
import org.elasticsearch.tasks.TaskId;
2524
import org.elasticsearch.tasks.TaskInfo;
26-
import org.elasticsearch.xcontent.ConstructingObjectParser;
27-
import org.elasticsearch.xcontent.ParseField;
28-
import org.elasticsearch.xcontent.XContentParser;
2925

3026
import java.io.IOException;
3127
import java.util.ArrayList;
@@ -35,13 +31,11 @@
3531
import java.util.function.Supplier;
3632
import java.util.stream.Collectors;
3733

38-
import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;
39-
4034
/**
4135
* Returns the list of tasks currently running on the nodes
4236
*/
4337
public class ListTasksResponse extends BaseTasksResponse {
44-
private static final String TASKS = "tasks";
38+
public static final String TASKS = "tasks";
4539

4640
private final List<TaskInfo> tasks;
4741

@@ -69,35 +63,6 @@ public void writeTo(StreamOutput out) throws IOException {
6963
out.writeCollection(tasks);
7064
}
7165

72-
protected static <T> ConstructingObjectParser<T, Void> setupParser(
73-
String name,
74-
TriFunction<List<TaskInfo>, List<TaskOperationFailure>, List<ElasticsearchException>, T> ctor
75-
) {
76-
ConstructingObjectParser<T, Void> parser = new ConstructingObjectParser<>(name, true, constructingObjects -> {
77-
int i = 0;
78-
@SuppressWarnings("unchecked")
79-
List<TaskInfo> tasks = (List<TaskInfo>) constructingObjects[i++];
80-
@SuppressWarnings("unchecked")
81-
List<TaskOperationFailure> tasksFailures = (List<TaskOperationFailure>) constructingObjects[i++];
82-
@SuppressWarnings("unchecked")
83-
List<ElasticsearchException> nodeFailures = (List<ElasticsearchException>) constructingObjects[i];
84-
return ctor.apply(tasks, tasksFailures, nodeFailures);
85-
});
86-
parser.declareObjectArray(optionalConstructorArg(), TaskInfo.PARSER, new ParseField(TASKS));
87-
parser.declareObjectArray(optionalConstructorArg(), (p, c) -> TaskOperationFailure.fromXContent(p), new ParseField(TASK_FAILURES));
88-
parser.declareObjectArray(
89-
optionalConstructorArg(),
90-
(p, c) -> ElasticsearchException.fromXContent(p),
91-
new ParseField(NODE_FAILURES)
92-
);
93-
return parser;
94-
}
95-
96-
private static final ConstructingObjectParser<ListTasksResponse, Void> PARSER = setupParser(
97-
"list_tasks_response",
98-
ListTasksResponse::new
99-
);
100-
10166
/**
10267
* Returns the list of tasks by node
10368
*/
@@ -250,10 +215,6 @@ public ChunkedToXContentObject groupedByNone() {
250215
}));
251216
}
252217

253-
public static ListTasksResponse fromXContent(XContentParser parser) {
254-
return PARSER.apply(parser, null);
255-
}
256-
257218
@Override
258219
public String toString() {
259220
return Strings.toString(ChunkedToXContent.wrapAsToXContent(groupedByNone()), true, true);

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/verify/VerifyRepositoryResponse.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
import org.elasticsearch.common.io.stream.StreamInput;
1515
import org.elasticsearch.common.io.stream.StreamOutput;
1616
import org.elasticsearch.common.io.stream.Writeable;
17-
import org.elasticsearch.xcontent.ObjectParser;
18-
import org.elasticsearch.xcontent.ParseField;
1917
import org.elasticsearch.xcontent.ToXContentObject;
2018
import org.elasticsearch.xcontent.XContentBuilder;
21-
import org.elasticsearch.xcontent.XContentParser;
2219

2320
import java.io.IOException;
2421
import java.util.Arrays;
@@ -34,12 +31,6 @@ public class VerifyRepositoryResponse extends ActionResponse implements ToXConte
3431
static final String NAME = "name";
3532

3633
public static class NodeView implements Writeable, ToXContentObject {
37-
private static final ObjectParser.NamedObjectParser<NodeView, Void> PARSER;
38-
static {
39-
ObjectParser<NodeView, Void> internalParser = new ObjectParser<>(NODES, true, null);
40-
internalParser.declareString(NodeView::setName, new ParseField(NAME));
41-
PARSER = (p, v, name) -> internalParser.parse(p, new NodeView(name), null);
42-
}
4334

4435
final String nodeId;
4536
String name;
@@ -104,15 +95,6 @@ public int hashCode() {
10495

10596
private List<NodeView> nodes;
10697

107-
private static final ObjectParser<VerifyRepositoryResponse, Void> PARSER = new ObjectParser<>(
108-
VerifyRepositoryResponse.class.getName(),
109-
true,
110-
VerifyRepositoryResponse::new
111-
);
112-
static {
113-
PARSER.declareNamedObjects(VerifyRepositoryResponse::setNodes, NodeView.PARSER, new ParseField("nodes"));
114-
}
115-
11698
public VerifyRepositoryResponse() {}
11799

118100
public VerifyRepositoryResponse(StreamInput in) throws IOException {
@@ -157,10 +139,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
157139
return builder;
158140
}
159141

160-
public static VerifyRepositoryResponse fromXContent(XContentParser parser) {
161-
return PARSER.apply(parser, null);
162-
}
163-
164142
@Override
165143
public String toString() {
166144
return Strings.toString(this);

server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,19 @@
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.common.settings.Settings;
15-
import org.elasticsearch.xcontent.ConstructingObjectParser;
1615
import org.elasticsearch.xcontent.ParseField;
1716
import org.elasticsearch.xcontent.XContentBuilder;
18-
import org.elasticsearch.xcontent.XContentParser;
1917

2018
import java.io.IOException;
2119
import java.util.Objects;
2220

23-
import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
24-
2521
/**
2622
* A response for a cluster update settings action.
2723
*/
2824
public class ClusterUpdateSettingsResponse extends AcknowledgedResponse {
2925

30-
private static final ParseField PERSISTENT = new ParseField("persistent");
31-
private static final ParseField TRANSIENT = new ParseField("transient");
32-
33-
private static final ConstructingObjectParser<ClusterUpdateSettingsResponse, Void> PARSER = new ConstructingObjectParser<>(
34-
"cluster_update_settings_response",
35-
true,
36-
args -> {
37-
return new ClusterUpdateSettingsResponse((boolean) args[0], (Settings) args[1], (Settings) args[2]);
38-
}
39-
);
40-
static {
41-
declareAcknowledgedField(PARSER);
42-
PARSER.declareObject(constructorArg(), (p, c) -> Settings.fromXContent(p), TRANSIENT);
43-
PARSER.declareObject(constructorArg(), (p, c) -> Settings.fromXContent(p), PERSISTENT);
44-
}
26+
static final ParseField PERSISTENT = new ParseField("persistent");
27+
static final ParseField TRANSIENT = new ParseField("transient");
4528

4629
final Settings transientSettings;
4730
final Settings persistentSettings;
@@ -83,10 +66,6 @@ protected void addCustomFields(XContentBuilder builder, Params params) throws IO
8366
builder.endObject();
8467
}
8568

86-
public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) {
87-
return PARSER.apply(parser, null);
88-
}
89-
9069
@Override
9170
public boolean equals(Object o) {
9271
if (super.equals(o)) {

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponse.java

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@
1313
import org.elasticsearch.common.io.stream.StreamInput;
1414
import org.elasticsearch.common.io.stream.StreamOutput;
1515
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
16-
import org.elasticsearch.xcontent.ConstructingObjectParser;
17-
import org.elasticsearch.xcontent.ParseField;
1816
import org.elasticsearch.xcontent.ToXContent;
19-
import org.elasticsearch.xcontent.XContentParser;
2017

2118
import java.io.IOException;
2219
import java.util.Iterator;
2320
import java.util.List;
2421
import java.util.Objects;
2522

26-
import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
27-
2823
/**
2924
* Snapshot status response
3025
*/
@@ -55,23 +50,6 @@ public void writeTo(StreamOutput out) throws IOException {
5550
out.writeCollection(snapshots);
5651
}
5752

58-
private static final ConstructingObjectParser<SnapshotsStatusResponse, Void> PARSER = new ConstructingObjectParser<>(
59-
"snapshots_status_response",
60-
true,
61-
(Object[] parsedObjects) -> {
62-
@SuppressWarnings("unchecked")
63-
List<SnapshotStatus> snapshots = (List<SnapshotStatus>) parsedObjects[0];
64-
return new SnapshotsStatusResponse(snapshots);
65-
}
66-
);
67-
static {
68-
PARSER.declareObjectArray(constructorArg(), SnapshotStatus.PARSER, new ParseField("snapshots"));
69-
}
70-
71-
public static SnapshotsStatusResponse fromXContent(XContentParser parser) throws IOException {
72-
return PARSER.parse(parser, null);
73-
}
74-
7553
@Override
7654
public boolean equals(Object o) {
7755
if (this == o) return true;

server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetScriptContextResponse.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.common.util.Maps;
1515
import org.elasticsearch.script.ScriptContextInfo;
16-
import org.elasticsearch.xcontent.ConstructingObjectParser;
1716
import org.elasticsearch.xcontent.ParseField;
1817
import org.elasticsearch.xcontent.ToXContentObject;
1918
import org.elasticsearch.xcontent.XContentBuilder;
20-
import org.elasticsearch.xcontent.XContentParser;
2119

2220
import java.io.IOException;
2321
import java.util.Collections;
@@ -31,28 +29,9 @@
3129

3230
public class GetScriptContextResponse extends ActionResponse implements ToXContentObject {
3331

34-
private static final ParseField CONTEXTS = new ParseField("contexts");
32+
static final ParseField CONTEXTS = new ParseField("contexts");
3533
final Map<String, ScriptContextInfo> contexts;
3634

37-
@SuppressWarnings("unchecked")
38-
public static final ConstructingObjectParser<GetScriptContextResponse, Void> PARSER = new ConstructingObjectParser<>(
39-
"get_script_context",
40-
true,
41-
(a) -> {
42-
Map<String, ScriptContextInfo> contexts = ((List<ScriptContextInfo>) a[0]).stream()
43-
.collect(Collectors.toMap(ScriptContextInfo::getName, c -> c));
44-
return new GetScriptContextResponse(contexts);
45-
}
46-
);
47-
48-
static {
49-
PARSER.declareObjectArray(
50-
ConstructingObjectParser.constructorArg(),
51-
(parser, ctx) -> ScriptContextInfo.PARSER.apply(parser, ctx),
52-
CONTEXTS
53-
);
54-
}
55-
5635
GetScriptContextResponse(StreamInput in) throws IOException {
5736
super(in);
5837
int size = in.readInt();
@@ -70,7 +49,7 @@ public class GetScriptContextResponse extends ActionResponse implements ToXConte
7049
}
7150

7251
// Parser constructor
73-
private GetScriptContextResponse(Map<String, ScriptContextInfo> contexts) {
52+
GetScriptContextResponse(Map<String, ScriptContextInfo> contexts) {
7453
this.contexts = Map.copyOf(contexts);
7554
}
7655

@@ -96,10 +75,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
9675
return builder;
9776
}
9877

99-
public static GetScriptContextResponse fromXContent(XContentParser parser) throws IOException {
100-
return PARSER.apply(parser, null);
101-
}
102-
10378
@Override
10479
public boolean equals(Object o) {
10580
if (this == o) {

0 commit comments

Comments
 (0)