Allow negative indices
This commit is contained in:
parent
ed568f81bf
commit
0040828d2d
10
salix.go
10
salix.go
@ -429,10 +429,16 @@ func (t *Template) getIndex(i ast.Index, local map[string]any) (any, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
intIndex := rindex.Interface().(int)
|
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)
|
out = rval.Index(intIndex)
|
||||||
} else {
|
} 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:
|
case reflect.Map:
|
||||||
if rindex.CanConvert(rval.Type().Key()) {
|
if rindex.CanConvert(rval.Type().Key()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user