diff --git a/sort.go b/sort.go index acb8325..bc376ed 100644 --- a/sort.go +++ b/sort.go @@ -71,16 +71,16 @@ func iterTop(w io.Writer, names map[string]string) { fmt.Fprintln(w, "type "+names["Base"]+"Iterator struct {") fmt.Fprintln(w, " sync.RWMutex") fmt.Fprintln(w, "") - fmt.Fprintln(w, " packs []*"+names["Base"]) + fmt.Fprintln(w, " things []*"+names["Base"]) fmt.Fprintln(w, " index int") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") 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}") + fmt.Fprintln(w, "func New"+names["Base"]+"Iterator(things []*"+names["Base"]+") *"+names["Base"]+"Iterator {") + fmt.Fprintln(w, " return &"+names["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 packs.") + 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() ") @@ -88,7 +88,7 @@ func iterTop(w io.Writer, names map[string]string) { fmt.Fprintln(w, "// fmt.Println(\"found UUID:\", d.Uuid") fmt.Fprintln(w, "// }") 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.things) {") fmt.Fprintln(w, " return false") fmt.Fprintln(w, " }") fmt.Fprintln(w, " it.index++") @@ -100,14 +100,14 @@ func iterTop(w io.Writer, names map[string]string) { func iterNext(w io.Writer, names map[string]string) { 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, " if it.packs[it.index-1] == nil {") - fmt.Fprintln(w, " for i, d := range it.packs {") + 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.packs))") + 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.packs[it.index-1]") + fmt.Fprintln(w, " return it.things[it.index-1]") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") } @@ -136,11 +136,11 @@ func iterSortAll(w io.Writer, names map[string]string) { 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, " packs := all.selectAll"+names["Base"]+"()") + fmt.Fprintln(w, " things := all.selectAll"+names["Base"]+"()") fmt.Fprintln(w, "") - fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(packs))") + fmt.Fprintln(w, " sort.Sort("+names["Base"]+""+names["sortBy"]+"(things))") fmt.Fprintln(w, "") - fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(packs)") + fmt.Fprintln(w, " iterator := New"+names["Base"]+"Iterator(things)") fmt.Fprintln(w, " return iterator") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") diff --git a/sortnew.go b/sortnew.go new file mode 100644 index 0000000..6479a2d --- /dev/null +++ b/sortnew.go @@ -0,0 +1,58 @@ +package main + +import ( + "os" + "strings" +) + +func makeNewSortfile() { + f, _ := os.OpenFile(sortmap["protobase"]+".newsort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + + header(f, sortmap) + + if sortmap["lock"] == "all" { + // if the lock is set to 'all' this means the mutex was put in the protoc-gen-go struct + } else { + syncLock(f, sortmap) + } + iterTop(f, sortmap) + 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) +}