From 95afc5e0b47cd4788ade944f87e3452c5d4991c7 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 11 Jan 2025 05:53:52 -0600 Subject: [PATCH] add a new iterator generate file --- generate.go | 19 ++++++------ generateIterator.go | 75 +++++++++++++++++++++++++++++++++++++++++++++ generateSort.go | 6 ++-- patchset.HAMDMADE | 63 +++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 generateIterator.go create mode 100644 patchset.HAMDMADE diff --git a/generate.go b/generate.go index e554b4d..30e0315 100644 --- a/generate.go +++ b/generate.go @@ -25,7 +25,7 @@ func (pb *Files) makeNewSortfile(pf *File) error { pf.selectAllFunc(wSort) pf.iterSelect(wSort) - pf.sortByFunc(wSort) + pf.sortByFunc(wSort, pf.Bases, pf.Base) /* pf.appendUnique(wFind) // Append() enforce no unique keys @@ -88,7 +88,7 @@ func (pf *File) processMessage(parent *MsgName, wSort, wFind io.Writer) error { for _, v := range parent.Vars { if !v.IsRepeated { - log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) + // log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) continue } log.Printf("\tFOUND REPEATED %s %s\n", v.VarName, v.VarType) @@ -124,18 +124,17 @@ func (pf *File) processMessage(parent *MsgName, wSort, wFind io.Writer) error { } func (parent *MsgName) addFindByMsg(w io.Writer, FRUIT, APPLES, APPLE, LOCK string) { - log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT) for _, v := range parent.Vars { if v.HasUnique { var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName) - log.Printf("\t\t(x %s) FindBy%s(string) *%s\n", FRUIT, COLOR, APPLE) + log.Printf("\tFIND: (x %s) FindBy%s(string) *%s\n", FRUIT, COLOR, APPLE) generateFindBy(w, FRUIT, APPLES, APPLE, COLOR, LOCK) } } } func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { - log.Printf("\tDELETE: %s %s for %s\n", APPLES, APPLE, FRUIT) + // log.Printf("\tDELETE: %s %s for %s\n", APPLES, APPLE, FRUIT) var COLORS []string for _, v := range parent.Vars { if !v.HasUnique { @@ -144,7 +143,7 @@ func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName) COLORS = append(COLORS, COLOR) - log.Printf("\t\t(x %s) DeleteBy%s(string) *%s\n", FRUIT, COLOR, APPLE) + log.Printf("\tDELETE: (x %s) DeleteBy%s(string) *%s\n", FRUIT, COLOR, APPLE) if argv.Delete { parent.deleteByWithCopy(w, FRUIT, APPLES, APPLE, COLOR) } else { @@ -154,18 +153,18 @@ func (parent *MsgName) addDeleteByMsg(w io.Writer, FRUIT, APPLES, APPLE string) } func (parent *MsgName) addInsertByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { - log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT) + // log.Printf("\tINSERT: %s %s for %s\n", APPLES, APPLE, FRUIT) for _, v := range parent.Vars { if v.HasUnique { var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName) - log.Printf("\t\t(x %s) InsertBy%s(string) *%s\n", FRUIT, COLOR, APPLE) + log.Printf("\tINSERT: (x %s) InsertBy%s(string) *%s\n", FRUIT, COLOR, APPLE) parent.insertBy(w, FRUIT, APPLES, APPLE, COLOR) } } } func (parent *MsgName) addAppendByMsg(w io.Writer, FRUIT, APPLES, APPLE string) { - log.Printf("\tAPPEND!: %s %s for %s\n", APPLES, APPLE, FRUIT) + // log.Printf("\tAPPEND!: %s %s for %s\n", APPLES, APPLE, FRUIT) var COLORS []string for _, v := range parent.Vars { @@ -173,7 +172,7 @@ func (parent *MsgName) addAppendByMsg(w io.Writer, FRUIT, APPLES, APPLE string) var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName) COLORS = append(COLORS, COLOR) - log.Printf("\t\t(x %s) AppendUniqueBy%s(%s)\n", FRUIT, COLOR, APPLE) + log.Printf("\tAPPEND: (x %s) AppendUniqueBy%s(%s)\n", FRUIT, COLOR, APPLE) parent.appendUniqueCOLOR(w, FRUIT, APPLES, APPLE, COLOR) } } diff --git a/generateIterator.go b/generateIterator.go new file mode 100644 index 0000000..a291c1e --- /dev/null +++ b/generateIterator.go @@ -0,0 +1,75 @@ +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, COLOR, 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() []*"+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, 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()") + 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) +} diff --git a/generateSort.go b/generateSort.go index e5cfa20..0bb77a6 100644 --- a/generateSort.go +++ b/generateSort.go @@ -73,9 +73,9 @@ func (pf *File) selectAllFunc(w io.Writer) { fmt.Fprintln(w, "") } -func (pf *File) sortByFunc(w io.Writer) { - var BASES string = pf.Bases.Name - var BASE string = pf.Base.Name +func (pf *File) sortByFunc(w io.Writer, parent, child *MsgName) { + var BASES string = parent.Name + var BASE string = child.Name for _, SORT := range pf.Base.Sort { fmt.Fprintln(w, "func (x *"+BASES+") SortBy"+SORT+"() *"+BASE+"Iterator {") diff --git a/patchset.HAMDMADE b/patchset.HAMDMADE new file mode 100644 index 0000000..62741dc --- /dev/null +++ b/patchset.HAMDMADE @@ -0,0 +1,63 @@ +// DEFINE THE ITERATOR. Only one per Patch message + +// NewPatchsetIterator initializes a new iterator. +func NewPatchIterator(things []*Patch) *PatchIterator { + return &PatchIterator{things: things} +} + +// safely returns a slice of pointers to the Patchset protobufs +func (x *Patchset) all() []*Patch { + x.Lock.RLock() + defer x.Lock.RUnlock() + + // Create a new slice to hold pointers to each Patchset + var tmp []*Patch + tmp = make([]*Patch, len(x.Patches)) + for i, p := range x.Patches { + tmp[i] = p // Copy pointers for safe iteration + } + + return tmp +} + +type PatchIterator struct { + sync.RWMutex + + things []*Patch + index int +} + +func (it *PatchIterator) Scan() bool { + if it.index >= len(it.things) { + return false + } + it.index++ + return true +} + +// Next() returns the next thing in the array +func (it *PatchIterator) Next() *Patch { + if it.things[it.index-1] == nil { + fmt.Println("Next() error in PatchIterator", it.index) + } + return it.things[it.index-1] +} + +// END DEFINE THE ITERATOR + +// START sort by Filename (this is all you need once the Iterator is defined) +type PatchFilename []*Patch + +func (a PatchFilename) Len() int { return len(a) } +func (a PatchFilename) Less(i, j int) bool { return a[i].Filename < a[j].Filename } +func (a PatchFilename) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +func (x *Patchset) SortByFilename() *PatchIterator { + things := x.all() + + sort.Sort(PatchFilename(things)) + + iterator := NewPatchIterator(things) + return iterator +} +// END sort by Filename