[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:
parent
bbaa23bdfd
commit
d5df738538
|
@ -31,16 +31,19 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class ObjectPath {
|
public class ObjectPath {
|
||||||
|
|
||||||
private final Map<String, Object> object;
|
private final Object object;
|
||||||
|
|
||||||
public static ObjectPath createFromXContent(String input) throws IOException {
|
public static ObjectPath createFromXContent(String input) throws IOException {
|
||||||
try (XContentParser parser = XContentFactory.xContent(input).createParser(input)) {
|
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());
|
return new ObjectPath(parser.mapOrdered());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectPath(Map<String, Object> map) throws IOException {
|
public ObjectPath(Object object) throws IOException {
|
||||||
this.object = map;
|
this.object = object;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -198,4 +198,28 @@ public class ObjectPathTests extends ESTestCase {
|
||||||
assertThat(object, notNullValue());
|
assertThat(object, notNullValue());
|
||||||
assertThat(object.toString(), equalTo("value1"));
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue