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.
|
|
package models
import ( "strconv" "strings" )
type ReferenceKind string
var ( RKDeviceID ReferenceKind = "DeviceID" RKBridgeID ReferenceKind = "BridgeID" RKTag ReferenceKind = "Tag" RKAll ReferenceKind = "All" RKName ReferenceKind = "Name" )
func (rk ReferenceKind) Matches(device *Device, value string) bool { switch rk { case RKName: if strings.HasPrefix(value, "*") { return strings.HasSuffix(device.Name, value[1:]) } else if strings.HasSuffix(value, "*") { return strings.HasPrefix(device.Name, value[:len(value)-1]) } else { return device.Name == value } case RKDeviceID: idStr := strconv.Itoa(device.ID) for _, idStr2 := range strings.Split(value, ",") { if idStr == idStr2 { return true } } case RKBridgeID: idStr := strconv.Itoa(device.BridgeID) for _, idStr2 := range strings.Split(value, ",") { if idStr == idStr2 { return true } } case RKTag: hadAny := false for _, tag := range strings.Split(value, ",") { if strings.HasPrefix(tag, "-") { if device.HasTag(tag[1:]) { return false } } else if strings.HasPrefix(tag, "+") { if !device.HasTag(tag[1:]) { return false } } else { if device.HasTag(tag) { hadAny = true } } }
return hadAny case RKAll: return true }
return false }
|