package models import ( "slices" ) // topic model type Topic struct { Publish []string `json:"publish,omitempty"` Subscribe []string `json:"subscribe,omitempty"` } func (t *Topic) AddSubscription(subs ...string) { for _, sub := range subs { if slices.Contains(t.Subscribe, sub) { continue } t.Subscribe = append(t.Subscribe, sub) slices.Sort(t.Subscribe) } } func (t *Topic) AddPublish(pubs ...string) { for _, pub := range pubs { if slices.Contains(t.Publish, pub) { continue } t.Subscribe = append(t.Publish, pub) slices.Sort(t.Publish) } } func (t *Topic) RemoveSubscription(subs ...string) { if len(t.Subscribe) == 1 { t.Subscribe = []string{} return } for _, toDelete := range subs { for i, a := range t.Subscribe { if a == toDelete { t.Subscribe = slices.Delete(t.Subscribe, i, i+1) } } } } func (t *Topic) RemovePublish(pubs ...string) { if len(t.Publish) == 1 { t.Publish = []string{} return } for _, toDelete := range pubs { for i, a := range t.Publish { if a == toDelete { t.Publish = slices.Delete(t.Publish, i, i+1) } } } }