Compare commits

..

25 Commits

Author SHA1 Message Date
Jeff Carr e9965f5109 error out if server returns nil 2025-09-22 08:52:33 -05:00
Jeff Carr 6c9a3dd632 http needs Marshal() 2025-09-22 08:50:37 -05:00
Jeff Carr 3a13970536 actually test http 2025-09-22 08:48:06 -05:00
Jeff Carr 0ccf2c09a4 notes about the purpose of those functions 2025-09-17 01:08:51 -05:00
Jeff Carr 737c5b7b90 that dumb 'order' hack is gone. but boy it helped at first 2025-09-16 23:34:43 -05:00
Jeff Carr 046d084222 about to clear out old 'order' logic 2025-09-16 23:25:04 -05:00
Jeff Carr 9a93384ee3 everything col switched to 'any' (?) 2025-09-16 23:12:39 -05:00
Jeff Carr 7d2c654cec strings kinda work as 'any' now 2025-09-16 09:53:05 -05:00
Jeff Carr eb17a09114 closer on StringFunc() converstion to any 2025-09-16 09:32:02 -05:00
Jeff Carr f1d297f4ff builds 2025-09-16 09:21:53 -05:00
Jeff Carr 04b9d93615 more work on 'any' 2025-09-15 05:10:19 -05:00
Jeff Carr cea3028333 more work on 'any' in GUI PB 2025-09-15 04:52:59 -05:00
Jeff Carr 66aacceee5 continue work to move to 'any' in GUI PB table 2025-09-15 03:50:46 -05:00
Jeff Carr 086329f1db added col.Attr 2025-09-14 09:35:40 -05:00
Jeff Carr 48e2465be1 seriously. I used Row instead of Col. moron! 2025-09-14 05:49:58 -05:00
Jeff Carr ddea4b6514 return new pointer from Append() 2025-09-13 05:31:59 -05:00
Jeff Carr 8a53184746 cleaner explination of outputs for 'http' and 'gui' functions 2025-09-09 19:13:03 -05:00
Jeff Carr d88cc3dd0d API change 2025-09-08 08:13:06 -05:00
Jeff Carr 4f114f7046 minor text 2025-09-08 05:34:23 -05:00
Jeff Carr da3244d265 much better standard autogen code 2025-09-08 04:34:43 -05:00
Jeff Carr 96cd7ffb2d basic http functions 2025-09-08 04:02:50 -05:00
Jeff Carr 1fe6c53267 autogen some http code 2025-09-08 03:37:27 -05:00
Jeff Carr 7d67ae7528 added Width 2025-09-04 18:47:19 -05:00
Jeff Carr 52089cf062 notes 2025-08-31 08:59:33 -05:00
Jeff Carr 96a32319c7 minor 2025-08-29 10:28:48 -05:00
9 changed files with 267 additions and 505 deletions

View File

@ -1,213 +0,0 @@
package zoopb
import (
"time"
"go.wit.com/gui"
"go.wit.com/lib/protobuf/guipb"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
func (x *Machines) NewTable(title string) *MachinesTable {
t := new(MachinesTable)
t.x = x
pb := new(guipb.Table)
pb.Title = title
t.pb = pb
return t
}
func (t *MachinesTable) AddStringFunc(title string, f func(*Machine) string) {
t.pb.Order = append(t.pb.Order, title)
sf := new(MachineStringFunc)
sf.title = title
sf.f = f
t.stringFuncs = append(t.stringFuncs, sf)
}
func (t *MachinesTable) AddIntFunc(title string, f func(*Machine) int) {
t.pb.Order = append(t.pb.Order, title)
sf := new(MachineIntFunc)
sf.title = title
sf.f = f
t.intFuncs = append(t.intFuncs, sf)
}
func (t *MachinesTable) AddTimeFunc(title string, f func(*Machine) time.Time) {
t.pb.Order = append(t.pb.Order, title)
sf := new(MachineTimeFunc)
sf.title = title
sf.f = f
t.timeFuncs = append(t.timeFuncs, sf)
}
func (mt *MachinesTable) ShowTable() {
log.Info("zoopb.ShowTable() SENDING TO GUI")
mt.MakeTable()
gui.ShowTable(mt.pb)
}
type MachineStringFunc struct {
title string
f func(*Machine) string
}
type MachineIntFunc struct {
title string
f func(*Machine) int
}
type MachineTimeFunc struct {
title string
f func(*Machine) time.Time
}
type MachinesTable struct {
// gt *gui.NodeTable
pb *guipb.Table
x *Machines
hostnames []string
stringFuncs []*MachineStringFunc
intFuncs []*MachineIntFunc
timeFuncs []*MachineTimeFunc
}
func (mt *MachinesTable) doStringFunc(name string) bool {
for _, sf := range mt.stringFuncs {
if sf.title != name {
continue
}
log.Info("zoopb: found stringfunc name:", name)
r := new(guipb.StringRow)
r.Header = new(guipb.Widget)
r.Header.Name = name
all := mt.x.All()
for all.Scan() {
m := all.Next()
r.Vals = append(r.Vals, sf.f(m))
log.Info("zoopb: adding", name, r.Vals)
}
mt.pb.StringRows = append(mt.pb.StringRows, r)
return true
}
return false
}
func (mt *MachinesTable) doIntFunc(name string) bool {
for _, sf := range mt.intFuncs {
if sf.title != name {
continue
}
log.Info("zoopb: found intfunc name:", name)
r := new(guipb.IntRow)
r.Header = new(guipb.Widget)
r.Header.Name = name
all := mt.x.All()
for all.Scan() {
m := all.Next()
r.Vals = append(r.Vals, int64(sf.f(m)))
log.Info("zoopb: adding", name, r.Vals)
}
mt.pb.IntRows = append(mt.pb.IntRows, r)
return true
}
return false
}
func (mt *MachinesTable) doTimeFunc(name string) bool {
for _, sf := range mt.timeFuncs {
if sf.title != name {
continue
}
log.Info("zoopb: found timefunc name:", name)
r := new(guipb.TimeRow)
r.Header = new(guipb.Widget)
r.Header.Name = name
all := mt.x.All()
for all.Scan() {
m := all.Next()
t := sf.f(m)
r.Vals = append(r.Vals, timestamppb.New(t)) // convert to protobuf time
log.Info("zoopb: adding", name, r.Vals)
}
mt.pb.TimeRows = append(mt.pb.TimeRows, r)
return true
}
return false
}
func (t *MachinesTable) AddHostname() {
// t.pb.Order = append(t.pb.Order, "Hostname")
t.AddStringFunc("Hostname", func(m *zoopb.Machine) string {
return m.Hostname
})
}
func (t *MachinesTable) AddMemory() {
t.pb.Order = append(t.pb.Order, "Memory")
}
func (t *MachinesTable) AddCpus() {
t.pb.Order = append(t.pb.Order, "Cpus")
}
func (mt *MachinesTable) MakeTable() {
for _, name := range mt.pb.Order {
log.Info("zoopb: looking for row name()", name)
switch name {
case "Hostname":
r := new(guipb.StringRow)
r.Header = new(guipb.Widget)
r.Header.Name = name
all := mt.x.All()
for all.Scan() {
m := all.Next()
r.Vals = append(r.Vals, m.Hostname)
log.Info("zoopb: adding", name, r.Vals)
}
mt.pb.StringRows = append(mt.pb.StringRows, r)
continue
case "Cpus":
i := new(guipb.IntRow)
i.Header = new(guipb.Widget)
i.Header.Name = name
all := mt.x.All()
for all.Scan() {
m := all.Next()
i.Vals = append(i.Vals, m.Cpus)
log.Info("zoopb: adding", name, i.Vals)
}
mt.pb.IntRows = append(mt.pb.IntRows, i)
continue
case "Memory":
i := new(guipb.IntRow)
i.Header = new(guipb.Widget)
i.Header.Name = name
all := mt.x.All()
for all.Scan() {
m := all.Next()
i.Vals = append(i.Vals, m.Memory)
log.Info("zoopb: adding", name, i.Vals)
}
mt.pb.IntRows = append(mt.pb.IntRows, i)
continue
default:
// mt.addFuncRow(name)
}
log.Info("zoopb: didn't find name. trying StringFuncs", name)
if mt.doStringFunc(name) {
continue
}
if mt.doIntFunc(name) {
continue
}
if mt.doTimeFunc(name) {
continue
}
}
}

14
README
View File

@ -1,4 +1,14 @@
# This app will autogenerate Sort() and Marshal() functions for .proto files # This app auto-generates *.pb.go files
#
# It assumes you are doing GO development in ~/go/src
# Although it might work with go.work directory setups
# I haven't tested that much yet because I haven't had time
# This app DOES NOT LIKE THINGS WHEN THE PATHS ARE NOT ABSOLUTE GO namespace paths
# that is, if you make a go.work file and tell it gitea.mystuff.org/foo is actually in foo/
# none of the autogen things will probably work. Notsure. Anyway, it's not interesting
# to me to try that since this is hard enough already I don't have time to debug that
# but will of course accept patches
# It was designed to work on .proto files designed with a .proto standard # It was designed to work on .proto files designed with a .proto standard
@ -11,3 +21,5 @@
# See the examples/ for a sample fruit.proto file that documents what is needed # See the examples/ for a sample fruit.proto file that documents what is needed
# You will notice if you have checked out this project the *pb.go files are ignored in the .gitignore
# file. See the app 'forge' on how publishing and versioning works with this codebase.

View File

@ -55,7 +55,7 @@ message Basket { // `autogenpb:nomutex`
repeated Apple stacks = 6; repeated Apple stacks = 6;
} }
// "Fruit" must exist. you can put anything in it // "Fruit" must exist. you can put anything in it
message Fruit { message Fruit { // `autogenpb:http` `autogenpb:marshal`
string brand = 1; // `autogenpb:unique` `autogenpb:sort` string brand = 1; // `autogenpb:unique` `autogenpb:sort`
Apple apples = 2; Apple apples = 2;
repeated Pear pears = 3; repeated Pear pears = 3;
@ -68,7 +68,7 @@ message Fruit {
} }
// "Fruits" MUST EXIST and start exactly this way // "Fruits" MUST EXIST and start exactly this way
// It must be "Fruit" + 's' and must match the name of this file: "fruit.proto" // It must be "Fruit" + 's' and must match the name of this file: "fruit.proto"
message Fruits { // `autogenpb:marshal` `autogenpb:mutex` `autogenpb:gui` message Fruits { // `autogenpb:marshal` `autogenpb:mutex` `autogenpb:gui` `autogenpb:http`
string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079` string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`
string version = 2; // `autogenpb:version:v0.0.1` string version = 2; // `autogenpb:version:v0.0.1`
repeated Fruit Fruits = 3; // THIS MUST BE "Fruit" and then "Fruit" + "s" repeated Fruit Fruits = 3; // THIS MUST BE "Fruit" and then "Fruit" + "s"

View File

@ -50,6 +50,7 @@ message MsgName {
bool doGui = 13; // if a gui.pb.go file should be created bool doGui = 13; // if a gui.pb.go file should be created
string guiVarName = 14; // the name of the variable to use string guiVarName = 14; // the name of the variable to use
MsgName localMsgs = 15; // messages can define other local only messages MsgName localMsgs = 15; // messages can define other local only messages
bool doHTTP = 16; // make http.pb.go files
} }
message Sort { message Sort {
@ -67,7 +68,6 @@ message FormatMsg {
ONEOF = 2; ONEOF = 2;
VAR = 3; VAR = 3;
} }
int64 depth = 1; // used to indent output int64 depth = 1; // used to indent output
int64 maxVarname = 2; // max string length of var names int64 maxVarname = 2; // max string length of var names
int64 maxVartype = 3; // max string length of var types int64 maxVartype = 3; // max string length of var types
@ -104,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) 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() string goPath = 13; // the version to use in a func NewMsgName()
bool doGui = 14; // if a gui.pb.go file should be created 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 // I know, I know, the whole point of using protobuf
// is so you don't need a uuid or versions because it's // is so you don't need a uuid or versions because it's
@ -122,4 +123,3 @@ message Identify { // `autogenpb:marshal`
string uuid = 1; // string uuid = 1; //
string version = 2; // string version = 2; //
} }
// footer was empty

View File

@ -14,7 +14,7 @@ import (
func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) string { func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) string {
LOCK := msg.getLockname("x") LOCK := msg.getLockname("x")
funcdef := "func (x *" + FRUIT + ") Append(y *" + APPLE + ")" funcdef := "func (x *" + FRUIT + ") Append(y *" + APPLE + ") *" + APPLE
// log.Printf("\t\t(x %s) APPEND(%s)\n", FRUIT, APPLE) // log.Printf("\t\t(x %s) APPEND(%s)\n", FRUIT, APPLE)
// append -- no check at all // append -- no check at all
@ -23,7 +23,10 @@ func (msg *MsgName) simpleAppend(w io.Writer, FRUIT, APPLES, APPLE string) strin
fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " "+LOCK+".Lock()")
fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", proto.Clone(y).(*"+APPLE+"))") fmt.Fprintln(w, " z := proto.Clone(y).(*"+APPLE+")")
fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", z)")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " return z")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")

View File

@ -49,39 +49,9 @@ func (pb *Files) makeGuiFile(pf *File) error {
FRUIT := msg.GuiVarName FRUIT := msg.GuiVarName
fruitVars := color.Vars fruitVars := color.Vars
pf.generateAutoTablePB(newf, FRUITS, FRUIT, fruitVars) 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) log.Printf("Added GUI functions for protobuf '%s' with record '%s' using mutex '%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, "\n")
fmt.Fprintf(newf, "// END GUI\n") fmt.Fprintf(newf, "// END GUI\n")
@ -141,6 +111,7 @@ func headerGui(w io.Writer, pf *File) {
fmt.Fprintln(w, " \"go.wit.com/gui\"") fmt.Fprintln(w, " \"go.wit.com/gui\"")
fmt.Fprintln(w, " \"go.wit.com/lib/protobuf/guipb\"") fmt.Fprintln(w, " \"go.wit.com/lib/protobuf/guipb\"")
fmt.Fprintln(w, " \"go.wit.com/log\"") fmt.Fprintln(w, " \"go.wit.com/log\"")
fmt.Fprintln(w, " anypb \"google.golang.org/protobuf/types/known/anypb\"")
fmt.Fprintln(w, " timestamppb \"google.golang.org/protobuf/types/known/timestamppb\"") fmt.Fprintln(w, " timestamppb \"google.golang.org/protobuf/types/known/timestamppb\"")
fmt.Fprintln(w, ")") fmt.Fprintln(w, ")")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
@ -156,61 +127,77 @@ func guiMain(w io.Writer, FRUITS string, FRUIT string) {
fmt.Fprintln(w, " return t") fmt.Fprintln(w, " return t")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddStringFunc(title string, f func(*"+FRUIT+") string) *"+FRUIT+"StringFunc {") fmt.Fprintln(w, "// force the application to choose the type of data. this allows the GUI plugin to be smarter")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddStringFunc(title string, f func(*"+FRUIT+") string) *"+FRUIT+"AnyFunc {")
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)") fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " sf := new("+FRUIT+"StringFunc)") fmt.Fprintln(w, " sf := new("+FRUIT+"AnyFunc)")
fmt.Fprintln(w, " sf.title = title") fmt.Fprintln(w, " sf.title = title")
fmt.Fprintln(w, " sf.f = f") fmt.Fprintln(w, " sf.f = func(x *"+FRUIT+") any {")
fmt.Fprintln(w, " sf.order = t.order") fmt.Fprintln(w, " return f(x)")
fmt.Fprintln(w, " t.order += 1") fmt.Fprintln(w, " }")
fmt.Fprintln(w, " t.stringFuncs = append(t.stringFuncs, sf)") fmt.Fprintln(w, " sf.attr = new(guipb.ColAttr)")
fmt.Fprintln(w, " sf.attr.Width = int32(sf.Width)")
fmt.Fprintln(w, " sf.attr.Type = guipb.ColAttr_STRING")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " t.anyFuncs = append(t.anyFuncs, sf)")
fmt.Fprintln(w, " return sf") fmt.Fprintln(w, " return sf")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddButtonFunc(title string, f func(*"+FRUIT+") string) *"+FRUIT+"ButtonFunc {") fmt.Fprintln(w, "// force the application to choose the type of data. this allows the GUI plugin to be smarter")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddButtonFunc(title string, f func(*"+FRUIT+") string) *"+FRUIT+"AnyFunc {")
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)") fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " sf := new("+FRUIT+"ButtonFunc)") fmt.Fprintln(w, " sf := new("+FRUIT+"AnyFunc)")
fmt.Fprintln(w, " sf.title = title") fmt.Fprintln(w, " sf.title = title")
fmt.Fprintln(w, " sf.f = f") fmt.Fprintln(w, " sf.f = func(x *"+FRUIT+") any {")
fmt.Fprintln(w, " sf.order = t.order") fmt.Fprintln(w, " return f(x)")
fmt.Fprintln(w, " t.order += 1") fmt.Fprintln(w, " }")
fmt.Fprintln(w, " t.buttonFuncs = append(t.buttonFuncs, sf)") fmt.Fprintln(w, " sf.attr = new(guipb.ColAttr)")
fmt.Fprintln(w, " sf.attr.Width = int32(sf.Width)")
fmt.Fprintln(w, " sf.attr.Type = guipb.ColAttr_STRING")
fmt.Fprintln(w, " sf.attr.Click = true")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " t.anyFuncs = append(t.anyFuncs, sf)")
fmt.Fprintln(w, " return sf") fmt.Fprintln(w, " return sf")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddIntFunc(title string, f func(*"+FRUIT+") int) *"+FRUIT+"IntFunc {") fmt.Fprintln(w, "// force the application to choose the type of data. this allows the GUI plugin to be smarter")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddIntFunc(title string, f func(*"+FRUIT+") int) *"+FRUIT+"AnyFunc {")
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)") fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " sf := new("+FRUIT+"IntFunc)") fmt.Fprintln(w, " sf := new("+FRUIT+"AnyFunc)")
fmt.Fprintln(w, " sf.title = title") fmt.Fprintln(w, " sf.title = title")
fmt.Fprintln(w, " sf.f = f") fmt.Fprintln(w, " sf.f = func(x *"+FRUIT+") any {")
fmt.Fprintln(w, " sf.order = t.order") fmt.Fprintln(w, " return f(x)")
fmt.Fprintln(w, " t.order += 1") fmt.Fprintln(w, " }")
fmt.Fprintln(w, " t.intFuncs = append(t.intFuncs, sf)") fmt.Fprintln(w, " sf.attr = new(guipb.ColAttr)")
fmt.Fprintln(w, " sf.attr.Width = int32(sf.Width)")
fmt.Fprintln(w, " sf.attr.Type = guipb.ColAttr_INT")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " t.anyFuncs = append(t.anyFuncs, sf)")
fmt.Fprintln(w, " return sf") fmt.Fprintln(w, " return sf")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddTimeFunc(title string, f func(*"+FRUIT+") time.Time) *"+FRUIT+"TimeFunc {") fmt.Fprintln(w, "// force the application to choose the type of data. this allows the GUI plugin to be smarter")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddTimeFunc(title string, f func(*"+FRUIT+") time.Time) *"+FRUIT+"AnyFunc {")
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)") fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " sf := new("+FRUIT+"TimeFunc)") fmt.Fprintln(w, " sf := new("+FRUIT+"AnyFunc)")
fmt.Fprintln(w, " sf.title = title") fmt.Fprintln(w, " sf.title = title")
fmt.Fprintln(w, " sf.f = f") fmt.Fprintln(w, " sf.f = func(x *"+FRUIT+") any {")
fmt.Fprintln(w, " sf.order = t.order") fmt.Fprintln(w, " return f(x)")
fmt.Fprintln(w, " t.order += 1") fmt.Fprintln(w, " }")
fmt.Fprintln(w, " t.timeFuncs = append(t.timeFuncs, sf)") fmt.Fprintln(w, " sf.attr = new(guipb.ColAttr)")
fmt.Fprintln(w, " sf.attr.Width = int32(sf.Width)")
fmt.Fprintln(w, " sf.attr.Type = guipb.ColAttr_TIME")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " // t.timeFuncs = append(t.timeFuncs, sf)")
fmt.Fprintln(w, " t.anyFuncs = append(t.anyFuncs, sf)")
fmt.Fprintln(w, " return sf") fmt.Fprintln(w, " return sf")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (sf *"+FRUIT+"StringFunc) SetTitle(title string) {") fmt.Fprintln(w, "func (sf *"+FRUIT+"AnyFunc) SetTitle(title string) {")
fmt.Fprintln(w, " sf.title = title")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "func (sf *"+FRUIT+"IntFunc) SetTitle(title string) {")
fmt.Fprintln(w, " sf.title = title")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "func (sf *"+FRUIT+"TimeFunc) SetTitle(title string) {")
fmt.Fprintln(w, " sf.title = title") fmt.Fprintln(w, " sf.title = title")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
@ -230,32 +217,12 @@ func guiMain(w io.Writer, FRUITS string, FRUIT string) {
fmt.Fprintln(w, " mt.parent.ShowTable(mt.pb)") fmt.Fprintln(w, " mt.parent.ShowTable(mt.pb)")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "type "+FRUIT+"StringFunc struct {") fmt.Fprintln(w, "type "+FRUIT+"AnyFunc struct {")
fmt.Fprintln(w, " title string") fmt.Fprintln(w, " title string")
fmt.Fprintln(w, " f func(*"+FRUIT+") string") fmt.Fprintln(w, " f func(*"+FRUIT+") any")
fmt.Fprintln(w, " Custom func(*"+FRUIT+")") fmt.Fprintln(w, " Custom func(*"+FRUIT+")")
fmt.Fprintln(w, " order int") fmt.Fprintln(w, " Width int")
fmt.Fprintln(w, "}") fmt.Fprintln(w, " attr *guipb.ColAttr")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "type "+FRUIT+"ButtonFunc struct {")
fmt.Fprintln(w, " title string")
fmt.Fprintln(w, " f func(*"+FRUIT+") string")
fmt.Fprintln(w, " Custom func(*"+FRUIT+")")
fmt.Fprintln(w, " order int")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "type "+FRUIT+"IntFunc struct {")
fmt.Fprintln(w, " title string")
fmt.Fprintln(w, " f func(*"+FRUIT+") int")
fmt.Fprintln(w, " Custom func(*"+FRUIT+")")
fmt.Fprintln(w, " order int")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "type "+FRUIT+"TimeFunc struct {")
fmt.Fprintln(w, " title string")
fmt.Fprintln(w, " f func(*"+FRUIT+") time.Time")
fmt.Fprintln(w, " Custom func(*"+FRUIT+")")
fmt.Fprintln(w, " order int")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "type "+FRUITS+"Table struct {") fmt.Fprintln(w, "type "+FRUITS+"Table struct {")
@ -263,122 +230,65 @@ func guiMain(w io.Writer, FRUITS string, FRUIT string) {
fmt.Fprintln(w, " parent *gui.Node") fmt.Fprintln(w, " parent *gui.Node")
fmt.Fprintln(w, " x *"+FRUITS+"") fmt.Fprintln(w, " x *"+FRUITS+"")
fmt.Fprintln(w, " hostnames []string") fmt.Fprintln(w, " hostnames []string")
fmt.Fprintln(w, " stringFuncs []*"+FRUIT+"StringFunc") fmt.Fprintln(w, " anyFuncs []*"+FRUIT+"AnyFunc")
fmt.Fprintln(w, " intFuncs []*"+FRUIT+"IntFunc")
fmt.Fprintln(w, " timeFuncs []*"+FRUIT+"TimeFunc")
fmt.Fprintln(w, " buttonFuncs []*"+FRUIT+"ButtonFunc")
fmt.Fprintln(w, " CustomFunc func(*"+FRUIT+")") fmt.Fprintln(w, " CustomFunc func(*"+FRUIT+")")
fmt.Fprintln(w, " order int")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
} }
func guiStringFuncs(w io.Writer, ZOOPB string, FRUITS string, FRUIT string) { func guiStringFuncs(w io.Writer, ZOOPB string, FRUITS string, FRUIT string) {
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doStringFunc(name string) bool {") fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doAnyFuncNew(sf *"+FRUIT+"AnyFunc) bool {")
fmt.Fprintln(w, " for _, sf := range mt.stringFuncs {") fmt.Fprintln(w, " r := new(guipb.AnyCol)")
fmt.Fprintln(w, " if sf.title != name {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": found stringfunc name:\", name)")
fmt.Fprintln(w, " r := new(guipb.StringRow)")
fmt.Fprintln(w, " r.Header = new(guipb.Widget)") fmt.Fprintln(w, " r.Header = new(guipb.Widget)")
fmt.Fprintln(w, " r.Header.Name = name") fmt.Fprintln(w, " r.Header.Name = sf.title")
fmt.Fprintln(w, " r.Attr = proto.Clone(sf.attr).(*guipb.ColAttr)")
fmt.Fprintln(w, "")
/*
fmt.Fprintln(w, " all := mt.x.All()")
fmt.Fprintln(w, " for all.Scan() {")
fmt.Fprintln(w, " m := all.Next()")
*/
fmt.Fprintln(w, " for m := range mt.x.IterAll() {")
fmt.Fprintln(w, " r.Vals = append(r.Vals, sf.f(m))")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": adding\", name, r.Vals)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " mt.pb.StringRows = append(mt.pb.StringRows, r)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doButtonFunc(name string) bool {")
fmt.Fprintln(w, " for _, sf := range mt.buttonFuncs {")
fmt.Fprintln(w, " if sf.title != name {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": found stringfunc name:\", name)")
fmt.Fprintln(w, " r := new(guipb.ButtonRow)")
fmt.Fprintln(w, " r.Header = new(guipb.Widget)")
fmt.Fprintln(w, " r.Header.Name = name")
fmt.Fprintln(w, " for m := range mt.x.IterAll() {")
fmt.Fprintln(w, " r.Vals = append(r.Vals, sf.f(m))")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": adding\", name, r.Vals)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " mt.pb.ButtonRows = append(mt.pb.ButtonRows, r)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doIntFunc(name string) bool {")
fmt.Fprintln(w, " for _, sf := range mt.intFuncs {")
fmt.Fprintln(w, " if sf.title != name {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": found intfunc name:\", name)")
fmt.Fprintln(w, " r := new(guipb.IntRow)")
fmt.Fprintln(w, " r.Header = new(guipb.Widget)")
fmt.Fprintln(w, " r.Header.Name = name")
fmt.Fprintln(w, " for m := range mt.x.IterAll() {")
fmt.Fprintln(w, " r.Vals = append(r.Vals, int64(sf.f(m)))")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": adding\", name, r.Vals)")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " mt.pb.IntRows = append(mt.pb.IntRows, r)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) doTimeFunc(name string) bool {")
fmt.Fprintln(w, " for _, sf := range mt.timeFuncs {")
fmt.Fprintln(w, " if sf.title != name {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": found timefunc name:\", name)")
fmt.Fprintln(w, " r := new(guipb.TimeRow)")
fmt.Fprintln(w, " r.Header = new(guipb.Widget)")
fmt.Fprintln(w, " r.Header.Name = name")
fmt.Fprintln(w, " for m := range mt.x.IterAll() {") fmt.Fprintln(w, " for m := range mt.x.IterAll() {")
fmt.Fprintln(w, " t := sf.f(m)") fmt.Fprintln(w, " t := sf.f(m)")
fmt.Fprintln(w, " r.Vals = append(r.Vals, timestamppb.New(t)) // convert to protobuf time") fmt.Fprintln(w, " switch r.Attr.Type {")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": adding\", name, r.Vals)") fmt.Fprintln(w, " case guipb.ColAttr_STRING:")
fmt.Fprintln(w, " // anyProto, err := anypb.New(tsProto)")
fmt.Fprintln(w, " stringValue := wrapperspb.String(t.(string))")
fmt.Fprintln(w, " anyProto, err := anypb.New(stringValue)")
fmt.Fprintln(w, " _ = err // do something with err someday (?)")
fmt.Fprintln(w, " r.Vals = append(r.Vals, anyProto)")
fmt.Fprintln(w, " // return col.Vals[row] true")
fmt.Fprintln(w, " case guipb.ColAttr_INT:")
fmt.Fprintln(w, " var finalInt int")
fmt.Fprintln(w, " finalInt = t.(int)")
fmt.Fprintln(w, " intVal := wrapperspb.Int32(int32(finalInt))")
fmt.Fprintln(w, " anyProto, _ := anypb.New(intVal)")
fmt.Fprintln(w, " r.Vals = append(r.Vals, anyProto)")
fmt.Fprintln(w, " case guipb.ColAttr_DURATION:")
fmt.Fprintln(w, " case guipb.ColAttr_TIME:")
fmt.Fprintln(w, " var goTime time.Time")
fmt.Fprintln(w, " goTime = t.(time.Time)")
fmt.Fprintln(w, " tsProto := timestamppb.New(goTime)")
fmt.Fprintln(w, " anyProto, err := anypb.New(tsProto)")
fmt.Fprintln(w, " _ = err // do something with err someday (?)")
fmt.Fprintln(w, " r.Vals = append(r.Vals, anyProto)")
fmt.Fprintln(w, " default:")
fmt.Fprintln(w, " log.Info(\"cell unhandled type\", r.Attr.Type)")
fmt.Fprintln(w, " }") fmt.Fprintln(w, " }")
fmt.Fprintln(w, " mt.pb.TimeRows = append(mt.pb.TimeRows, r)") fmt.Fprintln(w, " // cellTime := r.Vals[row]")
fmt.Fprintln(w, " // s := shell.FormatDuration(time.Since(cellTime.AsTime()))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " mt.pb.AnyCols = append(mt.pb.AnyCols, r)")
fmt.Fprintln(w, " return true") fmt.Fprintln(w, " return true")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) MakeTable() {") fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) MakeTable() {")
fmt.Fprintln(w, " for _, name := range mt.pb.Order {") fmt.Fprintln(w, " for _, sf := range mt.anyFuncs {")
fmt.Fprintln(w, " // log.Info(\""+ZOOPB+": looking for row name()\", name)") fmt.Fprintln(w, " mt.doAnyFuncNew(sf)")
fmt.Fprintln(w, " if mt.doStringFunc(name) {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if mt.doIntFunc(name) {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if mt.doTimeFunc(name) {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if mt.doButtonFunc(name) {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }") fmt.Fprintln(w, " }")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
} }
func guiAddStringFunc(w io.Writer, FRUITS string, FRUIT string, BRAND string) { func guiAddStringFunc(w io.Writer, FRUITS string, FRUIT string, BRAND string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) Add"+BRAND+"() *"+FRUIT+"StringFunc {") fmt.Fprintln(w, "func (t *"+FRUITS+"Table) Add"+BRAND+"() *"+FRUIT+"AnyFunc {")
fmt.Fprintln(w, " sf := t.AddStringFunc(\""+BRAND+"\", func(m *"+FRUIT+") string {") fmt.Fprintln(w, " sf := t.AddStringFunc(\""+BRAND+"\", func(m *"+FRUIT+") string {")
fmt.Fprintln(w, " return m."+BRAND+"") fmt.Fprintln(w, " return m."+BRAND+"")
fmt.Fprintln(w, " })") fmt.Fprintln(w, " })")
@ -388,22 +298,12 @@ func guiAddStringFunc(w io.Writer, FRUITS string, FRUIT string, BRAND string) {
func guiAddIntFunc(w io.Writer, FRUITS string, FRUIT string, BRAND string) { func guiAddIntFunc(w io.Writer, FRUITS string, FRUIT string, BRAND string) {
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) Add"+BRAND+"() {") fmt.Fprintln(w, "func (t *"+FRUITS+"Table) Add"+BRAND+"() *"+FRUIT+"AnyFunc {")
fmt.Fprintln(w, " t.AddIntFunc(\""+BRAND+"\", func(m *"+FRUIT+") int {") fmt.Fprintln(w, " custf := t.AddIntFunc(\""+BRAND+"\", func(m *"+FRUIT+") int {")
fmt.Fprintln(w, " return int(m."+BRAND+")") fmt.Fprintln(w, " return int(m."+BRAND+")")
fmt.Fprintln(w, " })") fmt.Fprintln(w, " })")
fmt.Fprintln(w, " return custf")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
/*
// examples of what these look like
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddMemory() {")
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, \"Memory\")")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddCpus() {")
fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, \"Cpus\")")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
*/
} }
func guiUpdate(w io.Writer, FRUITS string, FRUIT string) { func guiUpdate(w io.Writer, FRUITS string, FRUIT string) {
@ -412,28 +312,10 @@ func guiUpdate(w io.Writer, FRUITS string, FRUIT string) {
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "// START TABLE UPDATE") fmt.Fprintln(w, "// START TABLE UPDATE (doesn't work yet)")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) todoUpdate() {")
fmt.Fprintln(w, " for _, name := range mt.pb.Order {")
fmt.Fprintln(w, " // log.Info(\""+FRUIT+"pb: trying to update row()\", name)")
fmt.Fprintln(w, " if mt.updateStringFunc(name) {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if mt.updateTimeFunc(name) {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " /*")
fmt.Fprintln(w, " if mt.updateIntFunc(name) {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " */")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // mt.dumpStringFunc(\"Hostname\")")
fmt.Fprintln(w, " mt.parent.UpdateTable(mt.pb)")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) dumpStringFunc(name string) {") fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) dumpStringFunc(name string) {")
fmt.Fprintln(w, " for i, r := range mt.pb.StringRows {") fmt.Fprintln(w, " for i, r := range mt.pb.StringCols {")
fmt.Fprintln(w, " // log.Info(\"could use\", i, r.Header.Name, \"for name =\", name)") fmt.Fprintln(w, " // log.Info(\"could use\", i, r.Header.Name, \"for name =\", name)")
fmt.Fprintln(w, " if r.Header.Name == name {") fmt.Fprintln(w, " if r.Header.Name == name {")
fmt.Fprintln(w, " log.Info(\"dump Strings row\", i, r.Header.Name, r.Vals)") fmt.Fprintln(w, " log.Info(\"dump Strings row\", i, r.Header.Name, r.Vals)")
@ -442,70 +324,6 @@ func guiUpdate(w io.Writer, FRUITS string, FRUIT string) {
fmt.Fprintln(w, " }") fmt.Fprintln(w, " }")
fmt.Fprintln(w, "}") fmt.Fprintln(w, "}")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) updateStringFunc(name string) bool {")
fmt.Fprintln(w, " // log.Info(\"LOOKING FOR STRING row\", name)")
fmt.Fprintln(w, " var found *guipb.StringRow")
fmt.Fprintln(w, " for _, r := range mt.pb.StringRows {")
fmt.Fprintln(w, " // log.Info(\"could use\", i, r.Header.Name, \"for name =\", name)")
fmt.Fprintln(w, " if r.Header.Name == name {")
fmt.Fprintln(w, " // log.Info(\"found row\", i, r.Header.Name)")
fmt.Fprintln(w, " found = r")
fmt.Fprintln(w, " break")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if found == nil {")
fmt.Fprintln(w, " log.Info(\"did not find string row\", name)")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " for _, sf := range mt.stringFuncs {")
fmt.Fprintln(w, " if sf.title != name {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\""+FRUIT+"pb: starting\", name, found.Vals)")
fmt.Fprintln(w, " for i, _ := range found.Vals {")
fmt.Fprintln(w, " tmp := sf.f(mt.x."+FRUITS+"[i])")
fmt.Fprintln(w, " if tmp == \"www.wit.com\" {")
fmt.Fprintln(w, " log.Info(\"virtpb: FOUND WWW\", i)")
fmt.Fprintln(w, " tmp = \"new.www\"")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " found.Vals[i] = tmp")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\""+FRUIT+"pb: ending\", name, found.Vals)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) updateTimeFunc(name string) bool {")
fmt.Fprintln(w, " log.Info(\"LOOKING FOR TIME row\", name)")
fmt.Fprintln(w, " var found *guipb.TimeRow")
fmt.Fprintln(w, " for i, r := range mt.pb.TimeRows {")
fmt.Fprintln(w, " // log.Info(\"could use\", i, r.Header.Name, \"for name =\", name)")
fmt.Fprintln(w, " if r.Header.Name == name {")
fmt.Fprintln(w, " log.Info(\"found row\", i, r.Header.Name)")
fmt.Fprintln(w, " found = r")
fmt.Fprintln(w, " break")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if found == nil {")
fmt.Fprintln(w, " log.Info(\"did not find time row\", name)")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " for _, sf := range mt.timeFuncs {")
fmt.Fprintln(w, " if sf.title != name {")
fmt.Fprintln(w, " continue")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\"updateTimeFunc() has row len =\", len(mt.x."+FRUITS+"))")
fmt.Fprintln(w, " // log.Info(\"virtpb: starting\", name, found.Vals)")
fmt.Fprintln(w, " for i, _ := range found.Vals {")
fmt.Fprintln(w, " newt := sf.f(mt.x."+FRUITS+"[i])")
fmt.Fprintln(w, " found.Vals[i] = timestamppb.New(newt) // convert to protobuf time")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " // log.Info(\"virtpb: ending\", name, found.Vals)")
fmt.Fprintln(w, " return true")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return false")
fmt.Fprintln(w, "}")
} }
func guiTableDelete(w io.Writer, FRUITS string, FRUIT string) { func guiTableDelete(w io.Writer, FRUITS string, FRUIT string) {
@ -526,9 +344,8 @@ func guiTableCustom(w io.Writer, FRUITS string, fRUITS string, FRUIT string, fRU
fmt.Fprintln(w, " row := mt.x."+FRUITS+"[w.Location.Y-1]") 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, " // log.Info(\"got to "+fRUITS+"Custom() with\", w.Location.X, w.Location.Y-1)")
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, " for _, sf := range mt.buttonFuncs {") fmt.Fprintln(w, " for i, sf := range mt.anyFuncs {")
fmt.Fprintln(w, " if sf.order == int(w.Location.X) {") fmt.Fprintln(w, " if i == int(w.Location.X) {")
fmt.Fprintln(w, " // log.Info(\"found order\", sf.order)")
fmt.Fprintln(w, " if sf.Custom != nil {") fmt.Fprintln(w, " if sf.Custom != nil {")
fmt.Fprintln(w, " log.Info(\"doing Custom() func for button\")") fmt.Fprintln(w, " log.Info(\"doing Custom() func for button\")")
fmt.Fprintln(w, " sf.Custom(row)") fmt.Fprintln(w, " sf.Custom(row)")

129
generateHTTP.go Normal file
View File

@ -0,0 +1,129 @@
// 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 {
FRUITS := msg.Name
fRUITS := untitle(FRUITS)
httpSendReply(newf, FRUITS)
httpPost(newf, FRUITS, fRUITS)
log.Printf("func (p *%s) HttpPost(baseURL string, route string) (*%s, *httppb.HttpRequest, error)\n", FRUITS, FRUITS)
}
}
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 httpSendReply(w io.Writer, FRUITS string) {
fmt.Fprintln(w, "func (p *"+FRUITS+") SendReply(w http.ResponseWriter, reqPB *httppb.HttpRequest) error {")
fmt.Fprintln(w, " data, err := p.Marshal()")
fmt.Fprintln(w, " if err != nil {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, log.Sprintf(, err))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if len(data) == 0 {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, \"Patches PB data was nil/emtpy without Marsha() error\")")
fmt.Fprintln(w, " return nil")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " _, err = w.Write(data)")
fmt.Fprintln(w, " if err != nil {")
fmt.Fprintln(w, " // reqPB.Errors = append(reqPB.Errors, log.Sprintf(, i, err))")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " return err")
fmt.Fprintln(w, "}")
}
func httpPost(w io.Writer, FRUITS string, fRUITS string) {
fmt.Fprintln(w, "// Marshal protobuf, then http POST, then Unmarshal() to protobuf again")
fmt.Fprintln(w, "func (p *"+FRUITS+") HttpPost(baseURL string, route string) (*"+FRUITS+", *httppb.HttpRequest, error) {")
fmt.Fprintln(w, " data, err := p.Marshal()")
fmt.Fprintln(w, " if err != nil {")
fmt.Fprintln(w, " return nil, nil, err")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " tmp := filepath.Join(\""+fRUITS+"\",route)")
fmt.Fprintln(w, " reqPB, err := httppb.DoPost(baseURL, tmp, data)")
fmt.Fprintln(w, " if reqPB == nil {")
fmt.Fprintln(w, " return nil, nil, err")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " if len(reqPB.ServerData) == 0 {")
fmt.Fprintln(w, " return nil, reqPB, fmt.Errorf(\"server returned len(data)=0\")")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " newpb := new("+FRUITS+")")
fmt.Fprintln(w, " err = newpb.Unmarshal(reqPB.ServerData)")
fmt.Fprintln(w, " return newpb, reqPB, err")
fmt.Fprintln(w, "}")
}

11
main.go
View File

@ -19,8 +19,8 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/alexflint/go-arg"
"github.com/go-cmd/cmd" "github.com/go-cmd/cmd"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/fhelp" "go.wit.com/lib/fhelp"
"go.wit.com/lib/gui/shell" "go.wit.com/lib/gui/shell"
"go.wit.com/log" "go.wit.com/log"
@ -208,6 +208,15 @@ func main() {
badExit(err) badExit(err)
} }
} }
if pf.DoHTTP {
if err := pb.makeHTTPFile(pf); err != nil {
badExit(err)
}
}
log.Info("")
log.Info("you may have to run 'goimport -w *.go' on the new *pb.go files")
log.Info("")
} }
func okExit(s string) { func okExit(s string) {

View File

@ -192,7 +192,12 @@ func (pf *File) parseForMessage(line string) *MsgName {
} }
if strings.Contains(line, "`autogenpb:marshal`") { if strings.Contains(line, "`autogenpb:marshal`") {
msg.DoMarshal = true 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
msg.DoMarshal = true // http requires Marshal
} }
if strings.Contains(line, "`autogenpb:gui") { if strings.Contains(line, "`autogenpb:gui") {
log.Info("got autogenpb:gui") log.Info("got autogenpb:gui")