2024-11-29 08:30:19 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2024-11-29 11:08:11 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alexflint/go-arg"
|
|
|
|
"go.wit.com/lib/gui/shell"
|
|
|
|
"go.wit.com/log"
|
2024-11-29 08:30:19 -06:00
|
|
|
)
|
|
|
|
|
2024-11-29 11:08:11 -06:00
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
2024-11-29 10:20:06 -06:00
|
|
|
|
2024-11-29 08:30:19 -06:00
|
|
|
func main() {
|
2024-11-29 11:08:11 -06:00
|
|
|
pp := arg.MustParse(&argv)
|
|
|
|
|
2024-11-29 11:15:40 -06:00
|
|
|
// you need a proto file
|
|
|
|
if argv.Proto == "" {
|
|
|
|
log.Info("you must provide --proto <filename>")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !shell.Exists(argv.Proto) {
|
|
|
|
log.Info("protobuf", argv.Proto, "is missing")
|
2024-11-29 11:32:27 -06:00
|
|
|
if ! argv.DryRun {
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2024-11-29 11:15:40 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 11:21:56 -06:00
|
|
|
if !strings.HasSuffix(argv.Proto, ".proto") {
|
2024-11-29 11:15:40 -06:00
|
|
|
log.Info("protobuf", argv.Proto, "must end in .proto")
|
|
|
|
os.Exit(-1)
|
2024-11-29 11:08:11 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 11:21:56 -06:00
|
|
|
// you need --upbase and --lobase
|
|
|
|
if argv.UpBase == "" {
|
|
|
|
pp.WriteHelp(os.Stdout)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
2024-11-29 11:08:11 -06:00
|
|
|
cmd := []string{"go", "list", "-f", "'{{.Name}}'"}
|
|
|
|
result := shell.Run(cmd)
|
|
|
|
|
|
|
|
packageName := strings.Join(result.Stdout, "\n")
|
|
|
|
packageName = strings.TrimSpace(packageName)
|
|
|
|
packageName = strings.Trim(packageName, "'")
|
|
|
|
log.Info("packageName == ", packageName)
|
|
|
|
|
2024-11-29 11:15:40 -06:00
|
|
|
protobase := strings.TrimSuffix(argv.Proto, ".proto")
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
sortmap := make(map[string]string)
|
2024-11-29 11:08:11 -06:00
|
|
|
sortmap["package"] = packageName
|
2024-11-29 11:21:56 -06:00
|
|
|
sortmap["base"] = argv.LoBase
|
2024-11-29 11:08:11 -06:00
|
|
|
sortmap["lock"] = sortmap["base"] + "slock"
|
2024-11-29 11:21:56 -06:00
|
|
|
sortmap["Base"] = argv.UpBase
|
2024-11-29 11:08:11 -06:00
|
|
|
sortmap["Bases"] = sortmap["Base"] + "s"
|
2024-11-29 10:20:06 -06:00
|
|
|
|
2024-11-29 11:32:27 -06:00
|
|
|
sortparts := strings.Split(argv.Sort[0], ",")
|
|
|
|
sortmap["sortBy"] = sortparts[0]
|
|
|
|
sortmap["sortKey"] = sortparts[1]
|
|
|
|
|
|
|
|
if argv.DryRun {
|
|
|
|
for k, v := range sortmap {
|
|
|
|
log.Info(k, "=", v)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ := os.OpenFile(protobase+".sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
|
2024-11-29 10:30:58 -06:00
|
|
|
|
|
|
|
header(f, sortmap)
|
|
|
|
syncLock(f, sortmap)
|
|
|
|
iterTop(f, sortmap)
|
|
|
|
iterNext(f, sortmap)
|
|
|
|
iterSort(f, sortmap)
|
|
|
|
iterAppend(f, sortmap)
|
|
|
|
iterEnd(f, sortmap)
|
2024-11-29 08:30:19 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
func header(w io.Writer, names map[string]string) {
|
|
|
|
fmt.Fprintln(w, "package "+names["package"])
|
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
|
|
|
}
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
func syncLock(w io.Writer, names map[string]string) {
|
2024-11-29 08:30:19 -06:00
|
|
|
fmt.Fprintln(w, "// bad global lock until I figure out some other plan")
|
2024-11-29 10:30:58 -06:00
|
|
|
fmt.Fprintln(w, "var "+names["lock"]+" sync.RWMutex")
|
2024-11-29 08:30:19 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
2024-11-29 08:54:49 -06:00
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
func iterTop(w io.Writer, names map[string]string) {
|
|
|
|
fmt.Fprintln(w, "type "+names["Base"]+"Iterator struct {")
|
2024-11-29 08:54:49 -06:00
|
|
|
fmt.Fprintln(w, " sync.RWMutex")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 10:30:58 -06:00
|
|
|
fmt.Fprintln(w, " packs []*"+names["Base"])
|
2024-11-29 08:54:49 -06:00
|
|
|
fmt.Fprintln(w, " index int")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 10:30:58 -06:00
|
|
|
fmt.Fprintln(w, "// New"+names["Base"]+"Iterator initializes a new iterator.")
|
|
|
|
fmt.Fprintln(w, "func New"+names["Base"]+"Iterator(packs []*"+names["Base"]+") *"+names["Base"]+"Iterator {")
|
|
|
|
fmt.Fprintln(w, " return &"+names["Base"]+"Iterator{packs: packs}")
|
2024-11-29 08:54:49 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more packs.")
|
2024-11-29 09:36:54 -06:00
|
|
|
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, "// }")
|
2024-11-29 10:30:58 -06:00
|
|
|
fmt.Fprintln(w, "func (it *"+names["Base"]+"Iterator) Scan() bool {")
|
2024-11-29 08:54:49 -06:00
|
|
|
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, "}")
|
2024-11-29 09:36:54 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
func iterNext(w io.Writer, names map[string]string) {
|
2024-11-29 09:36:54 -06:00
|
|
|
fmt.Fprintln(w, "// Next() returns the next thing in the array")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, "func (it *"+names["Base"]+"Iterator) Next() *"+names["Base"]+" {")
|
2024-11-29 09:36:54 -06:00
|
|
|
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, "")
|
|
|
|
}
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
func iterSort(w io.Writer, names map[string]string) {
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, "func (all *"+names["Bases"]+") All() *"+names["Base"]+"Iterator {")
|
|
|
|
fmt.Fprintln(w, " "+names["base"]+"Pointers := all.selectAll"+names["Base"]+"()")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator("+names["base"]+"Pointers)")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, " return iterator")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Sort"+names["sortBy"]+"() *"+names["Base"]+"Iterator {")
|
|
|
|
fmt.Fprintln(w, " packs := all.selectAll"+names["Base"]+"()")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(packs))")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(packs)")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, " return iterator")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Len() int {")
|
|
|
|
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, " return len(all."+names["Bases"]+")")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 09:36:54 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
func iterEnd(w io.Writer, names map[string]string) {
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, "type "+names["Base"]+""+names["sortBy"]+" []*"+names["Base"]+"")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
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] }")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
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()")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
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"]+" {")
|
2024-11-29 10:20:06 -06:00
|
|
|
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, "")
|
2024-11-29 09:36:54 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 10:30:58 -06:00
|
|
|
func iterAppend(w io.Writer, names map[string]string) {
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "// enforces no duplicate Refname names")
|
2024-11-29 11:08:11 -06:00
|
|
|
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()")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, " if p.Refname == newP.Refname {")
|
|
|
|
fmt.Fprintln(w, " return false")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 11:08:11 -06:00
|
|
|
fmt.Fprintln(w, " all."+names["Bases"]+" = append(all."+names["Bases"]+", newP)")
|
2024-11-29 10:20:06 -06:00
|
|
|
fmt.Fprintln(w, " return true")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 08:54:49 -06:00
|
|
|
}
|