diff --git a/2e3bf05f82081567cb345c835955859d509ab945 b/2e3bf05f82081567cb345c835955859d509ab945 index 3ded626..73253c0 100644 --- a/2e3bf05f82081567cb345c835955859d509ab945 +++ b/2e3bf05f82081567cb345c835955859d509ab945 @@ -666,3 +666,159 @@ func (v *Patchs) Unmarshal(data []byte) error { } // `autogen:patch.newsort.pb.go` + +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.37-30-g805d3cd 2025.01.09_2100 +// 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" + "sort" + "sync" +) + +// bad global lock until modifying the .pb.go file is tested +// sync.RWMutex or sync.Mutex? +var patchMu sync.RWMutex + +type PatchIterator struct { + sync.RWMutex + + things []*Patch + index int +} + +// NewPatchIterator initializes a new iterator. +func NewPatchIterator(things []*Patch) *PatchIterator { + return &PatchIterator{things: things} +} + +// Scan moves to the next element and returns false if there are no more things. +// Use Scan() in a loop, similar to a while loop +// +// for iterator.Scan() +// d := iterator.Next( +// fmt.Println("found UUID:", d.Uuid +// } +func (it *PatchIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *PatchIterator) Next() *Patch { + if it.things[it.index-1] == nil { + for i, d := range it.things { + fmt.Println("i =", i, d) + } + } + return it.things[it.index-1] +} + +// safely returns a slice of pointers to the Patch protobufs +func (all *Patchs) selectAllPatch() []*Patch { + patchMu.RLock() + defer patchMu.RUnlock() + + // Create a new slice to hold pointers to each Patch + var tmp []*Patch + tmp = make([]*Patch, len(all.Patchs)) + for i, p := range all.Patchs { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// just a simple Append() with no checking (but still uses the mutex lock) +func (all *Patchs) Append(newP *Patch) bool { + patchMu.RLock() + defer patchMu.RUnlock() + + all.Patchs = append(all.Patchs, newP) + return true +} + +// enforces Patch is unique +func (all *Patchs) AppendUniqueFilename(newP *Patch) bool { + patchMu.RLock() + defer patchMu.RUnlock() + + for _, p := range all.Patchs { + if p.Filename == newP.Filename { + return false + } + } + + all.Patchs = append(all.Patchs, newP) + return true +} + +// enforces Patch is unique +func (all *Patchs) AppendUnique(newP *Patch) bool { + patchMu.RLock() + defer patchMu.RUnlock() + + for _, p := range all.Patchs { + if p.Filename == newP.Filename { + return false + } + } + + all.Patchs = append(all.Patchs, newP) + return true +} + +func (all *Patchs) SortByFilename() *PatchIterator { + things := all.selectAllPatch() + + sort.Sort(PatchFilename(things)) + + iterator := NewPatchIterator(things) + return iterator +} + +type PatchFilename []*Patch + +func (a PatchFilename) Len() int { return len(a) } +func (a PatchFilename) Less(i, j int) bool { return a[i].Filename < a[j].Filename } +func (a PatchFilename) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +func (all *Patchs) All() *PatchIterator { + PatchPointers := all.selectAllPatch() + + iterator := NewPatchIterator(PatchPointers) + return iterator +} + +func (all *Patchs) Len() int { + patchMu.RLock() + defer patchMu.RUnlock() + + return len(all.Patchs) +} + +func (all *Patchs) DeleteByFilename(s string) bool { + patchMu.RLock() + defer patchMu.RUnlock() + + for i, _ := range all.Patchs { + if all.Patchs[i].Filename == s { + all.Patchs[i] = all.Patchs[len(all.Patchs)-1] + all.Patchs = all.Patchs[:len(all.Patchs)-1] + return true + } + } + return false +}