2025-01-10 17:49:14 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2025-01-11 04:27:45 -06:00
|
|
|
// generates Find() and some other stuff
|
2025-01-11 04:03:41 -06:00
|
|
|
|
2025-01-13 03:36:55 -06:00
|
|
|
func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT string, sortvals *Sort, childVar *MsgVar) string {
|
2025-01-12 04:40:12 -06:00
|
|
|
LOCK := msg.getLockname("x")
|
2025-01-13 03:36:55 -06:00
|
|
|
if childVar.VarType != "string" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
VARNAME := sortvals.VarName
|
|
|
|
VARTYPE := sortvals.VarType
|
|
|
|
COLOR := childVar.VarName
|
2025-01-12 04:40:12 -06:00
|
|
|
funcdef := "func (x *" + STRUCT + ") " + FUNCNAME + "(s string) *" + VARTYPE
|
|
|
|
|
|
|
|
fmt.Fprintln(w, "// lookup a", STRUCT, "by the ", COLOR)
|
|
|
|
fmt.Fprintln(w, funcdef, "{")
|
2025-01-11 04:03:41 -06:00
|
|
|
fmt.Fprintln(w, " if x == nil {")
|
|
|
|
fmt.Fprintln(w, " return nil")
|
|
|
|
fmt.Fprintln(w, " }")
|
2025-01-10 21:33:00 -06:00
|
|
|
fmt.Fprintln(w, "")
|
2025-01-11 04:03:41 -06:00
|
|
|
fmt.Fprintln(w, " "+LOCK+".RLock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-12 04:40:12 -06:00
|
|
|
fmt.Fprintln(w, " for i, _ := range x."+VARNAME+" {")
|
|
|
|
fmt.Fprintln(w, " if x."+VARNAME+"[i]."+COLOR+" == s {")
|
|
|
|
fmt.Fprintln(w, " return x."+VARNAME+"[i]")
|
2025-01-11 04:03:41 -06:00
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " }")
|
|
|
|
fmt.Fprintln(w, " return nil")
|
2025-01-10 21:33:00 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2025-01-12 04:40:12 -06:00
|
|
|
|
|
|
|
return funcdef
|
2025-01-10 21:33:00 -06:00
|
|
|
}
|
|
|
|
|
2025-01-12 04:25:02 -06:00
|
|
|
func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, VARNAME string) string {
|
2025-01-10 18:26:44 -06:00
|
|
|
LOCK := msg.getLockname("x")
|
2025-01-12 04:25:02 -06:00
|
|
|
funcdef := "func (x *" + FRUIT + ") " + FUNCNAME + "(s string) bool"
|
2025-01-10 18:26:44 -06:00
|
|
|
|
2025-01-12 04:25:02 -06:00
|
|
|
fmt.Fprintln(w, funcdef, "{")
|
2025-01-10 21:33:00 -06:00
|
|
|
fmt.Fprintln(w, " "+LOCK+".Lock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
|
2025-01-10 18:26:44 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {")
|
2025-01-12 04:25:02 -06:00
|
|
|
fmt.Fprintln(w, " if x."+APPLES+"[i]."+VARNAME+" == s {")
|
2025-01-10 18:26:44 -06:00
|
|
|
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, "")
|
2025-01-12 03:59:59 -06:00
|
|
|
|
2025-01-12 04:25:02 -06:00
|
|
|
return funcdef
|
2025-01-10 18:26:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// this tries to return the deleted one but is wrong/gives warning if mutex lock is in struct
|
2025-01-12 04:25:02 -06:00
|
|
|
func (msg *MsgName) deleteByWithCopy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, VARNAME string) string {
|
2025-01-10 18:26:44 -06:00
|
|
|
LOCK := msg.getLockname("x")
|
|
|
|
|
2025-01-12 04:25:02 -06:00
|
|
|
funcdef := "func (x *" + FRUIT + ") " + FUNCNAME + "(s string) *" + APPLE
|
|
|
|
|
2025-01-11 04:03:41 -06:00
|
|
|
fmt.Fprintln(w, "// TESTING fails with 'go vet' warning")
|
2025-01-12 04:25:02 -06:00
|
|
|
fmt.Fprintln(w, funcdef, "{")
|
2025-01-10 21:33:00 -06:00
|
|
|
fmt.Fprintln(w, " "+LOCK+".Lock()")
|
|
|
|
fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
|
2025-01-10 18:26:44 -06:00
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " var newr "+APPLE)
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, " for i, _ := range x."+APPLES+" {")
|
2025-01-12 04:25:02 -06:00
|
|
|
fmt.Fprintln(w, " if x."+APPLES+"[i]."+VARNAME+" == s {")
|
2025-01-10 18:26:44 -06:00
|
|
|
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, "")
|
2025-01-12 03:59:59 -06:00
|
|
|
|
2025-01-12 04:25:02 -06:00
|
|
|
return funcdef
|
2025-01-10 18:26:44 -06:00
|
|
|
}
|
2025-01-11 04:03:41 -06:00
|
|
|
|
2025-01-11 04:27:45 -06:00
|
|
|
// new 2025 idea. useful? TODO: look at this again in 1y
|
2025-01-11 04:03:41 -06:00
|
|
|
func (msg *MsgName) insertBy(w io.Writer, FRUIT, APPLES, APPLE string, COLOR string) {
|
|
|
|
LOCK := msg.getLockname("x")
|
|
|
|
|
|
|
|
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+" == y {")
|
|
|
|
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 z")
|
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|