Browse Source

Merge remote-tracking branch 'dakrone/switch-explain-to-objectparser'

Lee Hinman 9 years ago
parent
commit
0b1b366fe8

+ 16 - 35
core/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainRequest.java

@@ -23,9 +23,11 @@ import org.elasticsearch.ElasticsearchParseException;
 import org.elasticsearch.action.ActionRequestValidationException;
 import org.elasticsearch.action.support.master.MasterNodeRequest;
 import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.ParseField;
 import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
+import org.elasticsearch.common.xcontent.ObjectParser;
 import org.elasticsearch.common.xcontent.XContentParser;
 
 import java.io.IOException;
@@ -38,6 +40,13 @@ import static org.elasticsearch.action.ValidateActions.addValidationError;
  */
 public class ClusterAllocationExplainRequest extends MasterNodeRequest<ClusterAllocationExplainRequest> {
 
+    private static ObjectParser<ClusterAllocationExplainRequest, Void> PARSER = new ObjectParser("cluster/allocation/explain");
+    static {
+        PARSER.declareString(ClusterAllocationExplainRequest::setIndex, new ParseField("index"));
+        PARSER.declareInt(ClusterAllocationExplainRequest::setShard, new ParseField("shard"));
+        PARSER.declareBoolean(ClusterAllocationExplainRequest::setPrimary, new ParseField("primary"));
+    }
+
     private String index;
     private Integer shard;
     private Boolean primary;
@@ -101,7 +110,7 @@ public class ClusterAllocationExplainRequest extends MasterNodeRequest<ClusterAl
     }
 
     @Nullable
-    public int getShard() {
+    public Integer getShard() {
         return this.shard;
     }
 
@@ -111,7 +120,7 @@ public class ClusterAllocationExplainRequest extends MasterNodeRequest<ClusterAl
     }
 
     @Nullable
-    public boolean isPrimary() {
+    public Boolean isPrimary() {
         return this.primary;
     }
 
@@ -139,40 +148,12 @@ public class ClusterAllocationExplainRequest extends MasterNodeRequest<ClusterAl
     }
 
     public static ClusterAllocationExplainRequest parse(XContentParser parser) throws IOException {
-        String currentFieldName = null;
-        String index = null;
-        Integer shard = null;
-        Boolean primary = null;
-        XContentParser.Token token;
-        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
-            if (token == XContentParser.Token.FIELD_NAME) {
-                currentFieldName = parser.currentName();
-            } else if (token.isValue()) {
-                if ("index".equals(currentFieldName)) {
-                    index = parser.text();
-                } else if ("shard".equals(currentFieldName)) {
-                    shard = parser.intValue();
-                } else if ("primary".equals(currentFieldName)) {
-                    primary = parser.booleanValue();
-                } else {
-                    throw new ElasticsearchParseException("unexpected field [" + currentFieldName + "] in allocation explain request");
-                }
-
-            } else if (token == XContentParser.Token.START_OBJECT) {
-                // the object was started
-                continue;
-            } else {
-                throw new ElasticsearchParseException("unexpected token [" + token + "] in allocation explain request");
-            }
-        }
-
-        if (index == null && shard == null && primary == null) {
-            // If it was an empty body, use the "any unassigned shard" request
-            return new ClusterAllocationExplainRequest();
-        } else if (index == null || shard == null || primary == null) {
-            throw new ElasticsearchParseException("'index', 'shard', and 'primary' must be specified in allocation explain request");
+        ClusterAllocationExplainRequest req = PARSER.parse(parser, new ClusterAllocationExplainRequest());
+        Exception e = req.validate();
+        if (e != null) {
+            throw new ElasticsearchParseException("'index', 'shard', and 'primary' must be specified in allocation explain request", e);
         }
-        return new ClusterAllocationExplainRequest(index, shard, primary);
+        return req;
     }
 
     @Override

+ 17 - 0
core/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java

@@ -58,6 +58,23 @@ public class ObjectParserTests extends ESTestCase {
         assertEquals(objectParser.toString(), "ObjectParser{name='foo', fields=[FieldParser{preferred_name=test, supportedTokens=[VALUE_STRING], type=STRING}, FieldParser{preferred_name=test_number, supportedTokens=[VALUE_STRING, VALUE_NUMBER], type=INT}, FieldParser{preferred_name=test_array, supportedTokens=[START_ARRAY, VALUE_STRING, VALUE_NUMBER], type=INT_ARRAY}, FieldParser{preferred_name=test_array, supportedTokens=[START_ARRAY, VALUE_STRING, VALUE_NUMBER], type=INT_ARRAY}, FieldParser{preferred_name=test_number, supportedTokens=[VALUE_STRING, VALUE_NUMBER], type=INT}]}");
     }
 
+    public void testEmptyObject() throws Exception {
+        XContentParser parser = XContentType.JSON.xContent().createParser("{}");
+        class TestStruct {
+            public String val = null;
+            public void setVal(String val) {
+                this.val = val;
+            }
+        }
+
+        ObjectParser<TestStruct, Void> objectParser = new ObjectParser("eggplant");
+        TestStruct s = new TestStruct();
+
+        objectParser.declareString(TestStruct::setVal, new ParseField("anything"));
+        objectParser.parse(parser, s);
+        assertNull("s.val should be null", s.val);
+    }
+
     public void testObjectOrDefault() throws IOException {
         XContentParser parser = XContentType.JSON.xContent().createParser("{\"object\" : { \"test\": 2}}");
         ObjectParser<StaticTestStruct, Void> objectParser = new ObjectParser("foo", StaticTestStruct::new);