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

import (
	"fmt"
	"io"
	"os"
)

func main() {
	f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
	header(f, "testautogen")
	syncLock(f, "gitTagslock")
	iterTop(f, "GitTag")
	iterNext(f, "GitTag")
}

func header(w io.Writer, name string) {
	fmt.Fprintln(w, "package "+name)
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// this is becoming a standard format")
	fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "import (")
	fmt.Fprintln(w, "	\"fmt\"")
	fmt.Fprintln(w, "	\"os\"")
	fmt.Fprintln(w, "	\"sort\"")
	fmt.Fprintln(w, "	\"sync\"")
	fmt.Fprintln(w, ")")
	fmt.Fprintln(w, "")
}

func syncLock(w io.Writer, name string) {
	fmt.Fprintln(w, "// bad global lock until I figure out some other plan")
	fmt.Fprintln(w, "var "+name+" sync.RWMutex")
	fmt.Fprintln(w, "")
}

func iterTop(w io.Writer, name string) {
	fmt.Fprintln(w, "type "+name+"Iterator struct {")
	fmt.Fprintln(w, "	sync.RWMutex")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "	packs []*"+name)
	fmt.Fprintln(w, "	index int")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// New"+name+"Iterator initializes a new iterator.")
	fmt.Fprintln(w, "func New"+name+"Iterator(packs []*"+name+") *"+name+"Iterator {")
	fmt.Fprintln(w, "	return &"+name+"Iterator{packs: packs}")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
	fmt.Fprintln(w, "// Scan moves to the next element and returns false if there are no more packs.")
	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 *"+name+"Iterator) Scan() bool {")
	fmt.Fprintln(w, "	if it.index >= len(it.packs) {")
	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 iterNext(w io.Writer, name string) {
	fmt.Fprintln(w, "// Next() returns the next thing in the array")
	fmt.Fprintln(w, "func (it *" + name + "Iterator) Next() *" + name + " {")
	fmt.Fprintln(w, "	if it.packs[it.index-1] == nil {")
	fmt.Fprintln(w, "		for i, d := range it.packs {")
	fmt.Fprintln(w, "			fmt.Println(\"i =\", i, d)")
	fmt.Fprintln(w, "		}")
	fmt.Fprintln(w, "		fmt.Println(\"protobuf autogenpb sort error len =\", len(it.packs))")
	fmt.Fprintln(w, "		fmt.Println(\"protobuf autogenpb sort error next == nil\", it.index, it.index-1)")
	fmt.Fprintln(w, "		os.Exit(-1)")
	fmt.Fprintln(w, "	}")
	fmt.Fprintln(w, "	return it.packs[it.index-1]")
	fmt.Fprintln(w, "}")
	fmt.Fprintln(w, "")
}

/*

func (r *Repos) All() *RepoIterator {
	repoPointers := r.selectAllRepo()

	iterator := NewRepoIterator(repoPointers)
	return iterator
}

func (r *Repos) SortByPath() *RepoIterator {
	packs := r.selectAllRepo()

	sort.Sort(RepoByPath(packs))

	iterator := NewRepoIterator(packs)
	return iterator
}

func (all *Repos) Len() int {
	repolock.RLock()
	defer repolock.RUnlock()

	return len(all.Repos)
}

type RepoByPath []*Repo

func (a RepoByPath) Len() int           { return len(a) }
func (a RepoByPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
func (a RepoByPath) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// safely returns a slice of pointers to the Repo protobufs
func (all *Repos) selectAllRepo() []*Repo {
	repolock.RLock()
	defer repolock.RUnlock()

	// Create a new slice to hold pointers to each Repo
	var aRepos []*Repo
	aRepos = make([]*Repo, len(all.Repos))
	for i, p := range all.Repos {
		aRepos[i] = p // Copy pointers for safe iteration
	}

	return aRepos
}
*/