close to being able to remove the old functions

This commit is contained in:
Jeff Carr 2025-01-10 22:27:38 -06:00
parent 6ce9c29135
commit f3297022a8
3 changed files with 97 additions and 52 deletions

View File

@ -151,7 +151,9 @@ func main() {
pb.marshal(pf) pb.marshal(pf)
// make the sort.pb.go file // make the sort.pb.go file
pb.makeNewSortfile(pf) if err := pb.makeNewSortfile(pf); err != nil {
badExit(err)
}
} }
func okExit(s string) { func okExit(s string) {

104
sort.go
View File

@ -48,25 +48,39 @@ func (pb *Files) makeNewSortfile(pf *File) error {
*/ */
// attempt to add sort functions for pf.Base // attempt to add sort functions for pf.Base
pf.processMessage(pf.Bases, wSort, wFind) if err := pf.processMessage(pf.Bases, wSort, wFind); err != nil {
pf.processMessage(pf.Base, wSort, wFind) return err
}
if err := pf.processMessage(pf.Base, wSort, wFind); err != nil {
return err
}
return nil return nil
} }
// 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
// APPLES == the variable name of the repeated struct
func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error { func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error {
var FRUIT string = cases.Title(language.English, cases.NoLower).String(msg.Name) var FRUIT string = cases.Title(language.English, cases.NoLower).String(msg.Name)
log.Printf("Generating functions for %s\n", FRUIT) log.Printf("Generating functions for %s\n", FRUIT)
for _, v := range msg.Vars { for _, v := range msg.Vars {
if !v.IsRepeated { if !v.IsRepeated {
log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType)
continue continue
} }
log.Printf("\tFOUND REPEATED %s %s\n", v.VarName, v.VarType)
// use easier to read variable names APPLE and APPLES in the code // use easier to read variable names APPLE and APPLES in the code
var APPLE string = v.VarType var APPLE string = v.VarType
var APPLES string = cases.Title(language.English, cases.NoLower).String(v.VarName) var APPLES string = cases.Title(language.English, cases.NoLower).String(v.VarName)
// try and find the message struct for APPLE // try and find the message struct for APPLE
var found *MsgName var found *MsgName
if pf.Base.Name == APPLE {
found = pf.Base
}
for _, m := range pf.MsgNames { for _, m := range pf.MsgNames {
if m.Name == APPLE { if m.Name == APPLE {
found = m found = m
@ -77,44 +91,74 @@ func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error {
return fmt.Errorf("failed to find struct %s", APPLE) return fmt.Errorf("failed to find struct %s", APPLE)
} }
found.addSortByMsg(FRUIT, APPLES, APPLE, wSort, wFind) log.Printf("FOUND: %s %s for %s\n", APPLES, APPLE, FRUIT)
found.simpleAppend(wFind, FRUIT, APPLES, APPLE)
found.addAppendByMsg(wFind, FRUIT, APPLES, APPLE)
found.addDeleteByMsg(wFind, FRUIT, APPLES, APPLE)
found.addInsertByMsg(wFind, FRUIT, APPLES, APPLE) // new idea
found.addSortByMsg(wSort, FRUIT, APPLES, APPLE)
} }
return nil return nil
} }
func (parent *MsgName) addSortByMsg(FRUIT, APPLES, APPLE string, wSort, wFind io.Writer) error { func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
log.Printf("\tFOUND!: %s %s for %s\n", APPLES, APPLE, FRUIT) log.Printf("\tDELETE: %s %s for %s\n", APPLES, APPLE, FRUIT)
var COLORS []string var COLORS []string
for _, v := range parent.Vars {
if !v.HasUnique {
continue
}
var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName)
COLORS = append(COLORS, COLOR)
log.Printf("\t\t(x %s) DeleteBy%s(string) *%s\n", FRUIT, COLOR, APPLE)
if argv.Delete {
parent.deleteByWithCopy(w, FRUIT, APPLES, APPLE, COLOR)
} else {
parent.deleteBy(w, FRUIT, APPLES, APPLE, COLOR)
}
}
}
func (parent *MsgName) addInsertByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT)
for _, v := range parent.Vars {
if v.HasUnique {
var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName)
log.Printf("\t\t(x %s) InsertBy%s(string) *%s\n", FRUIT, COLOR, APPLE)
parent.insertByColor(w, FRUIT, APPLES, APPLE, COLOR)
}
}
}
func (parent *MsgName) addAppendByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
log.Printf("\tAPPEND!: %s %s for %s\n", APPLES, APPLE, FRUIT)
var COLORS []string
for _, v := range parent.Vars {
if v.HasUnique {
var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName)
COLORS = append(COLORS, COLOR)
log.Printf("\t\t(x %s) AppendUniqueBy%s(%s)\n", FRUIT, COLOR, APPLE)
parent.appendUniqueBy(w, FRUIT, APPLES, APPLE, COLOR)
}
}
if len(COLORS) > 0 {
parent.appendUnique(w, FRUIT, APPLES, APPLE, COLORS)
}
}
func (parent *MsgName) addSortByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
log.Printf("\tSORT!: %s %s for %s\n", APPLES, APPLE, FRUIT)
for _, v := range parent.Vars { for _, v := range parent.Vars {
if v.HasSort { if v.HasSort {
// log.Printf("\tSort!: %s %s for %s\n", APPLES, APPLE, v.VarName) // log.Printf("\tSort!: %s %s for %s\n", APPLES, APPLE, v.VarName)
newS := cases.Title(language.English, cases.NoLower).String(v.VarName) newS := cases.Title(language.English, cases.NoLower).String(v.VarName)
log.Printf("\t(x %s) SortdBy%s() *%sIter\n", parent.Name, newS, APPLE) log.Printf("\t\t(x %s) SortdBy%s() *%sIter\n", FRUIT, 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, 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, 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)
}
return nil
}

View File

@ -30,29 +30,28 @@ func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
func (msg *MsgName) insertByColors(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { func (msg *MsgName) insertByColor(w io.Writer, FRUIT, APPLES, APPLE string, COLOR string) {
LOCK := msg.getLockname("x") LOCK := msg.getLockname("x")
for _, COLOR := range COLORS {
fmt.Fprintln(w, "// TESTING") fmt.Fprintln(w, "// TESTING")
fmt.Fprintln(w, "// returns an "+APPLE+" if "+COLOR+" matches, otherwise create") 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, "func (x *"+FRUIT+") InsertBy"+COLOR+" (y string) *"+APPLE+" {")
fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " "+LOCK+".Lock()")
fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") fmt.Fprintln(w, " for _, p := range x."+APPLES+" {")
fmt.Fprintln(w, " if p."+COLOR+" == "+COLOR+" {") fmt.Fprintln(w, " if p."+COLOR+" == y {")
fmt.Fprintln(w, " return p") fmt.Fprintln(w, " return p")
fmt.Fprintln(w, " }") fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }") fmt.Fprintln(w, " }")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " z := new("+APPLE+")") fmt.Fprintln(w, " z := new("+APPLE+")")
fmt.Fprintln(w, " z."+COLOR+" := y") fmt.Fprintln(w, " z."+COLOR+" = y")
fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", z)") fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", z)")
fmt.Fprintln(w, " return true") fmt.Fprintln(w, " return z")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
} }
}
func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) { func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLORS []string) {
LOCK := msg.getLockname("x") LOCK := msg.getLockname("x")