You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
56 lines
1.2 KiB
package script
|
|
|
|
import (
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Condition struct {
|
|
Scope string `json:"scope"`
|
|
Key string `json:"key"`
|
|
Op string `json:"op"`
|
|
Value string `json:"value,omitempty"`
|
|
Not bool `json:"not,omitempty"`
|
|
}
|
|
|
|
func (c *Condition) Check(set VariableSet, match string, devices []string) bool {
|
|
var value string
|
|
switch c.Scope {
|
|
case "global":
|
|
value = set.Global(c.Key)
|
|
case "match":
|
|
value = set.Match(match, c.Key)
|
|
case "devices":
|
|
value = set.Devices(devices, c.Key)
|
|
default:
|
|
return c.Not
|
|
}
|
|
|
|
valueInt, valueIntErr := strconv.ParseInt(value, 10, 64)
|
|
targetInt, _ := strconv.ParseInt(c.Value, 10, 64)
|
|
|
|
switch c.Op {
|
|
case "eq":
|
|
return !c.Not == (value == c.Value)
|
|
case "neq":
|
|
return !c.Not == (value != c.Value)
|
|
case "gt":
|
|
return !c.Not == (valueInt > targetInt)
|
|
case "lt":
|
|
return !c.Not == (valueInt < targetInt)
|
|
case "gte":
|
|
log.Println(c.Not, valueInt, targetInt)
|
|
return !c.Not == (valueInt >= targetInt)
|
|
case "lte":
|
|
return !c.Not == (valueInt <= targetInt)
|
|
case "exists":
|
|
return !c.Not == (value != "")
|
|
case "contains":
|
|
return !c.Not == strings.Contains(value, c.Value)
|
|
case "numerical":
|
|
return !c.Not == (valueIntErr == nil)
|
|
default:
|
|
return c.Not
|
|
}
|
|
}
|