From 63a3d0dc2439802ca3bf8db92d095733db6b7b7e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 10 Mar 2025 13:52:04 -0500 Subject: [PATCH] adding more table pb options --- example/fruit.proto | 4 +-- file.proto | 2 ++ generateGui.go | 81 +++++++++++++++++++++++++++++++++++++-------- protoParse.go | 24 ++++++++++++-- 4 files changed, 93 insertions(+), 18 deletions(-) diff --git a/example/fruit.proto b/example/fruit.proto index 4545e03..362c59b 100644 --- a/example/fruit.proto +++ b/example/fruit.proto @@ -21,8 +21,8 @@ message Apple { google.protobuf.Timestamp ctime = 3; // when the apple was born } -message Apples { - string name = 1; // `autogenpb:unique` // generates SortByxxx() and AppendUnique() functions +message Apples { // `autogenpb:gui:Apple` + string name = 1; // `autogenpb:unique` string genus = 2; // `autogenpb:unique` // generates same thing here but SortByGenus() repeated Apple apples = 3; } diff --git a/file.proto b/file.proto index 01c9d39..a947826 100644 --- a/file.proto +++ b/file.proto @@ -46,6 +46,8 @@ message MsgName { bool needIter = 10; // true if the sort iterator has not been generated yet bool needAll = 11; // true if the sort iterator has not been generated yet bool noMutex = 12; // only use the global mutex + bool doGui = 13; // if a gui.pb.go file should be created + string guiVarName = 14; // the name of the variable to use } message Sort { diff --git a/generateGui.go b/generateGui.go index 4f12a67..86d6179 100644 --- a/generateGui.go +++ b/generateGui.go @@ -32,22 +32,82 @@ func (pb *Files) makeGuiFile(pf *File) error { fmt.Fprintf(newf, "// START GUI\n") fmt.Fprintf(newf, "\n") - guiMain(newf, pf.Bases.Name, pf.Base.Name) - guiStringFuncs(newf, pf.Package, pf.Bases.Name, pf.Base.Name) + /* + FRUITS := pf.Bases.Name + FRUIT := pf.Base.Name + fruitVars := pf.Base.Vars + pf.generateAutoTablePB(newf, FRUITS, FRUIT, fruitVars) + */ - for _, v := range pf.Base.Vars { + for _, msg := range pf.allMsg() { + if msg.DoGui { + color := pf.findMsg(msg.GuiVarName) + if color == nil { + return fmt.Errorf("failed to find struct %s", msg.GuiVarName) + } + FRUITS := msg.Name + FRUIT := msg.GuiVarName + fruitVars := color.Vars + pf.generateAutoTablePB(newf, FRUITS, FRUIT, fruitVars) + log.Printf("NEED TO ADD GUI FOR %s with var %s and found msg struct %s\n", msg.Name, msg.GuiVarName, color.Lockname) + } + } + // os.Exit(-1) + + /* + guiMain(newf, pf.Bases.Name, pf.Base.Name) + guiStringFuncs(newf, pf.Package, pf.Bases.Name, pf.Base.Name) + + for _, v := range pf.Base.Vars { + if v.IsRepeated { + // can't work against slices + continue + } + if v.VarType == "string" { + log.Printf("make Add function here %s %s %s\n", pf.Bases.Name, pf.Base.Name, v.VarName) + guiAddStringFunc(newf, pf.Bases.Name, pf.Base.Name, v.VarName) + continue + } + if v.VarType == "int64" { + log.Printf("make Add function here %s %s %s\n", pf.Bases.Name, pf.Base.Name, v.VarName) + guiAddIntFunc(newf, pf.Bases.Name, pf.Base.Name, v.VarName) + continue + } + } + FRUITS := pf.Bases.Name + FRUIT := pf.Base.Name + fRUITS := untitle(pf.Bases.Name) + fRUIT := untitle(pf.Base.Name) + guiUpdate(newf, FRUITS, FRUIT) + guiTableDelete(newf, FRUITS, FRUIT) + guiTableCustom(newf, FRUITS, fRUITS, FRUIT, fRUIT) + */ + + fmt.Fprintf(newf, "\n") + fmt.Fprintf(newf, "// END GUI\n") + return nil +} + +func (pf *File) generateAutoTablePB(newf *os.File, FRUITS string, FRUIT string, fruitVars []*MsgVar) { + fRUITS := untitle(FRUITS) + fRUIT := untitle(FRUIT) + + guiMain(newf, FRUITS, FRUIT) + guiStringFuncs(newf, pf.Package, FRUITS, FRUIT) + + for _, v := range fruitVars { if v.IsRepeated { // can't work against slices continue } if v.VarType == "string" { - log.Printf("make Add function here %s %s %s\n", pf.Bases.Name, pf.Base.Name, v.VarName) - guiAddStringFunc(newf, pf.Bases.Name, pf.Base.Name, v.VarName) + log.Printf("make Add function here %s %s %s\n", FRUITS, FRUIT, v.VarName) + guiAddStringFunc(newf, FRUITS, FRUIT, v.VarName) continue } if v.VarType == "int64" { - log.Printf("make Add function here %s %s %s\n", pf.Bases.Name, pf.Base.Name, v.VarName) - guiAddIntFunc(newf, pf.Bases.Name, pf.Base.Name, v.VarName) + log.Printf("make Add function here %s %s %s\n", FRUITS, FRUIT, v.VarName) + guiAddIntFunc(newf, FRUITS, FRUIT, v.VarName) continue } /* @@ -63,17 +123,10 @@ func (pb *Files) makeGuiFile(pf *File) error { log.Printf("Adding %s\n", funcdef) */ } - FRUITS := pf.Bases.Name - FRUIT := pf.Base.Name - fRUITS := untitle(pf.Bases.Name) - fRUIT := untitle(pf.Base.Name) guiUpdate(newf, FRUITS, FRUIT) guiTableDelete(newf, FRUITS, FRUIT) guiTableCustom(newf, FRUITS, fRUITS, FRUIT, fRUIT) - fmt.Fprintf(newf, "\n") - fmt.Fprintf(newf, "// END GUI\n") - return nil } func headerGui(w io.Writer, pf *File) { diff --git a/protoParse.go b/protoParse.go index ba1ff4c..79f36df 100644 --- a/protoParse.go +++ b/protoParse.go @@ -54,6 +54,7 @@ func (pf *File) protoParse() error { if curmsg == nil { // log.Info("curmsg == nil", line) // can't contiue on nil below here + // TODO: look for and format enum structs here continue } // log.Info("curmsg != nil", line) @@ -153,6 +154,7 @@ func parseMsgVar(line string) *MsgVar { func (pf *File) parseForMessage(line string) *MsgName { fields := strings.Fields(line) if len(fields) == 0 || fields[0] != "message" { + // this line doesn't start with message return nil } var msg *MsgName @@ -189,10 +191,28 @@ func (pf *File) parseForMessage(line string) *MsgName { msg.DoMarshal = true // log.Info("Added Marshal=true:", msg.Name) } - if strings.Contains(line, "autogenpb:gui") { + if strings.Contains(line, "`autogenpb:gui") { log.Info("got autogenpb:gui") pf.DoGui = true - // os.Exit(-1) + msg.DoGui = true + parts := strings.Split(line, "autogenpb:gu") + if len(parts) != 2 { + log.Info("len(parts) != 2", line) + os.Exit(-1) + } + log.Info("line =", line) + log.Info("end =", parts[1]) + end := strings.Split(parts[1], "`") + if end[0] != "i" { + varname := strings.TrimPrefix(end[0], "i:") + msg.GuiVarName = varname + log.Info("varname =", varname) + } else { + if strings.HasSuffix(msgName, "s") { + varname := strings.TrimSuffix(msgName, "s") + msg.GuiVarName = varname + } + } } return msg }