continue attempt

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 09:36:54 -06:00
parent 1c0234a442
commit 766c2e1674
3 changed files with 75 additions and 100 deletions

View File

@ -1,99 +0,0 @@
package gitpb
// this is becoming a standard format
// todo: autogenerate this from the .proto file?
import (
"fmt"
"os"
"sort"
sync "sync"
)
// bad global lock until I figure out some other plan
var godeplock sync.RWMutex
type GoDepIterator struct {
sync.RWMutex
packs []*GoDep
index int
}
// NewGoDepGoDepIterator 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.
func (it *GoDepIterator) Scan() bool {
if it.index >= len(it.packs) {
return false
}
it.index++
return true
}
// GoDep returns the current repo.
func (it *GoDepIterator) GoDep() *GoDep {
if it.packs[it.index-1] == nil {
for i, d := range it.packs {
fmt.Println("i =", i, d)
}
fmt.Println("len =", len(it.packs))
fmt.Println("repo == nil", it.index, it.index-1)
os.Exit(-1)
}
return it.packs[it.index-1]
}
// Use Scan() in a loop, similar to a while loop
//
// for iterator.Scan() {
// d := iterator.GoDep()
// fmt.Println("GoDep UUID:", d.Uuid)
// }
func (r *Repo) AllGoDeps() *GoDepIterator {
repoPointers := r.selectAllGoDeps()
iterator := NewGoDepIterator(repoPointers)
return iterator
}
func (r *Repo) SortGoDepsByName() *GoDepIterator {
packs := r.selectAllGoDeps()
sort.Sort(GoDepByPath(packs))
iterator := NewGoDepIterator(packs)
return iterator
}
func (repo *Repo) Len() int {
refslock.RLock()
defer refslock.RUnlock()
return len(repo.GoDeps)
}
type GoDepByPath []*GoDep
func (a GoDepByPath) Len() int { return len(a) }
func (a GoDepByPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
func (a GoDepByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the GoDep protobufs
func (r *Repo) selectAllGoDeps() []*GoDep {
refslock.RLock()
defer refslock.RUnlock()
// Create a new slice to hold pointers to each GoDep
var allPacks []*GoDep
allPacks = make([]*GoDep, len(r.GoDeps))
for i, p := range r.GoDeps {
allPacks[i] = p // Copy pointers for safe iteration
}
return allPacks
}

71
main.go
View File

@ -11,6 +11,7 @@ func main() {
header(f, "testautogen")
syncLock(f, "gitTagslock")
iterTop(f, "GitTag")
iterNext(f, "GitTag")
}
func header(w io.Writer, name string) {
@ -48,6 +49,12 @@ func iterTop(w io.Writer, name string) {
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more packs.")
fmt.Fprintln(w, "// Use Scan() in a loop, similar to a while loop")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// for iterator.Scan() ")
fmt.Fprintln(w, "// d := iterator.Next(")
fmt.Fprintln(w, "// fmt.Println(\"found UUID:\", d.Uuid")
fmt.Fprintln(w, "// }")
fmt.Fprintln(w, "func (it *"+name+"Iterator) Scan() bool {")
fmt.Fprintln(w, " if it.index >= len(it.packs) {")
fmt.Fprintln(w, " return false")
@ -55,4 +62,68 @@ func iterTop(w io.Writer, name string) {
fmt.Fprintln(w, " it.index++")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterNext(w io.Writer, name string) {
fmt.Fprintln(w, "// Next() returns the next thing in the array")
fmt.Fprintln(w, "func (it *" + name + "Iterator) Next() *" + name + " {")
fmt.Fprintln(w, " if it.packs[it.index-1] == nil {")
fmt.Fprintln(w, " for i, d := range it.packs {")
fmt.Fprintln(w, " fmt.Println(\"i =\", i, d)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " fmt.Println(\"protobuf autogenpb sort error len =\", len(it.packs))")
fmt.Fprintln(w, " fmt.Println(\"protobuf autogenpb sort error next == nil\", it.index, it.index-1)")
fmt.Fprintln(w, " os.Exit(-1)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return it.packs[it.index-1]")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
/*
func (r *Repos) All() *RepoIterator {
repoPointers := r.selectAllRepo()
iterator := NewRepoIterator(repoPointers)
return iterator
}
func (r *Repos) SortByPath() *RepoIterator {
packs := r.selectAllRepo()
sort.Sort(RepoByPath(packs))
iterator := NewRepoIterator(packs)
return iterator
}
func (all *Repos) Len() int {
repolock.RLock()
defer repolock.RUnlock()
return len(all.Repos)
}
type RepoByPath []*Repo
func (a RepoByPath) Len() int { return len(a) }
func (a RepoByPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
func (a RepoByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the Repo protobufs
func (all *Repos) selectAllRepo() []*Repo {
repolock.RLock()
defer repolock.RUnlock()
// Create a new slice to hold pointers to each Repo
var aRepos []*Repo
aRepos = make([]*Repo, len(all.Repos))
for i, p := range all.Repos {
aRepos[i] = p // Copy pointers for safe iteration
}
return aRepos
}
*/

View File

@ -1,7 +1,10 @@
VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d)
run: clean test.pb.go goimports vet
all: clean test.pb.go run goimports vet
run:
../autogenpb
vet:
@GO111MODULE=off go vet