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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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, "")
|
|
|
|
}
|