bugpb/bug.sort.pb.go

187 lines
3.6 KiB
Go

// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
// This file was autogenerated with autogenpb v0.0.69-2-g626b737 2025-05-23_19:46:37_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 bugpb
import (
"fmt"
"iter"
"sync"
)
// a simple global lock
var bugMu sync.RWMutex
func (x *Bugs) fixUuid() {
if x == nil {
return
}
if x.Uuid == "cdf6711e-f948-4376-9e72-cf1912a96cee" {
return
}
x.Uuid = "cdf6711e-f948-4376-9e72-cf1912a96cee"
x.Version = "v1 go.wit.com/lib/protobuf/bugpb"
}
func NewBugs() *Bugs {
x := new(Bugs)
x.Uuid = "cdf6711e-f948-4376-9e72-cf1912a96cee"
x.Version = "v1 go.wit.com/lib/protobuf/bugpb"
return x
}
// START SORT
// DEFINE THE Bugs SCANNER.
// itializes a new scanner.
func newBugsScanner(things []*Bugs) *BugsScanner {
return &BugsScanner{things: things}
}
type BugsScanner struct {
sync.Mutex
things []*Bugs
index int
}
func (it *BugsScanner) 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 *BugsScanner) Next() *Bugs {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in BugsScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// DEFINE THE Bug SCANNER.
// itializes a new scanner.
func newBugScanner(things []*Bug) *BugScanner {
return &BugScanner{things: things}
}
type BugScanner struct {
sync.Mutex
things []*Bug
index int
}
func (it *BugScanner) 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 *BugScanner) Next() *Bug {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in BugScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// safely returns a slice of pointers to the FRUIT protobufs
func (x *Bugs) allBugs() []*Bug {
x.RLock()
defer x.RUnlock()
// Create a new slice to hold pointers to each FRUIT
var tmp []*Bug
tmp = make([]*Bug, len(x.Bugs))
for i, p := range x.Bugs {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// safely returns a slice of pointers to the Bug protobufs
func (x *Bugs) selectAllBugs() []*Bug {
x.RLock()
defer x.RUnlock()
// Create a new slice to hold pointers to each Bug
var tmp []*Bug
tmp = make([]*Bug, len(x.Bugs))
for i, p := range x.Bugs {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// END SORT
func (x *Bugs) Len() int {
x.RLock()
defer x.RUnlock()
return len(x.Bugs)
}
// just a simple Append() shortcut (but still uses the mutex lock)
func (x *Bugs) Append(y *Bug) {
x.Lock()
defer x.Unlock()
x.Bugs = append(x.Bugs, y)
}
func (x *Bugs) All() *BugScanner {
BugPointers := x.selectAllBugs()
scanner := newBugScanner(BugPointers)
return scanner
}
// Iterate 'for x := range' syntax using the awesome golang 1.24 'iter'
func (x *Bugs) IterAll() iter.Seq[*Bug] {
items := x.selectAllBugs()
// log.Println("Made All() Iter.Seq[] with length", len(items))
return func(yield func(*Bug) bool) {
for _, v := range items {
if !yield(v) {
return
}
}
}
}
func (x *Bugs) Delete(y *Bug) bool {
x.Lock()
defer x.Unlock()
for i, _ := range x.Bugs {
if x.Bugs[i] == y {
x.Bugs[i] = x.Bugs[len(x.Bugs)-1]
x.Bugs = x.Bugs[:len(x.Bugs)-1]
return true
}
}
return false
}