From 2394b2c0553d374b27796b9c013dc88708450623 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 12 Jan 2025 04:40:12 -0600 Subject: [PATCH] good stuff. Find() seems to work --- generate.go | 10 ++++++++++ generateFind.go | 17 +++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/generate.go b/generate.go index 3983973..458e6ac 100644 --- a/generate.go +++ b/generate.go @@ -275,9 +275,17 @@ func (pb *Files) makeNewSortfile(pf *File) error { } for _, v := range msg.Vars { + if v.IsRepeated { + continue + } if v.HasUnique { funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a " + v.VarType + ") *" + s.VarType + "(using" + v.VarName + ")" log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) + + FUNCNAME := "Find" + VARNAME + "By" + v.VarName + funcdef := msg.generateFindBy(wSort, FUNCNAME, PARENT, VARNAME, s.VarType, v.VarName) + // func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string { + log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) } else { if v.VarType == "string" { funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a string) []*" + s.VarType + " ???" @@ -387,6 +395,7 @@ func (pf *File) processMessage(parent *MsgName, wSort, wFind io.Writer) error { } */ +/* func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK string) { for _, v := range parent.Vars { if v.HasUnique { @@ -396,6 +405,7 @@ func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK stri } } } +*/ /* func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { diff --git a/generateFind.go b/generateFind.go index d1f660b..7d8637d 100644 --- a/generateFind.go +++ b/generateFind.go @@ -18,9 +18,12 @@ func (msg *MsgName) getLockname(s string) string { return msg.Lockname } -func generateFindBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, LOCK string) { - fmt.Fprintln(w, "// lookup a", FRUIT, "by the ", COLOR) - fmt.Fprintln(w, "func (x *"+FRUIT+") FindBy"+COLOR+"(s string) *"+APPLE+" {") +func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string { + LOCK := msg.getLockname("x") + funcdef := "func (x *" + STRUCT + ") " + FUNCNAME + "(s string) *" + VARTYPE + + fmt.Fprintln(w, "// lookup a", STRUCT, "by the ", COLOR) + fmt.Fprintln(w, funcdef, "{") fmt.Fprintln(w, " if x == nil {") fmt.Fprintln(w, " return nil") fmt.Fprintln(w, " }") @@ -28,14 +31,16 @@ func generateFindBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, LOCK string) { fmt.Fprintln(w, " "+LOCK+".RLock()") fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") fmt.Fprintln(w, "") - fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {") - fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == s {") - fmt.Fprintln(w, " return x."+APPLES+"[i]") + fmt.Fprintln(w, " for i, _ := range x."+VARNAME+" {") + fmt.Fprintln(w, " if x."+VARNAME+"[i]."+COLOR+" == s {") + fmt.Fprintln(w, " return x."+VARNAME+"[i]") fmt.Fprintln(w, " }") fmt.Fprintln(w, " }") fmt.Fprintln(w, " return nil") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") + + return funcdef } func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, VARNAME string) string {