Skip to content

Commit cb6f3be

Browse files
Extract RuleScope parser
1 parent cebce61 commit cb6f3be

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/DetectionRule.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@
77

88
import org.elasticsearch.Version;
99
import org.elasticsearch.common.ParseField;
10-
import org.elasticsearch.common.Strings;
1110
import org.elasticsearch.common.io.stream.StreamInput;
1211
import org.elasticsearch.common.io.stream.StreamOutput;
1312
import org.elasticsearch.common.io.stream.Writeable;
14-
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
15-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
16-
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
1713
import org.elasticsearch.common.xcontent.ObjectParser;
1814
import org.elasticsearch.common.xcontent.ToXContentObject;
1915
import org.elasticsearch.common.xcontent.XContentBuilder;
20-
import org.elasticsearch.common.xcontent.XContentFactory;
21-
import org.elasticsearch.common.xcontent.XContentParser;
2216
import org.elasticsearch.xpack.core.ml.MlParserType;
2317
import org.elasticsearch.xpack.core.ml.job.messages.Messages;
2418
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
@@ -28,7 +22,6 @@
2822
import java.util.Collections;
2923
import java.util.EnumMap;
3024
import java.util.EnumSet;
31-
import java.util.HashMap;
3225
import java.util.List;
3326
import java.util.Map;
3427
import java.util.Objects;
@@ -57,24 +50,7 @@ public class DetectionRule implements ToXContentObject, Writeable {
5750
ObjectParser<Builder, Void> parser = PARSERS.get(parserType);
5851
assert parser != null;
5952
parser.declareStringArray(Builder::setActions, ACTIONS_FIELD);
60-
parser.declareObject(Builder::setScope, (p, c) -> {
61-
Map<String, Object> unparsedScope = p.map();
62-
if (unparsedScope.isEmpty()) {
63-
return new RuleScope();
64-
}
65-
ConstructingObjectParser<FilterRef, Void> filterRefParser = FilterRef.PARSERS.get(parserType);
66-
Map<String, FilterRef> scope = new HashMap<>();
67-
for (Map.Entry<String, Object> entry : unparsedScope.entrySet()) {
68-
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
69-
builder.map((Map<String, ?>) entry.getValue());
70-
try (XContentParser scopeParser = XContentFactory.xContent(builder.contentType()).createParser(
71-
NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Strings.toString(builder))) {
72-
scope.put(entry.getKey(), filterRefParser.parse(scopeParser, null));
73-
}
74-
}
75-
}
76-
return new RuleScope(scope);
77-
}, SCOPE_FIELD);
53+
parser.declareObject(Builder::setScope, RuleScope.parser(parserType), SCOPE_FIELD);
7854
parser.declareObjectArray(Builder::setConditions, (p, c) ->
7955
RuleCondition.PARSERS.get(parserType).apply(p, c), CONDITIONS_FIELD);
8056
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
*/
66
package org.elasticsearch.xpack.core.ml.job.config;
77

8+
import org.elasticsearch.common.Strings;
89
import org.elasticsearch.common.io.stream.StreamInput;
910
import org.elasticsearch.common.io.stream.StreamOutput;
1011
import org.elasticsearch.common.io.stream.Writeable;
12+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
13+
import org.elasticsearch.common.xcontent.ContextParser;
14+
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
15+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
1116
import org.elasticsearch.common.xcontent.ToXContentObject;
1217
import org.elasticsearch.common.xcontent.XContentBuilder;
18+
import org.elasticsearch.common.xcontent.XContentFactory;
19+
import org.elasticsearch.common.xcontent.XContentParser;
20+
import org.elasticsearch.xpack.core.ml.MlParserType;
1321
import org.elasticsearch.xpack.core.ml.job.messages.Messages;
1422
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
1523

@@ -24,6 +32,27 @@
2432

2533
public class RuleScope implements ToXContentObject, Writeable {
2634

35+
public static ContextParser<Void, RuleScope> parser(MlParserType parserType) {
36+
return (p, c) -> {
37+
Map<String, Object> unparsedScope = p.map();
38+
if (unparsedScope.isEmpty()) {
39+
return new RuleScope();
40+
}
41+
ConstructingObjectParser<FilterRef, Void> filterRefParser = FilterRef.PARSERS.get(parserType);
42+
Map<String, FilterRef> scope = new HashMap<>();
43+
for (Map.Entry<String, Object> entry : unparsedScope.entrySet()) {
44+
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
45+
builder.map((Map<String, ?>) entry.getValue());
46+
try (XContentParser scopeParser = XContentFactory.xContent(builder.contentType()).createParser(
47+
NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Strings.toString(builder))) {
48+
scope.put(entry.getKey(), filterRefParser.parse(scopeParser, null));
49+
}
50+
}
51+
}
52+
return new RuleScope(scope);
53+
};
54+
}
55+
2756
private final Map<String, FilterRef> scope;
2857

2958
public RuleScope() {

0 commit comments

Comments
 (0)