autogenpb/main.go

167 lines
6.2 KiB
Go
Raw Normal View History

2024-11-29 08:30:19 -06:00
package main
import (
"fmt"
"io"
"os"
)
var names map[string]string
2024-11-29 08:30:19 -06:00
func main() {
f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
names = make(map[string]string)
names["package"] = "testautogen"
names["lock"] = "gitTagslock"
names["Base"] = "GitTag"
names["Bases"] = "GitTags"
names["base"] = "gitTag"
names["sortBy"] = "ByPath"
names["sortKey"] = "Refname"
header(f, "testautogen")
syncLock(f, "gitTagslock")
iterTop(f, "GitTag")
iterNext(f)
iterSort(f)
iterAppend(f)
iterEnd(f)
2024-11-29 08:30:19 -06:00
}
func header(w io.Writer, name string) {
fmt.Fprintln(w, "package "+name)
2024-11-29 08:30:19 -06:00
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// this is becoming a standard format")
fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "import (")
fmt.Fprintln(w, " \"fmt\"")
fmt.Fprintln(w, " \"os\"")
fmt.Fprintln(w, " \"sort\"")
fmt.Fprintln(w, " \"sync\"")
fmt.Fprintln(w, ")")
fmt.Fprintln(w, "")
2024-11-29 08:33:47 -06:00
}
func syncLock(w io.Writer, name string) {
2024-11-29 08:30:19 -06:00
fmt.Fprintln(w, "// bad global lock until I figure out some other plan")
fmt.Fprintln(w, "var "+name+" sync.RWMutex")
2024-11-29 08:30:19 -06:00
fmt.Fprintln(w, "")
}
func iterTop(w io.Writer, name string) {
fmt.Fprintln(w, "type "+name+"Iterator struct {")
fmt.Fprintln(w, " sync.RWMutex")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " packs []*"+name)
fmt.Fprintln(w, " index int")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// New"+name+"Iterator initializes a new iterator.")
fmt.Fprintln(w, "func New"+name+"Iterator(packs []*"+name+") *"+name+"Iterator {")
fmt.Fprintln(w, " return &"+name+"Iterator{packs: packs}")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more packs.")
fmt.Fprintln(w, "// Use Scan() in a loop, similar to a while loop")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// for iterator.Scan() ")
fmt.Fprintln(w, "// d := iterator.Next(")
fmt.Fprintln(w, "// fmt.Println(\"found UUID:\", d.Uuid")
fmt.Fprintln(w, "// }")
fmt.Fprintln(w, "func (it *"+name+"Iterator) Scan() bool {")
fmt.Fprintln(w, " if it.index >= len(it.packs) {")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " it.index++")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterNext(w io.Writer) {
fmt.Fprintln(w, "// Next() returns the next thing in the array")
fmt.Fprintln(w, "func (it *" + names["Base"] + "Iterator) Next() *" + names["Base"] + " {")
fmt.Fprintln(w, " if it.packs[it.index-1] == nil {")
fmt.Fprintln(w, " for i, d := range it.packs {")
fmt.Fprintln(w, " fmt.Println(\"i =\", i, d)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " fmt.Println(\"protobuf autogenpb sort error len =\", len(it.packs))")
fmt.Fprintln(w, " fmt.Println(\"protobuf autogenpb sort error next == nil\", it.index, it.index-1)")
fmt.Fprintln(w, " os.Exit(-1)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return it.packs[it.index-1]")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterSort(w io.Writer) {
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") All() *" + names["Base"] + "Iterator {")
fmt.Fprintln(w, " " + names["base"] + "Pointers := all.selectAll" + names["Base"] + "()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " iterator := New" + names["Base"] + "Iterator(" + names["base"] + "Pointers)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Sort" + names["sortBy"] + "() *" + names["Base"] + "Iterator {")
fmt.Fprintln(w, " packs := all.selectAll" + names["Base"] + "()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " sort.Sort(" + names["Base"] + "" + names["sortBy"] + "(packs))")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " iterator := New" + names["Base"] + "Iterator(packs)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Len() int {")
fmt.Fprintln(w, " " + names["lock"] + ".RLock()")
fmt.Fprintln(w, " defer " + names["lock"] + ".RUnlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " return len(all." + names["Bases"] + ")")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterEnd(w io.Writer) {
fmt.Fprintln(w, "type " + names["Base"] + "" + names["sortBy"] + " []*" + names["Base"] + "")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Len() int { return len(a) }")
fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Less(i, j int) bool { return a[i]." + names["sortKey"] + " < a[j]." + names["sortKey"] + " }")
fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// safely returns a slice of pointers to the " + names["Base"] + " protobufs")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") selectAll" + names["Base"] + "() []*" + names["Base"] + " {")
fmt.Fprintln(w, " " + names["lock"] + ".RLock()")
fmt.Fprintln(w, " defer " + names["lock"] + ".RUnlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " // Create a new slice to hold pointers to each " + names["Base"] + "")
fmt.Fprintln(w, " var aStuff []*" + names["Base"] + "")
fmt.Fprintln(w, " aStuff = make([]*" + names["Base"] + ", len(all." + names["Bases"] + "))")
fmt.Fprintln(w, " for i, p := range all." + names["Bases"] + " {")
fmt.Fprintln(w, " aStuff[i] = p // Copy pointers for safe iteration")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " return aStuff")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterAppend(w io.Writer) {
fmt.Fprintln(w, "// enforces no duplicate Refname names")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Append(newP *" + names["Base"] + ") bool {")
fmt.Fprintln(w, " " + names["lock"] + ".Lock()")
fmt.Fprintln(w, " defer " + names["lock"] + ".Unlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range all." + names["Bases"] + " {")
fmt.Fprintln(w, " if p.Refname == newP.Refname {")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " all." + names["Bases"] + " = append(all." + names["Bases"] + ", newP)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}