Simplify EvalBoolCondition

This commit is contained in:
Holger Obermaier 2022-02-15 13:35:24 +01:00
parent a8821b7ac5
commit 2f0e229936

View File

@ -283,8 +283,10 @@ func (c *metricAggregator) AddFunction(name string, function func(args ...interf
} }
func EvalBoolCondition(condition string, params map[string]interface{}) (bool, error) { func EvalBoolCondition(condition string, params map[string]interface{}) (bool, error) {
newcond := strings.ReplaceAll(condition, "'", "\"") newcond :=
newcond = strings.ReplaceAll(newcond, "%", "\\") strings.ReplaceAll(
strings.ReplaceAll(
condition, "'", "\""), "%", "\\")
language := gval.NewLanguage( language := gval.NewLanguage(
gval.Full(), gval.Full(),
metricCacheLanguage, metricCacheLanguage,
@ -293,31 +295,21 @@ func EvalBoolCondition(condition string, params map[string]interface{}) (bool, e
if err != nil { if err != nil {
return false, err return false, err
} }
var endResult bool = false endResult := false
err = nil err = nil
switch r := value.(type) { switch r := value.(type) {
case bool: case bool:
endResult = r endResult = r
case float64: case float64:
if r != 0.0 { endResult = r != 0.0
endResult = true
}
case float32: case float32:
if r != 0.0 { endResult = r != 0.0
endResult = true
}
case int: case int:
if r != 0 { endResult = r != 0
endResult = true
}
case int64: case int64:
if r != 0 { endResult = r != 0
endResult = true
}
case int32: case int32:
if r != 0 { endResult = r != 0
endResult = true
}
default: default:
err = fmt.Errorf("cannot evaluate '%s' to bool", newcond) err = fmt.Errorf("cannot evaluate '%s' to bool", newcond)
} }