From 99410110fe92a3a3f33818d3b133ed224ab7be3f Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 9 Jan 2025 21:39:47 -0600 Subject: [PATCH] Notes added by 'git notes append' --- 40c340c6267aa53164804aad6ed988183fc42d3f | 156 +++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/40c340c6267aa53164804aad6ed988183fc42d3f b/40c340c6267aa53164804aad6ed988183fc42d3f index 5ec4f61..7718b6a 100644 --- a/40c340c6267aa53164804aad6ed988183fc42d3f +++ b/40c340c6267aa53164804aad6ed988183fc42d3f @@ -98,3 +98,159 @@ func (v *ForgeConfigs) Unmarshal(data []byte) error { } // `autogen:forgeConfig.newsort.pb.go` + +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.37-30-g805d3cd 2025.01.09_2100 +// 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 forgepb + +import ( + "fmt" + "sort" + "sync" +) + +// bad global lock until modifying the .pb.go file is tested +// sync.RWMutex or sync.Mutex? +var forgeConfigMu sync.RWMutex + +type ForgeConfigIterator struct { + sync.RWMutex + + things []*ForgeConfig + index int +} + +// NewForgeConfigIterator initializes a new iterator. +func NewForgeConfigIterator(things []*ForgeConfig) *ForgeConfigIterator { + return &ForgeConfigIterator{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 *ForgeConfigIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *ForgeConfigIterator) Next() *ForgeConfig { + if it.things[it.index-1] == nil { + for i, d := range it.things { + fmt.Println("i =", i, d) + } + } + return it.things[it.index-1] +} + +// safely returns a slice of pointers to the ForgeConfig protobufs +func (all *ForgeConfigs) selectAllForgeConfig() []*ForgeConfig { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + // Create a new slice to hold pointers to each ForgeConfig + var tmp []*ForgeConfig + tmp = make([]*ForgeConfig, len(all.ForgeConfigs)) + for i, p := range all.ForgeConfigs { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// just a simple Append() with no checking (but still uses the mutex lock) +func (all *ForgeConfigs) Append(newP *ForgeConfig) bool { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + all.ForgeConfigs = append(all.ForgeConfigs, newP) + return true +} + +// enforces ForgeConfig is unique +func (all *ForgeConfigs) AppendUniqueGoPath(newP *ForgeConfig) bool { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + for _, p := range all.ForgeConfigs { + if p.GoPath == newP.GoPath { + return false + } + } + + all.ForgeConfigs = append(all.ForgeConfigs, newP) + return true +} + +// enforces ForgeConfig is unique +func (all *ForgeConfigs) AppendUnique(newP *ForgeConfig) bool { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + for _, p := range all.ForgeConfigs { + if p.GoPath == newP.GoPath { + return false + } + } + + all.ForgeConfigs = append(all.ForgeConfigs, newP) + return true +} + +func (all *ForgeConfigs) SortByGoPath() *ForgeConfigIterator { + things := all.selectAllForgeConfig() + + sort.Sort(ForgeConfigGoPath(things)) + + iterator := NewForgeConfigIterator(things) + 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] } + +func (all *ForgeConfigs) All() *ForgeConfigIterator { + ForgeConfigPointers := all.selectAllForgeConfig() + + iterator := NewForgeConfigIterator(ForgeConfigPointers) + return iterator +} + +func (all *ForgeConfigs) Len() int { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + return len(all.ForgeConfigs) +} + +func (all *ForgeConfigs) DeleteByGoPath(s string) bool { + forgeConfigMu.RLock() + defer forgeConfigMu.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 +}