MCPcopy
hub / github.com/flosch/pongo2 / Index

Method Index

value.go:300–319  ·  view source on GitHub ↗

Index gets the i-th item of an array, slice or string. Otherwise it will return NIL.

(i int)

Source from the content-addressed store, hash-verified

298// Index gets the i-th item of an array, slice or string. Otherwise
299// it will return NIL.
300func (v *Value) Index(i int) *Value {
301 switch v.getResolvedValue().Kind() {
302 case reflect.Array, reflect.Slice:
303 if i >= v.Len() {
304 return AsValue(nil)
305 }
306 return AsValue(v.getResolvedValue().Index(i).Interface())
307 case reflect.String:
308 // return AsValue(v.getResolvedValue().Slice(i, i+1).Interface())
309 s := v.getResolvedValue().String()
310 runes := []rune(s)
311 if i < len(runes) {
312 return AsValue(string(runes[i]))
313 }
314 return AsValue("")
315 default:
316 logf("Value.Slice() not available for type: %s\n", v.getResolvedValue().Kind().String())
317 return AsValue([]int{})
318 }
319}
320
321// Contains checks whether the underlying value (which must be of type struct, map,
322// string, array or slice) contains of another Value (e. g. used to check

Callers 7

resolveMethod · 0.95
ContainsMethod · 0.80
IterateOrderMethod · 0.80
filterFirstFunction · 0.80
filterJoinFunction · 0.80
filterLastFunction · 0.80
filterRandomFunction · 0.80

Calls 6

getResolvedValueMethod · 0.95
LenMethod · 0.95
AsValueFunction · 0.85
logfFunction · 0.85
InterfaceMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected