From 8ac4d90c22f1b884ea140d0a14f65ab56768cbe4 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 12 Jan 2025 04:25:02 -0600 Subject: [PATCH] Delete() is working again --- generate.go | 44 ++++++++++++++++++++++++-------------------- generateFind.go | 19 +++++++++++-------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/generate.go b/generate.go index bfacb07..3983973 100644 --- a/generate.go +++ b/generate.go @@ -200,8 +200,8 @@ func (pb *Files) makeNewSortfile(pf *File) error { log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) FRUIT := PARENT APPLES := msg.Name - APPLE := v.VarName - COLOR := v.VarType + APPLE := msg.VarName + COLOR := v.VarName FUNCNAME := "Delete" + msg.Name + "By" + v.VarName var funcdef string @@ -250,25 +250,29 @@ func (pb *Files) makeNewSortfile(pf *File) error { log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) } - /* - // delete() functions - for _, v := range msg.Vars { - if !v.HasUnique { - continue - } - funcname := "func (x *" + PARENT + ") Delete" + VARNAME + "By" + v.VarName + "(" + v.VarType + ") bool" - log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) - FRUIT := PARENT - APPLES := VARNAME - APPLE := v.VarName - COLOR := s.VarType - if argv.Delete { - msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR) - } else { - msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR) - } + // delete() functions + for _, v := range msg.Vars { + if v.IsRepeated { + continue } - */ + if !v.HasUnique { + continue + } + // funcname := "func (x *" + PARENT + ") Delete" + VARNAME + "By" + v.VarName + "(" + v.VarType + ") bool" + // log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) + FRUIT := PARENT + APPLES := VARNAME + APPLE := v.VarName + COLOR := v.VarType + FUNCNAME := "Delete" + VARNAME + "By" + v.VarName + var funcdef string + if argv.Delete { + funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName) + } else { + funcdef = msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName) + } + log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) + } for _, v := range msg.Vars { if v.HasUnique { diff --git a/generateFind.go b/generateFind.go index 4ab9d10..d1f660b 100644 --- a/generateFind.go +++ b/generateFind.go @@ -38,15 +38,16 @@ func generateFindBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, LOCK string) { fmt.Fprintln(w, "") } -func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME string) string { +func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, VARNAME string) string { LOCK := msg.getLockname("x") + funcdef := "func (x *" + FRUIT + ") " + FUNCNAME + "(s string) bool" - fmt.Fprintln(w, "func (x *"+FRUIT+") "+FUNCNAME+"(s string) bool {") + fmt.Fprintln(w, funcdef, "{") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, "") fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {") - fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == s {") + fmt.Fprintln(w, " if x."+APPLES+"[i]."+VARNAME+" == s {") fmt.Fprintln(w, " x."+APPLES+"[i] = x."+APPLES+"[len(x."+APPLES+")-1]") fmt.Fprintln(w, " x."+APPLES+" = x."+APPLES+"[:len(x."+APPLES+")-1]") fmt.Fprintln(w, " return true") @@ -56,22 +57,24 @@ func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME fmt.Fprintln(w, "}") fmt.Fprintln(w, "") - return "func (x *" + FRUIT + ") DeleteBy" + COLOR + "(s string) bool {" + return funcdef } // this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct -func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME string) string { +func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, VARNAME string) string { LOCK := msg.getLockname("x") + funcdef := "func (x *" + FRUIT + ") " + FUNCNAME + "(s string) *" + APPLE + fmt.Fprintln(w, "// TESTING fails with 'go vet' warning") - fmt.Fprintln(w, "func (x *"+FRUIT+") "+FUNCNAME+"(s string) *"+APPLE+" {") + fmt.Fprintln(w, funcdef, "{") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, "") fmt.Fprintln(w, " var newr "+APPLE) fmt.Fprintln(w, "") fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {") - fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == s {") + fmt.Fprintln(w, " if x."+APPLES+"[i]."+VARNAME+" == s {") fmt.Fprintln(w, " newr = *x."+APPLES+"[i]") fmt.Fprintln(w, " x."+APPLES+"[i] = x."+APPLES+"[len(x."+APPLES+")-1]") fmt.Fprintln(w, " x."+APPLES+" = x."+APPLES+"[:len(x."+APPLES+")-1]") @@ -82,7 +85,7 @@ func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, F fmt.Fprintln(w, "}") fmt.Fprintln(w, "") - return "func (x *" + FRUIT + ") DeleteBy" + COLOR + "(s string) *" + APPLE + " {" + return funcdef } // new 2025 idea. useful? TODO: look at this again in 1y