forgepb/set.sort.pb.go

192 lines
3.7 KiB
Go

// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
// This file was autogenerated with autogenpb v0.5.6-2-gfabf425 2025-09-23_15:21:15_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 forgepb
import (
"fmt"
"iter"
"sync"
"google.golang.org/protobuf/proto"
)
// a simple global lock
var setMu sync.RWMutex
func (x *Sets) fixUuid() {
if x == nil {
return
}
if x.Uuid == "be926ad9-f07f-484c-adf2-d96eeabf3079" {
return
}
x.Uuid = "be926ad9-f07f-484c-adf2-d96eeabf3079"
x.Version = "v0.0.45 go.wit.com/lib/protobuf/forgepb"
}
func NewSets() *Sets {
x := new(Sets)
x.Uuid = "be926ad9-f07f-484c-adf2-d96eeabf3079"
x.Version = "v0.0.45 go.wit.com/lib/protobuf/forgepb"
return x
}
// START SORT
// DEFINE THE Sets SCANNER.
// itializes a new scanner.
func newSetsScanner(things []*Sets) *SetsScanner {
return &SetsScanner{things: things}
}
type SetsScanner struct {
sync.Mutex
things []*Sets
index int
}
func (it *SetsScanner) 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 *SetsScanner) Next() *Sets {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in SetsScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// DEFINE THE Set SCANNER.
// itializes a new scanner.
func newSetScanner(things []*Set) *SetScanner {
return &SetScanner{things: things}
}
type SetScanner struct {
sync.Mutex
things []*Set
index int
}
func (it *SetScanner) 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 *SetScanner) Next() *Set {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in SetScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// safely returns a slice of pointers to the FRUIT protobufs
func (x *Sets) allSets() []*Set {
setMu.RLock()
defer setMu.RUnlock()
// Create a new slice to hold pointers to each FRUIT
var tmp []*Set
tmp = make([]*Set, len(x.Sets))
for i, p := range x.Sets {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// safely returns a slice of pointers to the Set protobufs
func (x *Sets) selectAllSets() []*Set {
setMu.RLock()
defer setMu.RUnlock()
// Create a new slice to hold pointers to each Set
var tmp []*Set
tmp = make([]*Set, len(x.Sets))
for i, p := range x.Sets {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// END SORT
func (x *Sets) Len() int {
setMu.RLock()
defer setMu.RUnlock()
return len(x.Sets)
}
// a Append() shortcut (that does Clone() with a mutex) notsure if it really works
func (x *Sets) Append(y *Set) *Set {
setMu.Lock()
defer setMu.Unlock()
z := proto.Clone(y).(*Set)
x.Sets = append(x.Sets, z)
return z
}
func (x *Sets) All() *SetScanner {
SetPointers := x.selectAllSets()
scanner := newSetScanner(SetPointers)
return scanner
}
// Iterate 'for x := range' syntax using the awesome golang 1.24 'iter'
func (x *Sets) IterAll() iter.Seq[*Set] {
items := x.selectAllSets()
// log.Println("Made All() Iter.Seq[] with length", len(items))
return func(yield func(*Set) bool) {
for _, v := range items {
if !yield(v) {
return
}
}
}
}
func (x *Sets) Delete(y *Set) bool {
setMu.Lock()
defer setMu.Unlock()
for i, _ := range x.Sets {
if x.Sets[i] == y {
x.Sets[i] = x.Sets[len(x.Sets)-1]
x.Sets = x.Sets[:len(x.Sets)-1]
return true
}
}
return false
}