autogenpb/main.go

59 lines
1.7 KiB
Go

package main
import (
"fmt"
"io"
"os"
)
func main() {
f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
header(f, "testautogen")
syncLock(f, "gitTagslock")
iterTop(f, "GitTag")
}
func header(w io.Writer, name string) {
fmt.Fprintln(w, "package "+name)
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, "")
}
func syncLock(w io.Writer, name string) {
fmt.Fprintln(w, "// bad global lock until I figure out some other plan")
fmt.Fprintln(w, "var "+name+" sync.RWMutex")
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, "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, "}")
}