From 47d2a95bc5b6990de924e66b37dbb5affb4c3cd4 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 12 Jan 2025 02:38:17 -0600 Subject: [PATCH] it compiled the test. sort seems to be working --- generate.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++-- generateSort.go | 20 +++++--------- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/generate.go b/generate.go index 418513f..876e4be 100644 --- a/generate.go +++ b/generate.go @@ -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 { diff --git a/generateSort.go b/generateSort.go index fd2291d..b92a4b1 100644 --- a/generateSort.go +++ b/generateSort.go @@ -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 }