diff --git a/e796788e22797478add8b8c84951b203e9aa9e1e b/e796788e22797478add8b8c84951b203e9aa9e1e index 596dbd5..9d0ef7c 100644 --- a/e796788e22797478add8b8c84951b203e9aa9e1e +++ b/e796788e22797478add8b8c84951b203e9aa9e1e @@ -432,3 +432,184 @@ func file_forgeConfig_proto_init() { } // `autogen:forgeConfig.sort.pb.go` + +package forgepb + +// 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" +) + +// bad global lock until I figure out some other plan +// redo/fix protoc-gen-go 1.35 and do it there? +// sync.RWMutex or sync.Mutex? +var forgeConfigsMu sync.RWMutex + +type ForgeConfigIterator struct { + sync.RWMutex + + packs []*ForgeConfig + index int +} + +// NewForgeConfigIterator initializes a new iterator. +func NewForgeConfigIterator(packs []*ForgeConfig) *ForgeConfigIterator { + return &ForgeConfigIterator{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 *ForgeConfigIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *ForgeConfigIterator) Next() *ForgeConfig { + 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 *ForgeConfigs) Append(newP *ForgeConfig) bool { + forgeConfigsMu.RLock() + defer forgeConfigsMu.RUnlock() + + all.ForgeConfigs = append(all.ForgeConfigs, newP) + return true +} + +func (all *ForgeConfigs) All() *ForgeConfigIterator { + forgeConfigPointers := all.selectAllForgeConfig() + + iterator := NewForgeConfigIterator(forgeConfigPointers) + return iterator +} + +func (all *ForgeConfigs) Len() int { + forgeConfigsMu.RLock() + defer forgeConfigsMu.RUnlock() + + return len(all.ForgeConfigs) +} + +func (all *ForgeConfigs) SortByGoPath() *ForgeConfigIterator { + packs := all.selectAllForgeConfig() + + sort.Sort(ForgeConfigGoPath(packs)) + + iterator := NewForgeConfigIterator(packs) + return iterator +} + +type ForgeConfigGoPath []*ForgeConfig + +func (a ForgeConfigGoPath) Len() int { return len(a) } +func (a ForgeConfigGoPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath } +func (a ForgeConfigGoPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// enforces GoPath is unique +func (all *ForgeConfigs) AppendUniqueGoPath(newP *ForgeConfig) bool { + forgeConfigsMu.RLock() + defer forgeConfigsMu.RUnlock() + + for _, p := range all.ForgeConfigs { + if p.GoPath == newP.GoPath { + return false + } + } + + all.ForgeConfigs = append(all.ForgeConfigs, newP) + return true +} + +func (all *ForgeConfigs) DeleteByGoPath(s string) bool { + forgeConfigsMu.RLock() + defer forgeConfigsMu.RUnlock() + + for i, _ := range all.ForgeConfigs { + if all.ForgeConfigs[i].GoPath == s { + all.ForgeConfigs[i] = all.ForgeConfigs[len(all.ForgeConfigs)-1] + all.ForgeConfigs = all.ForgeConfigs[:len(all.ForgeConfigs)-1] + return true + } + } + return false +} + +// enforces GoPath is unique +func (all *ForgeConfigs) ReplaceGoPath(newP *ForgeConfig) bool { // todo: make unique name here + forgeConfigsMu.RLock() + defer forgeConfigsMu.RUnlock() + + for _, p := range all.ForgeConfigs { + if p.GoPath == newP.GoPath { + return false + } + } + + all.ForgeConfigs = append(all.ForgeConfigs, newP) + return true +} + +// find a dependancy by the go path +func (all *ForgeConfigs) FindByGoPath(s string) *ForgeConfig { + if all == nil { + return nil + } + + forgeConfigsMu.RLock() + defer forgeConfigsMu.RUnlock() + + for i, _ := range all.ForgeConfigs { + if all.ForgeConfigs[i].GoPath == s { + return all.ForgeConfigs[i] + } + } + return nil +} + +// safely returns a slice of pointers to the ForgeConfig protobufs +func (all *ForgeConfigs) selectAllForgeConfig() []*ForgeConfig { + forgeConfigsMu.RLock() + defer forgeConfigsMu.RUnlock() + + // Create a new slice to hold pointers to each ForgeConfig + var aStuff []*ForgeConfig + aStuff = make([]*ForgeConfig, len(all.ForgeConfigs)) + for i, p := range all.ForgeConfigs { + aStuff[i] = p // Copy pointers for safe iteration + } + + return aStuff +}