getting to more standard code. still don't know how much this will work

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 10:30:58 -06:00
parent 6c425fc05e
commit 6e29eb7862
1 changed files with 31 additions and 31 deletions

62
main.go
View File

@ -6,31 +6,31 @@ import (
"os" "os"
) )
var names map[string]string
func main() { func main() {
f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600) 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" sortmap := make(map[string]string)
names["sortBy"] = "ByPath" sortmap["package"] = "testautogen"
names["sortKey"] = "Refname" sortmap["lock"] = "gitTagslock"
sortmap["Base"] = "GitTag"
sortmap["Bases"] = "GitTags"
header(f, "testautogen") sortmap["base"] = "gitTag"
syncLock(f, "gitTagslock") sortmap["sortBy"] = "ByPath"
iterTop(f, "GitTag") sortmap["sortKey"] = "Refname"
iterNext(f)
iterSort(f) header(f, sortmap)
iterAppend(f) syncLock(f, sortmap)
iterEnd(f) iterTop(f, sortmap)
iterNext(f, sortmap)
iterSort(f, sortmap)
iterAppend(f, sortmap)
iterEnd(f, sortmap)
} }
func header(w io.Writer, name string) { func header(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "package "+name) fmt.Fprintln(w, "package "+names["package"])
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "// this is becoming a standard format") fmt.Fprintln(w, "// this is becoming a standard format")
fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?") fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?")
@ -44,23 +44,23 @@ func header(w io.Writer, name string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func syncLock(w io.Writer, name string) { func syncLock(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// bad global lock until I figure out some other plan") fmt.Fprintln(w, "// bad global lock until I figure out some other plan")
fmt.Fprintln(w, "var "+name+" sync.RWMutex") fmt.Fprintln(w, "var "+names["lock"]+" sync.RWMutex")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func iterTop(w io.Writer, name string) { func iterTop(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "type "+name+"Iterator struct {") fmt.Fprintln(w, "type "+names["Base"]+"Iterator struct {")
fmt.Fprintln(w, " sync.RWMutex") fmt.Fprintln(w, " sync.RWMutex")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " packs []*"+name) fmt.Fprintln(w, " packs []*"+names["Base"])
fmt.Fprintln(w, " index int") fmt.Fprintln(w, " index int")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "// New"+name+"Iterator initializes a new iterator.") fmt.Fprintln(w, "// New"+names["Base"]+"Iterator initializes a new iterator.")
fmt.Fprintln(w, "func New"+name+"Iterator(packs []*"+name+") *"+name+"Iterator {") fmt.Fprintln(w, "func New"+names["Base"]+"Iterator(packs []*"+names["Base"]+") *"+names["Base"]+"Iterator {")
fmt.Fprintln(w, " return &"+name+"Iterator{packs: packs}") fmt.Fprintln(w, " return &"+names["Base"]+"Iterator{packs: packs}")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
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, "// Scan moves to the next element and returns false if there are no more packs.")
@ -70,7 +70,7 @@ func iterTop(w io.Writer, name string) {
fmt.Fprintln(w, "// d := iterator.Next(") fmt.Fprintln(w, "// d := iterator.Next(")
fmt.Fprintln(w, "// fmt.Println(\"found UUID:\", d.Uuid") fmt.Fprintln(w, "// fmt.Println(\"found UUID:\", d.Uuid")
fmt.Fprintln(w, "// }") fmt.Fprintln(w, "// }")
fmt.Fprintln(w, "func (it *"+name+"Iterator) Scan() bool {") fmt.Fprintln(w, "func (it *"+names["Base"]+"Iterator) Scan() bool {")
fmt.Fprintln(w, " if it.index >= len(it.packs) {") fmt.Fprintln(w, " if it.index >= len(it.packs) {")
fmt.Fprintln(w, " return false") fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }") fmt.Fprintln(w, " }")
@ -80,7 +80,7 @@ func iterTop(w io.Writer, name string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func iterNext(w io.Writer) { func iterNext(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// Next() returns the next thing in the array") 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, "func (it *" + names["Base"] + "Iterator) Next() *" + names["Base"] + " {")
fmt.Fprintln(w, " if it.packs[it.index-1] == nil {") fmt.Fprintln(w, " if it.packs[it.index-1] == nil {")
@ -97,7 +97,7 @@ func iterNext(w io.Writer) {
} }
func iterSort(w io.Writer) { func iterSort(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") All() *" + names["Base"] + "Iterator {") fmt.Fprintln(w, "func (all *" + names["Bases"] + ") All() *" + names["Base"] + "Iterator {")
fmt.Fprintln(w, " " + names["base"] + "Pointers := all.selectAll" + names["Base"] + "()") fmt.Fprintln(w, " " + names["base"] + "Pointers := all.selectAll" + names["Base"] + "()")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
@ -123,7 +123,7 @@ func iterSort(w io.Writer) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func iterEnd(w io.Writer) { func iterEnd(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "type " + names["Base"] + "" + names["sortBy"] + " []*" + names["Base"] + "") fmt.Fprintln(w, "type " + names["Base"] + "" + names["sortBy"] + " []*" + names["Base"] + "")
fmt.Fprintln(w, "") 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"] + ") Len() int { return len(a) }")
@ -147,7 +147,7 @@ func iterEnd(w io.Writer) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func iterAppend(w io.Writer) { func iterAppend(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "// enforces no duplicate Refname names") fmt.Fprintln(w, "// enforces no duplicate Refname names")
fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Append(newP *" + names["Base"] + ") bool {") fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Append(newP *" + names["Base"] + ") bool {")
fmt.Fprintln(w, " " + names["lock"] + ".Lock()") fmt.Fprintln(w, " " + names["lock"] + ".Lock()")