autogenpb/generateFind.go

110 lines
3.8 KiB
Go

package main
import (
"fmt"
"io"
)
// generates Find() and some other stuff
func (msg *MsgName) getLockname(s string) string {
// leave this function stubbed in for development of autogenpb
// if argv.Mutex {
// // use the mutex lock from the modified protoc.pb.go file
// return s + ".Lock"
// // return s // causes Marshal() to panic? always use the variable name 'Lock'?
// }
// a single global lock by struct name
return msg.Lockname
}
func generateFindBy(w io.Writer, FRUIT, APPLES, APPLE, COLOR, LOCK string) {
fmt.Fprintln(w, "// lookup a", FRUIT, "by the ", COLOR)
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, FUNCNAME string) string {
LOCK := msg.getLockname("x")
fmt.Fprintln(w, "func (x *"+FRUIT+") "+FUNCNAME+"(s string) bool {")
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]."+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, "")
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, FUNCNAME string) string {
LOCK := msg.getLockname("x")
fmt.Fprintln(w, "// TESTING fails with 'go vet' warning")
fmt.Fprintln(w, "func (x *"+FRUIT+") "+FUNCNAME+"(s string) *"+APPLE+" {")
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]."+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, "")
return "func (x *" + FRUIT + ") DeleteBy" + COLOR + "(s string) *" + APPLE + " {"
}
// 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, "")
}