Notes added by 'git notes append'
This commit is contained in:
parent
1566912520
commit
a91086901b
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue