// 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" "sort" "sync" "google.golang.org/protobuf/proto" ) // a simple global lock var patchMu sync.RWMutex func (x *Patches) fixUuid() { if x == nil { return } if x.Uuid == "2679065e-c81d-4a00-aca4-03c158a834fb" { return } x.Uuid = "2679065e-c81d-4a00-aca4-03c158a834fb" x.Version = "v2.0.0 go.wit.com/lib/protobuf/forgepb" } func NewPatches() *Patches { x := new(Patches) x.Uuid = "2679065e-c81d-4a00-aca4-03c158a834fb" x.Version = "v2.0.0 go.wit.com/lib/protobuf/forgepb" return x } // START SORT // DEFINE THE Patches SCANNER. // itializes a new scanner. func newPatchesScanner(things []*Patches) *PatchesScanner { return &PatchesScanner{things: things} } type PatchesScanner struct { sync.Mutex things []*Patches index int } func (it *PatchesScanner) 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 *PatchesScanner) Next() *Patches { if it.things[it.index-1] == nil { fmt.Println("Next() error in PatchesScanner", it.index) } return it.things[it.index-1] } // END DEFINE THE SCANNER // DEFINE THE Patch SCANNER. // itializes a new scanner. func newPatchScanner(things []*Patch) *PatchScanner { return &PatchScanner{things: things} } type PatchScanner struct { sync.Mutex things []*Patch index int } func (it *PatchScanner) 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 *PatchScanner) Next() *Patch { if it.things[it.index-1] == nil { fmt.Println("Next() error in PatchScanner", it.index) } return it.things[it.index-1] } // END DEFINE THE SCANNER // sort struct by Filename type sortPatchFilename []*Patch func (a sortPatchFilename) Len() int { return len(a) } func (a sortPatchFilename) Less(i, j int) bool { return a[i].Filename < a[j].Filename } func (a sortPatchFilename) Swap(i, j int) { a[i], a[j] = a[j], a[i] } // sort struct by CommitHash type sortPatchCommitHash []*Patch func (a sortPatchCommitHash) Len() int { return len(a) } func (a sortPatchCommitHash) Less(i, j int) bool { return a[i].CommitHash < a[j].CommitHash } func (a sortPatchCommitHash) Swap(i, j int) { a[i], a[j] = a[j], a[i] } // safely returns a slice of pointers to the FRUIT protobufs func (x *Patches) allPatches() []*Patch { x.RLock() defer x.RUnlock() // Create a new slice to hold pointers to each FRUIT var tmp []*Patch tmp = make([]*Patch, len(x.Patches)) for i, p := range x.Patches { tmp[i] = p // Copy pointers for safe iteration } return tmp } // safely returns a slice of pointers to the Patch protobufs func (x *Patches) selectAllPatches() []*Patch { x.RLock() defer x.RUnlock() // Create a new slice to hold pointers to each Patch var tmp []*Patch tmp = make([]*Patch, len(x.Patches)) for i, p := range x.Patches { tmp[i] = p // Copy pointers for safe iteration } return tmp } func (x *Patches) SortByFilename() *PatchScanner { // copy the pointers as fast as possible. things := x.selectAllPatches() // todo: try slices.SortFunc() instead to see what happens sort.Sort(sortPatchFilename(things)) // slices.SortFunc(things, func(a, b *Patches) bool { // return a.Filename < b.Filename // Sort by ??. let the compiler work it out?? // }) return newPatchScanner(things) } // 'for x := range' syntax using the awesome golang 1.24 'iter' func (x *Patches) IterByFilename() iter.Seq[*Patch] { items := x.selectAllPatches() sort.Sort(sortPatchFilename(items)) // log.Println("Made Iter.Seq[] with length", len(items)) return func(yield func(*Patch) bool) { for _, v := range items { if !yield(v) { return } } } } func (x *Patches) SortByCommitHash() *PatchScanner { // copy the pointers as fast as possible. things := x.selectAllPatches() // todo: try slices.SortFunc() instead to see what happens sort.Sort(sortPatchCommitHash(things)) // slices.SortFunc(things, func(a, b *Patches) bool { // return a.CommitHash < b.CommitHash // Sort by ??. let the compiler work it out?? // }) return newPatchScanner(things) } // 'for x := range' syntax using the awesome golang 1.24 'iter' func (x *Patches) IterByCommitHash() iter.Seq[*Patch] { items := x.selectAllPatches() sort.Sort(sortPatchCommitHash(items)) // log.Println("Made Iter.Seq[] with length", len(items)) return func(yield func(*Patch) bool) { for _, v := range items { if !yield(v) { return } } } } // END SORT func (x *Patches) Len() int { x.RLock() defer x.RUnlock() return len(x.Patches) } // a Append() shortcut (that does Clone() with a mutex) notsure if it really works func (x *Patches) Append(y *Patch) *Patch { x.Lock() defer x.Unlock() z := proto.Clone(y).(*Patch) x.Patches = append(x.Patches, z) return z } func (x *Patches) All() *PatchScanner { PatchPointers := x.selectAllPatches() scanner := newPatchScanner(PatchPointers) return scanner } // Iterate 'for x := range' syntax using the awesome golang 1.24 'iter' func (x *Patches) IterAll() iter.Seq[*Patch] { items := x.selectAllPatches() // log.Println("Made All() Iter.Seq[] with length", len(items)) return func(yield func(*Patch) bool) { for _, v := range items { if !yield(v) { return } } } } func (x *Patches) Delete(y *Patch) bool { x.Lock() defer x.Unlock() for i, _ := range x.Patches { if x.Patches[i] == y { x.Patches[i] = x.Patches[len(x.Patches)-1] x.Patches = x.Patches[:len(x.Patches)-1] return true } } return false } // lookup a Patches by the Filename func (x *Patches) FindByFilename(s string) *Patch { if x == nil { return nil } x.RLock() defer x.RUnlock() for i, _ := range x.Patches { if x.Patches[i].Filename == s { return x.Patches[i] } } return nil } // returns a Patch if Filename matches, otherwise create func (x *Patches) InsertByFilename(y string) *Patch { x.Lock() defer x.Unlock() for _, z := range x.Patches { if z.Filename == y { return z } } z := new(Patch) z.Filename = y x.Patches = append(x.Patches, z) return z } // lookup a Patches by the CommitHash func (x *Patches) FindByCommitHash(s string) *Patch { if x == nil { return nil } x.RLock() defer x.RUnlock() for i, _ := range x.Patches { if x.Patches[i].CommitHash == s { return x.Patches[i] } } return nil } // returns a Patch if CommitHash matches, otherwise create func (x *Patches) InsertByCommitHash(y string) *Patch { x.Lock() defer x.Unlock() for _, z := range x.Patches { if z.CommitHash == y { return z } } z := new(Patch) z.CommitHash = y x.Patches = append(x.Patches, z) return z } // lookup a Patches by the PatchId func (x *Patches) FindByPatchId(s string) *Patch { if x == nil { return nil } x.RLock() defer x.RUnlock() for i, _ := range x.Patches { if x.Patches[i].PatchId == s { return x.Patches[i] } } return nil } // returns a Patch if PatchId matches, otherwise create func (x *Patches) InsertByPatchId(y string) *Patch { x.Lock() defer x.Unlock() for _, z := range x.Patches { if z.PatchId == y { return z } } z := new(Patch) z.PatchId = y x.Patches = append(x.Patches, z) return z } func (x *Patches) DeleteByFilename(s string) bool { x.Lock() defer x.Unlock() for i, _ := range x.Patches { if x.Patches[i].Filename == s { x.Patches[i] = x.Patches[len(x.Patches)-1] x.Patches = x.Patches[:len(x.Patches)-1] return true } } return false } func (x *Patches) DeleteByCommitHash(s string) bool { x.Lock() defer x.Unlock() for i, _ := range x.Patches { if x.Patches[i].CommitHash == s { x.Patches[i] = x.Patches[len(x.Patches)-1] x.Patches = x.Patches[:len(x.Patches)-1] return true } } return false } func (x *Patches) DeleteByPatchId(s string) bool { x.Lock() defer x.Unlock() for i, _ := range x.Patches { if x.Patches[i].PatchId == s { x.Patches[i] = x.Patches[len(x.Patches)-1] x.Patches = x.Patches[:len(x.Patches)-1] return true } } return false } func (x *Patches) AppendByFilename(y *Patch) bool { x.Lock() defer x.Unlock() for _, p := range x.Patches { if p.Filename == y.Filename { return false } } x.Patches = append(x.Patches, proto.Clone(y).(*Patch)) return true } func (x *Patches) AppendByCommitHash(y *Patch) bool { x.Lock() defer x.Unlock() for _, p := range x.Patches { if p.CommitHash == y.CommitHash { return false } } x.Patches = append(x.Patches, proto.Clone(y).(*Patch)) return true } func (x *Patches) AppendByPatchId(y *Patch) bool { x.Lock() defer x.Unlock() for _, p := range x.Patches { if p.PatchId == y.PatchId { return false } } x.Patches = append(x.Patches, proto.Clone(y).(*Patch)) return true }