fix(condition): Partially support numbers with floating point (#434)
* docs: Don't include Pushover in alerting provider examples * fix(condition): Partially support numbers with floating point Fixes #433 Does not add support for decimal numbers, but it converts float64 to int64. The reason why I'm not just using float64 instead of int64 is because float64 does not support all the numbers that int64 supports, which means this would be a breaking change. Instead, this change at least supports the non-decimal part of floating point numbers. This is an improvement over the current implementation, as right now, numbers with decimals are just converted to 0 when compared using a non-equal operator
This commit is contained in:
@ -302,10 +302,18 @@ func sanitizeAndResolveNumerical(list []string, result *Result) (parameters []st
|
||||
parameters, resolvedParameters := sanitizeAndResolve(list, result)
|
||||
for _, element := range resolvedParameters {
|
||||
if duration, err := time.ParseDuration(element); duration != 0 && err == nil {
|
||||
// If the string is a duration, convert it to milliseconds
|
||||
resolvedNumericalParameters = append(resolvedNumericalParameters, duration.Milliseconds())
|
||||
} else if number, err := strconv.ParseInt(element, 10, 64); err != nil {
|
||||
// Default to 0 if the string couldn't be converted to an integer
|
||||
resolvedNumericalParameters = append(resolvedNumericalParameters, 0)
|
||||
// It's not an int, so we'll check if it's a float
|
||||
if f, err := strconv.ParseFloat(element, 64); err == nil {
|
||||
// It's a float, but we'll convert it to an int. We're losing precision here, but it's better than
|
||||
// just returning 0.
|
||||
resolvedNumericalParameters = append(resolvedNumericalParameters, int64(f))
|
||||
} else {
|
||||
// Default to 0 if the string couldn't be converted to an integer or a float
|
||||
resolvedNumericalParameters = append(resolvedNumericalParameters, 0)
|
||||
}
|
||||
} else {
|
||||
resolvedNumericalParameters = append(resolvedNumericalParameters, number)
|
||||
}
|
||||
|
@ -294,6 +294,13 @@ func TestCondition_evaluate(t *testing.T) {
|
||||
ExpectedSuccess: false,
|
||||
ExpectedOutput: "[BODY].data.id (1) > 5",
|
||||
},
|
||||
{
|
||||
Name: "body-jsonpath-float-using-greater-than-issue433", // As of v5.3.1, Gatus will convert a float to an int. We're losing precision, but it's better than just returning 0
|
||||
Condition: Condition("[BODY].balance > 100"),
|
||||
Result: &Result{body: []byte(`{"balance": "123.40000000000005"}`)},
|
||||
ExpectedSuccess: true,
|
||||
ExpectedOutput: "[BODY].balance > 100",
|
||||
},
|
||||
{
|
||||
Name: "body-jsonpath-complex-int-using-less-than",
|
||||
Condition: Condition("[BODY].data.id < 5"),
|
||||
|
Reference in New Issue
Block a user