package models import "slices" // bus model type Bus struct { Name string `json:"name"` Address []uint `json:"address,omitempty"` // address of bus Topic *Topic `json:"topic,omitempty"` // address of bus } func (b *Bus) AddAddress(address ...uint) { for _, a := range address { if slices.Contains(b.Address, a) { continue } b.Address = append(b.Address, a) slices.Sort(b.Address) } } func (b *Bus) AddSubscription(sub ...string) { if b.Topic == nil { b.Topic = &Topic{} } b.Topic.AddSubscription(sub...) } func (b *Bus) AddPublish(pub ...string) { if b.Topic == nil { b.Topic = &Topic{} } b.Topic.AddPublish(pub...) } func (b *Bus) RemoveAddress(address ...uint) { if len(b.Address) == 1 { b.Address = []uint{} return } for _, toDelete := range address { for i, a := range b.Address { if a == toDelete { b.Address = slices.Delete(b.Address, i, i+1) } } } } func (b *Bus) RemoveSubscription(sub ...string) { b.Topic.RemoveSubscription(sub...) } func (b *Bus) RemovePublish(pub ...string) { b.Topic.RemovePublish(pub...) }