diff options
| author | Jeff Carr <[email protected]> | 2025-01-09 17:15:53 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-01-09 17:15:53 -0600 |
| commit | 948af64ea2b6e586f4d152fcfc7ed168dae4c919 (patch) | |
| tree | 8ac36eed2d783ee57eeb8f45c84adc75e8f7c8d4 /sort.go | |
| parent | 7c10ff958e57d02d13287c4cdbe0aca3781e1600 (diff) | |
example doesn't seem to work. why? notsure
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'sort.go')
| -rw-r--r-- | sort.go | 147 |
1 files changed, 46 insertions, 101 deletions
@@ -3,65 +3,9 @@ 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") @@ -111,70 +55,71 @@ func (msg *MsgName) iterNext(w io.Writer) { 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"]+"()") +func (pf *File) iterAll(w io.Writer) { + var BASES string = pf.Bases.Name + var BASE string = pf.Base.Name + var LOCK string = pf.Bases.Lockname + + fmt.Fprintln(w, "func (all *"+BASES+") All() *"+BASE+"Iterator {") + fmt.Fprintln(w, " "+BASE+"Pointers := all.selectAll"+BASE+"()") fmt.Fprintln(w, "") - fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator("+names["base"]+"Pointers)") + fmt.Fprintln(w, " iterator := New"+BASE+"Iterator("+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, "func (all *"+BASES+") Len() int {") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") fmt.Fprintln(w, "") - fmt.Fprintln(w, " return len(all."+names["Bases"]+")") + fmt.Fprintln(w, " return len(all."+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, "") +func (pf *File) iterSortBy(w io.Writer) { + var BASES string = pf.Bases.Name + var BASE string = pf.Base.Name - 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, "") -} + for _, SORT := range pf.Base.Sort { + fmt.Fprintln(w, "func (all *"+BASES+") SortBy"+SORT+"() *"+BASE+"Iterator {") + fmt.Fprintln(w, " things := all.selectAll"+BASE+"()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " sort.Sort("+BASE+""+SORT+"(things))") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " iterator := New"+BASE+"Iterator(things)") + fmt.Fprintln(w, " return iterator") + fmt.Fprintln(w, "}") + 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, "type "+BASE+""+SORT+" []*"+BASE+"") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Len() int { return len(a) }") + fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Less(i, j int) bool { return a[i]."+SORT+" < a[j]."+SORT+" }") + fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }") + fmt.Fprintln(w, "") } +} + +func (pf *File) iterSelect(w io.Writer) { + var BASES string = pf.Bases.Name + var BASE string = pf.Base.Name + var LOCK string = pf.Bases.Lockname + + fmt.Fprintln(w, "// safely returns a slice of pointers to the "+BASE+" protobufs") + fmt.Fprintln(w, "func (all *"+BASES+") selectAll"+BASE+"() []*"+BASE+" {") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+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, " // Create a new slice to hold pointers to each "+BASE+"") + fmt.Fprintln(w, " var aStuff []*"+BASE+"") + fmt.Fprintln(w, " aStuff = make([]*"+BASE+", len(all."+BASES+"))") + fmt.Fprintln(w, " for i, p := range all."+BASES+" {") fmt.Fprintln(w, " aStuff[i] = p // Copy pointers for safe iteration") fmt.Fprintln(w, " }") fmt.Fprintln(w, "") |
