Use the XContentRegistry provided by the parser

This commit is contained in:
Colin Goodheart-Smithe 2018-01-08 14:14:43 +00:00
parent c4621d9dc7
commit 57c343bc21
9 changed files with 63 additions and 64 deletions

View File

@ -10,12 +10,10 @@ import org.elasticsearch.cluster.AbstractDiffable;
import org.elasticsearch.cluster.Diffable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -48,18 +46,17 @@ public class LifecyclePolicy extends AbstractDiffable<LifecyclePolicy>
public static final ParseField TYPE_FIELD = new ParseField("type");
@SuppressWarnings("unchecked")
public static ConstructingObjectParser<LifecyclePolicy, Tuple<String, NamedXContentRegistry>> PARSER = new ConstructingObjectParser<>(
"lifecycle_policy", false, (a, c) -> {
String name = c.v1();
public static ConstructingObjectParser<LifecyclePolicy, String> PARSER = new ConstructingObjectParser<>("lifecycle_policy", false,
(a, name) -> {
LifecycleType type = (LifecycleType) a[0];
List<Phase> phases = (List<Phase>) a[1];
Map<String, Phase> phaseMap = phases.stream().collect(Collectors.toMap(Phase::getName, Function.identity()));
return new LifecyclePolicy(type, name, phaseMap);
});
static {
PARSER.declareField(constructorArg(), (p, c) -> c.v2().parseNamedObject(LifecycleType.class, p.text(), p, null), TYPE_FIELD,
PARSER.declareField(constructorArg(), (p, c) -> p.namedObject(LifecycleType.class, p.text(), null), TYPE_FIELD,
ValueType.STRING);
PARSER.declareNamedObjects(constructorArg(), (p, c, n) -> Phase.parse(p, new Tuple<>(n, c.v2())), v -> {
PARSER.declareNamedObjects(constructorArg(), (p, c, n) -> Phase.parse(p, n), v -> {
throw new IllegalArgumentException("ordered " + PHASES_FIELD.getPreferredName() + " are not supported");
}, PHASES_FIELD);
}
@ -91,8 +88,8 @@ public class LifecyclePolicy extends AbstractDiffable<LifecyclePolicy>
phases = Collections.unmodifiableMap(in.readMap(StreamInput::readString, Phase::new));
}
public static LifecyclePolicy parse(XContentParser parser, Tuple<String, NamedXContentRegistry> context) {
return PARSER.apply(parser, context);
public static LifecyclePolicy parse(XContentParser parser, String name) {
return PARSER.apply(parser, name);
}
@Override

View File

@ -8,14 +8,12 @@ package org.elasticsearch.xpack.indexlifecycle;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -43,20 +41,20 @@ public class Phase implements ToXContentObject, Writeable {
public static final ParseField ACTIONS_FIELD = new ParseField("actions");
@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<Phase, Tuple<String, NamedXContentRegistry>> PARSER = new ConstructingObjectParser<>(
"phase", false, (a, c) -> new Phase(c.v1(), (TimeValue) a[0],
private static final ConstructingObjectParser<Phase, String> PARSER = new ConstructingObjectParser<>("phase", false,
(a, name) -> new Phase(name, (TimeValue) a[0],
convertListToMapValues(LifecycleAction::getWriteableName, (List<LifecycleAction>) a[1])));
static {
PARSER.declareField(ConstructingObjectParser.constructorArg(),
(p, c) -> TimeValue.parseTimeValue(p.text(), AFTER_FIELD.getPreferredName()), AFTER_FIELD, ValueType.VALUE);
PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(),
(p, c, n) -> c.v2().parseNamedObject(LifecycleAction.class, n, p, c.v2()), v -> {
(p, c, n) -> p.namedObject(LifecycleAction.class, n, null), v -> {
throw new IllegalArgumentException("ordered " + ACTIONS_FIELD.getPreferredName() + " are not supported");
}, ACTIONS_FIELD);
}
public static Phase parse(XContentParser parser, Tuple<String, NamedXContentRegistry> context) {
return PARSER.apply(parser, context);
public static Phase parse(XContentParser parser, String name) {
return PARSER.apply(parser, name);
}
private String name;

View File

@ -13,11 +13,9 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -106,10 +104,10 @@ public class PutLifecycleAction extends Action<PutLifecycleAction.Request, PutLi
public static class Request extends AcknowledgedRequest<Request> implements ToXContentObject {
public static final ParseField POLICY_FIELD = new ParseField("policy");
private static final ConstructingObjectParser<Request, Tuple<String, NamedXContentRegistry>> PARSER =
private static final ConstructingObjectParser<Request, String> PARSER =
new ConstructingObjectParser<>("put_lifecycle_request", a -> new Request((LifecyclePolicy) a[0]));
static {
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> LifecyclePolicy.parse(p, c), POLICY_FIELD);
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, name) -> LifecyclePolicy.parse(p, name), POLICY_FIELD);
}
private LifecyclePolicy policy;
@ -130,8 +128,8 @@ public class PutLifecycleAction extends Action<PutLifecycleAction.Request, PutLi
return null;
}
public static Request parseRequest(String name, XContentParser parser, NamedXContentRegistry namedXContentRegistry) {
return PARSER.apply(parser, new Tuple<>(name, namedXContentRegistry));
public static Request parseRequest(String name, XContentParser parser) {
return PARSER.apply(parser, name);
}
@Override

View File

@ -14,11 +14,9 @@ import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.metadata.MetaData.Custom;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -36,11 +34,11 @@ public class IndexLifecycleMetadata implements MetaData.Custom {
public static final ParseField POLICIES_FIELD = new ParseField("policies");
@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<IndexLifecycleMetadata, NamedXContentRegistry> PARSER = new ConstructingObjectParser<>(
public static final ConstructingObjectParser<IndexLifecycleMetadata, Void> PARSER = new ConstructingObjectParser<>(
TYPE, a -> new IndexLifecycleMetadata(
ObjectParserUtils.convertListToMapValues(LifecyclePolicy::getName, (List<LifecyclePolicy>) a[0])));
static {
PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> LifecyclePolicy.parse(p, new Tuple<>(n, c)),
PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> LifecyclePolicy.parse(p, n),
v -> {
throw new IllegalArgumentException("ordered " + POLICIES_FIELD.getPreferredName() + " are not supported");
}, POLICIES_FIELD);

View File

@ -33,8 +33,7 @@ public class RestPutLifecycleAction extends BaseRestHandler {
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
String lifecycleName = restRequest.param("name");
XContentParser parser = restRequest.contentParser();
PutLifecycleAction.Request putLifecycleRequest = PutLifecycleAction.Request.parseRequest(lifecycleName, parser,
restRequest.getXContentRegistry());
PutLifecycleAction.Request putLifecycleRequest = PutLifecycleAction.Request.parseRequest(lifecycleName, parser);
putLifecycleRequest.timeout(restRequest.paramAsTime("timeout", putLifecycleRequest.timeout()));
putLifecycleRequest.masterNodeTimeout(restRequest.paramAsTime("master_timeout", putLifecycleRequest.masterNodeTimeout()));

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.indexlifecycle;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.cluster.Diff;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.metadata.MetaData.Custom;
@ -17,9 +18,9 @@ import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractDiffableSerializationTestCase;
import org.elasticsearch.xpack.indexlifecycle.IndexLifecycleMetadata.IndexLifecycleMetadataDiff;
import org.junit.Before;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@ -30,17 +31,6 @@ import java.util.TreeMap;
public class IndexLifecycleMetadataTests extends AbstractDiffableSerializationTestCase<MetaData.Custom> {
private NamedXContentRegistry registry;
@Before
public void setup() {
List<NamedXContentRegistry.Entry> entries = Arrays
.asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse),
new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE),
(p) -> TestLifecycleType.INSTANCE));
registry = new NamedXContentRegistry(entries);
}
@Override
protected IndexLifecycleMetadata createTestInstance() {
int numPolicies = randomInt(5);
@ -65,7 +55,7 @@ public class IndexLifecycleMetadataTests extends AbstractDiffableSerializationTe
@Override
protected IndexLifecycleMetadata doParseInstance(XContentParser parser) throws IOException {
return IndexLifecycleMetadata.PARSER.apply(parser, registry);
return IndexLifecycleMetadata.PARSER.apply(parser, null);
}
@Override
@ -80,6 +70,15 @@ public class IndexLifecycleMetadataTests extends AbstractDiffableSerializationTe
new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, (in) -> TestLifecycleType.INSTANCE)));
}
@Override
protected NamedXContentRegistry xContentRegistry() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse));
entries.add(new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE),
(p) -> TestLifecycleType.INSTANCE));
return new NamedXContentRegistry(entries);
}
@Override
protected MetaData.Custom mutateInstance(MetaData.Custom instance) {
IndexLifecycleMetadata metadata = (IndexLifecycleMetadata) instance;

View File

@ -5,8 +5,8 @@
*/
package org.elasticsearch.xpack.indexlifecycle;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.common.unit.TimeValue;
@ -16,6 +16,7 @@ import org.elasticsearch.test.AbstractSerializingTestCase;
import org.junit.Before;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@ -34,11 +35,10 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
private Phase secondPhase;
private Phase thirdPhase;
private LifecyclePolicy policy;
private NamedXContentRegistry registry;
@Override
protected LifecyclePolicy doParseInstance(XContentParser parser) throws IOException {
return LifecyclePolicy.parse(parser, new Tuple<String, NamedXContentRegistry>(lifecycleName, registry));
return LifecyclePolicy.parse(parser, lifecycleName);
}
@Override
@ -48,6 +48,15 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, (in) -> TestLifecycleType.INSTANCE)));
}
@Override
protected NamedXContentRegistry xContentRegistry() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse));
entries.add(new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE),
(p) -> TestLifecycleType.INSTANCE));
return new NamedXContentRegistry(entries);
}
@Override
protected LifecyclePolicy createTestInstance() {
int numberPhases = randomInt(5);
@ -91,11 +100,6 @@ public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecycleP
@Before
public void setupPolicy() {
List<NamedXContentRegistry.Entry> entries = Arrays
.asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse),
new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE),
(p) -> TestLifecycleType.INSTANCE));
registry = new NamedXContentRegistry(entries);
indexName = randomAlphaOfLengthBetween(1, 20);
lifecycleName = randomAlphaOfLengthBetween(1, 20);
Map<String, Phase> phases = new LinkedHashMap<>();

View File

@ -5,8 +5,8 @@
*/
package org.elasticsearch.xpack.indexlifecycle;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.common.unit.TimeValue;
@ -24,15 +24,10 @@ import java.util.List;
import java.util.Map;
public class PhaseTests extends AbstractSerializingTestCase<Phase> {
private NamedXContentRegistry registry;
private String phaseName;
@Before
public void setup() {
List<NamedXContentRegistry.Entry> entries = Arrays
.asList(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse));
registry = new NamedXContentRegistry(entries);
phaseName = randomAlphaOfLength(20); // NORELEASE we need to randomise the phase name rather
// than use the same name for all instances
}
@ -49,8 +44,7 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
@Override
protected Phase doParseInstance(XContentParser parser) throws IOException {
return Phase.parse(parser, new Tuple<>(phaseName, registry));
return Phase.parse(parser, phaseName);
}
@Override
@ -63,6 +57,13 @@ public class PhaseTests extends AbstractSerializingTestCase<Phase> {
.asList(new NamedWriteableRegistry.Entry(LifecycleAction.class, DeleteAction.NAME, DeleteAction::new)));
}
@Override
protected NamedXContentRegistry xContentRegistry() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse));
return new NamedXContentRegistry(entries);
}
@Override
protected Phase mutateInstance(Phase instance) throws IOException {
String name = instance.getName();

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.xpack.indexlifecycle.action;
import org.elasticsearch.cluster.ClusterModule;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.unit.TimeValue;
@ -21,6 +22,7 @@ import org.elasticsearch.xpack.indexlifecycle.TestLifecycleType;
import org.elasticsearch.xpack.indexlifecycle.action.PutLifecycleAction.Request;
import org.junit.Before;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@ -29,16 +31,10 @@ import java.util.Map;
public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase<PutLifecycleAction.Request> {
private NamedXContentRegistry registry;
private String lifecycleName;
@Before
public void setup() {
List<NamedXContentRegistry.Entry> entries = Arrays.asList(
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse),
new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE),
(p) -> TestLifecycleType.INSTANCE));
registry = new NamedXContentRegistry(entries);
lifecycleName = randomAlphaOfLength(20); // NORELEASE we need to randomise the lifecycle name rather
// than use the same name for all instances
}
@ -55,7 +51,7 @@ public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase
@Override
protected Request doParseInstance(XContentParser parser) {
return PutLifecycleAction.Request.parseRequest(lifecycleName, parser, registry);
return PutLifecycleAction.Request.parseRequest(lifecycleName, parser);
}
protected NamedWriteableRegistry getNamedWriteableRegistry() {
@ -64,6 +60,15 @@ public class PutLifecycleRequestTests extends AbstractStreamableXContentTestCase
new NamedWriteableRegistry.Entry(LifecycleType.class, TestLifecycleType.TYPE, in -> TestLifecycleType.INSTANCE)));
}
@Override
protected NamedXContentRegistry xContentRegistry() {
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
entries.add(new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse));
entries.add(new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TestLifecycleType.TYPE),
(p) -> TestLifecycleType.INSTANCE));
return new NamedXContentRegistry(entries);
}
protected boolean supportsUnknownFields() {
return false;
}