diff --git a/91602683263cf9bdde1faaad22ba46b4f006a562 b/91602683263cf9bdde1faaad22ba46b4f006a562 index ed553d1..891fcc9 100644 --- a/91602683263cf9bdde1faaad22ba46b4f006a562 +++ b/91602683263cf9bdde1faaad22ba46b4f006a562 @@ -961,3 +961,255 @@ func file_droplet_proto_init() { } // `autogen:droplet.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 DropletIterator struct { + sync.RWMutex + + packs []*Droplet + index int +} + +// NewDropletIterator initializes a new iterator. +func NewDropletIterator(packs []*Droplet) *DropletIterator { + return &DropletIterator{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 *DropletIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *DropletIterator) Next() *Droplet { + 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 *Droplets) Append(newP *Droplet) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.Droplets = append(all.Droplets, newP) + return true +} + +func (all *Droplets) All() *DropletIterator { + dropletPointers := all.selectAllDroplet() + + iterator := NewDropletIterator(dropletPointers) + return iterator +} + +func (all *Droplets) Len() int { + all.Lock.RLock() + defer all.Lock.RUnlock() + + return len(all.Droplets) +} + +func (all *Droplets) SortByUuid() *DropletIterator { + packs := all.selectAllDroplet() + + sort.Sort(DropletUuid(packs)) + + iterator := NewDropletIterator(packs) + return iterator +} + +type DropletUuid []*Droplet + +func (a DropletUuid) Len() int { return len(a) } +func (a DropletUuid) Less(i, j int) bool { return a[i].Uuid < a[j].Uuid } +func (a DropletUuid) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces Uuid is unique +func (all *Droplets) AppendUniqueUuid(newP *Droplet) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Droplets { + if p.Uuid == newP.Uuid { + return false + } + } + + all.Droplets = append(all.Droplets, newP) + return true +} + +func (all *Droplets) DeleteByUuid(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Droplets { + if all.Droplets[i].Uuid == s { + all.Droplets[i] = all.Droplets[len(all.Droplets)-1] + all.Droplets = all.Droplets[:len(all.Droplets)-1] + return true + } + } + return false +} + +// enforces Uuid is unique +func (all *Droplets) ReplaceUuid(newP *Droplet) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Droplets { + if p.Uuid == newP.Uuid { + return false + } + } + + all.Droplets = append(all.Droplets, newP) + return true +} + +// find a dependancy by the go path +func (all *Droplets) FindByUuid(s string) *Droplet { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Droplets { + if all.Droplets[i].Uuid == s { + return all.Droplets[i] + } + } + return nil +} + +func (all *Droplets) SortByHostname() *DropletIterator { + packs := all.selectAllDroplet() + + sort.Sort(DropletHostname(packs)) + + iterator := NewDropletIterator(packs) + return iterator +} + +type DropletHostname []*Droplet + +func (a DropletHostname) Len() int { return len(a) } +func (a DropletHostname) Less(i, j int) bool { return a[i].Hostname < a[j].Hostname } +func (a DropletHostname) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces Hostname is unique +func (all *Droplets) AppendUniqueHostname(newP *Droplet) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Droplets { + if p.Hostname == newP.Hostname { + return false + } + } + + all.Droplets = append(all.Droplets, newP) + return true +} + +func (all *Droplets) DeleteByHostname(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Droplets { + if all.Droplets[i].Hostname == s { + all.Droplets[i] = all.Droplets[len(all.Droplets)-1] + all.Droplets = all.Droplets[:len(all.Droplets)-1] + return true + } + } + return false +} + +// enforces Hostname is unique +func (all *Droplets) ReplaceHostname(newP *Droplet) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Droplets { + if p.Hostname == newP.Hostname { + return false + } + } + + all.Droplets = append(all.Droplets, newP) + return true +} + +// find a dependancy by the go path +func (all *Droplets) FindByHostname(s string) *Droplet { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Droplets { + if all.Droplets[i].Hostname == s { + return all.Droplets[i] + } + } + return nil +} + +// safely returns a slice of pointers to the Droplet protobufs +func (all *Droplets) selectAllDroplet() []*Droplet { + all.Lock.RLock() + defer all.Lock.RUnlock() + + // Create a new slice to hold pointers to each Droplet + var aStuff []*Droplet + aStuff = make([]*Droplet, len(all.Droplets)) + for i, p := range all.Droplets { + aStuff[i] = p // Copy pointers for safe iteration + } + + return aStuff +}