autogenpb/generate.go

407 lines
12 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()
header(wSort, pf)
pf.syncLock(wSort)
2025-01-09 15:29:27 -06:00
fmt.Fprintf(wSort, "// START SORT\n")
fmt.Fprintf(wSort, "\n")
log.Printf("START ITERATORS\n")
// add iterators for all the structs to be used
for i, msg := range pf.allMsg() {
PARENT := msg.Name
LOCK := msg.Lockname
2025-01-10 04:22:53 -06:00
funcdef := newIter(wSort, msg)
log.Printf("ITER: %-2d %20s %20s %20s %20s %s\n", i, PARENT, "", "", LOCK, funcdef)
}
log.Printf("END ITERATORS\n")
log.Printf("\n")
log.Printf("START COMPARE\n")
for i, msg := range pf.allMsg() {
PARENT := msg.Name
for _, v := range msg.Vars {
if !v.HasSort {
continue
}
if v.IsRepeated {
// can't work against slices
continue
}
VARNAME := v.VarName
funcdef := newSortType(wSort, PARENT, VARNAME)
log.Printf("TYPE: %-2d %20s %20s %20s %10s %s\n", i, PARENT, "", "", "", funcdef)
}
}
log.Printf("END COMPARE\n")
log.Printf("\n")
// add selectAll() functions for all the sort combinations that will be used
for i, s := range pf.ToSort {
// log.Printf("SORT: %-2d %20s %20s %20s %20s\n", i, s.MsgName, s.VarType, s.VarName, s.Lockname)
FRUIT := s.MsgName
APPLE := s.VarType
APPLES := s.VarName
LOCK := s.Lockname
funcdef := newIterAll(wSort, FRUIT, APPLE, APPLES, LOCK)
log.Printf("JUNK: %-2d %20s %20s %20s %20s %s\n", i, FRUIT, APPLE, "", LOCK, funcdef)
}
log.Printf("\n")
log.Printf("START SELECT\n")
// make the sort iterators selectAll()
for i, s := range pf.ToSort {
PARENT := s.MsgName
CHILD := s.VarType
VARNAME := s.VarName
LOCK := s.Lockname
funcdef := addSelectAll(wSort, PARENT, CHILD, VARNAME, LOCK)
log.Printf("SORT: %-2d %20s %20s %20s %20s %s\n", i, PARENT, CHILD, VARNAME, LOCK, funcdef)
}
log.Printf("END SELECT\n")
log.Printf("\n")
log.Printf("START SORT\n")
// make the SortBy() functions
for i, s := range pf.ToSort {
// var funcname string
PARENT := s.MsgName
CHILD := s.VarType
VARNAME := s.VarName
log.Printf("SORT: %-2d %20s %20s %20s %20s %s\n", i, PARENT, CHILD, VARNAME, "", "")
msg := pf.findMsg(s.VarType)
if msg == nil {
return fmt.Errorf("failed to find struct %s", s.VarType)
}
var FUNCTYPE string
if PARENT == VARNAME {
FUNCTYPE = PARENT
} else {
FUNCTYPE = VARNAME
}
for _, v := range msg.Vars {
if v.IsRepeated {
// can't work against slices
continue
}
if v.HasSort {
// funcname := "func (x *" + FUNCTYPE + ") SortBy" + v.VarName + "(" + v.VarType + ") *[]iter" + s.VarType
// log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "fix", "", "", funcname)
// funcdef := "func (x *"+FRUIT+") SortBy"+COLOR+"() *"+APPLE+"Iterator"
if v.VarType == "string" {
var sortby string
// func newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELECT string) string {
if PARENT == VARNAME {
sortby = "SortBy" + v.VarName
} else {
sortby = "Sort" + VARNAME + "By" + v.VarName
}
sortname := s.VarType + v.VarName
selectName := "selectAll" + VARNAME
funcdef := newSortBy(wSort, PARENT, s.VarType, sortname, sortby, selectName)
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcdef)
} else {
funcname := "func (x *" + FUNCTYPE + ") SortBy" + v.VarName + "(" + v.VarType + ") *[]iter" + s.VarType + " # can not do this yet"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
}
}
}
}
log.Printf("END SORT\n")
log.Printf("\n")
fmt.Fprintf(wSort, "\n")
fmt.Fprintf(wSort, "// END SORT\n")
// make Len()
for _, msg := range pf.allMsg() {
PARENT := msg.Name
LOCK := msg.Lockname
for i, v := range msg.Vars {
if v.IsRepeated {
CHILD := v.VarType
VARNAME := v.VarName
// funcdef := "func (x *" + PARENT + ") Len" + VARNAME + "() int " + CHILD + " name:" + VARNAME
if PARENT == VARNAME {
// special case
funcdef := addLenFunc(wSort, PARENT, VARNAME, LOCK) // + " " + v.VarType + " name:" + v.VarName
funcdef += " # is special struct=varname"
log.Printf("LEN: %-2d %20s %20s %20s %20s %s\n", i, PARENT, CHILD, VARNAME, LOCK, funcdef)
}
}
}
}
log.Printf("\n")
2025-01-12 03:01:24 -06:00
// add All()
for _, s := range pf.ToSort {
PARENT := s.MsgName
CHILD := s.VarType
VARNAME := s.VarName
if PARENT == VARNAME {
funcdef := addAllFunc(wSort, PARENT, CHILD, VARNAME)
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
}
/*
// todo: figure out how to do more All() functions correction
var FUNCTYPE string
if PARENT == VARNAME {
FUNCTYPE = PARENT
} else {
FUNCTYPE = VARNAME
}
if s.VarType+"s" == s.VarName {
funcname = "func (x *" + FUNCTYPE + ") All() *[]iter" + s.VarType
} else {
funcname = "func (x *" + FUNCTYPE + ") all" + s.VarName + "() *[]iter" + s.VarType
}
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
*/
}
2025-01-12 03:59:59 -06:00
// make delete()
/*
for _, msg := range pf.allMsg() {
PARENT := msg.Name
for _, v := range msg.Vars {
if !v.HasUnique {
continue
}
funcname := "func (x *" + PARENT + ") Delete" + msg.Name + "By" + v.VarName + "(" + v.VarType + ") bool"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
FRUIT := PARENT
APPLES := msg.Name
2025-01-12 04:25:02 -06:00
APPLE := msg.VarName
COLOR := v.VarName
2025-01-12 03:59:59 -06:00
FUNCNAME := "Delete" + msg.Name + "By" + v.VarName
var funcdef string
if argv.Delete {
funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME)
} else {
funcdef = msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME)
}
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
}
}
*/
2025-01-12 03:01:24 -06:00
// add Find() Delete() Append() Insert()
log.Printf(" %-2s %20s %20s %20s %20s\n", "", "PARENT STRUCT", "VAR STRUCT TYPE", "VAR NAME", "LOCK")
for i, s := range pf.ToSort {
var funcname string
PARENT := s.MsgName
CHILD := s.VarType
VARNAME := s.VarName
LOCK := s.Lockname
log.Printf("SORT: %-2d %20s %20s %20s %20s %s\n", i, PARENT, CHILD, VARNAME, LOCK, "")
msg := pf.findMsg(s.VarType)
if msg == nil {
return fmt.Errorf("failed to find struct %s", s.VarType)
}
2025-01-12 04:58:53 -06:00
// find()
for _, v := range msg.Vars {
2025-01-12 03:59:59 -06:00
if v.IsRepeated {
continue
}
2025-01-12 04:58:53 -06:00
if v.HasUnique {
// funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a " + v.VarType + ") *" + s.VarType + "(using" + v.VarName + ")"
// log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
var FUNCNAME string
if PARENT == VARNAME {
// special case because of the enforced .proto format
FUNCNAME = "Find" + v.VarName
} else {
FUNCNAME = "Find" + VARNAME + "By" + v.VarName
}
funcdef := msg.generateFindBy(wSort, FUNCNAME, PARENT, VARNAME, s.VarType, v.VarName)
// func (msg *MsgName) generateFindBy(w io.Writer, FUNCNAME, STRUCT, VARNAME, VARTYPE, COLOR string) string {
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
} else {
if v.VarType == "string" {
// probably dumb. move to a database if you need this
// funcname = "func (x *" + PARENT + ") Find" + VARNAME + "By" + v.VarName + "(a string) []*" + s.VarType + " ???"
// log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
}
2025-01-12 03:59:59 -06:00
}
}
2025-01-12 03:59:59 -06:00
2025-01-12 04:25:02 -06:00
// delete() functions
for _, v := range msg.Vars {
if v.IsRepeated {
continue
}
2025-01-12 04:25:02 -06:00
if !v.HasUnique {
continue
}
// funcname := "func (x *" + PARENT + ") Delete" + VARNAME + "By" + v.VarName + "(" + v.VarType + ") bool"
// log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
FRUIT := PARENT
APPLES := VARNAME
APPLE := v.VarName
COLOR := v.VarType
2025-01-12 04:58:53 -06:00
var FUNCNAME string
if PARENT == VARNAME {
// special case because of the enforced .proto format
FUNCNAME = "Delete" + v.VarName
} else {
FUNCNAME = "Delete" + VARNAME + "By" + v.VarName
}
2025-01-12 04:25:02 -06:00
var funcdef string
if argv.Delete {
funcdef = msg.deleteByWithCopy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName)
} else {
funcdef = msg.deleteBy(wSort, FRUIT, APPLES, APPLE, COLOR, FUNCNAME, v.VarName)
}
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
}
2025-01-12 04:58:53 -06:00
var ucount int
// append() functions. these need to be rethought
for _, v := range msg.Vars {
2025-01-12 04:40:12 -06:00
if v.IsRepeated {
continue
}
2025-01-12 04:58:53 -06:00
if !v.HasUnique {
continue
}
ucount += 1
var FUNCNAME string
if PARENT == VARNAME {
// special case because of the enforced .proto format
FUNCNAME = "Append" + v.VarName
} else {
2025-01-12 04:58:53 -06:00
FUNCNAME = "Append" + VARNAME + "By" + v.VarName
}
2025-01-12 04:58:53 -06:00
funcdef := msg.addAppendBy(wSort, PARENT, FUNCNAME, VARNAME, v.VarName, s.VarType)
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "done", "", funcdef)
}
2025-01-12 04:58:53 -06:00
if ucount == 1 {
for _, v := range msg.Vars {
if !v.HasUnique {
continue
}
2025-01-12 04:58:53 -06:00
funcname = "func (x *" + PARENT + ") Insert(a *" + v.VarType + ") (*" + CHILD + ", isNew bool) // todo"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
}
}
if ucount > 1 {
2025-01-12 04:58:53 -06:00
funcname = "func (x *" + PARENT + ") Insert(a *" + CHILD + ") (*" + CHILD + ", isNew bool) // todo"
log.Printf(" %-2s %20s %20s %20s %s %s\n", "", "", "", "", "", funcname)
}
}
2025-01-10 11:22:08 -06:00
return nil
}
2025-01-11 09:49:44 -06:00
func (pf *File) findMsg(s string) *MsgName {
if pf.Bases.Name == s {
return pf.Bases
}
if pf.Base.Name == s {
return pf.Base
}
for _, msg := range pf.MsgNames {
if msg.Name == s {
return msg
}
}
return nil
2025-01-11 04:41:14 -06:00
}
func (pf *File) allMsg() []*MsgName {
var all []*MsgName
all = append(all, pf.Bases)
all = append(all, pf.Base)
for _, msg := range pf.MsgNames {
all = append(all, msg)
}
return all
}
2025-01-11 03:27:53 -06:00
func (pf *File) specialBases(wFind io.Writer) {
2025-01-10 23:55:12 -06:00
var FRUIT string = cases.Title(language.English, cases.NoLower).String(pf.Bases.Name)
var APPLES string = cases.Title(language.English, cases.NoLower).String(pf.Bases.Name)
var APPLE string = cases.Title(language.English, cases.NoLower).String(pf.Base.Name)
pf.Bases.simpleAppend(wFind, FRUIT, APPLES, APPLE)
}
2025-01-12 03:59:59 -06:00
/*
func (parent *MsgName) addInsertByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
2025-01-11 05:53:52 -06:00
// 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)
2025-01-11 05:53:52 -06:00
log.Printf("\tINSERT: (x %s) InsertBy%s(string) *%s\n", FRUIT, COLOR, APPLE)
2025-01-11 04:03:41 -06:00
parent.insertBy(w, FRUIT, APPLES, APPLE, COLOR)
2025-01-10 11:22:08 -06:00
}
}
}
func (parent *MsgName) addAppendByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
2025-01-11 05:53:52 -06:00
// log.Printf("\tAPPEND!: %s %s for %s\n", APPLES, APPLE, FRUIT)
var COLORS []string
for _, v := range parent.Vars {
2025-01-10 11:22:08 -06:00
if v.HasUnique {
var COLOR string = cases.Title(language.English, cases.NoLower).String(v.VarName)
COLORS = append(COLORS, COLOR)
2025-01-11 05:53:52 -06:00
log.Printf("\tAPPEND: (x %s) AppendUniqueBy%s(%s)\n", FRUIT, COLOR, APPLE)
2025-01-11 04:27:45 -06:00
parent.appendUniqueCOLOR(w, FRUIT, APPLES, APPLE, COLOR)
2025-01-10 11:22:08 -06:00
}
}
if len(COLORS) > 0 {
parent.appendUnique(w, FRUIT, APPLES, APPLE, COLORS)
}
}
func (parent *MsgName) addSortByMsg(w io.Writer, FRUIT, APPLES, APPLE string) {
log.Printf("\tSORT!: %s %s for %s\n", APPLES, APPLE, FRUIT)
for _, v := range parent.Vars {
if v.HasSort {
// log.Printf("\tSort!: %s %s for %s\n", APPLES, APPLE, v.VarName)
newS := cases.Title(language.English, cases.NoLower).String(v.VarName)
log.Printf("\t\t(x %s) SortdBy%s() *%sIter\n", FRUIT, newS, APPLE)
}
}
2025-01-10 11:22:08 -06:00
}
2025-01-12 04:58:53 -06:00
*/