2025-02-01 11:41:48 -06:00
|
|
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the GPL 3.0
|
|
|
|
|
2025-01-19 02:36:32 -06:00
|
|
|
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
|
|
|
|
}
|