it compiled the test. sort seems to be working

This commit is contained in:
Jeff Carr 2025-01-12 02:38:17 -06:00
parent cfd9ec5ccd
commit 47d2a95bc5
2 changed files with 76 additions and 16 deletions

View File

@ -19,6 +19,9 @@ func (pb *Files) makeNewSortfile(pf *File) error {
header(wSort, pf)
pf.syncLock(wSort)
fmt.Fprintf(wSort, "// START SORT\n")
fmt.Fprintf(wSort, "\n")
log.Printf("START ITERATORS\n")
// add iterators for all the structs to be used
for i, msg := range pf.allMsg() {
@ -38,6 +41,10 @@ func (pb *Files) makeNewSortfile(pf *File) error {
if !v.HasSort {
continue
}
if v.IsRepeated {
// can't work against slices
continue
}
VARNAME := v.VarName
funcdef := newSortType(wSort, PARENT, VARNAME)
log.Printf("TYPE: %-2d %20s %20s %20s %10s %s\n", i, PARENT, "", "", "", funcdef)
@ -58,6 +65,7 @@ func (pb *Files) makeNewSortfile(pf *File) error {
}
log.Printf("\n")
log.Printf("START SELECT\n")
// make the sort iterators selectAll()
for i, s := range pf.ToSort {
PARENT := s.MsgName
@ -68,8 +76,66 @@ func (pb *Files) makeNewSortfile(pf *File) error {
funcdef := addSelectAll(wSort, PARENT, CHILD, VARNAME, LOCK)
log.Printf("SORT: %-2d %20s %20s %20s %20s %s\n", i, PARENT, CHILD, VARNAME, LOCK, funcdef)
}
log.Printf("END SELECT\n")
log.Printf("\n")
log.Printf("START SORT\n")
// make the SortBy() functions
for i, s := range pf.ToSort {
// var funcname string
PARENT := s.MsgName
CHILD := s.VarType
VARNAME := s.VarName
log.Printf("SORT: %-2d %20s %20s %20s %20s %s\n", i, PARENT, CHILD, VARNAME, "", "")
msg := pf.findMsg(s.VarType)
if msg == nil {
return fmt.Errorf("failed to find struct %s", s.VarType)
}
var FUNCTYPE string
if PARENT == VARNAME {
FUNCTYPE = PARENT
} else {
FUNCTYPE = VARNAME
}
for _, v := range msg.Vars {
if v.IsRepeated {
// can't work against slices
continue
}
if v.HasSort {
// funcname := "func (x *" + FUNCTYPE + ") SortBy" + v.VarName + "(" + v.VarType + ") *[]iter" + s.VarType
// log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "fix", "", "", funcname)
// funcdef := "func (x *"+FRUIT+") SortBy"+COLOR+"() *"+APPLE+"Iterator"
if v.VarType == "string" {
var sortby string
// func newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELECT string) string {
if PARENT == VARNAME {
sortby = "SortBy" + v.VarName
} else {
sortby = "Sort" + VARNAME + "By" + v.VarName
}
sortname := s.VarType + v.VarName
selectName := "selectAll" + VARNAME
funcdef := newSortBy(wSort, PARENT, s.VarType, sortname, sortby, selectName)
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcdef)
} else {
funcname := "func (x *" + FUNCTYPE + ") SortBy" + v.VarName + "(" + v.VarType + ") *[]iter" + s.VarType + " # can not do this yet"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
}
}
}
}
log.Printf("END SORT\n")
log.Printf("\n")
fmt.Fprintf(wSort, "\n")
fmt.Fprintf(wSort, "// END SORT\n")
// make Len()
for _, msg := range pf.allMsg() {
PARENT := msg.Name
@ -131,8 +197,8 @@ func (pb *Files) makeNewSortfile(pf *File) error {
// funcdef := "func (x *"+FRUIT+") SortBy"+COLOR+"() *"+APPLE+"Iterator"
if v.VarType == "string" {
funcdef := newSortBy(wSort, FUNCTYPE, CHILD, VARNAME, v.VarName)
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcdef)
// funcdef := newSortBy(wSort, FUNCTYPE, CHILD, VARNAME, v.VarName)
// log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcdef)
} else {
funcname := "func (x *" + FUNCTYPE + ") SortBy" + v.VarName + "(" + v.VarType + ") *[]iter" + s.VarType + " # can not do this yet"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
@ -257,6 +323,7 @@ func addIterNew(w io.Write, msg *MsgName {
}
*/
/*
func (pf *File) newGenerateSort(w io.Writer, parent *MsgName) error {
var FRUIT string = parent.Name
var LOCK string = parent.Lockname
@ -296,6 +363,7 @@ func (pf *File) newGenerateSort(w io.Writer, parent *MsgName) error {
}
return nil
}
*/
func (pf *File) findMsg(s string) *MsgName {
if pf.Bases.Name == s {

View File

@ -87,25 +87,17 @@ func newSortType(w io.Writer, STRUCT, VARNAME string) string {
return "type " + STRUCT + VARNAME + " []*" + STRUCT + " // { return a[i]." + VARNAME + " < a[j]." + VARNAME + " }"
}
func newSortBy(w io.Writer, FRUIT, APPLE, APPLES, COLOR string) string {
funcdef := "func (x *" + FRUIT + ") SortBy" + COLOR + "() *" + APPLE + "Iterator // field: " + COLOR
func newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELECT string) string {
funcdef := "func (x *" + STRUCT + ") " + SORTBY + "() *" + ITER + "Iterator"
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, funcdef, "{")
fmt.Fprintln(w, " things := x."+SELECT+"()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Len() int { return len(a) }")
fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Less(i, j int) bool { return a[i]."+COLOR+" < a[j]."+COLOR+" }")
fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
fmt.Fprintln(w, " sort.Sort("+SORTNAME+"(things))")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (x *"+FRUIT+") SortBy"+COLOR+"() *"+APPLE+"Iterator {")
fmt.Fprintln(w, " things := x.all"+APPLES+"()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " sort.Sort("+APPLE+COLOR+"(things))")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " iterator := New"+APPLE+"Iterator(things)")
fmt.Fprintln(w, " iterator := New"+ITER+"Iterator(things)")
fmt.Fprintln(w, " return iterator")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "// END sort by", COLOR)
return funcdef
}