autogen some http code

This commit is contained in:
Jeff Carr 2025-09-08 03:37:27 -05:00
parent 7d67ae7528
commit 1fe6c53267
4 changed files with 148 additions and 1 deletions

View File

@ -50,6 +50,7 @@ message MsgName {
bool doGui = 13; // if a gui.pb.go file should be created
string guiVarName = 14; // the name of the variable to use
MsgName localMsgs = 15; // messages can define other local only messages
bool doHTTP = 16; // make http.pb.go files
}
message Sort {
@ -103,6 +104,7 @@ message File { // `autogenpb:var:w io.Wri
repeated Sort toSort = 12; // variables that are repeated can have the standard functions generated (Sort(), etc)
string goPath = 13; // the version to use in a func NewMsgName()
bool doGui = 14; // if a gui.pb.go file should be created
bool doHTTP = 15; // http.pb.go
}
// I know, I know, the whole point of using protobuf
// is so you don't need a uuid or versions because it's

135
generateHTTP.go Normal file
View File

@ -0,0 +1,135 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"fmt"
"io"
"os"
"go.wit.com/log"
)
func (pb *Files) makeHTTPFile(pf *File) error {
newf, _ := os.OpenFile(pf.Filebase+".http.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
defer newf.Close()
headerHTTP(newf, pf)
fmt.Fprintf(newf, "// START HTTP\n")
fmt.Fprintf(newf, "\n")
/*
FRUITS := pf.Bases.Name
FRUIT := pf.Base.Name
fruitVars := pf.Base.Vars
pf.generateAutoTablePB(newf, FRUITS, FRUIT, fruitVars)
*/
for _, msg := range pf.allMsg() {
if msg.DoHTTP {
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
httpCustom(newf, FRUITS, FRUIT, FRUIT)
log.Printf("NEED TO ADD GUI FOR %s with var %s and found msg struct %s\n", msg.Name, msg.GuiVarName, color.Lockname)
}
}
fmt.Fprintf(newf, "\n")
fmt.Fprintf(newf, "// END HTTP\n")
return nil
}
func headerHTTP(w io.Writer, pf *File) {
// header must come first
headerComment(w)
fmt.Fprintf(w, "package %s\n", pf.Package)
fmt.Fprintln(w, "")
fmt.Fprintln(w, "import (")
fmt.Fprintln(w, " \"time\"")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " \"go.wit.com/lib/protobuf/httppb\"")
fmt.Fprintln(w, ")")
fmt.Fprintln(w, "")
}
func httpTest(w io.Writer, FRUITS string, fRUITS string, FRUIT string, fRUIT string) {
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) "+fRUITS+"Custom(w *guipb.Widget) {")
fmt.Fprintln(w, " row := mt.x."+FRUITS+"[w.Location.Y-1]")
fmt.Fprintln(w, " // log.Info(\"got to "+fRUITS+"Custom() with\", w.Location.X, w.Location.Y-1)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, sf := range mt.buttonFuncs {")
fmt.Fprintln(w, " if sf.order == int(w.Location.X) {")
fmt.Fprintln(w, " // log.Info(\"found order\", sf.order)")
fmt.Fprintln(w, " if sf.Custom != nil {")
fmt.Fprintln(w, " log.Info(\"doing Custom() func for button\")")
fmt.Fprintln(w, " sf.Custom(row)")
fmt.Fprintln(w, " return")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " mt.CustomFunc(row)")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) Custom(f func(*"+FRUIT+")) {")
fmt.Fprintln(w, " mt.pb.RegisterCustom(mt."+fRUITS+"Custom)")
fmt.Fprintln(w, " mt.CustomFunc = f")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) GetUuid() string {")
fmt.Fprintln(w, " return mt.pb.Uuid")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "// END TABLE UPDATE")
}
func httpCustom(w io.Writer, FRUITS string, fRUITS string, FRUIT string) {
fmt.Fprintln(w, "// err handling here isn't great")
fmt.Fprintln(w, "func (p *Repos) HttpPost(baseURL string, route string) (*Repos, *httppb.HttpRequest, error) {")
fmt.Fprintln(w, " // if you ever have 'http://www.wit.com//' GO will regect the server recieving it.")
fmt.Fprintln(w, " // Even though the linux kernel gets the network payload")
fmt.Fprintln(w, " // also it never gives you an error about that, it just goes away invisably inside GO")
fmt.Fprintln(w, " tmpURL, _ := url.Parse(baseURL) // 'http://forge.grid.wit.com:2520')")
fmt.Fprintln(w, " finalURL := tmpURL.JoinPath(\"/repos/\", route)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " data, _ := p.Marshal()")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " var err error")
fmt.Fprintln(w, " var req *http.Request")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " log.Info(\"reposs PB HttpPost\", finalURL.String())")
fmt.Fprintln(w, " req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))")
fmt.Fprintln(w, " if req == nil {")
fmt.Fprintln(w, " return nil, nil, err")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " usr, _ := user.Current()")
fmt.Fprintln(w, " req.Header.Set(\"author\", usr.Username)")
fmt.Fprintln(w, " hostname, _ := os.Hostname()")
fmt.Fprintln(w, " req.Header.Set(\"hostname\", hostname)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " newdata, err := httppb.PostReq(req)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " reqPB, err2 := httppb.ReqToPB(req)")
fmt.Fprintln(w, " reqPB.URL = finalURL.String()")
fmt.Fprintln(w, " if err != nil {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, fmt.Sprintf(err))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if err2 != nil {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, fmt.Sprintf(err2))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " newpb := NewRepos()")
fmt.Fprintln(w, " err = newpb.Unmarshal(newdata)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " // log.Info(repos PB HttpPost finalURL.String(), p.Len(), newpb.Len())")
fmt.Fprintln(w, " return newpb, reqPB, err")
fmt.Fprintln(w, "}")
}

View File

@ -208,6 +208,12 @@ func main() {
badExit(err)
}
}
if pf.DoHTTP {
if err := pb.makeHTTPFile(pf); err != nil {
badExit(err)
}
}
}
func okExit(s string) {

View File

@ -192,7 +192,11 @@ func (pf *File) parseForMessage(line string) *MsgName {
}
if strings.Contains(line, "`autogenpb:marshal`") {
msg.DoMarshal = true
// log.Info("Added Marshal=true:", msg.Name)
}
if strings.Contains(line, "`autogenpb:http") {
log.Info("got autogenpb:http")
pf.DoHTTP = true
msg.DoHTTP = true
}
if strings.Contains(line, "`autogenpb:gui") {
log.Info("got autogenpb:gui")