diff --git a/a115ba144b00dc0339a8cf7eae6bdf2aab5fb4b5 b/a115ba144b00dc0339a8cf7eae6bdf2aab5fb4b5 index d01cb7a..94b352c 100644 --- a/a115ba144b00dc0339a8cf7eae6bdf2aab5fb4b5 +++ b/a115ba144b00dc0339a8cf7eae6bdf2aab5fb4b5 @@ -1746,3 +1746,253 @@ func file_repo_proto_init() { } // `autogen:repo.sort.pb.go` + +package gitpb + +// This file was autogenerated with autogenpb v0.0.28-1-g50cafc8 DO NOT EDIT +// 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" + "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) + } + 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) SortByFullPath() *RepoIterator { + packs := all.selectAllRepo() + + sort.Sort(RepoFullPath(packs)) + + iterator := NewRepoIterator(packs) + 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] } + +// enforces FullPath 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 +} + +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 +} + +// enforces FullPath is unique +func (all *Repos) ReplaceFullPath(newP *Repo) bool { // todo: make unique name here + 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 +} + +// 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 +} + +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 +}