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

Method Negate

value.go:243–266  ·  view source on GitHub ↗

Negate tries to negate the underlying value. It's mainly used for the NOT-operator and in conjunction with a call to return_value.IsTrue() afterwards. Example: AsValue(1).Negate().IsTrue() == false

()

Source from the content-addressed store, hash-verified

241// Example:
242// AsValue(1).Negate().IsTrue() == false
243func (v *Value) Negate() *Value {
244 switch v.getResolvedValue().Kind() {
245 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
246 reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
247 if v.Integer() != 0 {
248 return AsValue(0)
249 }
250 return AsValue(1)
251 case reflect.Float32, reflect.Float64:
252 if v.Float() != 0.0 {
253 return AsValue(float64(0.0))
254 }
255 return AsValue(float64(1.1))
256 case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
257 return AsValue(v.getResolvedValue().Len() == 0)
258 case reflect.Bool:
259 return AsValue(!v.getResolvedValue().Bool())
260 case reflect.Struct:
261 return AsValue(false)
262 default:
263 logf("Value.IsTrue() not available for type: %s\n", v.getResolvedValue().Kind().String())
264 return AsValue(true)
265 }
266}
267
268// Len returns the length for an array, chan, map, slice or string.
269// Otherwise it will return 0.

Callers 1

EvaluateMethod · 0.80

Calls 8

getResolvedValueMethod · 0.95
IntegerMethod · 0.95
FloatMethod · 0.95
AsValueFunction · 0.85
logfFunction · 0.85
BoolMethod · 0.80
LenMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected