2020package org .elasticsearch .client .dataframe .transforms .pivot ;
2121
2222import org .elasticsearch .common .ParsingException ;
23+ import org .elasticsearch .common .Strings ;
2324import org .elasticsearch .common .xcontent .ToXContentObject ;
2425import org .elasticsearch .common .xcontent .XContentBuilder ;
2526import org .elasticsearch .common .xcontent .XContentParser ;
2627
2728import java .io .IOException ;
2829import java .util .LinkedHashMap ;
29- import java .util .Locale ;
3030import java .util .Map ;
3131import java .util .Objects ;
3232
@@ -51,9 +51,7 @@ public static GroupConfig fromXContent(final XContentParser parser) throws IOExc
5151
5252 // be parsing friendly, whether the token needs to be advanced or not (similar to what ObjectParser does)
5353 XContentParser .Token token ;
54- if (parser .currentToken () == XContentParser .Token .START_OBJECT ) {
55- parser .currentToken ();
56- } else {
54+ if (parser .currentToken () != XContentParser .Token .START_OBJECT ) {
5755 token = parser .nextToken ();
5856 if (token != XContentParser .Token .START_OBJECT ) {
5957 throw new ParsingException (parser .getTokenLocation (), "Failed to parse object: Expected START_OBJECT but was: " + token );
@@ -70,37 +68,64 @@ public static GroupConfig fromXContent(final XContentParser parser) throws IOExc
7068 }
7169 continue ;
7270 }
73- String destinationFieldName = parser .currentName ();
7471
72+ String destinationFieldName = parser .currentName ();
7573 ensureExpectedToken (XContentParser .Token .START_OBJECT , token , parser ::getTokenLocation );
7674 token = parser .nextToken ();
7775 ensureExpectedToken (XContentParser .Token .FIELD_NAME , token , parser ::getTokenLocation );
78- SingleGroupSource . Type groupType = SingleGroupSource . Type . valueOf ( parser .currentName (). toUpperCase ( Locale . ROOT ) );
76+ String groupType = parser .currentName ();
7977
8078 token = parser .nextToken ();
81- ensureExpectedToken (XContentParser .Token .START_OBJECT , token , parser ::getTokenLocation );
82- SingleGroupSource groupSource ;
79+ if (token != XContentParser .Token .START_OBJECT ) {
80+ // need to consume up to dest field end obj
81+ consumeUntilEndObject (parser , 1 );
82+ continue ;
83+ }
84+
85+ SingleGroupSource groupSource = null ;
8386 switch (groupType ) {
84- case TERMS :
87+ case "terms" :
8588 groupSource = TermsGroupSource .fromXContent (parser );
8689 break ;
87- case HISTOGRAM :
90+ case "histogram" :
8891 groupSource = HistogramGroupSource .fromXContent (parser );
8992 break ;
90- case DATE_HISTOGRAM :
93+ case "date_histogram" :
9194 groupSource = DateHistogramGroupSource .fromXContent (parser );
9295 break ;
9396 default :
94- throw new ParsingException (parser .getTokenLocation (), "invalid grouping type: " + groupType );
97+ // not a valid group source. Consume up to the dest field end object
98+ consumeUntilEndObject (parser , 2 );
9599 }
96- // destination field end_object
97- parser .nextToken ();
98100
99- groups .put (destinationFieldName , groupSource );
101+ if (groupSource != null ) {
102+ groups .put (destinationFieldName , groupSource );
103+ // destination field end_object
104+ parser .nextToken ();
105+ }
100106 }
101107 return new GroupConfig (groups );
102108 }
103109
110+ /**
111+ * Consume tokens from the parser until {@code endObjectCount} of end object
112+ * tokens have been read. Nested objects that start and end inside the current
113+ * field are skipped and do contribute to the end object count.
114+ * @param parser The XContent parser
115+ * @param endObjectCount Number of end object tokens to consume
116+ * @throws IOException On parsing error
117+ */
118+ private static void consumeUntilEndObject (XContentParser parser , int endObjectCount ) throws IOException {
119+ do {
120+ XContentParser .Token token = parser .nextToken ();
121+ if (token == XContentParser .Token .START_OBJECT ) {
122+ endObjectCount ++;
123+ } else if (token == XContentParser .Token .END_OBJECT ) {
124+ endObjectCount --;
125+ }
126+ } while (endObjectCount != 0 );
127+ }
128+
104129 public GroupConfig (Map <String , SingleGroupSource > groups ) {
105130 this .groups = groups ;
106131 }
@@ -144,4 +169,9 @@ public boolean equals(Object other) {
144169 public int hashCode () {
145170 return Objects .hash (groups );
146171 }
172+
173+ @ Override
174+ public String toString () {
175+ return Strings .toString (this , true , true );
176+ }
147177}
0 commit comments