From ac9dcc0b1588428649482291a9ce8a11b9358ab5 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 11 Jan 2025 06:16:12 -0600 Subject: [PATCH] Notes added by 'git notes append' --- 7df10fba519944c1dae55625b10b0677b639febb | 174 +++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/7df10fba519944c1dae55625b10b0677b639febb b/7df10fba519944c1dae55625b10b0677b639febb index 3a4cb00..5508d8e 100644 --- a/7df10fba519944c1dae55625b10b0677b639febb +++ b/7df10fba519944c1dae55625b10b0677b639febb @@ -2018,3 +2018,177 @@ func file_repo_proto_init() { } // `autogen:repo.sort.pb.go` + +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.38-3-g25ae7fc 2025.01.10_0403 +// 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 gitpb + +import ( + "fmt" + "sort" + "sync" +) + +// bad global lock until modifying the .pb.go file is tested +// sync.RWMutex or sync.Mutex? +var repoMu sync.RWMutex + +type RepoIterator struct { + sync.RWMutex + + things []*Repo + index int +} + +// NewRepoIterator initializes a new iterator. +func NewRepoIterator(things []*Repo) *RepoIterator { + return &RepoIterator{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 *RepoIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *RepoIterator) Next() *Repo { + if it.things[it.index-1] == nil { + for i, d := range it.things { + fmt.Println("i =", i, d) + } + } + return it.things[it.index-1] +} + +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) +} + +// 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 tmp []*Repo + tmp = make([]*Repo, len(all.Repos)) + for i, p := range all.Repos { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// maybe seperate files here? +// just a simple Append() with no checking (but still uses the mutex lock) +func (all *Repos) Append(newP *Repo) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.Repos = append(all.Repos, newP) + return true +} + +// enforces Repo is unique +func (all *Repos) AppendUniqueFullPath(newP *Repo) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Repos { + if p.FullPath == newP.FullPath { + return false + } + } + + all.Repos = append(all.Repos, newP) + return true +} + +// enforces Repo is unique +func (all *Repos) AppendUnique(newP *Repo) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.Repos { + if p.FullPath == newP.FullPath { + return false + } + } + + all.Repos = append(all.Repos, newP) + return true +} + +func (all *Repos) SortByFullPath() *RepoIterator { + things := all.selectAllRepo() + + sort.Sort(RepoFullPath(things)) + + iterator := NewRepoIterator(things) + return iterator +} + +type RepoFullPath []*Repo + +func (a RepoFullPath) Len() int { return len(a) } +func (a RepoFullPath) Less(i, j int) bool { return a[i].FullPath < a[j].FullPath } +func (a RepoFullPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +func (all *Repos) DeleteByFullPath(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Repos { + if all.Repos[i].FullPath == s { + all.Repos[i] = all.Repos[len(all.Repos)-1] + all.Repos = all.Repos[:len(all.Repos)-1] + return true + } + } + return false +} + +// find a dependancy by the go path +func (all *Repos) FindByFullPath(s string) *Repo { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.Repos { + if all.Repos[i].FullPath == s { + return all.Repos[i] + } + } + return nil +}