From 0d2dc9fb2518a34d5f60836a92ebeef4d57a3943 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 11 Jan 2025 10:22:39 -0600 Subject: rename --- generateIterator.go | 113 ---------------------------------------------------- generateSort.go | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 113 deletions(-) delete mode 100644 generateIterator.go create mode 100644 generateSort.go diff --git a/generateIterator.go b/generateIterator.go deleted file mode 100644 index a710212..0000000 --- a/generateIterator.go +++ /dev/null @@ -1,113 +0,0 @@ -package main - -import ( - "fmt" - "io" -) - -// only make one of these for each message in the protobuf file -func newIter(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) { - fmt.Fprintln(w, "// DEFINE THE ITERATOR. is unique to the "+APPLE+" protobuf message") - fmt.Fprintln(w, "// itializes a new iterator.") - fmt.Fprintln(w, "func New"+APPLE+"Iterator(things []*"+APPLE+") *"+APPLE+"Iterator {") - fmt.Fprintln(w, " return &"+APPLE+"Iterator{things: things}") - 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"+APPLES+"() []*"+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 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, "") - 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, "") -} - -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, "") - fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Len() int { return len(a) }") - fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Less(i, j int) bool { return a[i]."+COLOR+" < a[j]."+COLOR+" }") - 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"+APPLES+"()") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " sort.Sort("+APPLE+COLOR+"(things))") - fmt.Fprintln(w, "") - fmt.Fprintln(w, " iterator := New"+APPLE+"Iterator(things)") - fmt.Fprintln(w, " return iterator") - 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, "}") -} diff --git a/generateSort.go b/generateSort.go new file mode 100644 index 0000000..a710212 --- /dev/null +++ b/generateSort.go @@ -0,0 +1,113 @@ +package main + +import ( + "fmt" + "io" +) + +// only make one of these for each message in the protobuf file +func newIter(w io.Writer, FRUIT, APPLE, APPLES, LOCK string) { + fmt.Fprintln(w, "// DEFINE THE ITERATOR. is unique to the "+APPLE+" protobuf message") + fmt.Fprintln(w, "// itializes a new iterator.") + fmt.Fprintln(w, "func New"+APPLE+"Iterator(things []*"+APPLE+") *"+APPLE+"Iterator {") + fmt.Fprintln(w, " return &"+APPLE+"Iterator{things: things}") + 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"+APPLES+"() []*"+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 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, "") + 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, "") +} + +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, "") + fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Len() int { return len(a) }") + fmt.Fprintln(w, "func (a "+APPLE+COLOR+") Less(i, j int) bool { return a[i]."+COLOR+" < a[j]."+COLOR+" }") + 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"+APPLES+"()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " sort.Sort("+APPLE+COLOR+"(things))") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " iterator := New"+APPLE+"Iterator(things)") + fmt.Fprintln(w, " return iterator") + 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, "}") +} -- cgit v1.2.3