diff --git a/21070d370331239fb90f6c70295d6595591f73d2 b/21070d370331239fb90f6c70295d6595591f73d2 index b621741..570080e 100644 --- a/21070d370331239fb90f6c70295d6595591f73d2 +++ b/21070d370331239fb90f6c70295d6595591f73d2 @@ -979,3 +979,447 @@ func file_droplet_proto_init() { } // `autogen:droplet.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_10:22:09_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 virtbuf + +import ( + "fmt" + "sync" +) + +// a simple global lock +var dropletMu sync.RWMutex + +func (x *Droplets) fixUuid() { + if x == nil { + return + } + if x.Uuid == "d5d492e2-38d4-476b-86f3-f5abf01f9d6d" { + return + } + x.Uuid = "d5d492e2-38d4-476b-86f3-f5abf01f9d6d" + x.Version = "v0.0.1 go.wit.com/lib/protobuf/virtbuf" +} + +func NewDroplets() *Droplets { + x := new(Droplets) + x.Uuid = "d5d492e2-38d4-476b-86f3-f5abf01f9d6d" + x.Version = "v0.0.1 go.wit.com/lib/protobuf/virtbuf" + return x +} + +// START SORT + +// DEFINE THE Droplets ITERATOR. +// itializes a new iterator. +func newDropletsIterator(things []*Droplets) *DropletsIterator { + return &DropletsIterator{things: things} +} + +type DropletsIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Droplets + index int +} + +func (it *DropletsIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *DropletsIterator) Next() *Droplets { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in DropletsIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Droplet ITERATOR. +// itializes a new iterator. +func newDropletIterator(things []*Droplet) *DropletIterator { + return &DropletIterator{things: things} +} + +type DropletIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Droplet + index int +} + +func (it *DropletIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *DropletIterator) Next() *Droplet { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in DropletIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Current ITERATOR. +// itializes a new iterator. +func newCurrentIterator(things []*Current) *CurrentIterator { + return &CurrentIterator{things: things} +} + +type CurrentIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Current + index int +} + +func (it *CurrentIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *CurrentIterator) Next() *Current { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in CurrentIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Archive ITERATOR. +// itializes a new iterator. +func newArchiveIterator(things []*Archive) *ArchiveIterator { + return &ArchiveIterator{things: things} +} + +type ArchiveIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Archive + index int +} + +func (it *ArchiveIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *ArchiveIterator) Next() *Archive { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in ArchiveIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Network ITERATOR. +// itializes a new iterator. +func newNetworkIterator(things []*Network) *NetworkIterator { + return &NetworkIterator{things: things} +} + +type NetworkIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Network + index int +} + +func (it *NetworkIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *NetworkIterator) Next() *Network { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in NetworkIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE Disk ITERATOR. +// itializes a new iterator. +func newDiskIterator(things []*Disk) *DiskIterator { + return &DiskIterator{things: things} +} + +type DiskIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*Disk + index int +} + +func (it *DiskIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *DiskIterator) Next() *Disk { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in DiskIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *Droplets) allDroplets() []*Droplet { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*Droplet + tmp = make([]*Droplet, len(x.Droplets)) + for i, p := range x.Droplets { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *Droplet) allNetworks() []*Network { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*Network + tmp = make([]*Network, len(x.Networks)) + for i, p := range x.Networks { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *Droplet) allDisks() []*Disk { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*Disk + tmp = make([]*Disk, len(x.Disks)) + for i, p := range x.Disks { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the Droplet protobufs +func (x *Droplets) selectAllDroplets() []*Droplet { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each Droplet + var tmp []*Droplet + tmp = make([]*Droplet, len(x.Droplets)) + for i, p := range x.Droplets { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the Network protobufs +func (x *Droplet) selectAllNetworks() []*Network { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each Network + var tmp []*Network + tmp = make([]*Network, len(x.Networks)) + for i, p := range x.Networks { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the Disk protobufs +func (x *Droplet) selectAllDisks() []*Disk { + x.RLock() + defer x.RUnlock() + + // Create a new slice to hold pointers to each Disk + var tmp []*Disk + tmp = make([]*Disk, len(x.Disks)) + for i, p := range x.Disks { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// END SORT + +func (x *Droplets) Len() int { + x.RLock() + defer x.RUnlock() + + return len(x.Droplets) +} + +// just a simple Append() shortcut (but still uses the mutex lock) +func (x *Droplets) Append(y *Droplet) { + x.Lock() + defer x.Unlock() + + x.Droplets = append(x.Droplets, y) +} + +func (x *Droplets) All() *DropletIterator { + DropletPointers := x.selectAllDroplets() + + iterator := newDropletIterator(DropletPointers) + return iterator +} + +func (x *Droplets) Delete(y *Droplet) bool { + x.Lock() + defer x.Unlock() + + for i, _ := range x.Droplets { + if x.Droplets[i] == y { + x.Droplets[i] = x.Droplets[len(x.Droplets)-1] + x.Droplets = x.Droplets[:len(x.Droplets)-1] + return true + } + } + return false +} + +// lookup a Droplets by the Uuid +func (x *Droplets) FindByUuid(s string) *Droplet { + if x == nil { + return nil + } + + x.RLock() + defer x.RUnlock() + + for i, _ := range x.Droplets { + if x.Droplets[i].Uuid == s { + return x.Droplets[i] + } + } + return nil +} + +// lookup a Droplets by the Hostname +func (x *Droplets) FindByHostname(s string) *Droplet { + if x == nil { + return nil + } + + x.RLock() + defer x.RUnlock() + + for i, _ := range x.Droplets { + if x.Droplets[i].Hostname == s { + return x.Droplets[i] + } + } + return nil +} + +func (x *Droplets) DeleteByUuid(s string) bool { + x.Lock() + defer x.Unlock() + + for i, _ := range x.Droplets { + if x.Droplets[i].Uuid == s { + x.Droplets[i] = x.Droplets[len(x.Droplets)-1] + x.Droplets = x.Droplets[:len(x.Droplets)-1] + return true + } + } + return false +} + +func (x *Droplets) DeleteByHostname(s string) bool { + x.Lock() + defer x.Unlock() + + for i, _ := range x.Droplets { + if x.Droplets[i].Hostname == s { + x.Droplets[i] = x.Droplets[len(x.Droplets)-1] + x.Droplets = x.Droplets[:len(x.Droplets)-1] + return true + } + } + return false +} + +func (x *Droplets) AppendByUuid(y *Droplet) bool { + x.Lock() + defer x.Unlock() + + for _, p := range x.Droplets { + if p.Uuid == y.Uuid { + return false + } + } + + x.Droplets = append(x.Droplets, y) + return true +} + +func (x *Droplets) AppendByHostname(y *Droplet) bool { + x.Lock() + defer x.Unlock() + + for _, p := range x.Droplets { + if p.Hostname == y.Hostname { + return false + } + } + + x.Droplets = append(x.Droplets, y) + return true +}