chatpb/book.sort.pb.go

287 lines
5.7 KiB
Go

// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
// This file was autogenerated with autogenpb v0.5.3-12-g0ccf2c0 2025-09-18_03:38:50_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 chatpb
import (
"fmt"
"iter"
"sort"
"sync"
"google.golang.org/protobuf/proto"
)
// a simple global lock
var bookMu sync.RWMutex
func (x *Books) fixUuid() {
if x == nil {
return
}
if x.Uuid == "8b6409ad-4498-43a6-b09a-7835c00dcb9a" {
return
}
x.Uuid = "8b6409ad-4498-43a6-b09a-7835c00dcb9a"
x.Version = "v0.0.1 go.wit.com/lib/protobuf/chatpb"
}
func NewBooks() *Books {
x := new(Books)
x.Uuid = "8b6409ad-4498-43a6-b09a-7835c00dcb9a"
x.Version = "v0.0.1 go.wit.com/lib/protobuf/chatpb"
return x
}
// START SORT
// DEFINE THE Books SCANNER.
// itializes a new scanner.
func newBooksScanner(things []*Books) *BooksScanner {
return &BooksScanner{things: things}
}
type BooksScanner struct {
sync.Mutex
things []*Books
index int
}
func (it *BooksScanner) 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 *BooksScanner) Next() *Books {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in BooksScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// DEFINE THE Book SCANNER.
// itializes a new scanner.
func newBookScanner(things []*Book) *BookScanner {
return &BookScanner{things: things}
}
type BookScanner struct {
sync.Mutex
things []*Book
index int
}
func (it *BookScanner) 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 *BookScanner) Next() *Book {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in BookScanner", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE SCANNER
// sort struct by Uuid
type sortBookUuid []*Book
func (a sortBookUuid) Len() int { return len(a) }
func (a sortBookUuid) Less(i, j int) bool { return a[i].Uuid < a[j].Uuid }
func (a sortBookUuid) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the FRUIT protobufs
func (x *Books) allBooks() []*Book {
x.RLock()
defer x.RUnlock()
// Create a new slice to hold pointers to each FRUIT
var tmp []*Book
tmp = make([]*Book, len(x.Books))
for i, p := range x.Books {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// safely returns a slice of pointers to the Book protobufs
func (x *Books) selectAllBooks() []*Book {
x.RLock()
defer x.RUnlock()
// Create a new slice to hold pointers to each Book
var tmp []*Book
tmp = make([]*Book, len(x.Books))
for i, p := range x.Books {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
func (x *Books) SortByUuid() *BookScanner {
// copy the pointers as fast as possible.
things := x.selectAllBooks()
// todo: try slices.SortFunc() instead to see what happens
sort.Sort(sortBookUuid(things))
// slices.SortFunc(things, func(a, b *Books) bool {
// return a.Uuid < b.Uuid // Sort by ??. let the compiler work it out??
// })
return newBookScanner(things)
}
// 'for x := range' syntax using the awesome golang 1.24 'iter'
func (x *Books) IterByUuid() iter.Seq[*Book] {
items := x.selectAllBooks()
sort.Sort(sortBookUuid(items))
// log.Println("Made Iter.Seq[] with length", len(items))
return func(yield func(*Book) bool) {
for _, v := range items {
if !yield(v) {
return
}
}
}
}
// END SORT
func (x *Books) Len() int {
x.RLock()
defer x.RUnlock()
return len(x.Books)
}
// a Append() shortcut (that does Clone() with a mutex) notsure if it really works
func (x *Books) Append(y *Book) *Book {
x.Lock()
defer x.Unlock()
z := proto.Clone(y).(*Book)
x.Books = append(x.Books, z)
return z
}
func (x *Books) All() *BookScanner {
BookPointers := x.selectAllBooks()
scanner := newBookScanner(BookPointers)
return scanner
}
// Iterate 'for x := range' syntax using the awesome golang 1.24 'iter'
func (x *Books) IterAll() iter.Seq[*Book] {
items := x.selectAllBooks()
// log.Println("Made All() Iter.Seq[] with length", len(items))
return func(yield func(*Book) bool) {
for _, v := range items {
if !yield(v) {
return
}
}
}
}
func (x *Books) Delete(y *Book) bool {
x.Lock()
defer x.Unlock()
for i, _ := range x.Books {
if x.Books[i] == y {
x.Books[i] = x.Books[len(x.Books)-1]
x.Books = x.Books[:len(x.Books)-1]
return true
}
}
return false
}
// lookup a Books by the Uuid
func (x *Books) FindByUuid(s string) *Book {
if x == nil {
return nil
}
x.RLock()
defer x.RUnlock()
for i, _ := range x.Books {
if x.Books[i].Uuid == s {
return x.Books[i]
}
}
return nil
}
// returns a Book if Uuid matches, otherwise create
func (x *Books) InsertByUuid(y string) *Book {
x.Lock()
defer x.Unlock()
for _, z := range x.Books {
if z.Uuid == y {
return z
}
}
z := new(Book)
z.Uuid = y
x.Books = append(x.Books, z)
return z
}
func (x *Books) DeleteByUuid(s string) bool {
x.Lock()
defer x.Unlock()
for i, _ := range x.Books {
if x.Books[i].Uuid == s {
x.Books[i] = x.Books[len(x.Books)-1]
x.Books = x.Books[:len(x.Books)-1]
return true
}
}
return false
}
func (x *Books) AppendByUuid(y *Book) bool {
x.Lock()
defer x.Unlock()
for _, p := range x.Books {
if p.Uuid == y.Uuid {
return false
}
}
x.Books = append(x.Books, proto.Clone(y).(*Book))
return true
}