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

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[:0:0], p.Aliases...)
  26. p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
  27. removed = gentools.ShallowCopy(&alias2)
  28. break
  29. }
  30. }
  31. }
  32. added = gentools.ShallowCopy(&alias)
  33. p.Aliases = append(p.Aliases, alias)
  34. return
  35. }
  36. func (p *Pointer) RemoveAlias(alias string) bool {
  37. if strings.HasPrefix(alias, "lucifer:name:") {
  38. // Name cannot be removed like this.
  39. return false
  40. }
  41. for i, alias2 := range p.Aliases {
  42. if alias2 == alias {
  43. p.Aliases = append(p.Aliases[:0:0], p.Aliases...)
  44. p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. func (p *Pointer) Name() string {
  51. for _, alias := range p.Aliases {
  52. if strings.HasPrefix(alias, "lucifer:name:") {
  53. return alias
  54. }
  55. }
  56. return ""
  57. }
  58. func (p *Pointer) Matches(matcher PointerMatcher) bool {
  59. if matcher.Match(p.ID) {
  60. return true
  61. }
  62. for _, alias := range p.Aliases {
  63. if matcher.Match(alias) {
  64. return true
  65. }
  66. }
  67. return false
  68. }