bug fix and improvment remove driver
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"slices"
|
||||
)
|
||||
|
||||
// driver model
|
||||
type Driver struct {
|
||||
Type string `json:"type"` // driver type name of driver in collection
|
||||
@@ -42,36 +38,25 @@ next:
|
||||
|
||||
func (d *Driver) RemoveBuses(buses []*Bus) {
|
||||
for _, bus := range buses {
|
||||
for _, currentBus := range d.Buses {
|
||||
for i := 0; i < len(d.Buses); i++ {
|
||||
currentBus := d.Buses[i]
|
||||
if currentBus.Name != bus.Name {
|
||||
continue
|
||||
}
|
||||
|
||||
currentBus.RemoveAddress(bus.Address...)
|
||||
|
||||
shouldRemove := true
|
||||
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
|
||||
shouldRemove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
|
||||
}
|
||||
if len(currentBus.Address) == 0 && shouldRemove {
|
||||
d.Buses = append(d.Buses[:i], d.Buses[i+1:]...)
|
||||
i-- // adjust index after removal
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user