diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 117 |
1 files changed, 77 insertions, 40 deletions
@@ -6,12 +6,27 @@ import ( "os" ) +var names map[string]string + func main() { f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600) + names = make(map[string]string) + names["package"] = "testautogen" + names["lock"] = "gitTagslock" + names["Base"] = "GitTag" + names["Bases"] = "GitTags" + + names["base"] = "gitTag" + names["sortBy"] = "ByPath" + names["sortKey"] = "Refname" + header(f, "testautogen") syncLock(f, "gitTagslock") iterTop(f, "GitTag") iterNext(f, "GitTag") + iterSort(f) + iterAppend(f) + iterEnd(f) } func header(w io.Writer, name string) { @@ -81,49 +96,71 @@ func iterNext(w io.Writer, name string) { 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 iterSort(w io.Writer) { + fmt.Fprintln(w, "func (all *" + names["Bases"] + ") All() *" + names["Base"] + "Iterator {") + fmt.Fprintln(w, " " + names["base"] + "Pointers := all.selectAll" + names["Base"] + "()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " iterator := New" + names["Base"] + "Iterator(" + names["base"] + "Pointers)") + fmt.Fprintln(w, " return iterator") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Sort" + names["sortBy"] + "() *" + names["Base"] + "Iterator {") + fmt.Fprintln(w, " packs := all.selectAll" + names["Base"] + "()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " sort.Sort(" + names["Base"] + "" + names["sortBy"] + "(packs))") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " iterator := New" + names["Base"] + "Iterator(packs)") + fmt.Fprintln(w, " return iterator") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "func (all *" + names["Bases"] + ") Len() int {") + fmt.Fprintln(w, " " + names["lock"] + ".RLock()") + fmt.Fprintln(w, " defer " + names["lock"] + ".RUnlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " return len(all." + names["Bases"] + ")") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") } -func (all *Repos) Len() int { - repolock.RLock() - defer repolock.RUnlock() - - return len(all.Repos) +func iterEnd(w io.Writer) { + fmt.Fprintln(w, "type " + names["Base"] + "" + names["sortBy"] + " []*" + names["Base"] + "") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Len() int { return len(a) }") + fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Less(i, j int) bool { return a[i]." + names["sortKey"] + " < a[j]." + names["sortKey"] + " }") + fmt.Fprintln(w, "func (a " + names["Base"] + "" + names["sortBy"] + ") Swap(i, j int) { a[i], a[j] = a[j], a[i] }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// safely returns a slice of pointers to the " + names["Base"] + " protobufs") + fmt.Fprintln(w, "func (all *" + names["Bases"] + ") selectAll" + names["Base"] + "() []*" + names["Base"] + " {") + fmt.Fprintln(w, " " + names["lock"] + ".RLock()") + fmt.Fprintln(w, " defer " + names["lock"] + ".RUnlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " // Create a new slice to hold pointers to each " + names["Base"] + "") + fmt.Fprintln(w, " var aStuff []*" + names["Base"] + "") + fmt.Fprintln(w, " aStuff = make([]*" + names["Base"] + ", len(all." + names["Bases"] + "))") + fmt.Fprintln(w, " for i, p := range all." + names["Bases"] + " {") + fmt.Fprintln(w, " aStuff[i] = p // Copy pointers for safe iteration") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " return aStuff") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") } -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 +func iterAppend(w io.Writer) { + fmt.Fprintln(w, "// enforces no duplicate Refname names") + fmt.Fprintln(w, "func (all *GitTags) Append(newP *GitTag) bool {") + fmt.Fprintln(w, " " + names["lock"] + ".Lock()") + fmt.Fprintln(w, " defer " + names["lock"] + ".Unlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " for _, p := range all.GitTags {") + fmt.Fprintln(w, " if p.Refname == newP.Refname {") + fmt.Fprintln(w, " return false") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, " }") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " all.GitTags = append(all.GitTags, newP)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") } -*/ |
