From 9d6a352287ca46ea2e23fe320522084a33a88b2d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 20 Jan 2025 08:02:15 -0600 Subject: [PATCH] Notes added by 'git notes append' --- 37292a32459f5fab6b9068759df426fcd5fa8512 | 174 +++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/37292a32459f5fab6b9068759df426fcd5fa8512 b/37292a32459f5fab6b9068759df426fcd5fa8512 index a4c8428..501690f 100644 --- a/37292a32459f5fab6b9068759df426fcd5fa8512 +++ b/37292a32459f5fab6b9068759df426fcd5fa8512 @@ -414,3 +414,177 @@ func file_machine_proto_init() { } // `autogen:machine.sort.pb.go` + +package zoopb + +// This file was autogenerated with autogenpb v0.0.33 DO NOT EDIT +// go install go.wit.com/apps/autogenpb@latest +// +// You can use it on simple protobuf files +// The .proto file must have a singular and plural form of a message +// (for those of you that know ruby on rails, it's like that) +// +// You can mark which repos you want to auto generate sort.pb.go and marshal.pb.go files for +// +// For an example, +// go-clone go.wit.com/lib/protobuf/gitpb +// + +import ( + "fmt" + "sort" + "sync" +) + +type MachineIterator struct { + sync.RWMutex + + packs []*Machine + index int +} + +// NewMachineIterator initializes a new iterator. +func NewMachineIterator(packs []*Machine) *MachineIterator { + return &MachineIterator{packs: packs} +} + +// Scan moves to the next element and returns false if there are no more packs. +// Use Scan() in a loop, similar to a while loop +// +// for iterator.Scan() +// d := iterator.Next( +// fmt.Println("found UUID:", d.Uuid +// } +func (it *MachineIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *MachineIterator) Next() *Machine { + if it.packs[it.index-1] == nil { + for i, d := range it.packs { + fmt.Println("i =", i, d) + } + // fmt.Println("protobuf autogenpb sort error len =", len(it.packs)) + // fmt.Println("protobuf autogenpb sort error next == nil", it.index, it.index-1) + } + return it.packs[it.index-1] +} + +// does not enforce any unique fields +func (all *Machines) Append(newP *Machine) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.Machines = append(all.Machines, newP) + return true +} + +func (all *Machines) All() *MachineIterator { + machinePointers := all.selectAllMachine() + + iterator := NewMachineIterator(machinePointers) + return iterator +} + +func (all *Machines) Len() int { + all.Lock.RLock() + defer all.Lock.RUnlock() + + return len(all.Machines) +} + +func (all *Machines) SortByHostname() *MachineIterator { + packs := all.selectAllMachine() + + sort.Sort(MachineHostname(packs)) + + iterator := NewMachineIterator(packs) + return iterator +} + +type MachineHostname []*Machine + +func (a MachineHostname) Len() int { return len(a) } +func (a MachineHostname) Less(i, j int) bool { return a[i].Hostname < a[j].Hostname } +func (a MachineHostname) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces Hostname is unique +func (all *Machines) AppendUniqueHostname(newP *Machine) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Machines { + if p.Hostname == newP.Hostname { + return false + } + } + + all.Machines = append(all.Machines, newP) + return true +} + +func (all *Machines) DeleteByHostname(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Machines { + if all.Machines[i].Hostname == s { + all.Machines[i] = all.Machines[len(all.Machines)-1] + all.Machines = all.Machines[:len(all.Machines)-1] + return true + } + } + return false +} + +// enforces Hostname is unique +func (all *Machines) ReplaceHostname(newP *Machine) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Machines { + if p.Hostname == newP.Hostname { + return false + } + } + + all.Machines = append(all.Machines, newP) + return true +} + +// find a dependancy by the go path +func (all *Machines) FindByHostname(s string) *Machine { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Machines { + if all.Machines[i].Hostname == s { + return all.Machines[i] + } + } + return nil +} + +// safely returns a slice of pointers to the Machine protobufs +func (all *Machines) selectAllMachine() []*Machine { + all.Lock.RLock() + defer all.Lock.RUnlock() + + // Create a new slice to hold pointers to each Machine + var aStuff []*Machine + aStuff = make([]*Machine, len(all.Machines)) + for i, p := range all.Machines { + aStuff[i] = p // Copy pointers for safe iteration + } + + return aStuff +}