107 lines
3.5 KiB
Go
107 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
// generates Find() and some other stuff
|
|
|
|
func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string {
|
|
LOCK := msg.getLockname("x")
|
|
funcdef := "func (x *" + STRUCT + ") " + FUNCNAME + "(s string) *" + VARTYPE
|
|
|
|
fmt.Fprintln(w, "// lookup a", STRUCT, "by the ", COLOR)
|
|
fmt.Fprintln(w, funcdef, "{")
|
|
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."+VARNAME+" {")
|
|
fmt.Fprintln(w, " if x."+VARNAME+"[i]."+COLOR+" == s {")
|
|
fmt.Fprintln(w, " return x."+VARNAME+"[i]")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " }")
|
|
fmt.Fprintln(w, " return nil")
|
|
fmt.Fprintln(w, "}")
|
|
fmt.Fprintln(w, "")
|
|
|
|
return funcdef
|
|
}
|
|
|
|
func (msg *MsgName) deleteBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, VARNAME string) string {
|
|
LOCK := msg.getLockname("x")
|
|
funcdef := "func (x *" + FRUIT + ") " + FUNCNAME + "(s string) bool"
|
|
|
|
fmt.Fprintln(w, funcdef, "{")
|
|
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]."+VARNAME+" == 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, "")
|
|
|
|
return funcdef
|
|
}
|
|
|
|
// 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, FUNCNAME, VARNAME string) string {
|
|
LOCK := msg.getLockname("x")
|
|
|
|
funcdef := "func (x *" + FRUIT + ") " + FUNCNAME + "(s string) *" + APPLE
|
|
|
|
fmt.Fprintln(w, "// TESTING fails with 'go vet' warning")
|
|
fmt.Fprintln(w, funcdef, "{")
|
|
fmt.Fprintln(w, " "+LOCK+".Lock()")
|
|
fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
|
|
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]."+VARNAME+" == 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, "")
|
|
|
|
return funcdef
|
|
}
|
|
|
|
// new 2025 idea. useful? TODO: look at this again in 1y
|
|
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, "")
|
|
}
|