From 88e0a8ee730ce8786d2700a86d7cdc364d83e528 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 20 Jan 2025 08:21:38 -0600 Subject: [PATCH] Notes added by 'git notes append' --- 91602683263cf9bdde1faaad22ba46b4f006a562 | 252 +++++++++++++++++++++++ 1 file changed, 252 insertions(+) diff --git a/91602683263cf9bdde1faaad22ba46b4f006a562 b/91602683263cf9bdde1faaad22ba46b4f006a562 index c614915..691ec98 100644 --- a/91602683263cf9bdde1faaad22ba46b4f006a562 +++ b/91602683263cf9bdde1faaad22ba46b4f006a562 @@ -2328,3 +2328,255 @@ func file_hypervisor_proto_init() { } // `autogen:hypervisor.sort.pb.go` + +package virtbuf + +// This file was autogenerated with autogenpb. +// 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" + "os" + "sort" + "sync" +) + +type HypervisorIterator struct { + sync.RWMutex + + packs []*Hypervisor + index int +} + +// NewHypervisorIterator initializes a new iterator. +func NewHypervisorIterator(packs []*Hypervisor) *HypervisorIterator { + return &HypervisorIterator{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 *HypervisorIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *HypervisorIterator) Next() *Hypervisor { + 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) + os.Exit(-1) + } + return it.packs[it.index-1] +} + +// does not enforce any unique fields +func (all *Hypervisors) Append(newP *Hypervisor) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.Hypervisors = append(all.Hypervisors, newP) + return true +} + +func (all *Hypervisors) All() *HypervisorIterator { + hypervisorPointers := all.selectAllHypervisor() + + iterator := NewHypervisorIterator(hypervisorPointers) + return iterator +} + +func (all *Hypervisors) Len() int { + all.Lock.RLock() + defer all.Lock.RUnlock() + + return len(all.Hypervisors) +} + +func (all *Hypervisors) SortByUuid() *HypervisorIterator { + packs := all.selectAllHypervisor() + + sort.Sort(HypervisorUuid(packs)) + + iterator := NewHypervisorIterator(packs) + return iterator +} + +type HypervisorUuid []*Hypervisor + +func (a HypervisorUuid) Len() int { return len(a) } +func (a HypervisorUuid) Less(i, j int) bool { return a[i].Uuid < a[j].Uuid } +func (a HypervisorUuid) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces Uuid is unique +func (all *Hypervisors) AppendUniqueUuid(newP *Hypervisor) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Hypervisors { + if p.Uuid == newP.Uuid { + return false + } + } + + all.Hypervisors = append(all.Hypervisors, newP) + return true +} + +func (all *Hypervisors) DeleteByUuid(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Hypervisors { + if all.Hypervisors[i].Uuid == s { + all.Hypervisors[i] = all.Hypervisors[len(all.Hypervisors)-1] + all.Hypervisors = all.Hypervisors[:len(all.Hypervisors)-1] + return true + } + } + return false +} + +// enforces Uuid is unique +func (all *Hypervisors) ReplaceUuid(newP *Hypervisor) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Hypervisors { + if p.Uuid == newP.Uuid { + return false + } + } + + all.Hypervisors = append(all.Hypervisors, newP) + return true +} + +// find a dependancy by the go path +func (all *Hypervisors) FindByUuid(s string) *Hypervisor { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Hypervisors { + if all.Hypervisors[i].Uuid == s { + return all.Hypervisors[i] + } + } + return nil +} + +func (all *Hypervisors) SortByHostname() *HypervisorIterator { + packs := all.selectAllHypervisor() + + sort.Sort(HypervisorHostname(packs)) + + iterator := NewHypervisorIterator(packs) + return iterator +} + +type HypervisorHostname []*Hypervisor + +func (a HypervisorHostname) Len() int { return len(a) } +func (a HypervisorHostname) Less(i, j int) bool { return a[i].Hostname < a[j].Hostname } +func (a HypervisorHostname) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces Hostname is unique +func (all *Hypervisors) AppendUniqueHostname(newP *Hypervisor) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Hypervisors { + if p.Hostname == newP.Hostname { + return false + } + } + + all.Hypervisors = append(all.Hypervisors, newP) + return true +} + +func (all *Hypervisors) DeleteByHostname(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Hypervisors { + if all.Hypervisors[i].Hostname == s { + all.Hypervisors[i] = all.Hypervisors[len(all.Hypervisors)-1] + all.Hypervisors = all.Hypervisors[:len(all.Hypervisors)-1] + return true + } + } + return false +} + +// enforces Hostname is unique +func (all *Hypervisors) ReplaceHostname(newP *Hypervisor) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Hypervisors { + if p.Hostname == newP.Hostname { + return false + } + } + + all.Hypervisors = append(all.Hypervisors, newP) + return true +} + +// find a dependancy by the go path +func (all *Hypervisors) FindByHostname(s string) *Hypervisor { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Hypervisors { + if all.Hypervisors[i].Hostname == s { + return all.Hypervisors[i] + } + } + return nil +} + +// safely returns a slice of pointers to the Hypervisor protobufs +func (all *Hypervisors) selectAllHypervisor() []*Hypervisor { + all.Lock.RLock() + defer all.Lock.RUnlock() + + // Create a new slice to hold pointers to each Hypervisor + var aStuff []*Hypervisor + aStuff = make([]*Hypervisor, len(all.Hypervisors)) + for i, p := range all.Hypervisors { + aStuff[i] = p // Copy pointers for safe iteration + } + + return aStuff +}