summaryrefslogtreecommitdiff
path: root/generateSort.go
blob: 100f28291d8ac932ddaf869b7c536dae0f39bc18 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main

import (
	"fmt"
	"io"
)

func (msg *MsgName) newIterAll(w io.Writer, FRUIT, APPLE, APPLES, LOCKold string) string {
	LOCK := msg.getLockname("x")

	funcdef := "func (x *" + FRUIT + ") all" + APPLES + "() []*" + APPLE + " {"

	fmt.Fprintln(w, "// safely returns a slice of pointers to the FRUIT protobufs")
	fmt.Fprintln(w, funcdef)
	fmt.Fprintln(w, "	"+LOCK+".RLock()")
	fmt.Fprintln(w, "	defer "+LOCK+".RUnlock()")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	// Create a new slice to hold pointers to each FRUIT")
	fmt.Fprintln(w, "	var tmp []*"+APPLE+"")
	fmt.Fprintln(w, "	tmp = make([]*"+APPLE+", len(x."+APPLES+"))")
	fmt.Fprintln(w, "	for i, p := range x."+APPLES+" {")
	fmt.Fprintln(w, "		tmp[i] = p // Copy pointers for safe iteration")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	return tmp")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")

	return funcdef
}

// only make one of these for each message in the protobuf file
func newIter(w io.Writer, msg *MsgName) string {
	if !msg.NeedIter {
		return "iter already done for " + msg.Name
	}
	msg.NeedIter = false
	APPLE := msg.Name

	// should this be 'new' or 'New' ? Does it matter? I think it's totally internal here
	// in this file where it is "new or "New". I changed it to lower case 2025.01.12
	funcdef := "func new" + APPLE + "Iterator(things []*" + APPLE + ") *" + APPLE + "Iterator"

	fmt.Fprintln(w, "// DEFINE THE", APPLE, "ITERATOR.")
	fmt.Fprintln(w, "// itializes a new iterator.")
	fmt.Fprintln(w, funcdef, "{")
	fmt.Fprintln(w, "	return &"+APPLE+"Iterator{things: things}")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "type "+APPLE+"Iterator struct {")
	fmt.Fprintln(w, "	sync.RWMutex // this isn't getting used properly yet?")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	things []*"+APPLE+"")
	fmt.Fprintln(w, "	index  int")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "func (it *"+APPLE+"Iterator) Scan() bool {")
	fmt.Fprintln(w, "	if it.index >= len(it.things) {")
	fmt.Fprintln(w, "		return false")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	it.index++")
	fmt.Fprintln(w, "	return true")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// Next() returns the next thing in the array")
	fmt.Fprintln(w, "func (it *"+APPLE+"Iterator) Next() *"+APPLE+" {")
	fmt.Fprintln(w, "	if it.things[it.index-1] == nil {")
	fmt.Fprintln(w, "		fmt.Println(\"Next() error in "+APPLE+"Iterator\", it.index)")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	return it.things[it.index-1]")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// END DEFINE THE ITERATOR")
	fmt.Fprintln(w, "")

	return funcdef
}

// maybe there are better ways in GO now adays // that's fine though. this is easy to read
// TODO; figure out what types this actually works on
// TODO; add timestamppb compare
func (msg *MsgName) newSortType(w io.Writer, STRUCT, VARNAME string) string {

	// Can these be lower case? Should they be lower case? maybe sort.Sort() requires upper case?
	fmt.Fprintln(w, "// sort struct by", VARNAME)
	fmt.Fprintln(w, "type "+STRUCT+VARNAME+" []*"+STRUCT+"")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "func (a "+STRUCT+VARNAME+") Len() int           { return len(a) }")
	fmt.Fprintln(w, "func (a "+STRUCT+VARNAME+") Less(i, j int) bool { return a[i]."+VARNAME+" < a[j]."+VARNAME+" }")
	fmt.Fprintln(w, "func (a "+STRUCT+VARNAME+") Swap(i, j int)      { a[i], a[j] = a[j], a[i] }")
	fmt.Fprintln(w, "")

	return "type " + STRUCT + VARNAME + " []*" + STRUCT + "  // { return a[i]." + VARNAME + " < a[j]." + VARNAME + " }"
}

func (msg *MsgName) newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELECT, VARNAME string) string {
	funcdef := "func (x *" + STRUCT + ") " + SORTBY + "() *" + ITER + "Iterator"

	fmt.Fprintln(w, funcdef, "{")
	fmt.Fprintln(w, "	// copy the pointers as fast as possible.")
	fmt.Fprintln(w, "	things := x."+SELECT+"()")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// todo: try slices.SortFunc() instead to see what happens")
	fmt.Fprintln(w, "	sort.Sort("+SORTNAME+"(things))")
	fmt.Fprintln(w, "//    slices.SortFunc(things, func(a, b *"+STRUCT+") bool {")
	fmt.Fprintln(w, "//        return a."+VARNAME+" < b."+VARNAME+" // Sort by ??. let the compiler work it out??")
	fmt.Fprintln(w, "//    })")
	// should this be 'new' or 'New' ? Does it matter? I think it's totally internal here
	// in this file where it is "new or "New". I changed it to lower case 2025.01.12
	fmt.Fprintln(w, "	return new"+ITER+"Iterator(things)")
	fmt.Fprintln(w, "}")

	return funcdef
}

func (msg *MsgName) addAllFunc(w io.Writer, FRUIT, APPLE, APPLES string) string {
	funcdef := "func (x *" + FRUIT + ") All() *" + APPLE + "Iterator {"

	fmt.Fprintln(w, funcdef)
	fmt.Fprintln(w, "	"+APPLE+"Pointers := x.selectAll"+APPLES+"()")
	fmt.Fprintln(w, "")
	// should this be 'new' or 'New' ? Does it matter? I think it's totally internal here. I think there are only 3 places
	// in this file where it is "new or "New". I changed it to lower case 2025.01.12
	fmt.Fprintln(w, "	iterator := new"+APPLE+"Iterator("+APPLE+"Pointers)")
	fmt.Fprintln(w, "	return iterator")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")

	return funcdef
}

func (msg *MsgName) addLenFunc(w io.Writer, FRUIT, APPLES, LOCKold string) string {
	LOCK := msg.getLockname("x")

	funcdef := "func (x *" + FRUIT + ") Len() int {"
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, funcdef)
	fmt.Fprintln(w, "	"+LOCK+".RLock()")
	fmt.Fprintln(w, "	defer "+LOCK+".RUnlock()")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	return len(x."+APPLES+")")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")

	return funcdef
}

func (msg *MsgName) addSelectAll(w io.Writer, FRUIT, APPLE, APPLES, LOCKold string) string {
	LOCK := msg.getLockname("x")

	funcdef := "func (x *" + FRUIT + ") selectAll" + APPLES + "() []*" + APPLE
	fmt.Fprintln(w, "// safely returns a slice of pointers to the "+APPLE+" protobufs")
	fmt.Fprintln(w, funcdef, "{")
	fmt.Fprintln(w, "	"+LOCK+".RLock()")
	fmt.Fprintln(w, "	defer "+LOCK+".RUnlock()")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	// Create a new slice to hold pointers to each "+APPLE+"")
	fmt.Fprintln(w, "	var tmp []*"+APPLE+"")
	fmt.Fprintln(w, "	tmp = make([]*"+APPLE+", len(x."+APPLES+"))")
	fmt.Fprintln(w, "	for i, p := range x."+APPLES+" {")
	fmt.Fprintln(w, "		tmp[i] = p // Copy pointers for safe iteration")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	return tmp")
	fmt.Fprintln(w, "}")

	return funcdef
}