diff --git a/d4ade3a09fdace62759d37cbf5064338a08b3332 b/d4ade3a09fdace62759d37cbf5064338a08b3332 index c07edb6..89efa4a 100644 --- a/d4ade3a09fdace62759d37cbf5064338a08b3332 +++ b/d4ade3a09fdace62759d37cbf5064338a08b3332 @@ -974,3 +974,255 @@ func file_goDep_proto_init() { } // `autogen:goDep.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 GoDepIterator struct { + sync.RWMutex + + packs []*GoDep + index int +} + +// NewGoDepIterator initializes a new iterator. +func NewGoDepIterator(packs []*GoDep) *GoDepIterator { + return &GoDepIterator{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 *GoDepIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *GoDepIterator) Next() *GoDep { + 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 *GoDeps) Append(newP *GoDep) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.GoDeps = append(all.GoDeps, newP) + return true +} + +func (all *GoDeps) All() *GoDepIterator { + goDepPointers := all.selectAllGoDep() + + iterator := NewGoDepIterator(goDepPointers) + return iterator +} + +func (all *GoDeps) Len() int { + all.Lock.RLock() + defer all.Lock.RUnlock() + + return len(all.GoDeps) +} + +func (all *GoDeps) SortByHash() *GoDepIterator { + packs := all.selectAllGoDep() + + sort.Sort(GoDepHash(packs)) + + iterator := NewGoDepIterator(packs) + return iterator +} + +type GoDepHash []*GoDep + +func (a GoDepHash) Len() int { return len(a) } +func (a GoDepHash) Less(i, j int) bool { return a[i].Hash < a[j].Hash } +func (a GoDepHash) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces Hash is unique +func (all *GoDeps) AppendUniqueHash(newP *GoDep) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.GoDeps { + if p.Hash == newP.Hash { + return false + } + } + + all.GoDeps = append(all.GoDeps, newP) + return true +} + +func (all *GoDeps) DeleteByHash(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.GoDeps { + if all.GoDeps[i].Hash == s { + all.GoDeps[i] = all.GoDeps[len(all.GoDeps)-1] + all.GoDeps = all.GoDeps[:len(all.GoDeps)-1] + return true + } + } + return false +} + +// enforces Hash is unique +func (all *GoDeps) ReplaceHash(newP *GoDep) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.GoDeps { + if p.Hash == newP.Hash { + return false + } + } + + all.GoDeps = append(all.GoDeps, newP) + return true +} + +// find a dependancy by the go path +func (all *GoDeps) FindByHash(s string) *GoDep { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.GoDeps { + if all.GoDeps[i].Hash == s { + return all.GoDeps[i] + } + } + return nil +} + +func (all *GoDeps) SortByGoPath() *GoDepIterator { + packs := all.selectAllGoDep() + + sort.Sort(GoDepGoPath(packs)) + + iterator := NewGoDepIterator(packs) + return iterator +} + +type GoDepGoPath []*GoDep + +func (a GoDepGoPath) Len() int { return len(a) } +func (a GoDepGoPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath } +func (a GoDepGoPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces GoPath is unique +func (all *GoDeps) AppendUniqueGoPath(newP *GoDep) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.GoDeps { + if p.GoPath == newP.GoPath { + return false + } + } + + all.GoDeps = append(all.GoDeps, newP) + return true +} + +func (all *GoDeps) DeleteByGoPath(s string) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.GoDeps { + if all.GoDeps[i].GoPath == s { + all.GoDeps[i] = all.GoDeps[len(all.GoDeps)-1] + all.GoDeps = all.GoDeps[:len(all.GoDeps)-1] + return true + } + } + return false +} + +// enforces GoPath is unique +func (all *GoDeps) ReplaceGoPath(newP *GoDep) bool { // todo: make unique name here + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.GoDeps { + if p.GoPath == newP.GoPath { + return false + } + } + + all.GoDeps = append(all.GoDeps, newP) + return true +} + +// find a dependancy by the go path +func (all *GoDeps) FindByGoPath(s string) *GoDep { + if all == nil { + return nil + } + + all.Lock.RLock() + defer all.Lock.RUnlock() + + for i, _ := range all.GoDeps { + if all.GoDeps[i].GoPath == s { + return all.GoDeps[i] + } + } + return nil +} + +// safely returns a slice of pointers to the GoDep protobufs +func (all *GoDeps) selectAllGoDep() []*GoDep { + all.Lock.RLock() + defer all.Lock.RUnlock() + + // Create a new slice to hold pointers to each GoDep + var aStuff []*GoDep + aStuff = make([]*GoDep, len(all.GoDeps)) + for i, p := range all.GoDeps { + aStuff[i] = p // Copy pointers for safe iteration + } + + return aStuff +}