Skip to content

Commit

Permalink
Refactor FilterXContentParser and DelegatingXContentParser (#83457)
Browse files Browse the repository at this point in the history
We have two implementations of XContentParser that both delegate all of its methods to a delegate, either an inner parser provided at construction (FilterXContentParser) or a more dynamic variant that is returned by overriding the delegate method (DelegatingXContentParser).

Effectively the two classes do exactly the same, the only difference being how the delegate parser is provided. While these two are two separate implementations, they could inherit from each other.

With this change we make FilterXContentParser be the previous DelegatingXContentParser, that allows to override the delegate method, and we introduce a new FilterXContentParserWrapper that takes the fixed delegate as a constructor argument.

Additionally, XContentSubParser is rewritten to extend FilterXContentParserWrapper.
  • Loading branch information
javanna committed Feb 17, 2022
1 parent bf00ab3 commit 35c9258
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 528 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*
* A fieldname named {@code "foo.bar.baz":...} will be parsed instead as {@code 'foo':{'bar':{'baz':...}}}
*/
public class DotExpandingXContentParser extends FilterXContentParser {
public class DotExpandingXContentParser extends FilterXContentParserWrapper {

private static class WrappingParser extends DelegatingXContentParser {
private static final class WrappingParser extends FilterXContentParser {

final Deque<XContentParser> parsers = new ArrayDeque<>();

Expand Down Expand Up @@ -135,7 +135,7 @@ public Token nextToken() throws IOException {
assert expandedTokens < subPaths.length * 2;
if (expandedTokens == subPaths.length * 2 - 1) {
state = State.PARSING_ORIGINAL_CONTENT;
Token token = in.currentToken();
Token token = delegate().currentToken();
if (token == Token.START_OBJECT || token == Token.START_ARRAY) {
innerLevel++;
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public Token currentToken() {
return switch (state) {
case EXPANDING_START_OBJECT -> expandedTokens % 2 == 1 ? Token.START_OBJECT : Token.FIELD_NAME;
case ENDING_EXPANDED_OBJECT -> Token.END_OBJECT;
case PARSING_ORIGINAL_CONTENT -> in.currentToken();
case PARSING_ORIGINAL_CONTENT -> delegate().currentToken();
};
}

Expand All @@ -181,14 +181,14 @@ public String currentName() throws IOException {
// whenever we are parsing some inner object/array we can easily delegate to the inner parser
// e.g. field.with.dots: { obj:{ parsing here } }
if (innerLevel > 0) {
return in.currentName();
return delegate().currentName();
}
Token token = currentToken();
// if we are parsing the outer object/array, only at the start object/array we need to return
// e.g. dots instead of field.with.dots otherwise we can simply delegate to the inner parser
// which will do the right thing
if (innerLevel == 0 && token != Token.START_OBJECT && token != Token.START_ARRAY) {
return in.currentName();
return delegate().currentName();
}
// note that innerLevel can be -1 if there are no inner object/array e.g. field.with.dots: value
// as well as while there is and we are parsing their END_OBJECT or END_ARRAY
Expand All @@ -199,7 +199,7 @@ public String currentName() throws IOException {
@Override
public void skipChildren() throws IOException {
if (state == State.EXPANDING_START_OBJECT) {
in.skipChildren();
delegate().skipChildren();
state = State.ENDING_EXPANDED_OBJECT;
}
if (state == State.PARSING_ORIGINAL_CONTENT) {
Expand Down Expand Up @@ -231,7 +231,7 @@ public boolean booleanValue() throws IOException {
return super.booleanValue();
}

private static class SingletonValueXContentParser extends FilterXContentParser {
private static class SingletonValueXContentParser extends FilterXContentParserWrapper {

protected SingletonValueXContentParser(XContentParser in) {
super(in);
Expand Down

0 comments on commit 35c9258

Please sign in to comment.