refactor CompositeValuesSourceParserHelper for reusage by making it public (#33945)

refactor CompositeValuesSourceParserHelper for reusage by making it public and moving toXContent into it
This commit is contained in:
Hendrik Muhs 2018-09-25 09:15:52 +02:00 committed by GitHub
parent 3af8fc74c7
commit bf6cf6b6d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View File

@ -193,11 +193,7 @@ public class CompositeAggregationBuilder extends AbstractAggregationBuilder<Comp
builder.field(SIZE_FIELD_NAME.getPreferredName(), size); builder.field(SIZE_FIELD_NAME.getPreferredName(), size);
builder.startArray(SOURCES_FIELD_NAME.getPreferredName()); builder.startArray(SOURCES_FIELD_NAME.getPreferredName());
for (CompositeValuesSourceBuilder<?> source: sources) { for (CompositeValuesSourceBuilder<?> source: sources) {
builder.startObject(); CompositeValuesSourceParserHelper.toXContent(source, builder, params);
builder.startObject(source.name());
source.toXContent(builder, params);
builder.endObject();
builder.endObject();
} }
builder.endArray(); builder.endArray();
if (after != null) { if (after != null) {

View File

@ -25,7 +25,9 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.AbstractObjectParser; import org.elasticsearch.common.xcontent.AbstractObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.script.Script; import org.elasticsearch.script.Script;
import org.elasticsearch.search.aggregations.support.ValueType; import org.elasticsearch.search.aggregations.support.ValueType;
@ -33,7 +35,7 @@ import java.io.IOException;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
class CompositeValuesSourceParserHelper { public class CompositeValuesSourceParserHelper {
static <VB extends CompositeValuesSourceBuilder<VB>, T> void declareValuesSourceFields(AbstractObjectParser<VB, T> objectParser, static <VB extends CompositeValuesSourceBuilder<VB>, T> void declareValuesSourceFields(AbstractObjectParser<VB, T> objectParser,
ValueType targetValueType) { ValueType targetValueType) {
objectParser.declareField(VB::field, XContentParser::text, objectParser.declareField(VB::field, XContentParser::text,
@ -57,7 +59,7 @@ class CompositeValuesSourceParserHelper {
objectParser.declareField(VB::order, XContentParser::text, new ParseField("order"), ObjectParser.ValueType.STRING); objectParser.declareField(VB::order, XContentParser::text, new ParseField("order"), ObjectParser.ValueType.STRING);
} }
static void writeTo(CompositeValuesSourceBuilder<?> builder, StreamOutput out) throws IOException { public static void writeTo(CompositeValuesSourceBuilder<?> builder, StreamOutput out) throws IOException {
final byte code; final byte code;
if (builder.getClass() == TermsValuesSourceBuilder.class) { if (builder.getClass() == TermsValuesSourceBuilder.class) {
code = 0; code = 0;
@ -72,7 +74,7 @@ class CompositeValuesSourceParserHelper {
builder.writeTo(out); builder.writeTo(out);
} }
static CompositeValuesSourceBuilder<?> readFrom(StreamInput in) throws IOException { public static CompositeValuesSourceBuilder<?> readFrom(StreamInput in) throws IOException {
int code = in.readByte(); int code = in.readByte();
switch(code) { switch(code) {
case 0: case 0:
@ -86,7 +88,7 @@ class CompositeValuesSourceParserHelper {
} }
} }
static CompositeValuesSourceBuilder<?> fromXContent(XContentParser parser) throws IOException { public static CompositeValuesSourceBuilder<?> fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation); ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation);
token = parser.nextToken(); token = parser.nextToken();
@ -117,4 +119,14 @@ class CompositeValuesSourceParserHelper {
parser.nextToken(); parser.nextToken();
return builder; return builder;
} }
public static XContentBuilder toXContent(CompositeValuesSourceBuilder<?> source, XContentBuilder builder, Params params)
throws IOException {
builder.startObject();
builder.startObject(source.name());
source.toXContent(builder, params);
builder.endObject();
builder.endObject();
return builder;
}
} }