summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GuiGenerate213
-rw-r--r--example/fruit.proto2
-rw-r--r--generateGui.go215
-rw-r--r--generateHeader.go1
-rw-r--r--main.go4
5 files changed, 433 insertions, 2 deletions
diff --git a/GuiGenerate b/GuiGenerate
new file mode 100644
index 0000000..aac8c37
--- /dev/null
+++ b/GuiGenerate
@@ -0,0 +1,213 @@
+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
+ }
+ }
+}
diff --git a/example/fruit.proto b/example/fruit.proto
index c546fd4..86a8f89 100644
--- a/example/fruit.proto
+++ b/example/fruit.proto
@@ -69,7 +69,7 @@ message Fruit {
// "Fruits" MUST EXIST and start exactly this way
// It must be "Fruit" + 's' and must match the name of this file: "fruit.proto"
-message Fruits { // `autogenpb:marshal` `autogenpb:mutex`
+message Fruits { // `autogenpb:marshal` `autogenpb:mutex` `autogenpb:gui`
string uuid = 1; // `autogenpb:uuid:be926ad9-f07f-484c-adf2-d96eeabf3079`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Fruit Fruits = 3; // THIS MUST BE "Fruit" and then "Fruit" + "s"
diff --git a/generateGui.go b/generateGui.go
new file mode 100644
index 0000000..c07a5bd
--- /dev/null
+++ b/generateGui.go
@@ -0,0 +1,215 @@
+// 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"
+)
+
+// this file is named poorly. It has more than Sort()
+
+func (pb *Files) makeGuiFile(pf *File) error {
+ newf, _ := os.OpenFile(pf.Filebase+".gui.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ defer newf.Close()
+
+ headerGui(newf, pf)
+
+ fmt.Fprintf(newf, "// START GUI\n")
+ fmt.Fprintf(newf, "\n")
+
+ guiMain(newf, "Fruits", "Fruit")
+ guiStringFuncs(newf, pf.Package, "Fruits", "Fruit")
+
+ fmt.Fprintf(newf, "\n")
+ fmt.Fprintf(newf, "// END GUI\n")
+
+ return nil
+}
+
+func headerGui(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/gui\"")
+ fmt.Fprintln(w, " \"go.wit.com/lib/protobuf/guipb\"")
+ fmt.Fprintln(w, " \"go.wit.com/log\"")
+ fmt.Fprintln(w, " timestamppb \"google.golang.org/protobuf/types/known/timestamppb\"")
+ fmt.Fprintln(w, ")")
+ fmt.Fprintln(w, "")
+}
+
+func guiMain(w io.Writer, FRUITS string, FRUIT string) {
+ fmt.Fprintln(w, "func (x *"+FRUITS+") NewTable(title string) *"+FRUITS+"Table {")
+ fmt.Fprintln(w, " t := new("+FRUITS+"Table)")
+ fmt.Fprintln(w, " t.x = x")
+ fmt.Fprintln(w, " pb := new(guipb.Table)")
+ fmt.Fprintln(w, " pb.Title = title")
+ fmt.Fprintln(w, " t.pb = pb")
+ fmt.Fprintln(w, " return t")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddStringFunc(title string, f func(*"+FRUIT+") string) {")
+ fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " sf := new("+FRUIT+"StringFunc)")
+ fmt.Fprintln(w, " sf.title = title")
+ fmt.Fprintln(w, " sf.f = f")
+ fmt.Fprintln(w, " t.stringFuncs = append(t.stringFuncs, sf)")
+ fmt.Fprintln(w, "}")
+
+ fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddIntFunc(title string, f func(*"+FRUIT+") int) {")
+ fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " sf := new("+FRUIT+"IntFunc)")
+ fmt.Fprintln(w, " sf.title = title")
+ fmt.Fprintln(w, " sf.f = f")
+ fmt.Fprintln(w, " t.intFuncs = append(t.intFuncs, sf)")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddTimeFunc(title string, f func(*"+FRUIT+") time.Time) {")
+ fmt.Fprintln(w, " t.pb.Order = append(t.pb.Order, title)")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " sf := new("+FRUIT+"TimeFunc)")
+ fmt.Fprintln(w, " sf.title = title")
+ fmt.Fprintln(w, " sf.f = f")
+ fmt.Fprintln(w, " t.timeFuncs = append(t.timeFuncs, sf)")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) ShowTable() {")
+ fmt.Fprintln(w, " log.Info(\"ShowTable() SENDING TO GUI\")")
+ fmt.Fprintln(w, " // mt.MakeTable()")
+ fmt.Fprintln(w, " gui.ShowTable(mt.pb)")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "type "+FRUIT+"StringFunc struct {")
+ fmt.Fprintln(w, " title string")
+ fmt.Fprintln(w, " f func(*"+FRUIT+") string")
+ 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, "}")
+ 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, "}")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, "type "+FRUITS+"Table struct {")
+ fmt.Fprintln(w, " // gt *gui.NodeTable")
+ fmt.Fprintln(w, " pb *guipb.Table")
+ fmt.Fprintln(w, " x *"+FRUITS+"")
+ fmt.Fprintln(w, " hostnames []string")
+ fmt.Fprintln(w, " stringFuncs []*"+FRUIT+"StringFunc")
+ fmt.Fprintln(w, " intFuncs []*"+FRUIT+"IntFunc")
+ fmt.Fprintln(w, " timeFuncs []*"+FRUIT+"TimeFunc")
+ fmt.Fprintln(w, "}")
+}
+
+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, " for _, sf := range mt.stringFuncs {")
+ 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.Name = name")
+ fmt.Fprintln(w, " all := mt.x.All()")
+ fmt.Fprintln(w, " for all.Scan() {")
+ fmt.Fprintln(w, " m := all.Next()")
+ 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) 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, " all := mt.x.All()")
+ fmt.Fprintln(w, " for all.Scan() {")
+ fmt.Fprintln(w, " m := all.Next()")
+ 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, " all := mt.x.All()")
+ fmt.Fprintln(w, " for all.Scan() {")
+ fmt.Fprintln(w, " m := all.Next()")
+ 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, " log.Info(\""+ZOOPB+": adding\", name, r.Vals)")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " mt.pb.TimeRows = append(mt.pb.TimeRows, r)")
+ fmt.Fprintln(w, " return true")
+ fmt.Fprintln(w, " }")
+ fmt.Fprintln(w, " return false")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ /*
+ fmt.Fprintln(w, "func (t *"+FRUITS+"Table) AddHostname() {")
+ fmt.Fprintln(w, " // t.pb.Order = append(t.pb.Order, \"Hostname\")")
+ fmt.Fprintln(w, "")
+ fmt.Fprintln(w, " t.AddStringFunc(\"Hostname\", func(m *"+ZOOPB+"."+FRUIT+") string {")
+ fmt.Fprintln(w, " return m.Hostname")
+ fmt.Fprintln(w, " })")
+ fmt.Fprintln(w, "}")
+ fmt.Fprintln(w, "")
+ 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, "")
+ */
+ fmt.Fprintln(w, "func (mt *"+FRUITS+"Table) MakeTable() {")
+ fmt.Fprintln(w, " for _, name := range mt.pb.Order {")
+ fmt.Fprintln(w, " log.Info(\""+ZOOPB+": looking for row name()\", name)")
+ 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, " }")
+ fmt.Fprintln(w, "}")
+}
diff --git a/generateHeader.go b/generateHeader.go
index d954150..7f2aca8 100644
--- a/generateHeader.go
+++ b/generateHeader.go
@@ -43,7 +43,6 @@ func header(w io.Writer, pf *File) {
fmt.Fprintf(w, "package %s\n", pf.Package)
fmt.Fprintln(w, "")
fmt.Fprintln(w, "import (")
- // fmt.Fprintln(w, " \"fmt\"")
fmt.Fprintln(w, " \"sort\"")
fmt.Fprintln(w, " \"sync\"")
fmt.Fprintln(w, ")")
diff --git a/main.go b/main.go
index edbd627..dfe56c4 100644
--- a/main.go
+++ b/main.go
@@ -187,6 +187,10 @@ func main() {
if err := pb.makeNewSortfile(pf); err != nil {
badExit(err)
}
+
+ if err := pb.makeGuiFile(pf); err != nil {
+ badExit(err)
+ }
}
func okExit(s string) {