Allow negative indices

This commit is contained in:
Elara 2024-02-23 22:06:34 -08:00
parent ed568f81bf
commit 0040828d2d

View File

@ -429,10 +429,16 @@ func (t *Template) getIndex(i ast.Index, local map[string]any) (any, error) {
}
intIndex := rindex.Interface().(int)
if intIndex < rval.Len() {
if intIndex < 0 {
intIndex = rval.Len() + intIndex
if intIndex < 0 {
return nil, ast.PosError(i, "%s: index out of range: %d (length %d)", valueToString(i), intIndex, rval.Len())
}
out = rval.Index(intIndex)
} else if intIndex < rval.Len() {
out = rval.Index(intIndex)
} else {
return nil, ast.PosError(i, "%s: index out of range: %d", valueToString(i), intIndex)
return nil, ast.PosError(i, "%s: index out of range: %d (length %d)", valueToString(i), intIndex, rval.Len())
}
case reflect.Map:
if rindex.CanConvert(rval.Type().Key()) {