From d8bfdb90cdd8a29407ac600ad0a4b927120df86a Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 11 Jan 2025 06:16:31 -0600 Subject: [PATCH] Notes added by 'git notes append' --- b548c280a63c78118468e95064806f0d1ec1509b | 238 +++++++++++++++++++++++ 1 file changed, 238 insertions(+) diff --git a/b548c280a63c78118468e95064806f0d1ec1509b b/b548c280a63c78118468e95064806f0d1ec1509b index d46b3b6..0b51e2e 100644 --- a/b548c280a63c78118468e95064806f0d1ec1509b +++ b/b548c280a63c78118468e95064806f0d1ec1509b @@ -968,3 +968,241 @@ func file_goDep_proto_init() { } // `autogen:goDep.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 goDepMu sync.RWMutex + +type GoDepIterator struct { + sync.RWMutex + + things []*GoDep + index int +} + +// NewGoDepIterator initializes a new iterator. +func NewGoDepIterator(things []*GoDep) *GoDepIterator { + return &GoDepIterator{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 *GoDepIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *GoDepIterator) Next() *GoDep { + 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 *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) +} + +// 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 tmp []*GoDep + tmp = make([]*GoDep, len(all.GoDeps)) + for i, p := range all.GoDeps { + 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 *GoDeps) Append(newP *GoDep) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.GoDeps = append(all.GoDeps, newP) + return true +} + +// enforces GoDep 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 +} + +// enforces GoDep 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 +} + +// enforces GoDep is unique +func (all *GoDeps) AppendUnique(newP *GoDep) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + for _, p := range all.GoDeps { + if p.Hash == newP.Hash { + return false + } + if p.GoPath == newP.GoPath { + return false + } + } + + all.GoDeps = append(all.GoDeps, newP) + return true +} + +func (all *GoDeps) SortByHash() *GoDepIterator { + things := all.selectAllGoDep() + + sort.Sort(GoDepHash(things)) + + iterator := NewGoDepIterator(things) + 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] } + +func (all *GoDeps) SortByGoPath() *GoDepIterator { + things := all.selectAllGoDep() + + sort.Sort(GoDepGoPath(things)) + + iterator := NewGoDepIterator(things) + 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] } + +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 +} + +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 +} + +// 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 +} + +// 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 +}