summaryrefslogtreecommitdiff
path: root/generateIterator.go
diff options
context:
space:
mode:
Diffstat (limited to 'generateIterator.go')
-rw-r--r--generateIterator.go44
1 files changed, 41 insertions, 3 deletions
diff --git a/generateIterator.go b/generateIterator.go
index f54bf60..a710212 100644
--- a/generateIterator.go
+++ b/generateIterator.go
@@ -14,7 +14,7 @@ func newIter(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) {
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "// safely returns a slice of pointers to the FRUIT protobufs")
- fmt.Fprintln(w, "func (x *"+FRUIT+") all() []*"+APPLE+" {")
+ fmt.Fprintln(w, "func (x *"+FRUIT+") all"+APPLES+"() []*"+APPLE+" {")
fmt.Fprintln(w, " "+LOCK+".RLock()")
fmt.Fprintln(w, " defer "+LOCK+".RUnlock()")
fmt.Fprintln(w, "")
@@ -55,7 +55,7 @@ func newIter(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) {
fmt.Fprintln(w, "")
}
-func newSortBy(w io.Writer, FRUIT, APPLE, COLOR string) {
+func newSortBy(w io.Writer, FRUIT, APPLE, APPLES, COLOR string) {
fmt.Fprintln(w, "// START sort by ", COLOR, "(this is all you need once the Iterator is defined)")
fmt.Fprintln(w, "type "+APPLE+COLOR+" []*"+APPLE+"")
fmt.Fprintln(w, "")
@@ -64,7 +64,7 @@ func newSortBy(w io.Writer, FRUIT, APPLE, COLOR string) {
fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Swap(i, j int) { a[i], a[j] = a[j], a[i] }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (x *"+FRUIT+") SortBy"+COLOR+"() *"+APPLE+"Iterator {")
- fmt.Fprintln(w, " things := x.all()")
+ fmt.Fprintln(w, " things := x.all"+APPLES+"()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " sort.Sort("+APPLE+COLOR+"(things))")
fmt.Fprintln(w, "")
@@ -73,3 +73,41 @@ func newSortBy(w io.Writer, FRUIT, APPLE, COLOR string) {
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "// END sort by", COLOR)
}
+
+func addAllFunc(w io.Writer, FRUIT, APPLE, LOCK string) {
+ fmt.Fprintln(w, "func (x *"+FRUIT+") All() *"+APPLE+"Iterator {")
+ fmt.Fprintln(w, " "+APPLE+"Pointers := x.selectAll"+APPLE+"()")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " iterator := New"+APPLE+"Iterator("+APPLE+"Pointers)")
+ fmt.Fprintln(w, " return iterator")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+}
+
+func addLenFunc(w io.Writer, FRUIT, APPLES, LOCK string) {
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "func (x *"+FRUIT+") Len() int {")
+ 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, "")
+}
+
+func addSelectAll(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) {
+ fmt.Fprintln(w, "// safely returns a slice of pointers to the "+APPLE+" protobufs")
+ fmt.Fprintln(w, "func (x *"+FRUIT+") selectAll"+APPLE+"() []*"+APPLE+" {")
+ 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, "}")
+}