From d7f20c19213e1b642c6484a0a37b0dd14793d4cd Mon Sep 17 00:00:00 2001 From: Jeff Carr <jcarr@wit.com> Date: Sat, 15 Feb 2025 17:25:57 -0600 Subject: [PATCH] Notes added by 'git notes append' --- 9a87c93bad7f3bc26586ef391ce90213742c8138 | 234 +++++++++++++++++++++++ 1 file changed, 234 insertions(+) diff --git a/9a87c93bad7f3bc26586ef391ce90213742c8138 b/9a87c93bad7f3bc26586ef391ce90213742c8138 index 995a30b..3fc8ea3 100644 --- a/9a87c93bad7f3bc26586ef391ce90213742c8138 +++ b/9a87c93bad7f3bc26586ef391ce90213742c8138 @@ -524,3 +524,237 @@ func file_forgeConfig_proto_init() { } // `autogen:forgeConfig.sort.pb.go` + +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// This file was autogenerated with autogenpb v0.0.54 2025-02-15_02:56:28_UTC +// 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" +) + +// a simple global lock +var forgeConfigMu sync.RWMutex + +func (x *ForgeConfigs) fixUuid() { + if x == nil { + return + } + if x.Uuid == "1941cd4f-1cfd-4bf6-aa75-c2c391907e81" { + return + } + x.Uuid = "1941cd4f-1cfd-4bf6-aa75-c2c391907e81" + x.Version = "v0.0.47 go.wit.com/lib/protobuf/forgepb" +} + +func NewForgeConfigs() *ForgeConfigs { + x := new(ForgeConfigs) + x.Uuid = "1941cd4f-1cfd-4bf6-aa75-c2c391907e81" + x.Version = "v0.0.47 go.wit.com/lib/protobuf/forgepb" + return x +} + +// START SORT + +// DEFINE THE ForgeConfigs ITERATOR. +// itializes a new iterator. +func newForgeConfigsIterator(things []*ForgeConfigs) *ForgeConfigsIterator { + return &ForgeConfigsIterator{things: things} +} + +type ForgeConfigsIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*ForgeConfigs + index int +} + +func (it *ForgeConfigsIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *ForgeConfigsIterator) Next() *ForgeConfigs { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in ForgeConfigsIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// DEFINE THE ForgeConfig ITERATOR. +// itializes a new iterator. +func newForgeConfigIterator(things []*ForgeConfig) *ForgeConfigIterator { + return &ForgeConfigIterator{things: things} +} + +type ForgeConfigIterator struct { + sync.RWMutex // this isn't getting used properly yet? + + things []*ForgeConfig + index int +} + +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 { + fmt.Println("Next() error in ForgeConfigIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// sort struct by GoPath +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] } + +// safely returns a slice of pointers to the FRUIT protobufs +func (x *ForgeConfigs) allForgeConfigs() []*ForgeConfig { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + // Create a new slice to hold pointers to each FRUIT + var tmp []*ForgeConfig + tmp = make([]*ForgeConfig, len(x.ForgeConfigs)) + for i, p := range x.ForgeConfigs { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +// safely returns a slice of pointers to the ForgeConfig protobufs +func (x *ForgeConfigs) selectAllForgeConfigs() []*ForgeConfig { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + // Create a new slice to hold pointers to each ForgeConfig + var tmp []*ForgeConfig + tmp = make([]*ForgeConfig, len(x.ForgeConfigs)) + for i, p := range x.ForgeConfigs { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} +func (x *ForgeConfigs) SortByGoPath() *ForgeConfigIterator { + // copy the pointers as fast as possible. + things := x.selectAllForgeConfigs() + + // todo: try slices.SortFunc() instead to see what happens + sort.Sort(ForgeConfigGoPath(things)) + // slices.SortFunc(things, func(a, b *ForgeConfigs) bool { + // return a.GoPath < b.GoPath // Sort by ??. let the compiler work it out?? + // }) + return newForgeConfigIterator(things) +} + +// END SORT + +func (x *ForgeConfigs) Len() int { + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + return len(x.ForgeConfigs) +} + +// just a simple Append() shortcut (but still uses the mutex lock) +func (x *ForgeConfigs) Append(y *ForgeConfig) { + forgeConfigMu.Lock() + defer forgeConfigMu.Unlock() + + x.ForgeConfigs = append(x.ForgeConfigs, y) +} + +func (x *ForgeConfigs) All() *ForgeConfigIterator { + ForgeConfigPointers := x.selectAllForgeConfigs() + + iterator := newForgeConfigIterator(ForgeConfigPointers) + return iterator +} + +func (x *ForgeConfigs) Delete(y *ForgeConfig) bool { + forgeConfigMu.Lock() + defer forgeConfigMu.Unlock() + + for i, _ := range x.ForgeConfigs { + if x.ForgeConfigs[i] == y { + x.ForgeConfigs[i] = x.ForgeConfigs[len(x.ForgeConfigs)-1] + x.ForgeConfigs = x.ForgeConfigs[:len(x.ForgeConfigs)-1] + return true + } + } + return false +} + +// lookup a ForgeConfigs by the GoPath +func (x *ForgeConfigs) FindByGoPath(s string) *ForgeConfig { + if x == nil { + return nil + } + + forgeConfigMu.RLock() + defer forgeConfigMu.RUnlock() + + for i, _ := range x.ForgeConfigs { + if x.ForgeConfigs[i].GoPath == s { + return x.ForgeConfigs[i] + } + } + return nil +} + +func (x *ForgeConfigs) DeleteByGoPath(s string) bool { + forgeConfigMu.Lock() + defer forgeConfigMu.Unlock() + + for i, _ := range x.ForgeConfigs { + if x.ForgeConfigs[i].GoPath == s { + x.ForgeConfigs[i] = x.ForgeConfigs[len(x.ForgeConfigs)-1] + x.ForgeConfigs = x.ForgeConfigs[:len(x.ForgeConfigs)-1] + return true + } + } + return false +} + +func (x *ForgeConfigs) AppendByGoPath(y *ForgeConfig) bool { + forgeConfigMu.Lock() + defer forgeConfigMu.Unlock() + + for _, p := range x.ForgeConfigs { + if p.GoPath == y.GoPath { + return false + } + } + + x.ForgeConfigs = append(x.ForgeConfigs, y) + return true +}