summaryrefslogtreecommitdiff
path: root/generateInsert.go
blob: 79eb6adf854866386a159b1fa12a60e5473123e1 (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
// 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
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, "")
}