diff --git a/812fd3eb5070a6acd068ebfafa9db914f5160ef5 b/812fd3eb5070a6acd068ebfafa9db914f5160ef5 index 32bb80a..b9e3b6a 100644 --- a/812fd3eb5070a6acd068ebfafa9db914f5160ef5 +++ b/812fd3eb5070a6acd068ebfafa9db914f5160ef5 @@ -424,3 +424,218 @@ func file_machine_proto_init() { } // `autogen:machine.sort.pb.go` + +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.50 2025-02-07_18:46:58_UTC +// go install go.wit.com/apps/autogenpb@latest +// +// define which structs (messages) you want to use in the .proto file +// Then sort.pb.go and marshal.pb.go files are autogenerated +// +// autogenpb uses it and has an example .proto file with instructions +// + +package zoopb + +import ( + "fmt" + "sync" +) + +// a simple global lock +var machineMu sync.RWMutex + +func (x *Machines) fixUuid() { + if x == nil { + return + } + if x.Uuid == "b57e7fac-a8fc-4949-9d50-fa38312dec87" { + return + } + x.Uuid = "b57e7fac-a8fc-4949-9d50-fa38312dec87" + x.Version = "v0.0.1 go.wit.com/lib/protobuf/zoopb" +} + +func NewMachines() *Machines { + x := new(Machines) + x.Uuid = "b57e7fac-a8fc-4949-9d50-fa38312dec87" + x.Version = "v0.0.1 go.wit.com/lib/protobuf/zoopb" + return x +} + +// START SORT + +// DEFINE THE Machines ITERATOR. +// itializes a new iterator. +func newMachinesIterator(things []*Machines) *MachinesIterator { + return &MachinesIterator{things: things} +} + +type MachinesIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Machines + index int +} + +func (it *MachinesIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *MachinesIterator) Next() *Machines { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in MachinesIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Machine ITERATOR. +// itializes a new iterator. +func newMachineIterator(things []*Machine) *MachineIterator { + return &MachineIterator{things: things} +} + +type MachineIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Machine + index int +} + +func (it *MachineIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *MachineIterator) Next() *Machine { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in MachineIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *Machines) allMachines() []*Machine { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*Machine + tmp = make([]*Machine, len(x.Machines)) + for i, p := range x.Machines { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the Machine protobufs +func (x *Machines) selectAllMachines() []*Machine { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each Machine + var tmp []*Machine + tmp = make([]*Machine, len(x.Machines)) + for i, p := range x.Machines { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// END SORT + +func (x *Machines) Len() int { + x.RLock() + defer x.RUnlock() + + return len(x.Machines) +} + +// just a simple Append() shortcut (but still uses the mutex lock) +func (x *Machines) Append(y *Machine) { + x.Lock() + defer x.Unlock() + + x.Machines = append(x.Machines, y) +} + +func (x *Machines) All() *MachineIterator { + MachinePointers := x.selectAllMachines() + + iterator := newMachineIterator(MachinePointers) + return iterator +} + +func (x *Machines) Delete(y *Machine) bool { + x.Lock() + defer x.Unlock() + + for i, _ := range x.Machines { + if x.Machines[i] == y { + x.Machines[i] = x.Machines[len(x.Machines)-1] + x.Machines = x.Machines[:len(x.Machines)-1] + return true + } + } + return false +} + +// lookup a Machines by the Hostname +func (x *Machines) FindByHostname(s string) *Machine { + if x == nil { + return nil + } + + x.RLock() + defer x.RUnlock() + + for i, _ := range x.Machines { + if x.Machines[i].Hostname == s { + return x.Machines[i] + } + } + return nil +} + +func (x *Machines) DeleteByHostname(s string) bool { + x.Lock() + defer x.Unlock() + + for i, _ := range x.Machines { + if x.Machines[i].Hostname == s { + x.Machines[i] = x.Machines[len(x.Machines)-1] + x.Machines = x.Machines[:len(x.Machines)-1] + return true + } + } + return false +} + +func (x *Machines) AppendByHostname(y *Machine) bool { + x.Lock() + defer x.Unlock() + + for _, p := range x.Machines { + if p.Hostname == y.Hostname { + return false + } + } + + x.Machines = append(x.Machines, y) + return true +}