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,38 +38,27 @@ 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)
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Driver) GetBus(name string) *Bus {
|
||||
|
@@ -7,14 +7,14 @@ func (d *Drivers) AddNewDriver(typ string) *Driver {
|
||||
if drv, ok := (*d)[typ]; ok {
|
||||
return drv
|
||||
}
|
||||
(*d)[typ] = &Driver{Type: typ}
|
||||
return (*d)[typ]
|
||||
drv := &Driver{Type: typ}
|
||||
(*d)[typ] = drv
|
||||
return drv
|
||||
}
|
||||
|
||||
func (d *Drivers) AddDriver(drv *Driver) {
|
||||
if driver, ok := (*d)[drv.Type]; ok {
|
||||
driver.AddBuses(drv.Buses)
|
||||
(*d)[drv.Type] = driver
|
||||
return
|
||||
}
|
||||
(*d)[drv.Type] = drv
|
||||
@@ -23,13 +23,14 @@ func (d *Drivers) AddDriver(drv *Driver) {
|
||||
func (d *Drivers) RemoveDriver(drv *Driver) {
|
||||
if driver, ok := (*d)[drv.Type]; ok {
|
||||
driver.RemoveBuses(drv.Buses)
|
||||
if len(driver.Buses) == 1 {
|
||||
if len(driver.Buses[0].Address) == 0 && driver.Buses[0].Topic == nil {
|
||||
(*d)[drv.Type] = &Driver{}
|
||||
return
|
||||
}
|
||||
d.CleanEmptyDrivers()
|
||||
}
|
||||
|
||||
func (d *Drivers) CleanEmptyDrivers() {
|
||||
for key, drv := range *d {
|
||||
if len(drv.Buses) == 0 {
|
||||
delete(*d, key)
|
||||
}
|
||||
(*d)[drv.Type] = driver
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ import (
|
||||
type Publish struct {
|
||||
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||
Path string `json:"path,omitempty"` // dbm path
|
||||
Type Type `json:"type,omitempty"` // dbm datatype
|
||||
Value any `json:"value,omitempty"` // dbm value
|
||||
|
Reference in New Issue
Block a user