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

  1. package device
  2. import (
  3. "strings"
  4. )
  5. type PointerMatcher interface {
  6. Match(str string) bool
  7. }
  8. type Pointer struct {
  9. ID string `json:"id"`
  10. Aliases []string `json:"aliases"`
  11. }
  12. func (p *Pointer) AddAlias(alias string) {
  13. if strings.HasPrefix(alias, "lucifer:name:") {
  14. for i, alias2 := range p.Aliases {
  15. if strings.HasPrefix(alias2, "lucifer:name:") {
  16. p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
  17. break
  18. }
  19. }
  20. }
  21. for _, alias2 := range p.Aliases {
  22. if alias2 == alias {
  23. return
  24. }
  25. }
  26. p.Aliases = append(p.Aliases, alias)
  27. }
  28. func (p *Pointer) RemoveAlias(alias string) {
  29. for i, alias2 := range p.Aliases {
  30. if alias2 == alias {
  31. p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
  32. break
  33. }
  34. }
  35. }
  36. func (p *Pointer) Name() string {
  37. for _, alias := range p.Aliases {
  38. if strings.HasPrefix(alias, "lucifer:name:") {
  39. return alias
  40. }
  41. }
  42. return ""
  43. }
  44. func (p *Pointer) Matches(matcher PointerMatcher) bool {
  45. if matcher.Match(p.ID) {
  46. return true
  47. }
  48. for _, alias := range p.Aliases {
  49. if matcher.Match(alias) {
  50. return true
  51. }
  52. }
  53. return false
  54. }