diff options
| author | Jeff Carr <[email protected]> | 2025-01-10 11:22:08 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-01-10 11:22:08 -0600 |
| commit | d36344e463e80d89d0cc8b028b6243823d184b35 (patch) | |
| tree | ea8c4c78907420631476fd5061500d896cded87b /sort.go | |
| parent | e07c6a35fd5d2ca1b3a6c09d9781d21f676f52f2 (diff) | |
make seperate find and sort files
Diffstat (limited to 'sort.go')
| -rw-r--r-- | sort.go | 108 |
1 files changed, 94 insertions, 14 deletions
@@ -3,35 +3,115 @@ package main import ( "fmt" "os" + + "go.wit.com/log" + "golang.org/x/text/cases" + "golang.org/x/text/language" ) // this file is named poorly. It has more than Sort() func (pb *Files) makeNewSortfile(pf *File) error { - w, _ := os.OpenFile(pf.Filebase+".sort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + wSort, _ := os.OpenFile(pf.Filebase+".sort.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + defer wSort.Close() + wFind, _ := os.OpenFile(pf.Filebase+".find.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + defer wFind.Close() + + header(wSort, pf) + header(wFind, pf) - header(w, pf) - pf.syncLock(w) + if !argv.Mutex { + pf.syncLock(wSort) + } if argv.Mutex { // use the mutex lock from the modified protoc.pb.go file pf.Bases.Lockname = "all.Lock" } - pf.Base.iterTop(w) - pf.Base.iterNext(w) - pf.selectAllFunc(w) - pf.iterSelect(w) + pf.Base.iterTop(wSort) + pf.Base.iterNext(wSort) + pf.selectAllFunc(wSort) + pf.iterSelect(wSort) - fmt.Fprintln(w, "// maybe seperate files here?") - - pf.appendUnique(w) // Append() enforce no unique keys - pf.sortByFunc(w) + pf.appendUnique(wFind) // Append() enforce no unique keys + pf.sortByFunc(wSort) if argv.Delete { - pf.deleteWithCopyFunc(w) + pf.deleteWithCopyFunc(wFind) } else { - pf.deleteFunc(w) + pf.deleteFunc(wFind) + } + pf.findFunc(wFind) + + // show the protobuf of the protobuf. Inception 2025 + pf.printMsgTable() + + // attempt to add sort functions for pf.Base + pf.processMessage(pf.Base) + return nil +} + +func (pf *File) processMessage(msg *MsgName) error { + log.Printf("%s\n", msg.Name) + for _, v := range msg.Vars { + if !v.IsRepeated { + // log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType) + continue + } + if err := pf.addSortByMsg(msg, v); err != nil { + return err + } } - pf.findFunc(w) return nil } + +func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar) error { + // log.Printf("\tLOOK HERE: %s %s\n", find.VarName, find.VarType) + var found *MsgName + for _, msg := range pf.MsgNames { + if msg.Name == find.VarType { + found = msg + break + } + } + if found == nil { + return fmt.Errorf("failed to find struct %s", find.VarType) + } + log.Printf("FOUND!: %s %s for %s\n", find.VarName, find.VarType, found.Name) + for _, v := range found.Vars { + if v.HasSort { + // log.Printf("\tSort!: %s %s for %s\n", find.VarName, find.VarType, v.VarName) + newS := cases.Title(language.English, cases.NoLower).String(v.VarName) + log.Printf("\t(x %s) SortdBy%s() *%sIter\n", parent.Name, newS, find.VarType) + } + if v.HasUnique { + // log.Printf("\tUniq!: %s %s for %s\n", find.VarName, find.VarType, v.VarName) + newS := cases.Title(language.English, cases.NoLower).String(v.VarName) + log.Printf("\t(x %s) AppendUniqueBy%s(%s)\n", parent.Name, newS, find.VarType) + log.Printf("\t(x %s) FindBy%s(string) *%s\n", parent.Name, newS, find.VarType) + if v.VarType == "string" { + log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, find.VarType) + } + } + } + return nil +} + +func (pf *File) printMsgTable() { + for _, msg := range pf.MsgNames { + log.Printf("%s\n", msg.Name) + for _, v := range msg.Vars { + var end string + if v.IsRepeated { + end += "(repeated) " + } + if v.HasSort { + end += "(sort) " + } + if v.HasUnique { + end += "(unique) " + } + log.Printf("\t%s %s %s\n", v.VarName, v.VarType, end) + } + } +} |
