maybe got it finally

This commit is contained in:
Jeff Carr 2025-01-11 10:19:37 -06:00
parent 12fb2c9f09
commit bb8e2290e0
4 changed files with 78 additions and 30 deletions

View File

@ -20,12 +20,14 @@ func (pb *Files) makeNewSortfile(pf *File) error {
header(wSort, pf) header(wSort, pf)
pf.Base.iterTop(wSort) // pf.Base.iterTop(wSort)
pf.Base.iterNext(wSort) // pf.Base.iterNext(wSort)
pf.selectAllFunc(wSort) // pf.selectAllFunc(wSort)
pf.iterSelect(wSort) // pf.iterSelect(wSort)
pf.sortByFunc(wSort, pf.Bases, pf.Base) // pf.sortByFunc(wSort, pf.Bases, pf.Base)
pf.newGenerateSort(wSort, pf.Bases)
pf.newGenerateSort(wSort, pf.Base)
/* /*
pf.appendUnique(wFind) // Append() enforce no unique keys pf.appendUnique(wFind) // Append() enforce no unique keys
@ -55,24 +57,35 @@ func (pf *File) newGenerateSort(w io.Writer, parent *MsgName) error {
var FRUIT string = parent.Name var FRUIT string = parent.Name
var LOCK string = parent.Lockname var LOCK string = parent.Lockname
var allLen bool
for _, v := range parent.Vars { for _, v := range parent.Vars {
if v.IsRepeated { if !v.IsRepeated {
continue
}
var APPLES string = v.VarName var APPLES string = v.VarName
var APPLE string = v.VarType var APPLE string = v.VarType
if !allLen {
// only can run these once for now
addAllFunc(w, FRUIT, APPLE, LOCK)
addLenFunc(w, FRUIT, APPLES, LOCK)
allLen = true
}
addSelectAll(w, FRUIT, APPLE, APPLES, LOCK)
newIter(w, FRUIT, APPLE, APPLES, LOCK) newIter(w, FRUIT, APPLE, APPLES, LOCK)
msg := pf.findMsg(APPLE) msg := pf.findMsg(APPLE)
if msg == nil { if msg == nil {
return fmt.Errorf("failed to find struct %s", APPLE) return fmt.Errorf("failed to find struct %s", APPLE)
} }
for _, v := range msg.Vars { for _, v := range msg.Vars {
if v.HasSort { if !v.HasSort {
continue
}
var COLOR string = v.VarName var COLOR string = v.VarName
newSortBy(w, FRUIT, APPLE, COLOR) newSortBy(w, FRUIT, APPLE, APPLES, COLOR)
} }
} }
}
}
return nil return nil
} }

View File

@ -14,7 +14,7 @@ func newIter(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) {
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "// safely returns a slice of pointers to the FRUIT protobufs") fmt.Fprintln(w, "// safely returns a slice of pointers to the FRUIT protobufs")
fmt.Fprintln(w, "func (x *"+FRUIT+") all() []*"+APPLE+" {") fmt.Fprintln(w, "func (x *"+FRUIT+") all"+APPLES+"() []*"+APPLE+" {")
fmt.Fprintln(w, " "+LOCK+".RLock()") fmt.Fprintln(w, " "+LOCK+".RLock()")
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
@ -55,7 +55,7 @@ func newIter(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func newSortBy(w io.Writer, FRUIT, APPLE, COLOR string) { func newSortBy(w io.Writer, FRUIT, APPLE, APPLES, COLOR string) {
fmt.Fprintln(w, "// START sort by ", COLOR, "(this is all you need once the Iterator is defined)") fmt.Fprintln(w, "// START sort by ", COLOR, "(this is all you need once the Iterator is defined)")
fmt.Fprintln(w, "type "+APPLE+COLOR+" []*"+APPLE+"") fmt.Fprintln(w, "type "+APPLE+COLOR+" []*"+APPLE+"")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
@ -64,7 +64,7 @@ func newSortBy(w io.Writer, FRUIT, APPLE, COLOR string) {
fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }") fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (x *"+FRUIT+") SortBy"+COLOR+"() *"+APPLE+"Iterator {") fmt.Fprintln(w, "func (x *"+FRUIT+") SortBy"+COLOR+"() *"+APPLE+"Iterator {")
fmt.Fprintln(w, " things := x.all()") fmt.Fprintln(w, " things := x.all"+APPLES+"()")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " sort.Sort("+APPLE+COLOR+"(things))") fmt.Fprintln(w, " sort.Sort("+APPLE+COLOR+"(things))")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
@ -73,3 +73,41 @@ func newSortBy(w io.Writer, FRUIT, APPLE, COLOR string) {
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "// END sort by", COLOR) fmt.Fprintln(w, "// END sort by", COLOR)
} }
func addAllFunc(w io.Writer, FRUIT, APPLE, LOCK string) {
fmt.Fprintln(w, "func (x *"+FRUIT+") All() *"+APPLE+"Iterator {")
fmt.Fprintln(w, " "+APPLE+"Pointers := x.selectAll"+APPLE+"()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " iterator := New"+APPLE+"Iterator("+APPLE+"Pointers)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func addLenFunc(w io.Writer, FRUIT, APPLES, LOCK string) {
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (x *"+FRUIT+") Len() int {")
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, "")
}
func addSelectAll(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) {
fmt.Fprintln(w, "// safely returns a slice of pointers to the "+APPLE+" protobufs")
fmt.Fprintln(w, "func (x *"+FRUIT+") selectAll"+APPLE+"() []*"+APPLE+" {")
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, "}")
}

View File

@ -1,10 +1,6 @@
package main package main
import ( /*
"fmt"
"io"
)
func (msg *MsgName) iterTop(w io.Writer) { func (msg *MsgName) iterTop(w io.Writer) {
var BASE string = msg.Name var BASE string = msg.Name
@ -117,3 +113,4 @@ func (pf *File) iterSelect(w io.Writer) {
fmt.Fprintln(w, " return tmp") fmt.Fprintln(w, " return tmp")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
} }
*/

View File

@ -135,11 +135,11 @@ func parseMsgVar(line string) *MsgVar {
if parts[0] == "repeated" { if parts[0] == "repeated" {
v.IsRepeated = true v.IsRepeated = true
v.VarType = parts[1] v.VarType = parts[1]
v.VarName = parts[2] v.VarName = cases.Title(language.English, cases.NoLower).String(parts[2])
return v return v
} }
v.VarType = parts[0] v.VarType = parts[0]
v.VarName = parts[1] v.VarName = cases.Title(language.English, cases.NoLower).String(parts[1])
return v return v
} }