-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add fromXContent method to GetResponse #22082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
086e3e9
d3cb7cb
8815682
99f41fe
5036ec8
64c37d9
6424cbb
4b570ce
a7a0d5b
dbe320a
5361c1c
34d2a5c
14cdd53
205933e
627b326
1fca7f8
c74fd5b
d4a47d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,12 +27,14 @@ | |
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.index.get.GetField; | ||
| import org.elasticsearch.index.get.GetResult; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Iterator; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * The response of a get action. | ||
|
|
@@ -42,7 +44,7 @@ | |
| */ | ||
| public class GetResponse extends ActionResponse implements Iterable<GetField>, ToXContent { | ||
|
|
||
| private GetResult getResult; | ||
| GetResult getResult; | ||
|
|
||
| GetResponse() { | ||
| } | ||
|
|
@@ -156,6 +158,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
| return getResult.toXContent(builder, params); | ||
|
||
| } | ||
|
|
||
| public static GetResponse fromXContent(XContentParser parser) throws IOException { | ||
| GetResult getResult = GetResult.fromXContent(parser); | ||
| return new GetResponse(getResult); | ||
| } | ||
|
|
||
| @Override | ||
| public void readFrom(StreamInput in) throws IOException { | ||
| super.readFrom(in); | ||
|
|
@@ -168,6 +175,23 @@ public void writeTo(StreamOutput out) throws IOException { | |
| getResult.writeTo(out); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| GetResponse getResponse = (GetResponse) o; | ||
| return Objects.equals(getResult, getResponse.getResult); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(getResult); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return Strings.toString(this, true); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -349,4 +349,22 @@ public static void writeRawField(String field, BytesReference source, XContentBu | |
| builder.rawField(field, source); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided | ||
| * {@link XContentType}. Wraps the output into a new anonymous object depending on the value of the wrapInObject argument. | ||
| */ | ||
| public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, boolean wrapInObject) throws IOException { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a test for this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done after merging: 87d8764 |
||
| try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { | ||
| if (wrapInObject) { | ||
| builder.startObject(); | ||
| } | ||
| toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
| if (wrapInObject) { | ||
| builder.endObject(); | ||
| } | ||
| return builder.bytes(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,17 +19,25 @@ | |
|
|
||
| package org.elasticsearch.index.get; | ||
|
|
||
| import org.elasticsearch.common.ParsingException; | ||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Streamable; | ||
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.index.mapper.MapperService; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class GetField implements Streamable, Iterable<Object> { | ||
| import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
|
||
| public class GetField implements Streamable, ToXContent, Iterable<Object> { | ||
|
|
||
| private String name; | ||
| private List<Object> values; | ||
|
|
@@ -38,8 +46,8 @@ private GetField() { | |
| } | ||
|
|
||
| public GetField(String name, List<Object> values) { | ||
| this.name = name; | ||
| this.values = values; | ||
| this.name = Objects.requireNonNull(name, "name must not be null"); | ||
| this.values = Objects.requireNonNull(values, "values must not be null"); | ||
| } | ||
|
|
||
| public String getName() { | ||
|
|
@@ -90,4 +98,67 @@ public void writeTo(StreamOutput out) throws IOException { | |
| out.writeGenericValue(obj); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startArray(name); | ||
| for (Object value : values) { | ||
| //this call doesn't really need to support writing any kind of object. | ||
| //Stored fields values are converted using MappedFieldType#valueForDisplay. | ||
| //As a result they can either be Strings, Numbers, Booleans, or BytesReference, that's all. | ||
| builder.value(value); | ||
| } | ||
| builder.endArray(); | ||
| return builder; | ||
| } | ||
|
|
||
| public static GetField fromXContent(XContentParser parser) throws IOException { | ||
| ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation); | ||
| String fieldName = parser.currentName(); | ||
| XContentParser.Token token = parser.nextToken(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this reference is not really useful
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch |
||
| ensureExpectedToken(XContentParser.Token.START_ARRAY, token, parser::getTokenLocation); | ||
| List<Object> values = new ArrayList<>(); | ||
| while((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { | ||
| Object value; | ||
| if (token == XContentParser.Token.VALUE_STRING) { | ||
| value = parser.text(); | ||
| } else if (token == XContentParser.Token.VALUE_NUMBER) { | ||
| value = parser.numberValue(); | ||
| } else if (token == XContentParser.Token.VALUE_BOOLEAN) { | ||
| value = parser.booleanValue(); | ||
| } else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) { | ||
| value = new BytesArray(parser.binaryValue()); | ||
| } else { | ||
| throw new ParsingException(parser.getTokenLocation(), "Failed to parse object: unsupported token found [" + token + "]"); | ||
| } | ||
| values.add(value); | ||
| } | ||
| return new GetField(fieldName, values); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| GetField objects = (GetField) o; | ||
| return Objects.equals(name, objects.name) && | ||
| Objects.equals(values, objects.values); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name, values); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "GetField{" + | ||
| "name='" + name + '\'' + | ||
| ", values=" + values + | ||
| '}'; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️