mirror of
https://github.com/elastic/logstash.git
synced 2025-04-23 22:27:21 -04:00
Allow negative array indexes to mean an offset from the end of the array, where -1 means the last element.
Fixes #6226
This commit is contained in:
parent
c720d26bcf
commit
32585eac8c
1 changed files with 22 additions and 7 deletions
|
@ -34,10 +34,15 @@ public class Accessors {
|
|||
return ((Map<String, Object>) target).remove(field.getKey());
|
||||
} else if (target instanceof List) {
|
||||
int i = Integer.parseInt(field.getKey());
|
||||
if (i < 0 || i >= ((List) target).size()) {
|
||||
return null;
|
||||
int size = ((List) target).size();
|
||||
if (i >= size || i < -size) {
|
||||
return null;
|
||||
} else if (i < 0) {
|
||||
// Offset from the end of the array.
|
||||
return ((List<Object>) target).remove(size + i);
|
||||
} else {
|
||||
return ((List<Object>) target).remove(i);
|
||||
}
|
||||
return ((List<Object>) target).remove(i);
|
||||
} else {
|
||||
throw newCollectionException(target);
|
||||
}
|
||||
|
@ -112,9 +117,15 @@ public class Accessors {
|
|||
}
|
||||
|
||||
private boolean foundInList(List<Object> target, int index) {
|
||||
if (index < 0 || index >= target.size()) {
|
||||
int size = ((List) target).size();
|
||||
if (index >= size || index < -size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index < 0) {
|
||||
index = size + index;
|
||||
}
|
||||
|
||||
return target.get(index) != null;
|
||||
}
|
||||
|
||||
|
@ -128,11 +139,15 @@ public class Accessors {
|
|||
return result;
|
||||
} else if (target instanceof List) {
|
||||
int i = Integer.parseInt(key);
|
||||
if (i < 0 || i >= ((List) target).size()) {
|
||||
int size = ((List) target).size();
|
||||
if (i >= size || i < -size) {
|
||||
return null;
|
||||
} else if (i < 0) {
|
||||
// Offset from the end of the array.
|
||||
return ((List<Object>) target).get(size + i);
|
||||
} else {
|
||||
return ((List<Object>) target).get(i);
|
||||
}
|
||||
Object result = ((List<Object>) target).get(i);
|
||||
return result;
|
||||
} else if (target == null) {
|
||||
return null;
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue