From 5f51583e995f529b6fabbecf2eaefd0e52662467 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 14 Dec 2024 15:22:14 -0600 Subject: [PATCH] Notes added by 'git notes append' --- 5341a70557f00b07e95802348c03aa25781ff5b7 | 176 +++++++++++++++++++++++ 1 file changed, 176 insertions(+) diff --git a/5341a70557f00b07e95802348c03aa25781ff5b7 b/5341a70557f00b07e95802348c03aa25781ff5b7 index c95a2b4..ee14706 100644 --- a/5341a70557f00b07e95802348c03aa25781ff5b7 +++ b/5341a70557f00b07e95802348c03aa25781ff5b7 @@ -1751,3 +1751,179 @@ func file_repo_proto_init() { } // `autogen:repo.sort.pb.go` + +package gitpb + +// 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" +) + +type RepoIterator struct { + sync.RWMutex + + packs []*Repo + index int +} + +// NewRepoIterator initializes a new iterator. +func NewRepoIterator(packs []*Repo) *RepoIterator { + return &RepoIterator{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 *RepoIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *RepoIterator) Next() *Repo { + 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 *Repos) Append(newP *Repo) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.Repos = append(all.Repos, newP) + return true +} + +func (all *Repos) All() *RepoIterator { + repoPointers := all.selectAllRepo() + + iterator := NewRepoIterator(repoPointers) + return iterator +} + +func (all *Repos) Len() int { + all.Lock.RLock() + defer all.Lock.RUnlock() + + return len(all.Repos) +} + +func (all *Repos) SortByGoPath() *RepoIterator { + packs := all.selectAllRepo() + + sort.Sort(RepoGoPath(packs)) + + iterator := NewRepoIterator(packs) + return iterator +} + +type RepoGoPath []*Repo + +func (a RepoGoPath) Len() int { return len(a) } +func (a RepoGoPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath } +func (a RepoGoPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces GoPath is unique +func (all *Repos) AppendUniqueGoPath(newP *Repo) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Repos { + if p.GoPath == newP.GoPath { + return false + } + } + + all.Repos = append(all.Repos, newP) + return true +} + +func (all *Repos) DeleteByGoPath(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Repos { + if all.Repos[i].GoPath == s { + all.Repos[i] = all.Repos[len(all.Repos)-1] + all.Repos = all.Repos[:len(all.Repos)-1] + return true + } + } + return false +} + +// enforces GoPath is unique +func (all *Repos) ReplaceGoPath(newP *Repo) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Repos { + if p.GoPath == newP.GoPath { + return false + } + } + + all.Repos = append(all.Repos, newP) + return true +} + +// find a dependancy by the go path +func (all *Repos) FindByGoPath(s string) *Repo { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Repos { + if all.Repos[i].GoPath == s { + return all.Repos[i] + } + } + return nil +} + +// safely returns a slice of pointers to the Repo protobufs +func (all *Repos) selectAllRepo() []*Repo { + all.Lock.RLock() + defer all.Lock.RUnlock() + + // Create a new slice to hold pointers to each Repo + var aStuff []*Repo + aStuff = make([]*Repo, len(all.Repos)) + for i, p := range all.Repos { + aStuff[i] = p // Copy pointers for safe iteration + } + + return aStuff +}