Notes added by 'git notes append'
This commit is contained in:
parent
b700839773
commit
be5331e70b
|
@ -987,3 +987,237 @@ func file_package_proto_init() {
|
|||
}
|
||||
|
||||
// `autogen:package.sort.pb.go`
|
||||
|
||||
// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
|
||||
// This file was autogenerated with autogenpb v0.0.50 2025-02-09_00:52:14_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 zoopb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// a simple global lock
|
||||
var packageMu sync.RWMutex
|
||||
|
||||
func (x *Packages) fixUuid() {
|
||||
if x == nil {
|
||||
return
|
||||
}
|
||||
if x.Uuid == "2f26cc03-ea30-4481-a333-ad0acc86e1d3" {
|
||||
return
|
||||
}
|
||||
x.Uuid = "2f26cc03-ea30-4481-a333-ad0acc86e1d3"
|
||||
x.Version = "v0.0.1 go.wit.com/lib/protobuf/zoopb"
|
||||
}
|
||||
|
||||
func NewPackages() *Packages {
|
||||
x := new(Packages)
|
||||
x.Uuid = "2f26cc03-ea30-4481-a333-ad0acc86e1d3"
|
||||
x.Version = "v0.0.1 go.wit.com/lib/protobuf/zoopb"
|
||||
return x
|
||||
}
|
||||
|
||||
// START SORT
|
||||
|
||||
// DEFINE THE Packages ITERATOR.
|
||||
// itializes a new iterator.
|
||||
func newPackagesIterator(things []*Packages) *PackagesIterator {
|
||||
return &PackagesIterator{things: things}
|
||||
}
|
||||
|
||||
type PackagesIterator struct {
|
||||
sync.RWMutex // this isn't getting used properly yet?
|
||||
|
||||
things []*Packages
|
||||
index int
|
||||
}
|
||||
|
||||
func (it *PackagesIterator) Scan() bool {
|
||||
if it.index >= len(it.things) {
|
||||
return false
|
||||
}
|
||||
it.index++
|
||||
return true
|
||||
}
|
||||
|
||||
// Next() returns the next thing in the array
|
||||
func (it *PackagesIterator) Next() *Packages {
|
||||
if it.things[it.index-1] == nil {
|
||||
fmt.Println("Next() error in PackagesIterator", it.index)
|
||||
}
|
||||
return it.things[it.index-1]
|
||||
}
|
||||
|
||||
// END DEFINE THE ITERATOR
|
||||
|
||||
// DEFINE THE Package ITERATOR.
|
||||
// itializes a new iterator.
|
||||
func newPackageIterator(things []*Package) *PackageIterator {
|
||||
return &PackageIterator{things: things}
|
||||
}
|
||||
|
||||
type PackageIterator struct {
|
||||
sync.RWMutex // this isn't getting used properly yet?
|
||||
|
||||
things []*Package
|
||||
index int
|
||||
}
|
||||
|
||||
func (it *PackageIterator) Scan() bool {
|
||||
if it.index >= len(it.things) {
|
||||
return false
|
||||
}
|
||||
it.index++
|
||||
return true
|
||||
}
|
||||
|
||||
// Next() returns the next thing in the array
|
||||
func (it *PackageIterator) Next() *Package {
|
||||
if it.things[it.index-1] == nil {
|
||||
fmt.Println("Next() error in PackageIterator", it.index)
|
||||
}
|
||||
return it.things[it.index-1]
|
||||
}
|
||||
|
||||
// END DEFINE THE ITERATOR
|
||||
|
||||
// sort struct by Name
|
||||
type PackageName []*Package
|
||||
|
||||
func (a PackageName) Len() int { return len(a) }
|
||||
func (a PackageName) Less(i, j int) bool { return a[i].Name < a[j].Name }
|
||||
func (a PackageName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
// safely returns a slice of pointers to the FRUIT protobufs
|
||||
func (x *Packages) allPackages() []*Package {
|
||||
x.RLock()
|
||||
defer x.RUnlock()
|
||||
|
||||
// Create a new slice to hold pointers to each FRUIT
|
||||
var tmp []*Package
|
||||
tmp = make([]*Package, len(x.Packages))
|
||||
for i, p := range x.Packages {
|
||||
tmp[i] = p // Copy pointers for safe iteration
|
||||
}
|
||||
|
||||
return tmp
|
||||
}
|
||||
|
||||
// safely returns a slice of pointers to the Package protobufs
|
||||
func (x *Packages) selectAllPackages() []*Package {
|
||||
x.RLock()
|
||||
defer x.RUnlock()
|
||||
|
||||
// Create a new slice to hold pointers to each Package
|
||||
var tmp []*Package
|
||||
tmp = make([]*Package, len(x.Packages))
|
||||
for i, p := range x.Packages {
|
||||
tmp[i] = p // Copy pointers for safe iteration
|
||||
}
|
||||
|
||||
return tmp
|
||||
}
|
||||
func (x *Packages) SortByName() *PackageIterator {
|
||||
// copy the pointers as fast as possible.
|
||||
things := x.selectAllPackages()
|
||||
|
||||
// todo: try slices.SortFunc() instead to see what happens
|
||||
sort.Sort(PackageName(things))
|
||||
// slices.SortFunc(things, func(a, b *Packages) bool {
|
||||
// return a.Name < b.Name // Sort by ??. let the compiler work it out??
|
||||
// })
|
||||
return newPackageIterator(things)
|
||||
}
|
||||
|
||||
// END SORT
|
||||
|
||||
func (x *Packages) Len() int {
|
||||
x.RLock()
|
||||
defer x.RUnlock()
|
||||
|
||||
return len(x.Packages)
|
||||
}
|
||||
|
||||
// just a simple Append() shortcut (but still uses the mutex lock)
|
||||
func (x *Packages) Append(y *Package) {
|
||||
x.Lock()
|
||||
defer x.Unlock()
|
||||
|
||||
x.Packages = append(x.Packages, y)
|
||||
}
|
||||
|
||||
func (x *Packages) All() *PackageIterator {
|
||||
PackagePointers := x.selectAllPackages()
|
||||
|
||||
iterator := newPackageIterator(PackagePointers)
|
||||
return iterator
|
||||
}
|
||||
|
||||
func (x *Packages) Delete(y *Package) bool {
|
||||
x.Lock()
|
||||
defer x.Unlock()
|
||||
|
||||
for i, _ := range x.Packages {
|
||||
if x.Packages[i] == y {
|
||||
x.Packages[i] = x.Packages[len(x.Packages)-1]
|
||||
x.Packages = x.Packages[:len(x.Packages)-1]
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// lookup a Packages by the Name
|
||||
func (x *Packages) FindByName(s string) *Package {
|
||||
if x == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
x.RLock()
|
||||
defer x.RUnlock()
|
||||
|
||||
for i, _ := range x.Packages {
|
||||
if x.Packages[i].Name == s {
|
||||
return x.Packages[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *Packages) DeleteByName(s string) bool {
|
||||
x.Lock()
|
||||
defer x.Unlock()
|
||||
|
||||
for i, _ := range x.Packages {
|
||||
if x.Packages[i].Name == s {
|
||||
x.Packages[i] = x.Packages[len(x.Packages)-1]
|
||||
x.Packages = x.Packages[:len(x.Packages)-1]
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (x *Packages) AppendByName(y *Package) bool {
|
||||
x.Lock()
|
||||
defer x.Unlock()
|
||||
|
||||
for _, p := range x.Packages {
|
||||
if p.Name == y.Name {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
x.Packages = append(x.Packages, y)
|
||||
return true
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue