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.
82 lines
1.5 KiB
82 lines
1.5 KiB
package device
|
|
|
|
import (
|
|
"git.aiterp.net/lucifer3/server/internal/gentools"
|
|
"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) (added *string, removed *string) {
|
|
if !strings.HasPrefix(alias, "lucifer:") {
|
|
return
|
|
}
|
|
|
|
for _, alias2 := range p.Aliases {
|
|
if alias2 == alias {
|
|
return
|
|
}
|
|
}
|
|
|
|
if strings.HasPrefix(alias, "lucifer:name:") {
|
|
for i, alias2 := range p.Aliases {
|
|
if strings.HasPrefix(alias2, "lucifer:name:") {
|
|
p.Aliases = append(p.Aliases[:0:0], p.Aliases...)
|
|
p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
|
|
removed = gentools.ShallowCopy(&alias2)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
added = gentools.ShallowCopy(&alias)
|
|
p.Aliases = append(p.Aliases, alias)
|
|
return
|
|
}
|
|
|
|
func (p *Pointer) RemoveAlias(alias string) bool {
|
|
if strings.HasPrefix(alias, "lucifer:name:") {
|
|
// Name cannot be removed like this.
|
|
return false
|
|
}
|
|
|
|
for i, alias2 := range p.Aliases {
|
|
if alias2 == alias {
|
|
p.Aliases = append(p.Aliases[:0:0], p.Aliases...)
|
|
p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
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
|
|
}
|