summaryrefslogtreecommitdiff
path: root/generateSort.go
blob: e5cfa20fca9ccd7ba9c3c2d05fbf36780df411b9 (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
package main

import (
	"fmt"
	"io"
)

func (msg *MsgName) iterTop(w io.Writer) {
	var BASE string = msg.Name

	fmt.Fprintln(w, "type "+BASE+"Iterator struct {")
	fmt.Fprintln(w, "	sync.RWMutex")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	things []*"+BASE)
	fmt.Fprintln(w, "	index int")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// New"+BASE+"Iterator initializes a new iterator.")
	fmt.Fprintln(w, "func New"+BASE+"Iterator(things []*"+BASE+") *"+BASE+"Iterator {")
	fmt.Fprintln(w, "	return &"+BASE+"Iterator{things: things}")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more things.")
	fmt.Fprintln(w, "// Use Scan() in a loop, similar to a while loop")
	fmt.Fprintln(w, "//")
	fmt.Fprintln(w, "//	for iterator.Scan() ")
	fmt.Fprintln(w, "//		d := iterator.Next(")
	fmt.Fprintln(w, "//		fmt.Println(\"found UUID:\", d.Uuid")
	fmt.Fprintln(w, "//	}")
	fmt.Fprintln(w, "func (it *"+BASE+"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, "")
}

func (msg *MsgName) iterNext(w io.Writer) {
	var BASE string = msg.Name

	fmt.Fprintln(w, "// Next() returns the next thing in the array")
	fmt.Fprintln(w, "func (it *"+BASE+"Iterator) Next() *"+BASE+" {")
	fmt.Fprintln(w, "	if it.things[it.index-1] == nil {")
	fmt.Fprintln(w, "		for i, d := range it.things {")
	fmt.Fprintln(w, "			fmt.Println(\"i =\", i, d)")
	fmt.Fprintln(w, "		}")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	return it.things[it.index-1]")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
}

func (pf *File) selectAllFunc(w io.Writer) {
	var BASES string = pf.Bases.Name
	var BASE string = pf.Base.Name
	var LOCK string = pf.Bases.Lockname

	fmt.Fprintln(w, "func (x *"+BASES+") All() *"+BASE+"Iterator {")
	fmt.Fprintln(w, "	"+BASE+"Pointers := x.selectAll"+BASE+"()")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	iterator := New"+BASE+"Iterator("+BASE+"Pointers)")
	fmt.Fprintln(w, "	return iterator")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "func (x *"+BASES+") Len() int {")
	fmt.Fprintln(w, "	"+LOCK+".RLock()")
	fmt.Fprintln(w, "	defer "+LOCK+".RUnlock()")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	return len(x."+BASES+")")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
}

func (pf *File) sortByFunc(w io.Writer) {
	var BASES string = pf.Bases.Name
	var BASE string = pf.Base.Name

	for _, SORT := range pf.Base.Sort {
		fmt.Fprintln(w, "func (x *"+BASES+") SortBy"+SORT+"() *"+BASE+"Iterator {")
		fmt.Fprintln(w, "	things := x.selectAll"+BASE+"()")
		fmt.Fprintln(w, "")
		fmt.Fprintln(w, "	sort.Sort("+BASE+""+SORT+"(things))")
		fmt.Fprintln(w, "")
		fmt.Fprintln(w, "	iterator := New"+BASE+"Iterator(things)")
		fmt.Fprintln(w, "	return iterator")
		fmt.Fprintln(w, "}")
		fmt.Fprintln(w, "")

		fmt.Fprintln(w, "type "+BASE+""+SORT+" []*"+BASE+"")
		fmt.Fprintln(w, "")
		fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Len() int           { return len(a) }")
		fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Less(i, j int) bool { return a[i]."+SORT+" < a[j]."+SORT+" }")
		fmt.Fprintln(w, "func (a "+BASE+""+SORT+") Swap(i, j int)      { a[i], a[j] = a[j], a[i] }")
		fmt.Fprintln(w, "")
	}
}

func (pf *File) iterSelect(w io.Writer) {
	var BASES string = pf.Bases.Name
	var BASE string = pf.Base.Name
	var LOCK string = pf.Bases.Lockname

	fmt.Fprintln(w, "// safely returns a slice of pointers to the "+BASE+" protobufs")
	fmt.Fprintln(w, "func (x *"+BASES+") selectAll"+BASE+"() []*"+BASE+" {")
	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 "+BASE+"")
	fmt.Fprintln(w, "	var tmp []*"+BASE+"")
	fmt.Fprintln(w, "	tmp = make([]*"+BASE+", len(x."+BASES+"))")
	fmt.Fprintln(w, "	for i, p := range x."+BASES+" {")
	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, "}")
}