add Delete()
This commit is contained in:
parent
ace99c8aa8
commit
35f8270d54
17
generate.go
17
generate.go
|
@ -197,6 +197,23 @@ func (pb *Files) makeNewSortfile(pf *File) error {
|
|||
*/
|
||||
}
|
||||
|
||||
// add Delete()
|
||||
for _, s := range pf.ToSort {
|
||||
PARENT := s.MsgName
|
||||
CHILD := s.VarType
|
||||
VARNAME := s.VarName
|
||||
|
||||
pmsg := pf.findMsg(s.MsgName)
|
||||
if pmsg == nil {
|
||||
return fmt.Errorf("failed to find struct %s", s.MsgName)
|
||||
}
|
||||
|
||||
if PARENT == VARNAME {
|
||||
funcdef := pmsg.addDeleteFunc(wSort, PARENT, VARNAME, CHILD)
|
||||
log.Printf("Adding %s\n", funcdef)
|
||||
}
|
||||
}
|
||||
|
||||
// add Find() Delete() Append() Insert()
|
||||
for _, s := range pf.ToSort {
|
||||
PARENT := s.MsgName
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// generate Delete() functions
|
||||
|
||||
func (msg *MsgName) addDeleteFunc(w io.Writer, FRUIT, APPLES, APPLE string) string {
|
||||
LOCK := msg.getLockname("x")
|
||||
funcdef := "func (x *" + FRUIT + ") Delete(y *" + APPLE + ") 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] == y {")
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
|
@ -37,76 +37,3 @@ func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT string, sortval
|
|||
|
||||
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, "")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// new 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, "")
|
||||
}
|
Loading…
Reference in New Issue