// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. // This file was autogenerated with autogenpb v0.5.5-2-g6c9a3dd 2025.09.22_0851 // 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" "iter" "sort" "sync" "google.golang.org/protobuf/proto" ) // 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 SCANNER. // itializes a new scanner. func newMachinesScanner(things []*Machines) *MachinesScanner { return &MachinesScanner{things: things} } type MachinesScanner struct { sync.Mutex things []*Machines index int } func (it *MachinesScanner) Scan() bool { if it.index >= len(it.things) { return false } it.Lock() it.index++ it.Unlock() return true } // Next() returns the next thing in the array func (it *MachinesScanner) Next() *Machines { if it.things[it.index-1] == nil { fmt.Println("Next() error in MachinesScanner", it.index) } return it.things[it.index-1] } // END DEFINE THE SCANNER // DEFINE THE Machine SCANNER. // itializes a new scanner. func newMachineScanner(things []*Machine) *MachineScanner { return &MachineScanner{things: things} } type MachineScanner struct { sync.Mutex things []*Machine index int } func (it *MachineScanner) Scan() bool { if it.index >= len(it.things) { return false } it.Lock() it.index++ it.Unlock() return true } // Next() returns the next thing in the array func (it *MachineScanner) Next() *Machine { if it.things[it.index-1] == nil { fmt.Println("Next() error in MachineScanner", it.index) } return it.things[it.index-1] } // END DEFINE THE SCANNER // sort struct by Hostname type sortMachineHostname []*Machine func (a sortMachineHostname) Len() int { return len(a) } func (a sortMachineHostname) Less(i, j int) bool { return a[i].Hostname < a[j].Hostname } func (a sortMachineHostname) Swap(i, j int) { a[i], a[j] = a[j], a[i] } // 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 } func (x *Machines) SortByHostname() *MachineScanner { // copy the pointers as fast as possible. things := x.selectAllMachines() // todo: try slices.SortFunc() instead to see what happens sort.Sort(sortMachineHostname(things)) // slices.SortFunc(things, func(a, b *Machines) bool { // return a.Hostname < b.Hostname // Sort by ??. let the compiler work it out?? // }) return newMachineScanner(things) } // 'for x := range' syntax using the awesome golang 1.24 'iter' func (x *Machines) IterByHostname() iter.Seq[*Machine] { items := x.selectAllMachines() sort.Sort(sortMachineHostname(items)) // log.Println("Made Iter.Seq[] with length", len(items)) return func(yield func(*Machine) bool) { for _, v := range items { if !yield(v) { return } } } } // END SORT func (x *Machines) Len() int { x.RLock() defer x.RUnlock() return len(x.Machines) } // a Append() shortcut (that does Clone() with a mutex) notsure if it really works func (x *Machines) Append(y *Machine) *Machine { x.Lock() defer x.Unlock() z := proto.Clone(y).(*Machine) x.Machines = append(x.Machines, z) return z } func (x *Machines) All() *MachineScanner { MachinePointers := x.selectAllMachines() scanner := newMachineScanner(MachinePointers) return scanner } // Iterate 'for x := range' syntax using the awesome golang 1.24 'iter' func (x *Machines) IterAll() iter.Seq[*Machine] { items := x.selectAllMachines() // log.Println("Made All() Iter.Seq[] with length", len(items)) return func(yield func(*Machine) bool) { for _, v := range items { if !yield(v) { return } } } } 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 } // returns a Machine if Hostname matches, otherwise create func (x *Machines) InsertByHostname(y string) *Machine { x.Lock() defer x.Unlock() for _, z := range x.Machines { if z.Hostname == y { return z } } z := new(Machine) z.Hostname = y x.Machines = append(x.Machines, z) return z } 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, proto.Clone(y).(*Machine)) return true }