new easier to read Find() Delete() and Append()

This commit is contained in:
Jeff Carr 2025-01-10 18:26:44 -06:00
parent 57f8f88ae9
commit dc86ae010f
3 changed files with 92 additions and 8 deletions

View File

@ -19,6 +19,14 @@ build:
simpleMutexProtoc:
../autogenpb --proto fruit.proto --package main
# why does this fail to compile? I'm not sure. maybe someone smart can figure it out
# basically, it just trys to return the deleted record but says something
# about the RWmutex lock being copied and GO fails to compile
# I'm don't grok what is going on. This autogenerated code should
# provide as simple as one could hope for automated way to try to debug it though!
simpleMutexProtocWithDeleteCopy:
../autogenpb --proto fruit.proto --package main --delete
simpleMutexGlobal:
../autogenpb --proto fruit.proto --package main --mutex=false

21
sort.go
View File

@ -76,6 +76,11 @@ func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writ
return fmt.Errorf("failed to find struct %s", find.VarType)
}
log.Printf("FOUND!: %s %s for %s\n", find.VarName, find.VarType, found.Name)
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 {
if v.HasSort {
// log.Printf("\tSort!: %s %s for %s\n", find.VarName, find.VarType, v.VarName)
@ -83,20 +88,24 @@ func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writ
log.Printf("\t(x %s) SortdBy%s() *%sIter\n", parent.Name, newS, find.VarType)
}
if v.HasUnique {
// log.Printf("\tUniq!: %s %s for %s\n", find.VarName, find.VarType, v.VarName)
newS := cases.Title(language.English, cases.NoLower).String(v.VarName)
log.Printf("\t(x %s) AppendUniqueBy%s(%s)\n", parent.Name, newS, find.VarType)
var FRUIT string = parent.Name
var APPLES string = cases.Title(language.English, cases.NoLower).String(find.VarName)
var APPLE string = find.VarType
var COLOR string = newS
COLORS = append(COLORS, COLOR)
log.Printf("\t(x %s) AppendUniqueBy%s(%s)\n", parent.Name, newS, find.VarType)
parent.appendUniqueBy(wFind, FRUIT, APPLES, APPLE, COLOR)
log.Printf("\t(x %s) FindBy%s(string) *%s\n", parent.Name, newS, find.VarType)
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)
}
}
}
if len(COLORS) > 0 {
parent.appendUnique(wFind, FRUIT, APPLES, APPLE, COLORS)
}
return nil
}

View File

@ -23,13 +23,13 @@ func (msg *MsgName) appendUnique(w io.Writer, FRUIT, APPLES, APPLE string, COLOR
fmt.Fprintln(w, " "+LOCK+".Lock()")
fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, p := range x."+APPLES+" {")
for _, COLOR := range COLORS {
fmt.Fprintln(w, " for _, p := range x."+APPLES+" {")
fmt.Fprintln(w, " if p."+COLOR+" == newP."+COLOR+" {")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
}
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", newP)")
fmt.Fprintln(w, " return true")
@ -57,3 +57,70 @@ func (msg *MsgName) appendUniqueBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR stri
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func (msg *MsgName) findBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) {
LOCK := msg.getLockname("x")
fmt.Fprintln(w, "// TESTING")
fmt.Fprintln(w, "// find a dependancy by the go path")
fmt.Fprintln(w, "func (x *"+FRUIT+") FindBy"+COLOR+"(s string) *"+APPLE+" {")
fmt.Fprintln(w, " if x == nil {")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
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, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR string) {
LOCK := msg.getLockname("x")
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, "")
fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {")
fmt.Fprintln(w, " if x."+APPLES+"[i]."+COLOR+" == 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")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}
// 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) {
LOCK := msg.getLockname("x")
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, "")
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, " 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]")
fmt.Fprintln(w, " return &newr")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
}