autogenpb/sort.go

112 lines
3.0 KiB
Go
Raw Normal View History

package main
import (
2025-01-10 04:22:53 -06:00
"fmt"
"io"
"os"
2025-01-10 11:22:08 -06:00
"go.wit.com/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
2025-01-10 04:22:53 -06:00
// this file is named poorly. It has more than Sort()
2025-01-09 15:51:45 -06:00
func (pb *Files) makeNewSortfile(pf *File) error {
2025-01-10 11:22:08 -06:00
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)
2025-01-10 11:22:08 -06:00
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"
2025-01-09 15:29:27 -06:00
}
2025-01-10 11:22:08 -06:00
pf.Base.iterTop(wSort)
pf.Base.iterNext(wSort)
pf.selectAllFunc(wSort)
pf.iterSelect(wSort)
2025-01-10 04:22:53 -06:00
2025-01-10 11:22:08 -06:00
pf.appendUnique(wFind) // Append() enforce no unique keys
pf.sortByFunc(wSort)
2025-01-10 04:22:53 -06:00
if argv.Delete {
2025-01-10 11:22:08 -06:00
pf.deleteWithCopyFunc(wFind)
2025-01-10 04:22:53 -06:00
} else {
2025-01-10 11:22:08 -06:00
pf.deleteFunc(wFind)
}
pf.findFunc(wFind)
// attempt to add sort functions for pf.Base
pf.processMessage(pf.Bases, wSort, wFind)
// pf.processMessage(pf.Base, wSort, wFind)
2025-01-10 11:22:08 -06:00
return nil
}
func (pf *File) processMessage(msg *MsgName, wSort, wFind io.Writer) error {
log.Printf("ADDING FIND AND SORT FOR MESSAGE %s WITH %+v\n", msg.Name, msg.Vars)
2025-01-10 11:22:08 -06:00
for _, v := range msg.Vars {
if !v.IsRepeated {
log.Printf("\tSKIP %s %s\n", v.VarName, v.VarType)
2025-01-10 11:22:08 -06:00
continue
}
if err := pf.addSortByMsg(msg, v, wSort, wFind); err != nil {
2025-01-10 11:22:08 -06:00
return err
}
2025-01-10 04:22:53 -06:00
}
2025-01-09 15:51:45 -06:00
return nil
}
2025-01-10 11:22:08 -06:00
func (pf *File) addSortByMsg(parent *MsgName, find *MsgVar, wSort, wFind io.Writer) error {
log.Printf("\tLOOK HERE: %s %s\n", find.VarName, find.VarType)
2025-01-10 11:22:08 -06:00
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)
var COLORS []string
var FRUIT string = parent.Name
var APPLES string = cases.Title(language.English, cases.NoLower).String(find.VarName)
var APPLE string = find.VarType
2025-01-10 11:22:08 -06:00
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 {
newS := cases.Title(language.English, cases.NoLower).String(v.VarName)
var COLOR string = newS
COLORS = append(COLORS, COLOR)
log.Printf("\t(x %s) AppendUniqueBy%s(%s)\n", parent.Name, newS, find.VarType)
parent.appendUniqueBy(wFind, FRUIT, APPLES, APPLE, COLOR)
2025-01-10 11:22:08 -06:00
if v.VarType == "string" {
log.Printf("\t(x %s) FindBy%s(string) *%s\n", FRUIT, COLOR, APPLE)
parent.findBy(wFind, FRUIT, APPLES, APPLE, COLOR)
2025-01-10 11:22:08 -06:00
log.Printf("\t(x %s) DeleteBy%s(string) *%s\n", parent.Name, newS, find.VarType)
parent.deleteBy(wFind, FRUIT, APPLES, APPLE, COLOR)
2025-01-10 11:22:08 -06:00
}
}
}
if len(COLORS) > 0 {
parent.appendUnique(wFind, FRUIT, APPLES, APPLE, COLORS)
}
2025-01-10 11:22:08 -06:00
return nil
}