From 6ce9c29135cd181755e0e76a5ddba98e37ae49d8 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 Jan 2025 21:33:00 -0600 Subject: [PATCH] lucked out. compiled and ran and worked on the first pass --- example/main.go | 4 +-- sort.go | 75 +++++++++++++++++++++++++++---------------------- sortFunc.go | 2 +- sortNew.go | 48 ++++++++++++++++++++++++++++--- 4 files changed, 89 insertions(+), 40 deletions(-) diff --git a/example/main.go b/example/main.go index c180350..9fdf1ca 100644 --- a/example/main.go +++ b/example/main.go @@ -109,10 +109,10 @@ func (pb *Fruits) addThings() { tmp := new(Fruit) tmp.Brand = name tmp.City = "paris" - if pb.AppendUniqueBrand(tmp) { + if pb.AppendUniqueByBrand(tmp) { // log.Printf("AppendUniqueBrand() ok len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len()) } else { - log.Printf("AppendUniqueBrand() ERROR len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len()) + log.Printf("AppendUniqueByBrand() ERROR len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len()) os.Exit(-1) } pb.Append(tmp) diff --git a/sort.go b/sort.go index efbb14e..f9763f7 100644 --- a/sort.go +++ b/sort.go @@ -35,75 +35,84 @@ func (pb *Files) makeNewSortfile(pf *File) error { pf.selectAllFunc(wSort) pf.iterSelect(wSort) - pf.appendUnique(wFind) // Append() enforce no unique keys pf.sortByFunc(wSort) - if argv.Delete { - pf.deleteWithCopyFunc(wFind) - } else { - pf.deleteFunc(wFind) - } - pf.findFunc(wFind) + + /* + pf.appendUnique(wFind) // Append() enforce no unique keys + if argv.Delete { + pf.deleteWithCopyFunc(wFind) + } else { + pf.deleteFunc(wFind) + } + pf.findFunc(wFind) + */ // attempt to add sort functions for pf.Base pf.processMessage(pf.Bases, wSort, wFind) - // pf.processMessage(pf.Base, wSort, wFind) + pf.processMessage(pf.Base, wSort, wFind) return nil } func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error { - log.Printf("Generating functions for %s\n", msg.Name) + var FRUIT string = cases.Title(language.English, cases.NoLower).String(msg.Name) + log.Printf("Generating functions for %s\n", FRUIT) for _, v := range msg.Vars { if !v.IsRepeated { log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) continue } - if err := pf.addSortByMsg(msg, v, wSort, wFind); err != nil { - return err + // use easier to read variable names APPLE and APPLES in the code + var APPLE string = v.VarType + var APPLES string = cases.Title(language.English, cases.NoLower).String(v.VarName) + + // try and find the message struct for APPLE + var found *MsgName + for _, m := range pf.MsgNames { + if m.Name == APPLE { + found = m + break + } } + if found == nil { + return fmt.Errorf("failed to find struct %s", APPLE) + } + + found.addSortByMsg(FRUIT, APPLES, APPLE, wSort, wFind) } return nil } -func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writer) error { - log.Printf("\tLOOK HERE: %s %s\n", find.VarName, find.VarType) - var found *MsgName - for _, msg := range pf.MsgNames { - if msg.Name == find.VarType { - found = msg - break - } - } - if found == nil { - return fmt.Errorf("failed to find struct %s", find.VarType) - } - log.Printf("FOUND!: %s %s for %s\n", find.VarName, find.VarType, found.Name) +func (parent *MsgName) addSortByMsg(FRUIT, APPLES, APPLE string, wSort, wFind io.Writer) error { + log.Printf("\tFOUND!: %s %s for %s\n", APPLES, APPLE, FRUIT) var COLORS []string - var FRUIT string = parent.Name - var APPLES string = cases.Title(language.English, cases.NoLower).String(find.VarName) - var APPLE string = find.VarType - for _, v := range found.Vars { + for _, v := range parent.Vars { if v.HasSort { - // log.Printf("\tSort!: %s %s for %s\n", find.VarName, find.VarType, v.VarName) + // log.Printf("\tSort!: %s %s for %s\n", APPLES, APPLE, v.VarName) newS := cases.Title(language.English, cases.NoLower).String(v.VarName) - log.Printf("\t(x %s) SortdBy%s() *%sIter\n", parent.Name, newS, find.VarType) + log.Printf("\t(x %s) SortdBy%s() *%sIter\n", parent.Name, newS, APPLE) } if v.HasUnique { newS := cases.Title(language.English, cases.NoLower).String(v.VarName) var COLOR string = newS COLORS = append(COLORS, COLOR) - log.Printf("\t(x %s) AppendUniqueBy%s(%s)\n", parent.Name, newS, find.VarType) + log.Printf("\t(x %s) AppendUniqueBy%s(%s)\n", parent.Name, newS, APPLE) parent.appendUniqueBy(wFind, FRUIT, APPLES, APPLE, COLOR) if v.VarType == "string" { log.Printf("\t(x %s) FindBy%s(string) *%s\n", FRUIT, COLOR, APPLE) parent.findBy(wFind, FRUIT, APPLES, APPLE, COLOR) - log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, find.VarType) - parent.deleteBy(wFind, FRUIT, APPLES, APPLE, COLOR) + log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, APPLE) + if argv.Delete { + parent.deleteByWithCopy(wFind, FRUIT, APPLES, APPLE, COLOR) + } else { + parent.deleteBy(wFind, FRUIT, APPLES, APPLE, COLOR) + } } } } + parent.insertByColors(wFind, FRUIT, APPLES, APPLE, COLORS) if len(COLORS) > 0 { parent.appendUnique(wFind, FRUIT, APPLES, APPLE, COLORS) } diff --git a/sortFunc.go b/sortFunc.go index 869a2ab..2a5af91 100644 --- a/sortFunc.go +++ b/sortFunc.go @@ -127,7 +127,7 @@ func (pf *File) iterSelect(w io.Writer) { fmt.Fprintln(w, "}") } -func (pf *File) appendUnique(w io.Writer) { +func (pf *File) appendUniqueOld(w io.Writer) { var MSG string = pf.Bases.Name var BASE string = pf.Base.Name var LOCK string = pf.Bases.Lockname diff --git a/sortNew.go b/sortNew.go index 6f31348..7ed81dd 100644 --- a/sortNew.go +++ b/sortNew.go @@ -14,6 +14,46 @@ func (msg *MsgName) getLockname(s string) string { return msg.Lockname } +// I like these functions the best. +func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) { + LOCK := msg.getLockname("x") + + // append -- no check at all + fmt.Fprintln(w, "// TESTING 2") + fmt.Fprintln(w, "// just a simple Append() shortcut (but still uses the mutex lock)") + fmt.Fprintln(w, "func (x *"+FRUIT+") Append(y *"+APPLE+") {") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", y)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +} + +func (msg *MsgName) insertByColors(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { + LOCK := msg.getLockname("x") + for _, COLOR := range COLORS { + fmt.Fprintln(w, "// TESTING") + fmt.Fprintln(w, "// returns an "+APPLE+" if "+COLOR+" matches, otherwise create") + fmt.Fprintln(w, "func (x *"+FRUIT+") InsertBy"+COLOR+" (y string) "+APPLE+" {") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") + fmt.Fprintln(w, " if p."+COLOR+" == "+COLOR+" {") + fmt.Fprintln(w, " return p") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " z := new("+APPLE+")") + fmt.Fprintln(w, " z."+COLOR+" := y") + fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", z)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + } +} + func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { LOCK := msg.getLockname("x") @@ -86,8 +126,8 @@ func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { fmt.Fprintln(w, "// TESTING") fmt.Fprintln(w, "func (x *"+FRUIT+") DeleteBy"+COLOR+"(s string) bool {") - fmt.Fprintln(w, " "+LOCK+".RLock()") - fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") + 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 {") @@ -107,8 +147,8 @@ func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR st fmt.Fprintln(w, "// TESTING") fmt.Fprintln(w, "func (x *"+FRUIT+") DeleteBy"+COLOR+"(s string) *"+APPLE+" {") - fmt.Fprintln(w, " "+LOCK+".RLock()") - fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, "") fmt.Fprintln(w, " var newr "+APPLE) fmt.Fprintln(w, "")