diff --git a/b55548a58b43cf8d30edbe0440a3fd8efbddabfd b/b55548a58b43cf8d30edbe0440a3fd8efbddabfd index 812b4ed..5a3b701 100644 --- a/b55548a58b43cf8d30edbe0440a3fd8efbddabfd +++ b/b55548a58b43cf8d30edbe0440a3fd8efbddabfd @@ -1031,3 +1031,184 @@ func file_patch_proto_init() { } // `autogen:patch.sort.pb.go` + +package forgepb + +// This file was autogenerated with autogenpb. +// go install go.wit.com/apps/autogenpb@latest +// +// You can use it on simple protobuf files +// The .proto file must have a singular and plural form of a message +// (for those of you that know ruby on rails, it's like that) +// +// You can mark which repos you want to auto generate sort.pb.go and marshal.pb.go files for +// +// For an example, +// go-clone go.wit.com/lib/protobuf/gitpb +// + +import ( + "fmt" + "os" + "sort" + "sync" +) + +// bad global lock until I figure out some other plan +// redo/fix protoc-gen-go 1.35 and do it there? +// sync.RWMutex or sync.Mutex? +var patchsMu sync.RWMutex + +type PatchIterator struct { + sync.RWMutex + + packs []*Patch + index int +} + +// NewPatchIterator initializes a new iterator. +func NewPatchIterator(packs []*Patch) *PatchIterator { + return &PatchIterator{packs: packs} +} + +// Scan moves to the next element and returns false if there are no more packs. +// 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.packs) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *PatchIterator) Next() *Patch { + if it.packs[it.index-1] == nil { + for i, d := range it.packs { + fmt.Println("i =", i, d) + } + fmt.Println("protobuf autogenpb sort error len =", len(it.packs)) + fmt.Println("protobuf autogenpb sort error next == nil", it.index, it.index-1) + os.Exit(-1) + } + return it.packs[it.index-1] +} + +// does not enforce any unique fields +func (all *Patchs) Append(newP *Patch) bool { + patchsMu.RLock() + defer patchsMu.RUnlock() + + all.Patchs = append(all.Patchs, newP) + return true +} + +func (all *Patchs) All() *PatchIterator { + patchPointers := all.selectAllPatch() + + iterator := NewPatchIterator(patchPointers) + return iterator +} + +func (all *Patchs) Len() int { + patchsMu.RLock() + defer patchsMu.RUnlock() + + return len(all.Patchs) +} + +func (all *Patchs) SortByFilename() *PatchIterator { + packs := all.selectAllPatch() + + sort.Sort(PatchFilename(packs)) + + iterator := NewPatchIterator(packs) + 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] } + +// enforces Filename is unique +func (all *Patchs) AppendUniqueFilename(newP *Patch) bool { + patchsMu.RLock() + defer patchsMu.RUnlock() + + for _, p := range all.Patchs { + if p.Filename == newP.Filename { + return false + } + } + + all.Patchs = append(all.Patchs, newP) + return true +} + +func (all *Patchs) DeleteByFilename(s string) bool { + patchsMu.RLock() + defer patchsMu.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 +} + +// enforces Filename is unique +func (all *Patchs) ReplaceFilename(newP *Patch) bool { // todo: make unique name here + patchsMu.RLock() + defer patchsMu.RUnlock() + + for _, p := range all.Patchs { + if p.Filename == newP.Filename { + return false + } + } + + all.Patchs = append(all.Patchs, newP) + return true +} + +// find a dependancy by the go path +func (all *Patchs) FindByFilename(s string) *Patch { + if all == nil { + return nil + } + + patchsMu.RLock() + defer patchsMu.RUnlock() + + for i, _ := range all.Patchs { + if all.Patchs[i].Filename == s { + return all.Patchs[i] + } + } + return nil +} + +// safely returns a slice of pointers to the Patch protobufs +func (all *Patchs) selectAllPatch() []*Patch { + patchsMu.RLock() + defer patchsMu.RUnlock() + + // Create a new slice to hold pointers to each Patch + var aStuff []*Patch + aStuff = make([]*Patch, len(all.Patchs)) + for i, p := range all.Patchs { + aStuff[i] = p // Copy pointers for safe iteration + } + + return aStuff +}