Skip to content

Commit 35c9258

Browse files
authored
Refactor FilterXContentParser and DelegatingXContentParser (#83457)
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.
1 parent bf00ab3 commit 35c9258

File tree

10 files changed

+98
-528
lines changed

10 files changed

+98
-528
lines changed

libs/x-content/src/main/java/org/elasticsearch/xcontent/DelegatingXContentParser.java

Lines changed: 0 additions & 244 deletions
This file was deleted.

libs/x-content/src/main/java/org/elasticsearch/xcontent/DotExpandingXContentParser.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*
1818
* A fieldname named {@code "foo.bar.baz":...} will be parsed instead as {@code 'foo':{'bar':{'baz':...}}}
1919
*/
20-
public class DotExpandingXContentParser extends FilterXContentParser {
20+
public class DotExpandingXContentParser extends FilterXContentParserWrapper {
2121

22-
private static class WrappingParser extends DelegatingXContentParser {
22+
private static final class WrappingParser extends FilterXContentParser {
2323

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

@@ -135,7 +135,7 @@ public Token nextToken() throws IOException {
135135
assert expandedTokens < subPaths.length * 2;
136136
if (expandedTokens == subPaths.length * 2 - 1) {
137137
state = State.PARSING_ORIGINAL_CONTENT;
138-
Token token = in.currentToken();
138+
Token token = delegate().currentToken();
139139
if (token == Token.START_OBJECT || token == Token.START_ARRAY) {
140140
innerLevel++;
141141
}
@@ -170,7 +170,7 @@ public Token currentToken() {
170170
return switch (state) {
171171
case EXPANDING_START_OBJECT -> expandedTokens % 2 == 1 ? Token.START_OBJECT : Token.FIELD_NAME;
172172
case ENDING_EXPANDED_OBJECT -> Token.END_OBJECT;
173-
case PARSING_ORIGINAL_CONTENT -> in.currentToken();
173+
case PARSING_ORIGINAL_CONTENT -> delegate().currentToken();
174174
};
175175
}
176176

@@ -181,14 +181,14 @@ public String currentName() throws IOException {
181181
// whenever we are parsing some inner object/array we can easily delegate to the inner parser
182182
// e.g. field.with.dots: { obj:{ parsing here } }
183183
if (innerLevel > 0) {
184-
return in.currentName();
184+
return delegate().currentName();
185185
}
186186
Token token = currentToken();
187187
// if we are parsing the outer object/array, only at the start object/array we need to return
188188
// e.g. dots instead of field.with.dots otherwise we can simply delegate to the inner parser
189189
// which will do the right thing
190190
if (innerLevel == 0 && token != Token.START_OBJECT && token != Token.START_ARRAY) {
191-
return in.currentName();
191+
return delegate().currentName();
192192
}
193193
// note that innerLevel can be -1 if there are no inner object/array e.g. field.with.dots: value
194194
// as well as while there is and we are parsing their END_OBJECT or END_ARRAY
@@ -199,7 +199,7 @@ public String currentName() throws IOException {
199199
@Override
200200
public void skipChildren() throws IOException {
201201
if (state == State.EXPANDING_START_OBJECT) {
202-
in.skipChildren();
202+
delegate().skipChildren();
203203
state = State.ENDING_EXPANDED_OBJECT;
204204
}
205205
if (state == State.PARSING_ORIGINAL_CONTENT) {
@@ -231,7 +231,7 @@ public boolean booleanValue() throws IOException {
231231
return super.booleanValue();
232232
}
233233

234-
private static class SingletonValueXContentParser extends FilterXContentParser {
234+
private static class SingletonValueXContentParser extends FilterXContentParserWrapper {
235235

236236
protected SingletonValueXContentParser(XContentParser in) {
237237
super(in);

0 commit comments

Comments
 (0)