summaryrefslogtreecommitdiff
path: root/generateInsert.go
blob: eb1fc956990082c7b80fec90d4496264eb1e99ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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
}