summaryrefslogtreecommitdiff
path: root/generateSortFuncs.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-15 19:43:22 -0500
committerJeff Carr <[email protected]>2025-10-15 19:43:22 -0500
commit30e8de23f6ae78136e93add8e1464da5e45b020e (patch)
tree144ea63b3a3061a05d1ba45d63adfecba108532d /generateSortFuncs.go
parent0c6082d44c7fbf46d8d44d50972d9f3302414d5b (diff)
rename filesv0.5.26
Diffstat (limited to 'generateSortFuncs.go')
-rw-r--r--generateSortFuncs.go241
1 files changed, 241 insertions, 0 deletions
diff --git a/generateSortFuncs.go b/generateSortFuncs.go
new file mode 100644
index 0000000..305896e
--- /dev/null
+++ b/generateSortFuncs.go
@@ -0,0 +1,241 @@
+// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
+// Use of this source code is governed by the GPL 3.0
+
+package main
+
+import (
+ "fmt"
+ "io"
+)
+
+func (msg *MsgName) newScannerAll(w io.Writer, FRUIT, APPLE, APPLES, LOCKold string) string {
+ LOCK := msg.getLockname("x")
+
+ funcdef := "func (x *" + FRUIT + ") all" + APPLES + "() []*" + APPLE + " {"
+
+ fmt.Fprintln(w, "// safely returns a slice of pointers to the FRUIT protobufs")
+ fmt.Fprintln(w, funcdef)
+ 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 FRUIT")
+ fmt.Fprintln(w, " var tmp []*"+APPLE+"")
+ fmt.Fprintln(w, " tmp = make([]*"+APPLE+", len(x."+APPLES+"))")
+ fmt.Fprintln(w, " for i, p := range x."+APPLES+" {")
+ fmt.Fprintln(w, " tmp[i] = p // Copy pointers for safe iteration")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " return tmp")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+
+ return funcdef
+}
+
+// only make one of these for each message in the protobuf file
+func newScannerDefines(w io.Writer, msg *MsgName) string {
+ if !msg.NeedIter {
+ return "iter already done for " + msg.Name
+ }
+ msg.NeedIter = false
+ APPLE := msg.Name
+
+ // should this be 'new' or 'New' ? Does it matter? I think it's totally internal here
+ // in this file where it is "new or "New". I changed it to lower case 2025.01.12
+ funcdef := "func new" + APPLE + "Scanner(things []*" + APPLE + ") *" + APPLE + "Scanner"
+
+ fmt.Fprintln(w, "// DEFINE THE", APPLE, "SCANNER.")
+ fmt.Fprintln(w, "// itializes a new scanner.")
+ fmt.Fprintln(w, funcdef, "{")
+ fmt.Fprintln(w, " return &"+APPLE+"Scanner{things: things}")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "type "+APPLE+"Scanner struct {")
+ fmt.Fprintln(w, " sync.Mutex")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " things []*"+APPLE+"")
+ fmt.Fprintln(w, " index int")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "func (it *"+APPLE+"Scanner) Scan() bool {")
+ fmt.Fprintln(w, " if it.index >= len(it.things) {")
+ fmt.Fprintln(w, " return false")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " it.Lock()")
+ fmt.Fprintln(w, " it.index++")
+ fmt.Fprintln(w, " it.Unlock()")
+ fmt.Fprintln(w, " return true")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "// Next() returns the next thing in the array")
+ fmt.Fprintln(w, "func (it *"+APPLE+"Scanner) Next() *"+APPLE+" {")
+ fmt.Fprintln(w, " if it.things[it.index-1] == nil {")
+ fmt.Fprintln(w, " fmt.Println(\"Next() error in "+APPLE+"Scanner\", it.index)")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " return it.things[it.index-1]")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "// END DEFINE THE SCANNER")
+ fmt.Fprintln(w, "")
+
+ return funcdef
+}
+
+// maybe there are better ways in GO now adays // that's fine though. this is easy to read
+// TODO; figure out what types this actually works on
+// TODO; add timestamppb compare
+func (msg *MsgName) newSortType(w io.Writer, STRUCT, VARNAME string) string {
+ SORTNAME := "sort" + STRUCT + VARNAME
+
+ // Can these be lower case? Should they be lower case? maybe sort.Sort() requires upper case?
+ fmt.Fprintln(w, "// sort struct by", VARNAME)
+ fmt.Fprintln(w, "type "+SORTNAME+" []*"+STRUCT+"")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "func (a "+SORTNAME+") Len() int { return len(a) }")
+ fmt.Fprintln(w, "func (a "+SORTNAME+") Less(i, j int) bool { return a[i]."+VARNAME+" < a[j]."+VARNAME+" }")
+ fmt.Fprintln(w, "func (a "+SORTNAME+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
+ fmt.Fprintln(w, "")
+
+ return "type " + STRUCT + VARNAME + " []*" + STRUCT + " // { return a[i]." + VARNAME + " < a[j]." + VARNAME + " }"
+}
+
+// provdes a sorted iter. Does not modify the protobuf
+func (msg *MsgName) newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELECT, VARNAME string) string {
+ funcdef := "func (x *" + STRUCT + ") " + SORTBY + "() *" + ITER + "Scanner"
+
+ fmt.Fprintln(w, funcdef, "{")
+ fmt.Fprintln(w, " // copy the pointers as fast as possible.")
+ fmt.Fprintln(w, " things := x."+SELECT+"()")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "// todo: try slices.SortFunc() instead to see what happens")
+ fmt.Fprintln(w, " sort.Sort("+SORTNAME+"(things))")
+ fmt.Fprintln(w, "// slices.SortFunc(things, func(a, b *"+STRUCT+") bool {")
+ fmt.Fprintln(w, "// return a."+VARNAME+" < b."+VARNAME+" // Sort by ??. let the compiler work it out??")
+ fmt.Fprintln(w, "// })")
+ // should this be 'new' or 'New' ? Does it matter? I think it's totally internal here
+ // in this file where it is "new or "New". I changed it to lower case 2025.01.12
+ fmt.Fprintln(w, " return new"+ITER+"Scanner(things)")
+ fmt.Fprintln(w, "}")
+
+ return funcdef
+}
+
+// sorts the protobuf in place.
+func (msg *MsgName) newSort(w io.Writer, STRUCT, FUNCNAME, SORTNAME, LOCK string) string {
+ funcdef := "func (pb *" + STRUCT + ") " + FUNCNAME + "() "
+
+ fmt.Fprintln(w, funcdef, "{")
+ fmt.Fprintln(w, " "+LOCK+".Lock()")
+ fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " sort.Sort("+SORTNAME+"(pb."+STRUCT+"))")
+ fmt.Fprintln(w, "}")
+
+ return funcdef
+}
+
+func (msg *MsgName) addIterAllFunc(w io.Writer, FRUIT, APPLE, APPLES string) string {
+ funcdef := "func (x *" + FRUIT + ") All() *" + APPLE + "Scanner {"
+
+ fmt.Fprintln(w, funcdef)
+ fmt.Fprintln(w, " "+APPLE+"Pointers := x.selectAll"+APPLES+"()")
+ fmt.Fprintln(w, "")
+ // should this be 'new' or 'New' ? Does it matter? I think it's totally internal here. I think there are only 3 places
+ // in this file where it is "new or "New". I changed it to lower case 2025.01.12
+ fmt.Fprintln(w, " scanner := new"+APPLE+"Scanner("+APPLE+"Pointers)")
+ fmt.Fprintln(w, " return scanner")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+
+ return funcdef
+}
+
+func (msg *MsgName) addIterBy(w io.Writer, FRUITS, FRUIT, APPLE, SORTNAME string) string {
+ fmt.Fprintln(w, "// 'for x := range' syntax using the awesome golang 1.24 'iter'")
+ fmt.Fprintln(w, "func (x *"+FRUITS+") IterBy"+APPLE+"() iter.Seq[*"+FRUIT+"] {")
+ fmt.Fprintln(w, " items := x.selectAll"+FRUITS+"()")
+ fmt.Fprintln(w, " sort.Sort("+SORTNAME+"(items))")
+ fmt.Fprintln(w, " // log.Println(\"Made Iter.Seq[] with length\", len(items))")
+ fmt.Fprintln(w, " return func(yield func(*"+FRUIT+") bool) {")
+ fmt.Fprintln(w, " for _, v := range items {")
+ fmt.Fprintln(w, " if !yield(v) {")
+ fmt.Fprintln(w, " return")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, "}")
+
+ return ""
+}
+
+func (msg *MsgName) addIterAll(w io.Writer, FRUITS, FRUIT string) string {
+ funcdef := "func (x *" + FRUITS + ") IterAll() iter.Seq[*" + FRUIT + "] {"
+ fmt.Fprintln(w, "// Iterate 'for x := range' syntax using the awesome golang 1.24 'iter'")
+ fmt.Fprintln(w, funcdef)
+ fmt.Fprintln(w, " items := x.selectAll"+FRUITS+"()")
+ fmt.Fprintln(w, " // log.Println(\"Made All() Iter.Seq[] with length\", len(items))")
+ fmt.Fprintln(w, " return func(yield func(*"+FRUIT+") bool) {")
+ fmt.Fprintln(w, " for _, v := range items {")
+ fmt.Fprintln(w, " if !yield(v) {")
+ fmt.Fprintln(w, " return")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, "}")
+
+ return funcdef
+}
+
+func (msg *MsgName) addAllFunc(w io.Writer, FRUIT, APPLE, APPLES string) string {
+ funcdef := "func (x *" + FRUIT + ") All() *" + APPLE + "Scanner {"
+
+ fmt.Fprintln(w, funcdef)
+ fmt.Fprintln(w, " "+APPLE+"Pointers := x.selectAll"+APPLES+"()")
+ fmt.Fprintln(w, "")
+ // should this be 'new' or 'New' ? Does it matter? I think it's totally internal here. I think there are only 3 places
+ // in this file where it is "new or "New". I changed it to lower case 2025.01.12
+ fmt.Fprintln(w, " scanner := new"+APPLE+"Scanner("+APPLE+"Pointers)")
+ fmt.Fprintln(w, " return scanner")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+
+ return funcdef
+}
+
+func (msg *MsgName) addLenFunc(w io.Writer, FRUIT, APPLES, LOCKold string) string {
+ LOCK := msg.getLockname("x")
+
+ funcdef := "func (x *" + FRUIT + ") Len() int {"
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, funcdef)
+ fmt.Fprintln(w, " "+LOCK+".RLock()")
+ fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " return len(x."+APPLES+")")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+
+ return funcdef
+}
+
+func (msg *MsgName) addSelectAll(w io.Writer, FRUIT, APPLE, APPLES, LOCKold string) string {
+ LOCK := msg.getLockname("x")
+
+ funcdef := "func (x *" + FRUIT + ") selectAll" + APPLES + "() []*" + APPLE
+ fmt.Fprintln(w, "// safely returns a slice of pointers to the "+APPLE+" protobufs")
+ fmt.Fprintln(w, funcdef, "{")
+ 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 "+APPLE+"")
+ fmt.Fprintln(w, " var tmp []*"+APPLE+"")
+ fmt.Fprintln(w, " tmp = make([]*"+APPLE+", len(x."+APPLES+"))")
+ fmt.Fprintln(w, " for i, p := range x."+APPLES+" {")
+ fmt.Fprintln(w, " tmp[i] = p // Copy pointers for safe iteration")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " return tmp")
+ fmt.Fprintln(w, "}")
+
+ return funcdef
+}