diff --git a/generate.go b/generate.go index e50a18d..bfacb07 100644 --- a/generate.go +++ b/generate.go @@ -187,6 +187,34 @@ func (pb *Files) makeNewSortfile(pf *File) error { */ } + // make delete() + /* + for _, msg := range pf.allMsg() { + PARENT := msg.Name + + for _, v := range msg.Vars { + if !v.HasUnique { + continue + } + funcname := "func (x *" + PARENT + ") Delete" + msg.Name + "By" + v.VarName + "(" + v.VarType + ") bool" + log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) + FRUIT := PARENT + APPLES := msg.Name + APPLE := v.VarName + COLOR := v.VarType + FUNCNAME := "Delete" + msg.Name + "By" + v.VarName + + var funcdef string + if argv.Delete { + funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME) + } else { + funcdef = msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME) + } + log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) + } + } + */ + // add Find() Delete() Append() Insert() log.Printf(" %-2s %20s %20s %20s %20s\n", "", "PARENT STRUCT", "VAR STRUCT TYPE", "VAR NAME", "LOCK") for i, s := range pf.ToSort { @@ -204,19 +232,43 @@ func (pb *Files) makeNewSortfile(pf *File) error { } var ucount int + // append() functions for _, v := range msg.Vars { - if v.HasUnique { - ucount += 1 - funcname := "func (x *" + PARENT + ") Append" + VARNAME + "By" + v.VarName + "(" + v.VarType + ")" - log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) + if v.IsRepeated { + continue } + if !v.HasUnique { + continue + } + ucount += 1 + // funcname := "func (x *" + PARENT + ") Append" + VARNAME + "By" + v.VarName + "(" + v.VarType + ")" + // log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname) + // func (msg *MsgName) addAppendBy(w io.Writer, STRUCT, FUNCNAME, VARNAME, CHILD, COLOR string) string { + // func (msg *MsgName) addAppendBy(w io.Writer, STRUCT, FUNCNAME, STRUCTVAR, VARNAME, VARTYPE string) string { + FUNCNAME := "Append" + VARNAME + "By" + v.VarName + funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, v.VarName, s.VarType) + log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef) } - for _, v := range msg.Vars { - if v.HasUnique { + + /* + // 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) + } } - } + */ for _, v := range msg.Vars { if v.HasUnique { @@ -244,124 +296,9 @@ func (pb *Files) makeNewSortfile(pf *File) error { } } - - /* - // everything struct used in here needs a sort iterator (but just one) - for FRUIT, APPLE := range pf.IterMap { - msg := pf.findMsg(FRUIT) - if msg == nil { - return fmt.Errorf("failed to find struct %s", FRUIT) - } - log.Printf("Add newIter() message %s\n", FRUIT) - newIter(wSort, msg) - - child := pf.findMsg(APPLE) - if child == nil { - return fmt.Errorf("failed to find struct %s", APPLE) - } - log.Printf("Add newIter() message %s\n", APPLE) - newIter(wSort, child) - - // now add the allKEY() functions - msg.newIterAll(wSort, FRUIT, APPLE) - } - */ - - /* - log.Info("START") - log.Printf("Add all() FRUIT=%s APPLE=%s\n", FRUIT, APPLE) - addAllFunc(w, FRUIT, APPLE, LOCK) - log.Printf("Add len() FRUIT=%s APPLES=%s\n", FRUIT, APPLES) - addLenFunc(w, FRUIT, APPLES, LOCK) - log.Printf("Add selectAll() FRUIT=%s APPLE=%s APPLES=%s\n", FRUIT, APPLE, APPLES) - addSelectAll(w, FRUIT, APPLE, APPLES, LOCK) - // newIter(w, FRUIT, APPLE, APPLES, LOCK) - log.Info("END") - */ - - /* - return nil - pf.newGenerateSort(wSort, pf.Bases) - pf.newGenerateSort(wSort, pf.Base) - */ - - /* - pf.appendUnique(wFind) // Append() enforce no unique keys - if argv.Delete { - pf.deleteWithCopyFunc(wFind) - } else { - pf.deleteFunc(wFind) - } - pf.findFunc(wFind) - */ - - /* - wFind, _ := os.OpenFile(pf.Filebase+".find.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - defer wFind.Close() - header(wFind, pf) - pf.specialBases(wFind) - - // attempt to add sort functions for pf.Base - if err := pf.processMessage(pf.Bases, wSort, wFind); err != nil { - return err - } - if err := pf.processMessage(pf.Base, wSort, wFind); err != nil { - return err - } - */ return nil } -/* -func addIterNew(w io.Write, msg *MsgName { - var FRUIT string = parent.Name - var LOCK string = parent.Lockname - newIter(w, FRUIT, APPLE, APPLES, LOCK) -} -*/ - -/* -func (pf *File) newGenerateSort(w io.Writer, parent *MsgName) error { - var FRUIT string = parent.Name - var LOCK string = parent.Lockname - - for _, v := range parent.Vars { - if !v.IsRepeated { - continue - } - var APPLES string = v.VarName - var APPLE string = v.VarType - msg := pf.findMsg(APPLE) - if msg == nil { - return fmt.Errorf("failed to find struct %s", APPLE) - } - if msg.NeedIter { - parent.NeedIter = false - // only can run these once for now - log.Info("START") - log.Printf("Add all() FRUIT=%s APPLE=%s\n", FRUIT, APPLE) - // addAllFunc(w, FRUIT, APPLE, LOCK) - log.Printf("Add len() FRUIT=%s APPLES=%s\n", FRUIT, APPLES) - addLenFunc(w, FRUIT, APPLES, LOCK) - log.Printf("Add selectAll() FRUIT=%s APPLE=%s APPLES=%s\n", FRUIT, APPLE, APPLES) - addSelectAll(w, FRUIT, APPLE, APPLES, LOCK) - // newIter(w, FRUIT, APPLE, APPLES, LOCK) - log.Info("END") - } - - for _, v := range msg.Vars { - if !v.HasSort { - continue - } - var COLOR string = v.VarName - newSortBy(w, FRUIT, APPLE, APPLES, COLOR) - } - - } - return nil -} -*/ - func (pf *File) findMsg(s string) *MsgName { if pf.Bases.Name == s { return pf.Bases @@ -396,6 +333,8 @@ func (pf *File) specialBases(wFind io.Writer) { } +/* + // to simplify being able to read the code, FRUIT, APPLES and APPLE are used // FRUIT == the string name of the message in the protobuf file // APPLE == the type of the repeated variable @@ -442,6 +381,7 @@ func (pf *File) processMessage(parent *MsgName, wSort, wFind io.Writer) error { } return nil } +*/ func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK string) { for _, v := range parent.Vars { @@ -453,6 +393,7 @@ func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK stri } } +/* func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { // log.Printf("\tDELETE: %s %s for %s\n", APPLES, APPLE, FRUIT) var COLORS []string @@ -471,6 +412,7 @@ func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) } } } +*/ func (parent *MsgName) addInsertByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { // log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT) diff --git a/generateAppend.go b/generateAppend.go index 5c70316..cd34a4e 100644 --- a/generateAppend.go +++ b/generateAppend.go @@ -66,3 +66,29 @@ func (msg *MsgName) appendUniqueCOLOR(w io.Writer, FRUIT, APPLES, APPLE, COLOR s fmt.Fprintln(w, "}") fmt.Fprintln(w, "") } + +// Unique Append. rejects Append() if value is already defined +// compares the field 'COLOR' in the STRUCT with VARNAME +// that's not the right description above, but whatever, you get the idea +func (msg *MsgName) addAppendBy(w io.Writer, STRUCT, FUNCNAME, STRUCTVAR, VARNAME, VARTYPE string) string { + LOCK := msg.getLockname("x") + + funcdef := "func (x *" + STRUCT + ") " + FUNCNAME + "(y *" + VARTYPE + ") bool" + + fmt.Fprintln(w, funcdef, "{") + fmt.Fprintln(w, " "+LOCK+".Lock()") + fmt.Fprintln(w, " defer "+LOCK+".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range x."+STRUCTVAR+" {") + fmt.Fprintln(w, " if p."+VARNAME+" == y."+VARNAME+" {") + fmt.Fprintln(w, " return false") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " x."+STRUCTVAR+" = append(x."+STRUCTVAR+", y)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + + return funcdef +} diff --git a/generateFind.go b/generateFind.go index 1174efa..4ab9d10 100644 --- a/generateFind.go +++ b/generateFind.go @@ -38,10 +38,10 @@ 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 string) { +func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME string) string { LOCK := msg.getLockname("x") - fmt.Fprintln(w, "func (x *"+FRUIT+") DeleteBy"+COLOR+"(s string) bool {") + fmt.Fprintln(w, "func (x *"+FRUIT+") "+FUNCNAME+"(s string) bool {") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, "") @@ -55,14 +55,16 @@ func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) { fmt.Fprintln(w, " return false") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") + + return "func (x *" + FRUIT + ") DeleteBy" + COLOR + "(s string) bool {" } // 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 string) { +func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME string) string { LOCK := msg.getLockname("x") fmt.Fprintln(w, "// TESTING fails with 'go vet' warning") - fmt.Fprintln(w, "func (x *"+FRUIT+") DeleteBy"+COLOR+"(s string) *"+APPLE+" {") + fmt.Fprintln(w, "func (x *"+FRUIT+") "+FUNCNAME+"(s string) *"+APPLE+" {") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, "") @@ -79,6 +81,8 @@ func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR st fmt.Fprintln(w, " return nil") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") + + return "func (x *" + FRUIT + ") DeleteBy" + COLOR + "(s string) *" + APPLE + " {" } // new 2025 idea. useful? TODO: look at this again in 1y