add a new iterator generate file

This commit is contained in:
Jeff Carr 2025-01-11 05:53:52 -06:00
parent fed674d04a
commit 95afc5e0b4
4 changed files with 150 additions and 13 deletions

View File

@ -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)
}
}

75
generateIterator.go Normal file
View File

@ -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)
}

View File

@ -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 {")

63
patchset.HAMDMADE Normal file
View File

@ -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