bug fix and improvment remove driver
This commit is contained in:
@@ -1,9 +1,5 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
|
||||||
"slices"
|
|
||||||
)
|
|
||||||
|
|
||||||
// driver model
|
// driver model
|
||||||
type Driver struct {
|
type Driver struct {
|
||||||
Type string `json:"type"` // driver type name of driver in collection
|
Type string `json:"type"` // driver type name of driver in collection
|
||||||
@@ -42,36 +38,25 @@ next:
|
|||||||
|
|
||||||
func (d *Driver) RemoveBuses(buses []*Bus) {
|
func (d *Driver) RemoveBuses(buses []*Bus) {
|
||||||
for _, bus := range buses {
|
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 {
|
if currentBus.Name != bus.Name {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
currentBus.RemoveAddress(bus.Address...)
|
currentBus.RemoveAddress(bus.Address...)
|
||||||
|
|
||||||
|
shouldRemove := true
|
||||||
if bus.Topic != nil {
|
if bus.Topic != nil {
|
||||||
currentBus.RemoveSubscription(bus.Topic.Subscribe...)
|
currentBus.RemoveSubscription(bus.Topic.Subscribe...)
|
||||||
currentBus.RemovePublish(bus.Topic.Publish...)
|
currentBus.RemovePublish(bus.Topic.Publish...)
|
||||||
|
shouldRemove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
if len(currentBus.Address) == 0 && shouldRemove {
|
||||||
|
d.Buses = append(d.Buses[:i], d.Buses[i+1:]...)
|
||||||
|
i-- // adjust index after removal
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,14 +7,14 @@ func (d *Drivers) AddNewDriver(typ string) *Driver {
|
|||||||
if drv, ok := (*d)[typ]; ok {
|
if drv, ok := (*d)[typ]; ok {
|
||||||
return drv
|
return drv
|
||||||
}
|
}
|
||||||
(*d)[typ] = &Driver{Type: typ}
|
drv := &Driver{Type: typ}
|
||||||
return (*d)[typ]
|
(*d)[typ] = drv
|
||||||
|
return drv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Drivers) AddDriver(drv *Driver) {
|
func (d *Drivers) AddDriver(drv *Driver) {
|
||||||
if driver, ok := (*d)[drv.Type]; ok {
|
if driver, ok := (*d)[drv.Type]; ok {
|
||||||
driver.AddBuses(drv.Buses)
|
driver.AddBuses(drv.Buses)
|
||||||
(*d)[drv.Type] = driver
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
(*d)[drv.Type] = drv
|
(*d)[drv.Type] = drv
|
||||||
@@ -23,13 +23,14 @@ func (d *Drivers) AddDriver(drv *Driver) {
|
|||||||
func (d *Drivers) RemoveDriver(drv *Driver) {
|
func (d *Drivers) RemoveDriver(drv *Driver) {
|
||||||
if driver, ok := (*d)[drv.Type]; ok {
|
if driver, ok := (*d)[drv.Type]; ok {
|
||||||
driver.RemoveBuses(drv.Buses)
|
driver.RemoveBuses(drv.Buses)
|
||||||
if len(driver.Buses) == 1 {
|
}
|
||||||
if len(driver.Buses[0].Address) == 0 && driver.Buses[0].Topic == nil {
|
d.CleanEmptyDrivers()
|
||||||
(*d)[drv.Type] = &Driver{}
|
}
|
||||||
return
|
|
||||||
}
|
func (d *Drivers) CleanEmptyDrivers() {
|
||||||
|
for key, drv := range *d {
|
||||||
|
if len(drv.Buses) == 0 {
|
||||||
|
delete(*d, key)
|
||||||
}
|
}
|
||||||
(*d)[drv.Type] = driver
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,11 +9,12 @@ import (
|
|||||||
|
|
||||||
// publish model
|
// publish model
|
||||||
type Publish struct {
|
type Publish struct {
|
||||||
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||||
Path string `json:"path,omitempty"` // dbm path
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Type Type `json:"type,omitempty"` // dbm datatype
|
Path string `json:"path,omitempty"` // dbm path
|
||||||
Value any `json:"value,omitempty"` // dbm value
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns input parameter as assigned dbm datatype
|
// returns input parameter as assigned dbm datatype
|
||||||
|
Reference in New Issue
Block a user