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

Method Contains

value.go:327–361  ·  view source on GitHub ↗

Contains checks whether the underlying value (which must be of type struct, map, string, array or slice) contains of another Value (e. g. used to check whether a struct contains of a specific field or a map contains a specific key). Example: AsValue("Hello, World!").Contains(AsValue("World")) == tr

(other *Value)

Source from the content-addressed store, hash-verified

325// Example:
326// AsValue("Hello, World!").Contains(AsValue("World")) == true
327func (v *Value) Contains(other *Value) bool {
328 switch v.getResolvedValue().Kind() {
329 case reflect.Struct:
330 fieldValue := v.getResolvedValue().FieldByName(other.String())
331 return fieldValue.IsValid()
332 case reflect.Map:
333 var mapValue reflect.Value
334 switch other.Interface().(type) {
335 case int:
336 mapValue = v.getResolvedValue().MapIndex(other.getResolvedValue())
337 case string:
338 mapValue = v.getResolvedValue().MapIndex(other.getResolvedValue())
339 default:
340 logf("Value.Contains() does not support lookup type '%s'\n", other.getResolvedValue().Kind().String())
341 return false
342 }
343
344 return mapValue.IsValid()
345 case reflect.String:
346 return strings.Contains(v.getResolvedValue().String(), other.String())
347
348 case reflect.Slice, reflect.Array:
349 for i := 0; i < v.getResolvedValue().Len(); i++ {
350 item := v.getResolvedValue().Index(i)
351 if other.EqualValueTo(AsValue(item.Interface())) {
352 return true
353 }
354 }
355 return false
356
357 default:
358 logf("Value.Contains() not available for type: %s\n", v.getResolvedValue().Kind().String())
359 return false
360 }
361}
362
363// CanSlice checks whether the underlying value is of type array, slice or string.
364// You normally would use CanSlice() before using the Slice() operation.

Callers 2

EvaluateMethod · 0.80
TestTemplatesFunction · 0.80

Calls 8

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

Tested by 1

TestTemplatesFunction · 0.64