[TEST] ObjectPath to support parsing yaml or json that have an array as root object

ObjectPath used a Map up until now for the internal representation of its navigable object. That works in most of the cases, but there could also be an array as root object, in which case a List needs to be used instead of a Map. This commit changes the internal representation of the object to Object which can either be a List or a Map. The change is minimal as ObjectPath already had the checks in place to verify the type of the object in the current position and navigate through it.

  Note: The new test added to ObjectPathTest uses yaml format explicitly as auto-detection of json format works only for a json object that starts with '{', not if the root object is actually an array and starts with '['.
This commit is contained in:
javanna 2016-06-25 15:35:20 +02:00 committed by Luca Cavanna
parent bbaa23bdfd
commit d5df738538
2 changed files with 30 additions and 3 deletions

View File

@ -31,16 +31,19 @@ import java.util.Map;
*/
public class ObjectPath {
private final Map<String, Object> object;
private final Object object;
public static ObjectPath createFromXContent(String input) throws IOException {
try (XContentParser parser = XContentFactory.xContent(input).createParser(input)) {
if (parser.nextToken() == XContentParser.Token.START_ARRAY) {
return new ObjectPath(parser.listOrderedMap());
}
return new ObjectPath(parser.mapOrdered());
}
}
public ObjectPath(Map<String, Object> map) throws IOException {
this.object = map;
public ObjectPath(Object object) throws IOException {
this.object = object;
}
/**

View File

@ -198,4 +198,28 @@ public class ObjectPathTests extends ESTestCase {
assertThat(object, notNullValue());
assertThat(object.toString(), equalTo("value1"));
}
@SuppressWarnings("unchecked")
public void testEvaluateArrayAsRoot() throws Exception {
String input = "---\n" +
"- alias: \"test_alias1\"\n" +
" index: \"test1\"\n" +
"- alias: \"test_alias2\"\n" +
" index: \"test2\"";
ObjectPath objectPath = ObjectPath.createFromXContent(input);
Object object = objectPath.evaluate("");
assertThat(object, notNullValue());
assertThat(object, instanceOf(List.class));
assertThat(((List<Object>)object).size(), equalTo(2));
object = objectPath.evaluate("0");
assertThat(object, notNullValue());
assertThat(object, instanceOf(Map.class));
assertThat(((Map<String, Object>)object).get("alias"), equalTo("test_alias1"));
object = objectPath.evaluate("1.index");
assertThat(object, notNullValue());
assertThat(object, instanceOf(String.class));
assertThat(object, equalTo("test2"));
}
}