Skip to content

Commit

Permalink
Better handling of fields that have . in their name when doing prop…
Browse files Browse the repository at this point in the history
…erty based navigation, closes elastic#1875.
  • Loading branch information
kimchy committed Apr 19, 2012
1 parent 03c9eaf commit 98b1f36
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Expand Up @@ -53,15 +53,24 @@ private static void extractRawValues(List values, Map<String, Object> part, Stri
if (index == pathElements.length) {
return;
}
String currentPath = pathElements[index];
Object currentValue = part.get(currentPath);

String key = pathElements[index];
Object currentValue = part.get(key);
int nextIndex = index + 1;
while (currentValue == null && nextIndex != pathElements.length) {
key += "." + pathElements[nextIndex];
currentValue = part.get(key);
nextIndex++;
}

if (currentValue == null) {
return;
}

if (currentValue instanceof Map) {
extractRawValues(values, (Map<String, Object>) currentValue, pathElements, index + 1);
extractRawValues(values, (Map<String, Object>) currentValue, pathElements, nextIndex);
} else if (currentValue instanceof List) {
extractRawValues(values, (List) currentValue, pathElements, index + 1);
extractRawValues(values, (List) currentValue, pathElements, nextIndex);
} else {
values.add(currentValue);
}
Expand Down Expand Up @@ -101,7 +110,15 @@ private static Object extractValue(String[] pathElements, int index, Object curr
}
if (currentValue instanceof Map) {
Map map = (Map) currentValue;
return extractValue(pathElements, index + 1, map.get(pathElements[index]));
String key = pathElements[index];
Object mapValue = map.get(key);
int nextIndex = index + 1;
while (mapValue == null && nextIndex != pathElements.length) {
key += "." + pathElements[nextIndex];
mapValue = map.get(key);
nextIndex++;
}
return extractValue(pathElements, nextIndex, mapValue);
}
if (currentValue instanceof List) {
List valueList = (List) currentValue;
Expand Down
Expand Up @@ -148,5 +148,51 @@ public void testExtractValue() throws Exception {
assertThat(extListValue.size(), equalTo(2));
assertThat(extListValue.get(0).toString(), equalTo("value1"));
assertThat(extListValue.get(1).toString(), equalTo("value2"));

// fields with . in them
builder = XContentFactory.jsonBuilder().startObject()
.field("xxx.yyy", "value")
.endObject();
map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose();
assertThat(XContentMapValues.extractValue("xxx.yyy", map).toString(), equalTo("value"));

builder = XContentFactory.jsonBuilder().startObject()
.startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject()
.endObject();

map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose();
assertThat(XContentMapValues.extractValue("path1.xxx.path2.yyy.test", map).toString(), equalTo("value"));
}

@SuppressWarnings({"unchecked"})
@Test
public void testExtractRawValue() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
.field("test", "value")
.endObject();

Map<String, Object> map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose();
assertThat(XContentMapValues.extractRawValues("test", map).get(0).toString(), equalTo("value"));

builder = XContentFactory.jsonBuilder().startObject()
.field("test.me", "value")
.endObject();

map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose();
assertThat(XContentMapValues.extractRawValues("test.me", map).get(0).toString(), equalTo("value"));

builder = XContentFactory.jsonBuilder().startObject()
.startObject("path1").startObject("path2").field("test", "value").endObject().endObject()
.endObject();

map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose();
assertThat(XContentMapValues.extractRawValues("path1.path2.test", map).get(0).toString(), equalTo("value"));

builder = XContentFactory.jsonBuilder().startObject()
.startObject("path1.xxx").startObject("path2.yyy").field("test", "value").endObject().endObject()
.endObject();

map = XContentFactory.xContent(XContentType.JSON).createParser(builder.string()).mapAndClose();
assertThat(XContentMapValues.extractRawValues("path1.xxx.path2.yyy.test", map).get(0).toString(), equalTo("value"));
}
}

0 comments on commit 98b1f36

Please sign in to comment.