Skip to content

Commit

Permalink
ditch getValueNegativeIndex and let the superclass do more
Browse files Browse the repository at this point in the history
  • Loading branch information
David Byron committed Oct 18, 2018
1 parent f9c49ad commit 7d79492
Showing 1 changed file with 11 additions and 59 deletions.
70 changes: 11 additions & 59 deletions src/main/java/com/hubspot/jinjava/el/ext/JinjavaListELResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,73 +31,25 @@ public boolean isReadOnly(ELContext context, Object base, Object property) {
@Override
public Object getValue(ELContext context, Object base, Object property) {
try {
final Object superValue = super.getValue(context, base, property);
if (superValue != null) {
return superValue;
// If we're dealing with a negative index, convert it to a positive one.
if (isResolvable(base)) {
int index = toIndex(property);
List<?> list = (List<?>) base;
if (index < 0) {
// Leave the range checking to the superclass.
index += list.size();
}
return super.getValue(context, base, index);
}
return getValueNegativeIndex(context, base, property);
return super.getValue(context, base, property);
} catch (IllegalArgumentException e) {
return null;
}
}

/**
* Based on ListELResolver::getValue. If the base object is a list, and the
* given index is negative, returns the value at the given index. The index is
* specified by the property argument, and coerced into an integer. If the
* coercion could not be performed, an IllegalArgumentException is thrown. If
* the index is positive or out of bounds, null is returned. If the base is a
* List, the propertyResolved property of the ELContext object must be set to
* true by this resolver, before returning. If this property is not true after
* this method is called, the caller should ignore the return value.
*
* <p>-1: the last element
* -2: the second to last element
* etc.
*
* @param context
* The context of this evaluation.
* @param base
* The list to analyze. Only bases of type List are handled by this resolver.
* @param property
* The index of the element in the list to return the acceptable type for. Will be
* coerced into an integer, but otherwise ignored by this resolver.
* @return If the propertyResolved property of ELContext was set to true, then the value at the
* given index or null if the index was out of bounds. Otherwise, undefined.
* @throws IllegalArgumentException
* if the property could not be coerced into an integer.
* @throws NullPointerException
* if context is null
* @throws ELException
* if an exception was thrown while performing the property or variable resolution.
* The thrown exception must be included as the cause property of this exception, if
* available.
* Copied from the unfortunately private ListELResolver.isResolvable
*/
private Object getValueNegativeIndex(ELContext context, Object base, Object property) {
if (context == null) {
throw new NullPointerException("context is null");
}

Object result = null;
if (isResolvable(base)) {
int index = toIndex(property);
List<?> list = (List<?>) base;
// Handle negative indeces. Assume 0 and positive indeces are handled
// elsewhere.
//
// For example, with a 4 element list, -4 means the element at index 0.
// -5 is out of bounds.
if ((index < 0) && (index >= (-1 * list.size()))) {
result = list.get(list.size() + index);
}
context.setPropertyResolved(true);
}
return result;
}

/**
* Copied from the unfortunately private ListELResolver.isResolvable
*/
private static boolean isResolvable(Object base) {
return base instanceof List<?>;
}
Expand Down

0 comments on commit 7d79492

Please sign in to comment.