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.

80 lines
1.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package device
  2. import (
  3. "git.aiterp.net/lucifer3/server/internal/gentools"
  4. "strings"
  5. )
  6. type PointerMatcher interface {
  7. Match(str string) bool
  8. }
  9. type Pointer struct {
  10. ID string `json:"id"`
  11. Aliases []string `json:"aliases"`
  12. }
  13. func (p *Pointer) AddAlias(alias string) (added *string, removed *string) {
  14. if !strings.HasPrefix(alias, "lucifer:") {
  15. return
  16. }
  17. for _, alias2 := range p.Aliases {
  18. if alias2 == alias {
  19. return
  20. }
  21. }
  22. if strings.HasPrefix(alias, "lucifer:name:") {
  23. for i, alias2 := range p.Aliases {
  24. if strings.HasPrefix(alias2, "lucifer:name:") {
  25. p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
  26. removed = gentools.ShallowCopy(&alias2)
  27. break
  28. }
  29. }
  30. }
  31. added = gentools.ShallowCopy(&alias)
  32. p.Aliases = append(p.Aliases, alias)
  33. return
  34. }
  35. func (p *Pointer) RemoveAlias(alias string) bool {
  36. if strings.HasPrefix(alias, "lucifer:name:") {
  37. // Name cannot be removed like this.
  38. return false
  39. }
  40. for i, alias2 := range p.Aliases {
  41. if alias2 == alias {
  42. p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. func (p *Pointer) Name() string {
  49. for _, alias := range p.Aliases {
  50. if strings.HasPrefix(alias, "lucifer:name:") {
  51. return alias
  52. }
  53. }
  54. return ""
  55. }
  56. func (p *Pointer) Matches(matcher PointerMatcher) bool {
  57. if matcher.Match(p.ID) {
  58. return true
  59. }
  60. for _, alias := range p.Aliases {
  61. if matcher.Match(alias) {
  62. return true
  63. }
  64. }
  65. return false
  66. }