autogenpb/generateInsert.go

38 lines
1.1 KiB
Go

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"fmt"
"io"
)
// new idea. useful? TODO: look at this again in 1y
// Mar 2025. yes. this is useful
func (msg *MsgName) insertBy(w io.Writer, FRUITS, FRUIT, APPLE string, COLOR string) string {
LOCK := msg.getLockname("x")
funcdef := "func (x *" + FRUITS + ") InsertBy" + APPLE + " (y " + COLOR + ") *" + FRUIT + " {"
fmt.Fprintln(w, "// returns a "+FRUIT+" if "+APPLE+" matches, otherwise create")
fmt.Fprintln(w, funcdef)
fmt.Fprintln(w, " "+LOCK+".Lock()")
fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, z := range x."+FRUITS+" {")
fmt.Fprintln(w, " if z."+APPLE+" == y {")
fmt.Fprintln(w, " return z")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " z := new("+FRUIT+")")
fmt.Fprintln(w, " z."+APPLE+" = y")
fmt.Fprintln(w, " x."+FRUITS+" = append(x."+FRUITS+", z)")
fmt.Fprintln(w, " return z")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
return funcdef
}