autogenpb/sort.go

336 lines
12 KiB
Go

package main
import (
"fmt"
"io"
"os"
"strings"
"go.wit.com/log"
)
// passes in the protobuf file protobuf
func (pb *Files) makeSortfile(pf *File) {
f, _ := os.OpenFile(pf.Filebase+".sort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
// header(f, pf)
// pf.iterTop(f, sortmap["base"])
// iterNext(f, sortmap)
iterAppend(f, sortmap) // Append() enforce no unique keys
iterSortAll(f, sortmap)
if argv.Append != "" {
sortmap["append"] = string(argv.Append)
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
}
for _, s := range uniqueKeys {
// log.Info("found unique key in .proto", s)
sortmap["sortBy"] = s
sortmap["sortKey"] = s
iterSortBy(f, sortmap)
sortmap["append"] = sortmap["sortKey"]
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
iterDelete(f, sortmap)
iterReplace(f, sortmap)
iterFind(f, sortmap)
}
for _, s := range argv.Sort {
sortparts := strings.Split(s, ",")
sortmap["sortBy"] = sortparts[0]
sortmap["sortKey"] = sortparts[1]
iterSortBy(f, sortmap)
sortmap["append"] = sortmap["sortKey"]
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
iterDelete(f, sortmap)
iterReplace(f, sortmap)
iterFind(f, sortmap)
}
iterEnd(f, sortmap)
}
func (pf *File) syncLock(w io.Writer) {
if pf.Bases == nil {
log.Info("BASES == nil in syncLock")
return
}
var LOCK string = pf.Bases.Lockname
fmt.Fprintln(w, "// bad global lock until modifying the .pb.go file is tested")
fmt.Fprintln(w, "// sync.RWMutex or sync.Mutex?")
fmt.Fprintln(w, "var "+LOCK+" sync.RWMutex")
fmt.Fprintln(w, "")
}
func (msg *MsgName) iterTop(w io.Writer) {
var BASE string = msg.Name
fmt.Fprintln(w, "type "+BASE+"Iterator struct {")
fmt.Fprintln(w, " sync.RWMutex")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " things []*"+BASE)
fmt.Fprintln(w, " index int")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// New"+BASE+"Iterator initializes a new iterator.")
fmt.Fprintln(w, "func New"+BASE+"Iterator(things []*"+BASE+") *"+BASE+"Iterator {")
fmt.Fprintln(w, " return &"+BASE+"Iterator{things: things}")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more things.")
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 *"+BASE+"Iterator) Scan() bool {")
fmt.Fprintln(w, " if it.index >= len(it.things) {")
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 (msg *MsgName) iterNext(w io.Writer) {
var BASE string = msg.Name
fmt.Fprintln(w, "// Next() returns the next thing in the array")
fmt.Fprintln(w, "func (it *"+BASE+"Iterator) Next() *"+BASE+" {")
fmt.Fprintln(w, " if it.things[it.index-1] == nil {")
fmt.Fprintln(w, " for i, d := range it.things {")
fmt.Fprintln(w, " fmt.Println(\"i =\", i, d)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // fmt.Println(\"protobuf autogenpb sort error len =\", len(it.things))")
fmt.Fprintln(w, " // fmt.Println(\"protobuf autogenpb sort error next == nil\", it.index, it.index-1)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return it.things[it.index-1]")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterSortAll(w io.Writer, names map[string]string) {
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"]+") Len() int {")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
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 iterSortBy(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") SortBy"+names["sortBy"]+"() *"+names["Base"]+"Iterator {")
fmt.Fprintln(w, " things := all.selectAll"+names["Base"]+"()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(things))")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(things)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
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, "")
}
func iterEnd(w io.Writer, names map[string]string) {
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"]+" {")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
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, "}")
}
func iterAppend(w io.Writer, names map[string]string) {
}
func (pf *File) appendUnique(w io.Writer) {
var MSG string = pf.Bases.Name
var BASE string = pf.Base.Name
var LOCK string = pf.Bases.Lockname
if argv.Mutex {
// use the mutex lock from the modified protoc.pb.go file
LOCK = "all.Lock"
} else {
LOCK = pf.Bases.Lockname
}
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique(newP *"+BASE+") bool {")
fmt.Fprintln(w, " "+LOCK+".RLock()")
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range all."+MSG+" {")
for _, KEY := range pf.Base.Unique {
fmt.Fprintln(w, " if p."+KEY+" == newP."+KEY+" {")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
}
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
for _, KEY := range pf.Base.Unique {
fmt.Fprintln(w, "// enforces "+BASE+" is unique")
fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique"+KEY+"(newP *"+BASE+") bool {")
fmt.Fprintln(w, " "+LOCK+".RLock()")
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range all."+MSG+" {")
fmt.Fprintln(w, " if p."+KEY+" == newP."+KEY+" {")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
}
func iterReplace(w io.Writer, names map[string]string) {
if names["append"] == "" {
// can't continue without a key field
}
fmt.Fprintln(w, "// enforces "+names["append"]+" is unique")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") Replace"+names["append"]+"(newP *"+names["Base"]+") bool { // todo: make unique name here")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if p."+names["append"]+" == newP."+names["append"]+" {")
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, "")
}
func iterDelete(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) bool {")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
fmt.Fprintln(w, " all."+names["Bases"]+"[i] = all."+names["Bases"]+"[len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " all."+names["Bases"]+" = all."+names["Bases"]+"[:len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
// this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct
func iterDeleteWithCopy(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *"+names["Bases"]+") DeleteBy"+names["append"]+"(s string) *"+names["Base"]+" {")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " var newr "+names["Base"])
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
fmt.Fprintln(w, " newr = *all."+names["Bases"]+"[i]")
fmt.Fprintln(w, " all."+names["Bases"]+"[i] = all."+names["Bases"]+"[len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " all."+names["Bases"]+" = all."+names["Bases"]+"[:len(all."+names["Bases"]+")-1]")
fmt.Fprintln(w, " return &newr")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func iterFind(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// find a dependancy by the go path")
fmt.Fprintln(w, "func (all *"+names["Bases"]+") FindBy"+names["append"]+"(s string) *"+names["Base"]+" {")
fmt.Fprintln(w, " if all == nil {")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
if sortmap["lock"] == "all" {
fmt.Fprintln(w, " "+names["lock"]+".Lock.RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".Lock.RUnlock()")
} else {
fmt.Fprintln(w, " "+names["lock"]+".RLock()")
fmt.Fprintln(w, " defer "+names["lock"]+".RUnlock()")
}
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for i, _ := range all."+names["Bases"]+" {")
fmt.Fprintln(w, " if all."+names["Bases"]+"[i]."+names["append"]+" == s {")
fmt.Fprintln(w, " return all."+names["Bases"]+"[i]")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}