From a39dcc06f4c3dbf71fe12a75810bc2d17d1fcb38 Mon Sep 17 00:00:00 2001 From: Adrian Zuercher Date: Wed, 30 Jul 2025 11:55:30 +0200 Subject: [PATCH] add remove driver function --- models/bus.go | 24 ++++++++++++++++++++++-- models/driver.go | 34 ++++++++++++++++++++++++++++++++++ models/topic.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/models/bus.go b/models/bus.go index b162776..9c4cb26 100644 --- a/models/bus.go +++ b/models/bus.go @@ -16,9 +16,7 @@ func (b *Bus) AddAddress(address ...uint) { } b.Address = append(b.Address, a) slices.Sort(b.Address) - } - } func (b *Bus) AddSubscription(sub ...string) { @@ -34,3 +32,25 @@ func (b *Bus) AddPublish(pub ...string) { } 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...) +} diff --git a/models/driver.go b/models/driver.go index 3fb7168..89034c0 100644 --- a/models/driver.go +++ b/models/driver.go @@ -1,5 +1,7 @@ package models +import "slices" + // driver model type Driver struct { Type string `json:"type"` // driver type name of driver in collection @@ -36,6 +38,38 @@ next: } } +func (d *Driver) RemoveBus(bus *Bus) { + for _, currentBus := range d.Buses { + if currentBus.Name != bus.Name { + continue + } + + currentBus.RemoveAddress(bus.Address...) + if bus.Topic != nil { + currentBus.RemoveSubscription(bus.Topic.Subscribe...) + currentBus.RemovePublish(bus.Topic.Publish...) + if len(currentBus.Address) == 0 { + var remove bool = true + if currentBus.Topic != nil { + remove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0 + } + if remove { + if len(d.Buses) == 1 { + d.Buses = []*Bus{} + return + } + for i, b := range d.Buses { + if bus.Name == b.Name { + d.Buses = slices.Delete(d.Buses, i, i+1) + } + } + } + } + break + } + } +} + func (d *Driver) GetBus(name string) *Bus { for _, b := range d.Buses { if b.Name == name { diff --git a/models/topic.go b/models/topic.go index 4ec1488..0025186 100644 --- a/models/topic.go +++ b/models/topic.go @@ -27,3 +27,31 @@ func (t *Topic) AddPublish(pubs ...string) { 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) + } + } + } +}