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.
65 lines
1.1 KiB
65 lines
1.1 KiB
package device
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
type PointerMatcher interface {
|
|
Match(str string) bool
|
|
}
|
|
|
|
type Pointer struct {
|
|
ID string `json:"id"`
|
|
Aliases []string `json:"aliases"`
|
|
}
|
|
|
|
func (p *Pointer) AddAlias(alias string) {
|
|
if strings.HasPrefix(alias, "lucifer:name:") {
|
|
for i, alias2 := range p.Aliases {
|
|
if strings.HasPrefix(alias2, "lucifer:name:") {
|
|
p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, alias2 := range p.Aliases {
|
|
if alias2 == alias {
|
|
return
|
|
}
|
|
}
|
|
|
|
p.Aliases = append(p.Aliases, alias)
|
|
}
|
|
|
|
func (p *Pointer) RemoveAlias(alias string) {
|
|
for i, alias2 := range p.Aliases {
|
|
if alias2 == alias {
|
|
p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (p *Pointer) Name() string {
|
|
for _, alias := range p.Aliases {
|
|
if strings.HasPrefix(alias, "lucifer:name:") {
|
|
return alias
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
func (p *Pointer) Matches(matcher PointerMatcher) bool {
|
|
if matcher.Match(p.ID) {
|
|
return true
|
|
}
|
|
for _, alias := range p.Aliases {
|
|
if matcher.Match(alias) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|