From aaa6f3cb755bb4e06fa0ce18e9532e4ba8e311ef Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 Jan 2025 04:27:32 -0600 Subject: [PATCH] Notes added by 'git notes append' --- 2b050a04ed00d3ca14bc27a548dea8c8d1a90764 | 97 ++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/2b050a04ed00d3ca14bc27a548dea8c8d1a90764 b/2b050a04ed00d3ca14bc27a548dea8c8d1a90764 index c310ca2..2830b92 100644 --- a/2b050a04ed00d3ca14bc27a548dea8c8d1a90764 +++ b/2b050a04ed00d3ca14bc27a548dea8c8d1a90764 @@ -640,3 +640,100 @@ func file_file_proto_init() { } // `autogen:file.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_0416 +// 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 main + +import ( + "fmt" + "sync" +) + +// bad global lock until modifying the .pb.go file is tested +// sync.RWMutex or sync.Mutex? +var fileMu sync.RWMutex + +type FileIterator struct { + sync.RWMutex + + things []*File + index int +} + +// NewFileIterator initializes a new iterator. +func NewFileIterator(things []*File) *FileIterator { + return &FileIterator{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 *FileIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *FileIterator) Next() *File { + 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 *Files) All() *FileIterator { + FilePointers := all.selectAllFile() + + iterator := NewFileIterator(FilePointers) + return iterator +} + +func (all *Files) Len() int { + all.Lock.RLock() + defer all.Lock.RUnlock() + + return len(all.Files) +} + +// safely returns a slice of pointers to the File protobufs +func (all *Files) selectAllFile() []*File { + all.Lock.RLock() + defer all.Lock.RUnlock() + + // Create a new slice to hold pointers to each File + var tmp []*File + tmp = make([]*File, len(all.Files)) + for i, p := range all.Files { + 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 *Files) Append(newP *File) bool { + all.Lock.RLock() + defer all.Lock.RUnlock() + + all.Files = append(all.Files, newP) + return true +}