2020-04-15 01:20:00 +02:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Condition string
|
|
|
|
|
|
|
|
func (c *Condition) evaluate(result *Result) bool {
|
|
|
|
condition := string(*c)
|
|
|
|
success := false
|
|
|
|
var resolvedCondition string
|
|
|
|
if strings.Contains(condition, "==") {
|
|
|
|
parts := sanitizeAndResolve(strings.Split(condition, "=="), result)
|
|
|
|
success = parts[0] == parts[1]
|
|
|
|
resolvedCondition = fmt.Sprintf("%v == %v", parts[0], parts[1])
|
|
|
|
} else if strings.Contains(condition, "!=") {
|
|
|
|
parts := sanitizeAndResolve(strings.Split(condition, "!="), result)
|
|
|
|
success = parts[0] != parts[1]
|
|
|
|
resolvedCondition = fmt.Sprintf("%v != %v", parts[0], parts[1])
|
|
|
|
} else if strings.Contains(condition, "<=") {
|
|
|
|
parts := sanitizeAndResolveNumerical(strings.Split(condition, "<="), result)
|
|
|
|
success = parts[0] <= parts[1]
|
|
|
|
resolvedCondition = fmt.Sprintf("%v <= %v", parts[0], parts[1])
|
|
|
|
} else if strings.Contains(condition, ">=") {
|
|
|
|
parts := sanitizeAndResolveNumerical(strings.Split(condition, ">="), result)
|
|
|
|
success = parts[0] >= parts[1]
|
|
|
|
resolvedCondition = fmt.Sprintf("%v >= %v", parts[0], parts[1])
|
|
|
|
} else if strings.Contains(condition, ">") {
|
|
|
|
parts := sanitizeAndResolveNumerical(strings.Split(condition, ">"), result)
|
|
|
|
success = parts[0] > parts[1]
|
|
|
|
resolvedCondition = fmt.Sprintf("%v > %v", parts[0], parts[1])
|
|
|
|
} else if strings.Contains(condition, "<") {
|
|
|
|
parts := sanitizeAndResolveNumerical(strings.Split(condition, "<"), result)
|
|
|
|
success = parts[0] < parts[1]
|
|
|
|
resolvedCondition = fmt.Sprintf("%v < %v", parts[0], parts[1])
|
|
|
|
} else {
|
|
|
|
result.Errors = append(result.Errors, fmt.Sprintf("invalid condition '%s' has been provided", condition))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
conditionToDisplay := condition
|
2020-08-13 03:42:13 +02:00
|
|
|
// If the condition isn't a success, return what the resolved condition was too
|
2020-04-15 01:20:00 +02:00
|
|
|
if !success {
|
|
|
|
log.Printf("[Condition][evaluate] Condition '%s' did not succeed because '%s' is false", condition, resolvedCondition)
|
2020-08-13 03:42:13 +02:00
|
|
|
conditionToDisplay = fmt.Sprintf("%s (%s)", condition, resolvedCondition)
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
|
|
|
result.ConditionResults = append(result.ConditionResults, &ConditionResult{Condition: conditionToDisplay, Success: success})
|
|
|
|
return success
|
|
|
|
}
|